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