Hirokazu Yamamoto | f5e607f | 2010-10-01 10:48:47 +0000 | [diff] [blame] | 1 | from __future__ import with_statement, print_function |
Christian Heimes | e8954f8 | 2007-11-22 11:21:16 +0000 | [diff] [blame] | 2 | # Script for building the _ssl and _hashlib modules for Windows. |
| 3 | # Uses Perl to setup the OpenSSL environment correctly |
| 4 | # and build OpenSSL, then invokes a simple nmake session |
| 5 | # for the actual _ssl.pyd and _hashlib.pyd DLLs. |
| 6 | |
| 7 | # THEORETICALLY, you can: |
| 8 | # * Unpack the latest SSL release one level above your main Python source |
| 9 | # directory. It is likely you will already find the zlib library and |
| 10 | # any other external packages there. |
| 11 | # * Install ActivePerl and ensure it is somewhere on your path. |
| 12 | # * Run this script from the PCBuild directory. |
| 13 | # |
| 14 | # it should configure and build SSL, then build the _ssl and _hashlib |
| 15 | # Python extensions without intervention. |
| 16 | |
Christian Heimes | 2336111 | 2007-11-23 07:05:03 +0000 | [diff] [blame] | 17 | # Modified by Christian Heimes |
| 18 | # Now this script supports pre-generated makefiles and assembly files. |
| 19 | # Developers don't need an installation of Perl anymore to build Python. A svn |
| 20 | # checkout from our svn repository is enough. |
| 21 | # |
| 22 | # In Order to create the files in the case of an update you still need Perl. |
| 23 | # Run build_ssl in this order: |
| 24 | # python.exe build_ssl.py Release x64 |
| 25 | # python.exe build_ssl.py Release Win32 |
| 26 | |
Zachary Ware | 10c997a | 2015-07-15 23:33:15 -0500 | [diff] [blame] | 27 | from __future__ import with_statement |
Christian Heimes | e8954f8 | 2007-11-22 11:21:16 +0000 | [diff] [blame] | 28 | import os, sys, re, shutil |
Zachary Ware | 14269d9 | 2016-11-07 13:29:07 -0600 | [diff] [blame^] | 29 | import subprocess |
Christian Heimes | e8954f8 | 2007-11-22 11:21:16 +0000 | [diff] [blame] | 30 | |
| 31 | # Find all "foo.exe" files on the PATH. |
| 32 | def find_all_on_path(filename, extras = None): |
| 33 | entries = os.environ["PATH"].split(os.pathsep) |
| 34 | ret = [] |
| 35 | for p in entries: |
| 36 | fname = os.path.abspath(os.path.join(p, filename)) |
| 37 | if os.path.isfile(fname) and fname not in ret: |
| 38 | ret.append(fname) |
| 39 | if extras: |
| 40 | for p in extras: |
| 41 | fname = os.path.abspath(os.path.join(p, filename)) |
| 42 | if os.path.isfile(fname) and fname not in ret: |
| 43 | ret.append(fname) |
| 44 | return ret |
| 45 | |
| 46 | # Find a suitable Perl installation for OpenSSL. |
| 47 | # cygwin perl does *not* work. ActivePerl does. |
| 48 | # Being a Perl dummy, the simplest way I can check is if the "Win32" package |
| 49 | # is available. |
| 50 | def find_working_perl(perls): |
| 51 | for perl in perls: |
Zachary Ware | 14269d9 | 2016-11-07 13:29:07 -0600 | [diff] [blame^] | 52 | try: |
| 53 | subprocess.check_output([perl, "-e", "use win32;"]) |
| 54 | except Subprocess.CalledProcessError: |
Christian Heimes | e8954f8 | 2007-11-22 11:21:16 +0000 | [diff] [blame] | 55 | continue |
Zachary Ware | 14269d9 | 2016-11-07 13:29:07 -0600 | [diff] [blame^] | 56 | else: |
| 57 | return perl |
Christian Heimes | e8954f8 | 2007-11-22 11:21:16 +0000 | [diff] [blame] | 58 | print("Can not find a suitable PERL:") |
| 59 | if perls: |
| 60 | print(" the following perl interpreters were found:") |
| 61 | for p in perls: |
| 62 | print(" ", p) |
| 63 | print(" None of these versions appear suitable for building OpenSSL") |
| 64 | else: |
| 65 | print(" NO perl interpreters were found on this machine at all!") |
| 66 | print(" Please install ActivePerl and ensure it appears on your path") |
Christian Heimes | e8954f8 | 2007-11-22 11:21:16 +0000 | [diff] [blame] | 67 | return None |
| 68 | |
Martin v. Löwis | 0fc2b74 | 2012-05-18 13:58:30 +0200 | [diff] [blame] | 69 | # Fetch SSL directory from VC properties |
| 70 | def get_ssl_dir(): |
| 71 | propfile = (os.path.join(os.path.dirname(__file__), 'pyproject.vsprops')) |
| 72 | with open(propfile) as f: |
| 73 | m = re.search('openssl-([^"]+)"', f.read()) |
Zachary Ware | 4734372 | 2015-07-16 00:24:48 -0500 | [diff] [blame] | 74 | return "..\..\externals\openssl-"+m.group(1) |
Martin v. Löwis | 0fc2b74 | 2012-05-18 13:58:30 +0200 | [diff] [blame] | 75 | |
Christian Heimes | e8954f8 | 2007-11-22 11:21:16 +0000 | [diff] [blame] | 76 | |
Christian Heimes | 2336111 | 2007-11-23 07:05:03 +0000 | [diff] [blame] | 77 | def create_makefile64(makefile, m32): |
| 78 | """Create and fix makefile for 64bit |
Christian Heimes | e8954f8 | 2007-11-22 11:21:16 +0000 | [diff] [blame] | 79 | |
| 80 | Replace 32 with 64bit directories |
| 81 | """ |
| 82 | if not os.path.isfile(m32): |
| 83 | return |
Martin v. Löwis | c3f5ca1 | 2009-12-21 19:25:56 +0000 | [diff] [blame] | 84 | with open(m32) as fin: |
| 85 | with open(makefile, 'w') as fout: |
Christian Heimes | e8954f8 | 2007-11-22 11:21:16 +0000 | [diff] [blame] | 86 | for line in fin: |
| 87 | line = line.replace("=tmp32", "=tmp64") |
| 88 | line = line.replace("=out32", "=out64") |
| 89 | line = line.replace("=inc32", "=inc64") |
| 90 | # force 64 bit machine |
| 91 | line = line.replace("MKLIB=lib", "MKLIB=lib /MACHINE:X64") |
| 92 | line = line.replace("LFLAGS=", "LFLAGS=/MACHINE:X64 ") |
| 93 | # don't link against the lib on 64bit systems |
| 94 | line = line.replace("bufferoverflowu.lib", "") |
| 95 | fout.write(line) |
| 96 | os.unlink(m32) |
| 97 | |
Christian Heimes | 2336111 | 2007-11-23 07:05:03 +0000 | [diff] [blame] | 98 | def fix_makefile(makefile): |
| 99 | """Fix some stuff in all makefiles |
| 100 | """ |
| 101 | if not os.path.isfile(makefile): |
| 102 | return |
Martin v. Löwis | 9e05135 | 2008-02-29 18:54:45 +0000 | [diff] [blame] | 103 | fin = open(makefile) |
Zachary Ware | 10c997a | 2015-07-15 23:33:15 -0500 | [diff] [blame] | 104 | with open(makefile) as fin: |
Christian Heimes | 2336111 | 2007-11-23 07:05:03 +0000 | [diff] [blame] | 105 | lines = fin.readlines() |
Zachary Ware | 10c997a | 2015-07-15 23:33:15 -0500 | [diff] [blame] | 106 | with open(makefile, 'w') as fout: |
Christian Heimes | 2336111 | 2007-11-23 07:05:03 +0000 | [diff] [blame] | 107 | for line in lines: |
| 108 | if line.startswith("PERL="): |
| 109 | continue |
| 110 | if line.startswith("CP="): |
| 111 | line = "CP=copy\n" |
| 112 | if line.startswith("MKDIR="): |
| 113 | line = "MKDIR=mkdir\n" |
Christian Heimes | 3e9ac99 | 2007-11-24 01:53:59 +0000 | [diff] [blame] | 114 | if line.startswith("CFLAG="): |
| 115 | line = line.strip() |
| 116 | for algo in ("RC5", "MDC2", "IDEA"): |
| 117 | noalgo = " -DOPENSSL_NO_%s" % algo |
| 118 | if noalgo not in line: |
| 119 | line = line + noalgo |
| 120 | line = line + '\n' |
Christian Heimes | 2336111 | 2007-11-23 07:05:03 +0000 | [diff] [blame] | 121 | fout.write(line) |
| 122 | |
Christian Heimes | e8954f8 | 2007-11-22 11:21:16 +0000 | [diff] [blame] | 123 | def run_configure(configure, do_script): |
Zachary Ware | 10c997a | 2015-07-15 23:33:15 -0500 | [diff] [blame] | 124 | print("perl Configure "+configure+" no-idea no-mdc2") |
| 125 | os.system("perl Configure "+configure+" no-idea no-mdc2") |
Christian Heimes | e8954f8 | 2007-11-22 11:21:16 +0000 | [diff] [blame] | 126 | print(do_script) |
| 127 | os.system(do_script) |
| 128 | |
| 129 | def main(): |
| 130 | build_all = "-a" in sys.argv |
| 131 | if sys.argv[1] == "Release": |
| 132 | debug = False |
| 133 | elif sys.argv[1] == "Debug": |
| 134 | debug = True |
| 135 | else: |
| 136 | raise ValueError(str(sys.argv)) |
| 137 | |
| 138 | if sys.argv[2] == "Win32": |
| 139 | arch = "x86" |
| 140 | configure = "VC-WIN32" |
| 141 | do_script = "ms\\do_nasm" |
| 142 | makefile="ms\\nt.mak" |
| 143 | m32 = makefile |
| 144 | elif sys.argv[2] == "x64": |
| 145 | arch="amd64" |
| 146 | configure = "VC-WIN64A" |
| 147 | do_script = "ms\\do_win64a" |
| 148 | makefile = "ms\\nt64.mak" |
| 149 | m32 = makefile.replace('64', '') |
| 150 | #os.environ["VSEXTCOMP_USECL"] = "MS_OPTERON" |
| 151 | else: |
| 152 | raise ValueError(str(sys.argv)) |
| 153 | |
| 154 | make_flags = "" |
| 155 | if build_all: |
| 156 | make_flags = "-a" |
| 157 | # perl should be on the path, but we also look in "\perl" and "c:\\perl" |
| 158 | # as "well known" locations |
Zachary Ware | 71d6ed7 | 2015-04-13 10:46:40 -0500 | [diff] [blame] | 159 | perls = find_all_on_path("perl.exe", [r"\perl\bin", |
| 160 | r"C:\perl\bin", |
| 161 | r"\perl64\bin", |
| 162 | r"C:\perl64\bin", |
| 163 | ]) |
Christian Heimes | e8954f8 | 2007-11-22 11:21:16 +0000 | [diff] [blame] | 164 | perl = find_working_perl(perls) |
Hirokazu Yamamoto | 59734be | 2010-12-09 08:01:18 +0000 | [diff] [blame] | 165 | if perl: |
| 166 | print("Found a working perl at '%s'" % (perl,)) |
Zachary Ware | 14269d9 | 2016-11-07 13:29:07 -0600 | [diff] [blame^] | 167 | # Set PERL for the makefile to find it |
| 168 | os.environ["PERL"] = perl |
Hirokazu Yamamoto | 59734be | 2010-12-09 08:01:18 +0000 | [diff] [blame] | 169 | else: |
Christian Heimes | 2336111 | 2007-11-23 07:05:03 +0000 | [diff] [blame] | 170 | print("No Perl installation was found. Existing Makefiles are used.") |
Christian Heimes | e8954f8 | 2007-11-22 11:21:16 +0000 | [diff] [blame] | 171 | sys.stdout.flush() |
| 172 | # Look for SSL 2 levels up from pcbuild - ie, same place zlib etc all live. |
Martin v. Löwis | 0fc2b74 | 2012-05-18 13:58:30 +0200 | [diff] [blame] | 173 | ssl_dir = get_ssl_dir() |
Christian Heimes | e8954f8 | 2007-11-22 11:21:16 +0000 | [diff] [blame] | 174 | if ssl_dir is None: |
| 175 | sys.exit(1) |
| 176 | |
Zachary Ware | 247b644 | 2014-11-01 17:11:08 -0500 | [diff] [blame] | 177 | # add our copy of NASM to PATH. It will be on the same level as openssl |
| 178 | for dir in os.listdir(os.path.join(ssl_dir, os.pardir)): |
| 179 | if dir.startswith('nasm'): |
| 180 | nasm_dir = os.path.join(ssl_dir, os.pardir, dir) |
| 181 | nasm_dir = os.path.abspath(nasm_dir) |
Zachary Ware | c26afcc | 2015-04-09 20:16:05 -0500 | [diff] [blame] | 182 | old_path = os.environ['PATH'] |
| 183 | os.environ['PATH'] = os.pathsep.join([nasm_dir, old_path]) |
Zachary Ware | 247b644 | 2014-11-01 17:11:08 -0500 | [diff] [blame] | 184 | break |
| 185 | else: |
| 186 | print('NASM was not found, make sure it is on PATH') |
| 187 | |
| 188 | |
Christian Heimes | e8954f8 | 2007-11-22 11:21:16 +0000 | [diff] [blame] | 189 | old_cd = os.getcwd() |
| 190 | try: |
| 191 | os.chdir(ssl_dir) |
| 192 | # rebuild makefile when we do the role over from 32 to 64 build |
| 193 | if arch == "amd64" and os.path.isfile(m32) and not os.path.isfile(makefile): |
| 194 | os.unlink(m32) |
| 195 | |
| 196 | # If the ssl makefiles do not exist, we invoke Perl to generate them. |
| 197 | # Due to a bug in this script, the makefile sometimes ended up empty |
| 198 | # Force a regeneration if it is. |
| 199 | if not os.path.isfile(makefile) or os.path.getsize(makefile)==0: |
Christian Heimes | 2336111 | 2007-11-23 07:05:03 +0000 | [diff] [blame] | 200 | if perl is None: |
| 201 | print("Perl is required to build the makefiles!") |
| 202 | sys.exit(1) |
| 203 | |
Christian Heimes | e8954f8 | 2007-11-22 11:21:16 +0000 | [diff] [blame] | 204 | print("Creating the makefiles...") |
| 205 | sys.stdout.flush() |
| 206 | # Put our working Perl at the front of our path |
| 207 | os.environ["PATH"] = os.path.dirname(perl) + \ |
| 208 | os.pathsep + \ |
| 209 | os.environ["PATH"] |
| 210 | run_configure(configure, do_script) |
Christian Heimes | 2336111 | 2007-11-23 07:05:03 +0000 | [diff] [blame] | 211 | if debug: |
| 212 | print("OpenSSL debug builds aren't supported.") |
| 213 | #if arch=="x86" and debug: |
| 214 | # # the do_masm script in openssl doesn't generate a debug |
| 215 | # # build makefile so we generate it here: |
| 216 | # os.system("perl util\mk1mf.pl debug "+configure+" >"+makefile) |
Christian Heimes | e8954f8 | 2007-11-22 11:21:16 +0000 | [diff] [blame] | 217 | |
Christian Heimes | 2336111 | 2007-11-23 07:05:03 +0000 | [diff] [blame] | 218 | if arch == "amd64": |
| 219 | create_makefile64(makefile, m32) |
| 220 | fix_makefile(makefile) |
Hirokazu Yamamoto | 63e9b50 | 2010-09-21 16:25:21 +0000 | [diff] [blame] | 221 | shutil.copy(r"crypto\buildinf.h", r"crypto\buildinf_%s.h" % arch) |
| 222 | shutil.copy(r"crypto\opensslconf.h", r"crypto\opensslconf_%s.h" % arch) |
Christian Heimes | e8954f8 | 2007-11-22 11:21:16 +0000 | [diff] [blame] | 223 | |
| 224 | # Now run make. |
Christian Heimes | 2336111 | 2007-11-23 07:05:03 +0000 | [diff] [blame] | 225 | if arch == "amd64": |
Benjamin Peterson | 4e33a86 | 2014-05-31 11:01:37 -0700 | [diff] [blame] | 226 | rc = os.system("nasm -f win64 -DNEAR -Ox -g ms\\uptable.asm") |
Christian Heimes | 2336111 | 2007-11-23 07:05:03 +0000 | [diff] [blame] | 227 | if rc: |
Benjamin Peterson | 4e33a86 | 2014-05-31 11:01:37 -0700 | [diff] [blame] | 228 | print("nasm assembler has failed.") |
Christian Heimes | 2336111 | 2007-11-23 07:05:03 +0000 | [diff] [blame] | 229 | sys.exit(rc) |
| 230 | |
Hirokazu Yamamoto | 63e9b50 | 2010-09-21 16:25:21 +0000 | [diff] [blame] | 231 | shutil.copy(r"crypto\buildinf_%s.h" % arch, r"crypto\buildinf.h") |
| 232 | shutil.copy(r"crypto\opensslconf_%s.h" % arch, r"crypto\opensslconf.h") |
Christian Heimes | 2336111 | 2007-11-23 07:05:03 +0000 | [diff] [blame] | 233 | |
| 234 | #makeCommand = "nmake /nologo PERL=\"%s\" -f \"%s\"" %(perl, makefile) |
| 235 | makeCommand = "nmake /nologo -f \"%s\"" % makefile |
Christian Heimes | e8954f8 | 2007-11-22 11:21:16 +0000 | [diff] [blame] | 236 | print("Executing ssl makefiles:", makeCommand) |
| 237 | sys.stdout.flush() |
| 238 | rc = os.system(makeCommand) |
| 239 | if rc: |
| 240 | print("Executing "+makefile+" failed") |
| 241 | print(rc) |
| 242 | sys.exit(rc) |
| 243 | finally: |
| 244 | os.chdir(old_cd) |
| 245 | sys.exit(rc) |
| 246 | |
| 247 | if __name__=='__main__': |
| 248 | main() |