buildglibc.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #!/usr/bin/env python3
  2. import os
  3. import shutil
  4. import subprocess
  5. import sys
  6. from pkg_resources import parse_version
  7. def prependText(filename, text) :
  8. data = ""
  9. with open(filename, 'r') as original:
  10. data = original.read()
  11. with open(filename, 'w') as modified:
  12. modified.write(text)
  13. modified.write(data)
  14. def appendText(filename, text) :
  15. with open(filename, "a") as myfile:
  16. myfile.write(text)
  17. home = os.getcwd()
  18. glibc = "glibc-2.19"
  19. glibcParent = "" # glibc parent directory
  20. glibcDir = "" # glibc dir (ex. glibc-2.19)
  21. buildDir = "glibc-build"
  22. installDir = os.path.dirname(home) + '/Runtime/'
  23. commandStr = ""
  24. commandOutput = ""
  25. quiet = False
  26. debug_flags = ""
  27. index = 0
  28. for arg in sys.argv[1:]:
  29. index += 1
  30. if (arg == '--src' or arg == '-s') and index + 1 < len(sys.argv):
  31. glibc = sys.argv[index + 1]
  32. if arg == '--quiet' or arg == '-q':
  33. quiet = True
  34. if arg == '--debug':
  35. debug_flags = "-g"
  36. version = parse_version(glibc.replace("glibc-", ""))
  37. if True:
  38. #########################################
  39. #### get the locations of directories ###
  40. #########################################
  41. if not quiet:
  42. iput = input('use {0} as the source of GNU libc? ([y]/n):'.format(glibc)).lower()
  43. if not iput == 'y' and not iput == '' :
  44. glibc = input('enter the glibc source to install with: ')
  45. if not quiet:
  46. iput = input('{0} contains glibc code to compile? ([y]/n): '.format(glibc)).lower()
  47. if not iput == 'y' and not iput == '':
  48. glibc = input('directory containing glibc code to compile: ')
  49. if os.path.isdir(glibc) :
  50. glibc = os.path.abspath(glibc)
  51. glibcParent,glibcDir = os.path.split(glibc)
  52. print('building in {0}: {1}'.format(glibcParent, glibcDir))
  53. if not quiet:
  54. iput = input('use {0} as the directory to build glibc in? ([y]/n): '.format(buildDir)).lower()
  55. if not iput == 'y' and not iput == '':
  56. buildDir = input('the directory to build glibc in: ')
  57. buildDir = os.path.abspath(buildDir)
  58. print('using build dir: {0}'.format(buildDir))
  59. if os.path.isdir(buildDir) :
  60. if not quiet:
  61. clean = input('clean build (delete {0}, rerun configure, etc.)? ([y]/n): '.format(buildDir))
  62. else:
  63. clean = 'y'
  64. if clean == 'y' or clean == '':
  65. shutil.rmtree(buildDir)
  66. os.makedirs(buildDir)
  67. else :
  68. print('Then just go to {0} and type make...'.format(buildDir))
  69. exit(0)
  70. else :
  71. os.makedirs(buildDir)
  72. if not quiet:
  73. iput = input('use {0} as the directory to install glibc in? ([y]/n): '.format(installDir)).lower()
  74. if not iput == 'y' and not iput == '':
  75. installDir = input('the directory to install glibc in: ')
  76. installDir = os.path.abspath(installDir)
  77. print('using install dir: {0}'.format(installDir))
  78. if True:
  79. ################################
  80. #### doctor glibc's Makefile ###
  81. ################################
  82. os.chdir(buildDir)
  83. cflags = '{0} -O2 -U_FORTIFY_SOURCE -fno-stack-protector -Wno-unused-value'.format(debug_flags)
  84. extra_defs = ''
  85. disabled_features = { 'nscd' }
  86. extra_flags = '--with-tls --without-selinux --disable-test {0}'.format(' '.join(['--disable-' + f for f in disabled_features]))
  87. if version <= parse_version('2.21'):
  88. extra_flags += ' --enable-add-ons=nptl'
  89. ## configure
  90. commandStr = r'CFLAGS="{2}" {3} {0}/configure --prefix={1} {4} | tee configure.out'.format(glibc, installDir, cflags, extra_defs, extra_flags)
  91. print(commandStr)
  92. commandOutput = subprocess.call(commandStr, shell=True)