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