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