Steve Dower | ac2bd5b | 2015-07-03 09:08:47 -0700 | [diff] [blame] | 1 | #! /usr/bin/env python3 |
Zachary Ware | 10c2dba | 2014-05-09 09:07:50 -0500 | [diff] [blame] | 2 | # Script for preparing OpenSSL for building on Windows. |
| 3 | # Uses Perl to create nmake makefiles and otherwise prepare the way |
| 4 | # for building on 32 or 64 bit platforms. |
| 5 | |
| 6 | # Script originally authored by Mark Hammond. |
| 7 | # Major revisions by: |
| 8 | # Martin v. Löwis |
| 9 | # Christian Heimes |
| 10 | # Zachary Ware |
| 11 | |
| 12 | # THEORETICALLY, you can: |
| 13 | # * Unpack the latest OpenSSL release where $(opensslDir) in |
| 14 | # PCbuild\pyproject.props expects it to be. |
| 15 | # * Install ActivePerl and ensure it is somewhere on your path. |
| 16 | # * Run this script with the OpenSSL source dir as the only argument. |
| 17 | # |
| 18 | # it should configure OpenSSL such that it is ready to be built by |
| 19 | # ssl.vcxproj on 32 or 64 bit platforms. |
| 20 | |
Zachary Ware | 8c9d99f | 2016-09-05 12:54:08 -0500 | [diff] [blame] | 21 | from __future__ import print_function |
| 22 | |
Zachary Ware | 10c2dba | 2014-05-09 09:07:50 -0500 | [diff] [blame] | 23 | import os |
Steve Dower | 68d663c | 2017-07-17 11:15:48 +0200 | [diff] [blame] | 24 | import re |
Zachary Ware | 10c2dba | 2014-05-09 09:07:50 -0500 | [diff] [blame] | 25 | import sys |
Tim Golden | faf4d9c | 2014-05-09 18:01:44 +0100 | [diff] [blame] | 26 | import subprocess |
Zachary Ware | 16f164e | 2016-02-22 04:02:30 -0600 | [diff] [blame] | 27 | from shutil import copy |
Zachary Ware | 10c2dba | 2014-05-09 09:07:50 -0500 | [diff] [blame] | 28 | |
| 29 | # Find all "foo.exe" files on the PATH. |
Zachary Ware | 16f164e | 2016-02-22 04:02:30 -0600 | [diff] [blame] | 30 | def find_all_on_path(filename, extras=None): |
Zachary Ware | 10c2dba | 2014-05-09 09:07:50 -0500 | [diff] [blame] | 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 | |
Zachary Ware | 16f164e | 2016-02-22 04:02:30 -0600 | [diff] [blame] | 44 | |
Zachary Ware | 10c2dba | 2014-05-09 09:07:50 -0500 | [diff] [blame] | 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: |
Tim Golden | faf4d9c | 2014-05-09 18:01:44 +0100 | [diff] [blame] | 51 | try: |
| 52 | subprocess.check_output([perl, "-e", "use Win32;"]) |
| 53 | except subprocess.CalledProcessError: |
Zachary Ware | 10c2dba | 2014-05-09 09:07:50 -0500 | [diff] [blame] | 54 | continue |
Tim Golden | faf4d9c | 2014-05-09 18:01:44 +0100 | [diff] [blame] | 55 | else: |
| 56 | return perl |
| 57 | |
Zachary Ware | 10c2dba | 2014-05-09 09:07:50 -0500 | [diff] [blame] | 58 | if perls: |
Tim Golden | faf4d9c | 2014-05-09 18:01:44 +0100 | [diff] [blame] | 59 | print("The following perl interpreters were found:") |
Zachary Ware | 10c2dba | 2014-05-09 09:07:50 -0500 | [diff] [blame] | 60 | for p in perls: |
| 61 | print(" ", p) |
| 62 | print(" None of these versions appear suitable for building OpenSSL") |
| 63 | else: |
Tim Golden | faf4d9c | 2014-05-09 18:01:44 +0100 | [diff] [blame] | 64 | print("NO perl interpreters were found on this machine at all!") |
Zachary Ware | 10c2dba | 2014-05-09 09:07:50 -0500 | [diff] [blame] | 65 | print(" Please install ActivePerl and ensure it appears on your path") |
Zachary Ware | 10c2dba | 2014-05-09 09:07:50 -0500 | [diff] [blame] | 66 | |
Zachary Ware | 10c2dba | 2014-05-09 09:07:50 -0500 | [diff] [blame] | 67 | |
Zachary Ware | 16f164e | 2016-02-22 04:02:30 -0600 | [diff] [blame] | 68 | def copy_includes(makefile, suffix): |
Steve Dower | 68d663c | 2017-07-17 11:15:48 +0200 | [diff] [blame] | 69 | dir = 'inc'+suffix+'\\openssl' |
Zachary Ware | 8c9d99f | 2016-09-05 12:54:08 -0500 | [diff] [blame] | 70 | try: |
| 71 | os.makedirs(dir) |
| 72 | except OSError: |
| 73 | pass |
Zachary Ware | a59f963 | 2015-04-09 15:48:32 -0500 | [diff] [blame] | 74 | copy_if_different = r'$(PERL) $(SRC_D)\util\copy-if-different.pl' |
Zachary Ware | 10c2dba | 2014-05-09 09:07:50 -0500 | [diff] [blame] | 75 | with open(makefile) as fin: |
Zachary Ware | 16f164e | 2016-02-22 04:02:30 -0600 | [diff] [blame] | 76 | for line in fin: |
Zachary Ware | a59f963 | 2015-04-09 15:48:32 -0500 | [diff] [blame] | 77 | if copy_if_different in line: |
Zachary Ware | 16f164e | 2016-02-22 04:02:30 -0600 | [diff] [blame] | 78 | perl, script, src, dest = line.split() |
| 79 | if not '$(INCO_D)' in dest: |
| 80 | continue |
| 81 | # We're in the root of the source tree |
| 82 | src = src.replace('$(SRC_D)', '.').strip('"') |
| 83 | dest = dest.strip('"').replace('$(INCO_D)', dir) |
| 84 | print('copying', src, 'to', dest) |
| 85 | copy(src, dest) |
| 86 | |
Zachary Ware | 10c2dba | 2014-05-09 09:07:50 -0500 | [diff] [blame] | 87 | |
| 88 | def run_configure(configure, do_script): |
| 89 | print("perl Configure "+configure+" no-idea no-mdc2") |
| 90 | os.system("perl Configure "+configure+" no-idea no-mdc2") |
| 91 | print(do_script) |
| 92 | os.system(do_script) |
| 93 | |
Steve Dower | 68d663c | 2017-07-17 11:15:48 +0200 | [diff] [blame] | 94 | def fix_uplink(): |
| 95 | # uplink.c tries to find the OPENSSL_Applink function exported from the current |
| 96 | # executable. However, we export it from _ssl[_d].pyd instead. So we update the |
| 97 | # module name here before building. |
| 98 | with open('ms\\uplink.c', 'r', encoding='utf-8') as f1: |
| 99 | code = list(f1) |
| 100 | os.replace('ms\\uplink.c', 'ms\\uplink.c.orig') |
| 101 | already_patched = False |
| 102 | with open('ms\\uplink.c', 'w', encoding='utf-8') as f2: |
| 103 | for line in code: |
| 104 | if not already_patched: |
| 105 | if re.search('MODIFIED FOR CPYTHON _ssl MODULE', line): |
| 106 | already_patched = True |
| 107 | elif re.match(r'^\s+if\s*\(\(h\s*=\s*GetModuleHandle[AW]?\(NULL\)\)\s*==\s*NULL\)', line): |
| 108 | f2.write("/* MODIFIED FOR CPYTHON _ssl MODULE */\n") |
| 109 | f2.write('if ((h = GetModuleHandleW(L"_ssl.pyd")) == NULL) if ((h = GetModuleHandleW(L"_ssl_d.pyd")) == NULL)\n') |
| 110 | already_patched = True |
| 111 | f2.write(line) |
| 112 | if not already_patched: |
| 113 | print("WARN: failed to patch ms\\uplink.c") |
Zachary Ware | 10c2dba | 2014-05-09 09:07:50 -0500 | [diff] [blame] | 114 | |
| 115 | def prep(arch): |
Steve Dower | 68d663c | 2017-07-17 11:15:48 +0200 | [diff] [blame] | 116 | makefile_template = "ms\\ntdll{}.mak" |
Zachary Ware | 16f164e | 2016-02-22 04:02:30 -0600 | [diff] [blame] | 117 | generated_makefile = makefile_template.format('') |
Zachary Ware | 10c2dba | 2014-05-09 09:07:50 -0500 | [diff] [blame] | 118 | if arch == "x86": |
| 119 | configure = "VC-WIN32" |
| 120 | do_script = "ms\\do_nasm" |
Zachary Ware | 16f164e | 2016-02-22 04:02:30 -0600 | [diff] [blame] | 121 | suffix = "32" |
Zachary Ware | 10c2dba | 2014-05-09 09:07:50 -0500 | [diff] [blame] | 122 | elif arch == "amd64": |
| 123 | configure = "VC-WIN64A" |
| 124 | do_script = "ms\\do_win64a" |
Zachary Ware | 16f164e | 2016-02-22 04:02:30 -0600 | [diff] [blame] | 125 | suffix = "64" |
Zachary Ware | 10c2dba | 2014-05-09 09:07:50 -0500 | [diff] [blame] | 126 | else: |
| 127 | raise ValueError('Unrecognized platform: %s' % arch) |
| 128 | |
Zachary Ware | 16f164e | 2016-02-22 04:02:30 -0600 | [diff] [blame] | 129 | print("Creating the makefiles...") |
| 130 | sys.stdout.flush() |
Steve Dower | 68d663c | 2017-07-17 11:15:48 +0200 | [diff] [blame] | 131 | # run configure, copy includes, patch files |
Zachary Ware | 16f164e | 2016-02-22 04:02:30 -0600 | [diff] [blame] | 132 | run_configure(configure, do_script) |
| 133 | makefile = makefile_template.format(suffix) |
| 134 | try: |
| 135 | os.unlink(makefile) |
| 136 | except FileNotFoundError: |
| 137 | pass |
| 138 | os.rename(generated_makefile, makefile) |
| 139 | copy_includes(makefile, suffix) |
Zachary Ware | 10c2dba | 2014-05-09 09:07:50 -0500 | [diff] [blame] | 140 | |
Steve Dower | 68d663c | 2017-07-17 11:15:48 +0200 | [diff] [blame] | 141 | print('patching ms\\uplink.c...') |
| 142 | fix_uplink() |
Zachary Ware | 10c2dba | 2014-05-09 09:07:50 -0500 | [diff] [blame] | 143 | |
| 144 | def main(): |
| 145 | if len(sys.argv) == 1: |
| 146 | print("Not enough arguments: directory containing OpenSSL", |
| 147 | "sources must be supplied") |
| 148 | sys.exit(1) |
| 149 | |
Steve Dower | 68d663c | 2017-07-17 11:15:48 +0200 | [diff] [blame] | 150 | if len(sys.argv) == 3 and sys.argv[2] not in ('x86', 'amd64'): |
| 151 | print("Second argument must be x86 or amd64") |
| 152 | sys.exit(1) |
| 153 | |
| 154 | if len(sys.argv) > 3: |
Zachary Ware | 10c2dba | 2014-05-09 09:07:50 -0500 | [diff] [blame] | 155 | print("Too many arguments supplied, all we need is the directory", |
Steve Dower | 68d663c | 2017-07-17 11:15:48 +0200 | [diff] [blame] | 156 | "containing OpenSSL sources and optionally the architecture") |
Zachary Ware | 10c2dba | 2014-05-09 09:07:50 -0500 | [diff] [blame] | 157 | sys.exit(1) |
| 158 | |
| 159 | ssl_dir = sys.argv[1] |
Steve Dower | 68d663c | 2017-07-17 11:15:48 +0200 | [diff] [blame] | 160 | arch = sys.argv[2] if len(sys.argv) >= 3 else None |
Zachary Ware | 10c2dba | 2014-05-09 09:07:50 -0500 | [diff] [blame] | 161 | |
Charles-François Natali | 6315ffa | 2014-06-20 22:41:21 +0100 | [diff] [blame] | 162 | if not os.path.isdir(ssl_dir): |
Zachary Ware | 10c2dba | 2014-05-09 09:07:50 -0500 | [diff] [blame] | 163 | print(ssl_dir, "is not an existing directory!") |
| 164 | sys.exit(1) |
| 165 | |
| 166 | # perl should be on the path, but we also look in "\perl" and "c:\\perl" |
| 167 | # as "well known" locations |
Zachary Ware | 7dfa094 | 2015-04-13 10:53:11 -0500 | [diff] [blame] | 168 | perls = find_all_on_path("perl.exe", [r"\perl\bin", |
| 169 | r"C:\perl\bin", |
| 170 | r"\perl64\bin", |
| 171 | r"C:\perl64\bin", |
| 172 | ]) |
Zachary Ware | 10c2dba | 2014-05-09 09:07:50 -0500 | [diff] [blame] | 173 | perl = find_working_perl(perls) |
| 174 | if perl: |
| 175 | print("Found a working perl at '%s'" % (perl,)) |
| 176 | else: |
| 177 | sys.exit(1) |
Zachary Ware | 16f164e | 2016-02-22 04:02:30 -0600 | [diff] [blame] | 178 | if not find_all_on_path('nmake.exe'): |
| 179 | print('Could not find nmake.exe, try running env.bat') |
| 180 | sys.exit(1) |
Steve Dower | 79993a9 | 2016-03-08 12:50:57 -0800 | [diff] [blame] | 181 | if not find_all_on_path('nasm.exe'): |
| 182 | print('Could not find nasm.exe, please add to PATH') |
| 183 | sys.exit(1) |
Zachary Ware | 10c2dba | 2014-05-09 09:07:50 -0500 | [diff] [blame] | 184 | sys.stdout.flush() |
| 185 | |
| 186 | # Put our working Perl at the front of our path |
| 187 | os.environ["PATH"] = os.path.dirname(perl) + \ |
| 188 | os.pathsep + \ |
| 189 | os.environ["PATH"] |
| 190 | |
| 191 | old_cwd = os.getcwd() |
| 192 | try: |
| 193 | os.chdir(ssl_dir) |
Steve Dower | 68d663c | 2017-07-17 11:15:48 +0200 | [diff] [blame] | 194 | if arch: |
Zachary Ware | 10c2dba | 2014-05-09 09:07:50 -0500 | [diff] [blame] | 195 | prep(arch) |
Steve Dower | 68d663c | 2017-07-17 11:15:48 +0200 | [diff] [blame] | 196 | else: |
| 197 | for arch in ['amd64', 'x86']: |
| 198 | prep(arch) |
Zachary Ware | 10c2dba | 2014-05-09 09:07:50 -0500 | [diff] [blame] | 199 | finally: |
| 200 | os.chdir(old_cwd) |
| 201 | |
| 202 | if __name__=='__main__': |
| 203 | main() |