buildglibc.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #!/usr/bin/python
  2. import sys, os, string, subprocess, shutil, fileinput, multiprocessing, re, resource
  3. def replaceAll(fd,searchExp,replaceExp):
  4. for line in fileinput.input(fd, inplace=1):
  5. if searchExp in line:
  6. line = line.replace(searchExp,replaceExp)
  7. sys.stdout.write(line)
  8. def prependText(filename, text) :
  9. data = ""
  10. with open(filename, 'r') as original:
  11. data = original.read()
  12. with open(filename, 'w') as modified:
  13. modified.write(text)
  14. modified.write(data)
  15. def appendText(filename, text) :
  16. with open(filename, "a") as myfile:
  17. myfile.write(text)
  18. home = os.getcwd()
  19. glibc = "glibc-2.19"
  20. glibcParent = "" # glibc parent directory
  21. glibcDir = "" # glibc dir (ex. glibc-2.19)
  22. buildDir = "glibc-build"
  23. installDir = os.path.dirname(home) + '/Runtime/'
  24. commandStr = ""
  25. commandOutput = ""
  26. quiet = False
  27. debug_flags = ""
  28. for arg in sys.argv[1:]:
  29. if arg == '--quiet' or arg == '-q':
  30. quiet = True
  31. if arg == '--debug':
  32. debug_flags = "-g"
  33. if True:
  34. #########################################
  35. #### get the locations of directories ###
  36. #########################################
  37. if not quiet:
  38. iput = raw_input('use {0} as the source of GNU libc? ([y]/n):'.format(glibc)).lower()
  39. if not iput == 'y' and not iput == '' :
  40. glibc = raw_input('enter the glibc source to install with: ')
  41. if not quiet:
  42. iput = raw_input('{0} contains glibc code to compile? ([y]/n): '.format(glibc)).lower()
  43. if not iput == 'y' and not iput == '':
  44. glibc = raw_input('directory containing glibc code to compile: ')
  45. if os.path.isdir(glibc) :
  46. glibc = os.path.abspath(glibc)
  47. glibcParent,glibcDir = os.path.split(glibc)
  48. print 'building in {0}: {1}'.format(glibcParent, glibcDir)
  49. if not quiet:
  50. iput = raw_input('use {0} as the directory to build glibc in? ([y]/n): '.format(buildDir)).lower()
  51. if not iput == 'y' and not iput == '':
  52. buildDir = raw_input('the directory to build glibc in: ')
  53. buildDir = os.path.abspath(buildDir)
  54. print 'using build dir: {0}'.format(buildDir)
  55. if os.path.isdir(buildDir) :
  56. if not quiet:
  57. clean = raw_input('clean build (delete {0}, rerun configure, etc.)? ([y]/n): '.format(buildDir))
  58. else:
  59. clean = 'y'
  60. if clean == 'y' or clean == '':
  61. shutil.rmtree(buildDir)
  62. os.makedirs(buildDir)
  63. else :
  64. print 'Then just go to {0} and type make...'.format(buildDir)
  65. exit(0)
  66. else :
  67. os.makedirs(buildDir)
  68. if not quiet:
  69. iput = raw_input('use {0} as the directory to install glibc in? ([y]/n): '.format(installDir)).lower()
  70. if not iput == 'y' and not iput == '':
  71. installDir = raw_input('the directory to install glibc in: ')
  72. installDir = os.path.abspath(installDir)
  73. print 'using install dir: {0}'.format(installDir)
  74. if True:
  75. ################################
  76. #### doctor glibc's Makefile ###
  77. ################################
  78. os.chdir(buildDir)
  79. cflags = '{0} -O2 -U_FORTIFY_SOURCE -fno-stack-protector'.format(debug_flags)
  80. extra_defs = ''
  81. disabled_features = { 'nscd' }
  82. extra_flags = '--with-tls --enable-add-ons=nptl --without-selinux --disable-test {0}'.format(' '.join(['--disable-' + f for f in disabled_features]))
  83. ## configure
  84. commandStr = r'CFLAGS="{2}" {3} {0}/configure --prefix={1} {4} | tee configure.out'.format(glibc, installDir, cflags, extra_defs, extra_flags)
  85. print commandStr
  86. commandOutput = subprocess.call(commandStr, shell=True)
  87. ## Enable parallel builds
  88. numCPUs = multiprocessing.cpu_count()
  89. ## Don't use up all the cores!
  90. numCPUs = numCPUs - 1
  91. if numCPUs == 0:
  92. numCPUs = 1
  93. replaceAll('Makefile', r'# PARALLELMFLAGS = -j4', r'PARALLELMFLAGS = -j{0}'.format(numCPUs))
  94. link_binaries = [ ( 'elf', 'ld-linux-x86-64.so.2' ),
  95. ( 'nptl', 'libpthread.so.0' ),
  96. ( '', 'libc.so' ),
  97. ( '', 'libc.so.6' ),
  98. ( 'nptl_db','libthread_db.so.1' ),
  99. ( 'math', 'libm.so.6' ),
  100. ( 'dlfcn', 'libdl.so.2' ),
  101. ( 'login', 'libutil.so.1' ),
  102. ( 'csu', 'crt1.o' ),
  103. ( 'csu', 'crti.o' ),
  104. ( 'csu', 'crtn.o' ),
  105. ( 'rt', 'librt.so.1' ),
  106. ( 'resolv', 'libnss_dns.so.2' ),
  107. ( 'resolv', 'libresolv.so.2' ),
  108. ( 'libos', 'liblibos.so.1' ) ]
  109. if True:
  110. for (dir, bin) in link_binaries:
  111. if os.path.lexists(installDir + '/' + bin):
  112. continue
  113. print installDir + '/' + bin + ' -> ' + buildDir + '/' + dir + '/' + bin
  114. os.symlink(os.path.relpath(buildDir + '/' + dir + '/' + bin, installDir), installDir + '/' + bin)