/PC/VS7.1/build_ssl.py

http://unladen-swallow.googlecode.com/ · Python · 181 lines · 136 code · 11 blank · 34 comment · 40 complexity · 8e25ed46ef4211a2d19f59b28bc1377b 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. import os, sys, re
  15. # Find all "foo.exe" files on the PATH.
  16. def find_all_on_path(filename, extras = None):
  17. entries = os.environ["PATH"].split(os.pathsep)
  18. ret = []
  19. for p in entries:
  20. fname = os.path.abspath(os.path.join(p, filename))
  21. if os.path.isfile(fname) and fname not in ret:
  22. ret.append(fname)
  23. if extras:
  24. for p in extras:
  25. fname = os.path.abspath(os.path.join(p, filename))
  26. if os.path.isfile(fname) and fname not in ret:
  27. ret.append(fname)
  28. return ret
  29. # Find a suitable Perl installation for OpenSSL.
  30. # cygwin perl does *not* work. ActivePerl does.
  31. # Being a Perl dummy, the simplest way I can check is if the "Win32" package
  32. # is available.
  33. def find_working_perl(perls):
  34. for perl in perls:
  35. fh = os.popen(perl + ' -e "use Win32;"')
  36. fh.read()
  37. rc = fh.close()
  38. if rc:
  39. continue
  40. return perl
  41. print "Can not find a suitable PERL:"
  42. if perls:
  43. print " the following perl interpreters were found:"
  44. for p in perls:
  45. print " ", p
  46. print " None of these versions appear suitable for building OpenSSL"
  47. else:
  48. print " NO perl interpreters were found on this machine at all!"
  49. print " Please install ActivePerl and ensure it appears on your path"
  50. print "The Python SSL module was not built"
  51. return None
  52. # Locate the best SSL directory given a few roots to look into.
  53. def find_best_ssl_dir(sources):
  54. candidates = []
  55. for s in sources:
  56. try:
  57. # note: do not abspath s; the build will fail if any
  58. # higher up directory name has spaces in it.
  59. fnames = os.listdir(s)
  60. except os.error:
  61. fnames = []
  62. for fname in fnames:
  63. fqn = os.path.join(s, fname)
  64. if os.path.isdir(fqn) and fname.startswith("openssl-"):
  65. candidates.append(fqn)
  66. # Now we have all the candidates, locate the best.
  67. best_parts = []
  68. best_name = None
  69. for c in candidates:
  70. parts = re.split("[.-]", os.path.basename(c))[1:]
  71. # eg - openssl-0.9.7-beta1 - ignore all "beta" or any other qualifiers
  72. if len(parts) >= 4:
  73. continue
  74. if parts > best_parts:
  75. best_parts = parts
  76. best_name = c
  77. if best_name is not None:
  78. print "Found an SSL directory at '%s'" % (best_name,)
  79. else:
  80. print "Could not find an SSL directory in '%s'" % (sources,)
  81. sys.stdout.flush()
  82. return best_name
  83. def run_configure(configure, do_script):
  84. os.system("perl Configure "+configure)
  85. os.system(do_script)
  86. def main():
  87. build_all = "-a" in sys.argv
  88. if sys.argv[1] == "Release":
  89. arch = "x86"
  90. debug = False
  91. configure = "VC-WIN32"
  92. do_script = "ms\\do_masm"
  93. makefile = "ms\\nt.mak"
  94. elif sys.argv[1] == "Debug":
  95. arch = "x86"
  96. debug = True
  97. configure = "VC-WIN32"
  98. do_script = "ms\\do_masm"
  99. makefile="ms\\d32.mak"
  100. elif sys.argv[1] == "ReleaseItanium":
  101. arch = "ia64"
  102. debug = False
  103. configure = "VC-WIN64I"
  104. do_script = "ms\\do_win64i"
  105. makefile = "ms\\nt.mak"
  106. os.environ["VSEXTCOMP_USECL"] = "MS_ITANIUM"
  107. elif sys.argv[1] == "ReleaseAMD64":
  108. arch="amd64"
  109. debug=False
  110. configure = "VC-WIN64A"
  111. do_script = "ms\\do_win64a"
  112. makefile = "ms\\nt.mak"
  113. os.environ["VSEXTCOMP_USECL"] = "MS_OPTERON"
  114. make_flags = ""
  115. if build_all:
  116. make_flags = "-a"
  117. # perl should be on the path, but we also look in "\perl" and "c:\\perl"
  118. # as "well known" locations
  119. perls = find_all_on_path("perl.exe", ["\\perl\\bin", "C:\\perl\\bin"])
  120. perl = find_working_perl(perls)
  121. if perl is None:
  122. sys.exit(1)
  123. print "Found a working perl at '%s'" % (perl,)
  124. sys.stdout.flush()
  125. # Look for SSL 2 levels up from pcbuild - ie, same place zlib etc all live.
  126. ssl_dir = find_best_ssl_dir(("..\\..\\..",))
  127. if ssl_dir is None:
  128. sys.exit(1)
  129. old_cd = os.getcwd()
  130. try:
  131. os.chdir(ssl_dir)
  132. # If the ssl makefiles do not exist, we invoke Perl to generate them.
  133. # Due to a bug in this script, the makefile sometimes ended up empty
  134. # Force a regeneration if it is.
  135. if not os.path.isfile(makefile) or os.path.getsize(makefile)==0:
  136. print "Creating the makefiles..."
  137. sys.stdout.flush()
  138. # Put our working Perl at the front of our path
  139. os.environ["PATH"] = os.path.dirname(perl) + \
  140. os.pathsep + \
  141. os.environ["PATH"]
  142. run_configure(configure, do_script)
  143. if arch=="x86" and debug:
  144. # the do_masm script in openssl doesn't generate a debug
  145. # build makefile so we generate it here:
  146. os.system("perl util\mk1mf.pl debug "+configure+" >"+makefile)
  147. # Now run make.
  148. makeCommand = "nmake /nologo PERL=\"%s\" -f \"%s\"" %(perl, makefile)
  149. print "Executing ssl makefiles:", makeCommand
  150. sys.stdout.flush()
  151. rc = os.system(makeCommand)
  152. if rc:
  153. print "Executing "+makefile+" failed"
  154. print rc
  155. sys.exit(rc)
  156. finally:
  157. os.chdir(old_cd)
  158. # And finally, we can build the _ssl module itself for Python.
  159. defs = "SSL_DIR=\"%s\"" % (ssl_dir,)
  160. if debug:
  161. defs = defs + " " + "DEBUG=1"
  162. if arch in ('amd64', 'ia64'):
  163. defs = defs + " EXTRA_CFLAGS=/GS- EXTRA_LIBS=bufferoverflowU.lib"
  164. makeCommand = 'nmake /nologo -f _ssl.mak ' + defs + " " + make_flags
  165. print "Executing:", makeCommand
  166. sys.stdout.flush()
  167. rc = os.system(makeCommand)
  168. sys.exit(rc)
  169. if __name__=='__main__':
  170. main()