buildglibc.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. 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. quiet = False
  28. debug_flags = ""
  29. for arg in sys.argv[1:]:
  30. if arg == '--quiet' or arg == '-q':
  31. quiet = True
  32. if arg == 'debug':
  33. debug_flags = "-g"
  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. ################################
  75. #### doctor glibc's Makefile ###
  76. ################################
  77. os.chdir(buildDir)
  78. cflags = '{0} -O2 -U_FORTIFY_SOURCE -fno-stack-protector'.format(debug_flags)
  79. extra_defs = ''
  80. disabled_features = { 'nscd' }
  81. extra_flags = '--with-tls --enable-add-ons=nptl --without-selinux {0}'.format(' '.join(['--disable-' + f for f in disabled_features]))
  82. ## configure
  83. commandStr = r'CFLAGS="{2}" {3} {0}/configure --prefix={1} {4} | tee configure.out'.format(glibc, installDir, cflags, extra_defs, extra_flags)
  84. print commandStr
  85. commandOutput = subprocess.call(commandStr, shell=True)
  86. ## Enable parallel builds
  87. numCPUs = multiprocessing.cpu_count()
  88. ## Don't use up all the cores!
  89. numCPUs = numCPUs - 1
  90. if numCPUs == 0:
  91. numCPUs = 1
  92. replaceAll('Makefile', r'# PARALLELMFLAGS = -j4', r'PARALLELMFLAGS = -j{0}'.format(numCPUs))
  93. link_binaries = [ ( 'elf', 'ld-linux-x86-64.so.2' ),
  94. ( 'nptl', 'libpthread.so.0' ),
  95. ( 'nptl_db','libthread_db.so.1' ),
  96. ( 'math', 'libm.so.6' ),
  97. ( 'dlfcn', 'libdl.so.2' ),
  98. ( 'login', 'libutil.so.1' ),
  99. ( 'csu', 'crt1.o' ),
  100. ( 'csu', 'crti.o' ),
  101. ( 'csu', 'crtn.o' ),
  102. ( 'libos', 'liblibos.so.1' ) ]
  103. for (dir, bin) in link_binaries:
  104. print bin + ' -> ' + dir + '/' + bin
  105. os.symlink(dir + '/' + bin, bin)
  106. print '\n\n\nNow type \'make\' in \'{0}\'\n\n'.format(buildDir)
  107. except:
  108. print 'uh-oh: {0}'.format(sys.exc_info()[0])