/PC/VS8.0/build_ssl.py

http://unladen-swallow.googlecode.com/ · Python · 250 lines · 174 code · 21 blank · 55 comment · 60 complexity · c573cf1ae5ea00606c67d5c019b9afdf MD5 · raw file

  1. # Script for building the _ssl and _hashlib modules for Windows.
  2. # Uses Perl to setup the OpenSSL environment correctly
  3. # and build OpenSSL, then invokes a simple nmake session
  4. # for the actual _ssl.pyd and _hashlib.pyd DLLs.
  5. # THEORETICALLY, you can:
  6. # * Unpack the latest SSL release one level above your main Python source
  7. # directory. It is likely you will already find the zlib library and
  8. # any other external packages there.
  9. # * Install ActivePerl and ensure it is somewhere on your path.
  10. # * Run this script from the PCBuild directory.
  11. #
  12. # it should configure and build SSL, then build the _ssl and _hashlib
  13. # Python extensions without intervention.
  14. # Modified by Christian Heimes
  15. # Now this script supports pre-generated makefiles and assembly files.
  16. # Developers don't need an installation of Perl anymore to build Python. A svn
  17. # checkout from our svn repository is enough.
  18. #
  19. # In Order to create the files in the case of an update you still need Perl.
  20. # Run build_ssl in this order:
  21. # python.exe build_ssl.py Release x64
  22. # python.exe build_ssl.py Release Win32
  23. import os, sys, re, shutil
  24. # Find all "foo.exe" files on the PATH.
  25. def find_all_on_path(filename, extras = None):
  26. entries = os.environ["PATH"].split(os.pathsep)
  27. ret = []
  28. for p in entries:
  29. fname = os.path.abspath(os.path.join(p, filename))
  30. if os.path.isfile(fname) and fname not in ret:
  31. ret.append(fname)
  32. if extras:
  33. for p in extras:
  34. fname = os.path.abspath(os.path.join(p, filename))
  35. if os.path.isfile(fname) and fname not in ret:
  36. ret.append(fname)
  37. return ret
  38. # Find a suitable Perl installation for OpenSSL.
  39. # cygwin perl does *not* work. ActivePerl does.
  40. # Being a Perl dummy, the simplest way I can check is if the "Win32" package
  41. # is available.
  42. def find_working_perl(perls):
  43. for perl in perls:
  44. fh = os.popen(perl + ' -e "use Win32;"')
  45. fh.read()
  46. rc = fh.close()
  47. if rc:
  48. continue
  49. return perl
  50. print("Can not find a suitable PERL:")
  51. if perls:
  52. print(" the following perl interpreters were found:")
  53. for p in perls:
  54. print(" ", p)
  55. print(" None of these versions appear suitable for building OpenSSL")
  56. else:
  57. print(" NO perl interpreters were found on this machine at all!")
  58. print(" Please install ActivePerl and ensure it appears on your path")
  59. return None
  60. # Locate the best SSL directory given a few roots to look into.
  61. def find_best_ssl_dir(sources):
  62. candidates = []
  63. for s in sources:
  64. try:
  65. # note: do not abspath s; the build will fail if any
  66. # higher up directory name has spaces in it.
  67. fnames = os.listdir(s)
  68. except os.error:
  69. fnames = []
  70. for fname in fnames:
  71. fqn = os.path.join(s, fname)
  72. if os.path.isdir(fqn) and fname.startswith("openssl-"):
  73. candidates.append(fqn)
  74. # Now we have all the candidates, locate the best.
  75. best_parts = []
  76. best_name = None
  77. for c in candidates:
  78. parts = re.split("[.-]", os.path.basename(c))[1:]
  79. # eg - openssl-0.9.7-beta1 - ignore all "beta" or any other qualifiers
  80. if len(parts) >= 4:
  81. continue
  82. if parts > best_parts:
  83. best_parts = parts
  84. best_name = c
  85. if best_name is not None:
  86. print("Found an SSL directory at '%s'" % (best_name,))
  87. else:
  88. print("Could not find an SSL directory in '%s'" % (sources,))
  89. sys.stdout.flush()
  90. return best_name
  91. def create_makefile64(makefile, m32):
  92. """Create and fix makefile for 64bit
  93. Replace 32 with 64bit directories
  94. """
  95. if not os.path.isfile(m32):
  96. return
  97. with open(m32) as fin:
  98. with open(makefile, 'w') as fout:
  99. for line in fin:
  100. line = line.replace("=tmp32", "=tmp64")
  101. line = line.replace("=out32", "=out64")
  102. line = line.replace("=inc32", "=inc64")
  103. # force 64 bit machine
  104. line = line.replace("MKLIB=lib", "MKLIB=lib /MACHINE:X64")
  105. line = line.replace("LFLAGS=", "LFLAGS=/MACHINE:X64 ")
  106. # don't link against the lib on 64bit systems
  107. line = line.replace("bufferoverflowu.lib", "")
  108. fout.write(line)
  109. os.unlink(m32)
  110. def fix_makefile(makefile):
  111. """Fix some stuff in all makefiles
  112. """
  113. if not os.path.isfile(makefile):
  114. return
  115. with open(makefile) as fin:
  116. lines = fin.readlines()
  117. with open(makefile, 'w') as fout:
  118. for line in lines:
  119. if line.startswith("PERL="):
  120. continue
  121. if line.startswith("CP="):
  122. line = "CP=copy\n"
  123. if line.startswith("MKDIR="):
  124. line = "MKDIR=mkdir\n"
  125. if line.startswith("CFLAG="):
  126. line = line.strip()
  127. for algo in ("RC5", "MDC2", "IDEA"):
  128. noalgo = " -DOPENSSL_NO_%s" % algo
  129. if noalgo not in line:
  130. line = line + noalgo
  131. line = line + '\n'
  132. fout.write(line)
  133. def run_configure(configure, do_script):
  134. print("perl Configure "+configure)
  135. os.system("perl Configure "+configure)
  136. print(do_script)
  137. os.system(do_script)
  138. def main():
  139. build_all = "-a" in sys.argv
  140. if sys.argv[1] == "Release":
  141. debug = False
  142. elif sys.argv[1] == "Debug":
  143. debug = True
  144. else:
  145. raise ValueError(str(sys.argv))
  146. if sys.argv[2] == "Win32":
  147. arch = "x86"
  148. configure = "VC-WIN32"
  149. do_script = "ms\\do_nasm"
  150. makefile="ms\\nt.mak"
  151. m32 = makefile
  152. elif sys.argv[2] == "x64":
  153. arch="amd64"
  154. configure = "VC-WIN64A"
  155. do_script = "ms\\do_win64a"
  156. makefile = "ms\\nt64.mak"
  157. m32 = makefile.replace('64', '')
  158. #os.environ["VSEXTCOMP_USECL"] = "MS_OPTERON"
  159. else:
  160. raise ValueError(str(sys.argv))
  161. make_flags = ""
  162. if build_all:
  163. make_flags = "-a"
  164. # perl should be on the path, but we also look in "\perl" and "c:\\perl"
  165. # as "well known" locations
  166. perls = find_all_on_path("perl.exe", ["\\perl\\bin", "C:\\perl\\bin"])
  167. perl = find_working_perl(perls)
  168. if perl is None:
  169. print("No Perl installation was found. Existing Makefiles are used.")
  170. print("Found a working perl at '%s'" % (perl,))
  171. sys.stdout.flush()
  172. # Look for SSL 2 levels up from pcbuild - ie, same place zlib etc all live.
  173. ssl_dir = find_best_ssl_dir(("..\\..\\..",))
  174. if ssl_dir is None:
  175. sys.exit(1)
  176. old_cd = os.getcwd()
  177. try:
  178. os.chdir(ssl_dir)
  179. # rebuild makefile when we do the role over from 32 to 64 build
  180. if arch == "amd64" and os.path.isfile(m32) and not os.path.isfile(makefile):
  181. os.unlink(m32)
  182. # If the ssl makefiles do not exist, we invoke Perl to generate them.
  183. # Due to a bug in this script, the makefile sometimes ended up empty
  184. # Force a regeneration if it is.
  185. if not os.path.isfile(makefile) or os.path.getsize(makefile)==0:
  186. if perl is None:
  187. print("Perl is required to build the makefiles!")
  188. sys.exit(1)
  189. print("Creating the makefiles...")
  190. sys.stdout.flush()
  191. # Put our working Perl at the front of our path
  192. os.environ["PATH"] = os.path.dirname(perl) + \
  193. os.pathsep + \
  194. os.environ["PATH"]
  195. run_configure(configure, do_script)
  196. if debug:
  197. print("OpenSSL debug builds aren't supported.")
  198. #if arch=="x86" and debug:
  199. # # the do_masm script in openssl doesn't generate a debug
  200. # # build makefile so we generate it here:
  201. # os.system("perl util\mk1mf.pl debug "+configure+" >"+makefile)
  202. if arch == "amd64":
  203. create_makefile64(makefile, m32)
  204. fix_makefile(makefile)
  205. shutil.copy(r"crypto\buildinf.h", r"crypto\buildinf_%s.h" % arch)
  206. shutil.copy(r"crypto\opensslconf.h", r"crypto\opensslconf_%s.h" % arch)
  207. # Now run make.
  208. if arch == "amd64":
  209. rc = os.system(r"ml64 -c -Foms\uptable.obj ms\uptable.asm")
  210. if rc:
  211. print("ml64 assembler has failed.")
  212. sys.exit(rc)
  213. shutil.copy(r"crypto\buildinf_%s.h" % arch, r"crypto\buildinf.h")
  214. shutil.copy(r"crypto\opensslconf_%s.h" % arch, r"crypto\opensslconf.h")
  215. #makeCommand = "nmake /nologo PERL=\"%s\" -f \"%s\"" %(perl, makefile)
  216. makeCommand = "nmake /nologo -f \"%s\"" % makefile
  217. print("Executing ssl makefiles:", makeCommand)
  218. sys.stdout.flush()
  219. rc = os.system(makeCommand)
  220. if rc:
  221. print("Executing "+makefile+" failed")
  222. print(rc)
  223. sys.exit(rc)
  224. finally:
  225. os.chdir(old_cd)
  226. sys.exit(rc)
  227. if __name__=='__main__':
  228. main()