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