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