buildglibc.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #!/usr/bin/python
  2. import sys, os, string, subprocess, shutil, fileinput, multiprocessing, re
  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. try:
  19. home = os.getcwd()
  20. glibc = "glibc-2.17"
  21. glibcParent = "" # glibc parent directory
  22. glibcDir = "" # glibc dir (ex. glibc-2.17)
  23. buildDir = "build"
  24. installDir = "/usr/local/graphene"
  25. commandStr = ""
  26. commandOutput = ""
  27. debug_flags = ""
  28. if len(sys.argv) > 1 and sys.argv[1] == 'debug':
  29. debug_flags = "-g"
  30. #########################################
  31. #### get the locations of directories ###
  32. #########################################
  33. iput = raw_input('use {0} as the source of GNU libc? ([y]/n):'.format(glibc)).lower()
  34. if not iput == 'y' and not iput == '' :
  35. glibc = raw_input('enter the glibc source to install with: ')
  36. iput = raw_input('{0} contains glibc code to compile? ([y]/n): '.format(glibc)).lower()
  37. if not iput == 'y' and not iput == '':
  38. glibc = raw_input('directory containing glibc code to compile: ')
  39. if os.path.isdir(glibc) :
  40. glibc = os.path.abspath(glibc)
  41. glibcParent,glibcDir = os.path.split(glibc)
  42. print '{0} + {1}'.format(glibcParent, glibcDir)
  43. iput = raw_input('use {0} as the directory to build glibc in? ([y]/n): '.format(buildDir)).lower()
  44. if not iput == 'y' and not iput == '':
  45. buildDir = raw_input('the directory to build glibc in: ')
  46. buildDir = os.path.abspath(buildDir)
  47. print 'using build dir: {0}'.format(buildDir)
  48. if os.path.isdir(buildDir) :
  49. clean = raw_input('clean build (delete {0}, rerun configure, etc.)? ([y]/n): '.format(buildDir))
  50. if clean == 'y' or clean == '':
  51. shutil.rmtree(buildDir)
  52. os.makedirs(buildDir)
  53. else :
  54. print 'Then just go to {0} and type make...'.format(buildDir)
  55. exit(0)
  56. else :
  57. os.makedirs(buildDir)
  58. iput = raw_input('use {0} as the directory to install glibc in? ([y]/n): '.format(installDir)).lower()
  59. if not iput == 'y' and not iput == '':
  60. installDir = raw_input('the directory to install glibc in: ')
  61. installDir = os.path.abspath(installDir)
  62. print 'using install dir: {0}'.format(installDir)
  63. ################################
  64. #### doctor glibc's Makefile ###
  65. ################################
  66. os.chdir(buildDir)
  67. cflags= '{0} -O2 -U_FORTIFY_SOURCE -fno-stack-protector'.format(debug_flags)
  68. disabled_features = { 'nscd' }
  69. extra_flags = '--with-tls --enable-add-ons=nptl --without-selinux {0}'.format(' '.join(['--disable-' + f for f in disabled_features]))
  70. ## configure
  71. commandStr = r'CFLAGS="{2}" {0}/configure --prefix={1} {3} | tee configure.out'.format(glibc, installDir, cflags, extra_flags)
  72. print commandStr
  73. commandOutput = subprocess.call(commandStr, shell=True)
  74. ## Enable parallel builds
  75. numCPUs = multiprocessing.cpu_count()
  76. ## Don't use up all the cores!
  77. numCPUs = numCPUs - 1
  78. if numCPUs == 0:
  79. numCPUs = 1
  80. replaceAll('Makefile', r'# PARALLELMFLAGS = -j4', r'PARALLELMFLAGS = -j{0}'.format(numCPUs))
  81. link_binaries = [ ( 'elf', 'ld-linux-x86-64.so.2' ),
  82. ( 'nptl', 'libpthread.so.0' ),
  83. ( 'nptl_db','libthread_db.so.1' ),
  84. ( 'dlfcn', 'libdl.so.2' ),
  85. ( 'csu', 'crt1.o' ),
  86. ( 'csu', 'crti.o' ),
  87. ( 'csu', 'crtn.o' ),
  88. ( 'libos', 'liblibos.so.1' ) ]
  89. for (dir, bin) in link_binaries:
  90. print bin + ' -> ' + dir + '/' + bin
  91. os.symlink(dir + '/' + bin, bin)
  92. print '\n\n\nNow type \'make\' in \'{0}\'\n\n'.format(buildDir)
  93. except:
  94. print 'uh-oh: {0}'.format(sys.exc_info()[0])