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