blob: 0f3c63ee24ffba080ff7a0aeb524fe4dacaa120a [file] [log] [blame]
Steve Dowerac2bd5b2015-07-03 09:08:47 -07001#! /usr/bin/env python3
Zachary Ware10c2dba2014-05-09 09:07:50 -05002# 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 Ware8c9d99f2016-09-05 12:54:08 -050021from __future__ import print_function
22
Zachary Ware10c2dba2014-05-09 09:07:50 -050023import os
Steve Dower68d663c2017-07-17 11:15:48 +020024import re
Zachary Ware10c2dba2014-05-09 09:07:50 -050025import sys
Tim Goldenfaf4d9c2014-05-09 18:01:44 +010026import subprocess
Zachary Ware16f164e2016-02-22 04:02:30 -060027from shutil import copy
Zachary Ware10c2dba2014-05-09 09:07:50 -050028
29# Find all "foo.exe" files on the PATH.
Zachary Ware16f164e2016-02-22 04:02:30 -060030def find_all_on_path(filename, extras=None):
Zachary Ware10c2dba2014-05-09 09:07:50 -050031 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 Ware16f164e2016-02-22 04:02:30 -060044
Zachary Ware10c2dba2014-05-09 09:07:50 -050045# 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.
49def find_working_perl(perls):
50 for perl in perls:
Tim Goldenfaf4d9c2014-05-09 18:01:44 +010051 try:
52 subprocess.check_output([perl, "-e", "use Win32;"])
53 except subprocess.CalledProcessError:
Zachary Ware10c2dba2014-05-09 09:07:50 -050054 continue
Tim Goldenfaf4d9c2014-05-09 18:01:44 +010055 else:
56 return perl
57
Zachary Ware10c2dba2014-05-09 09:07:50 -050058 if perls:
Tim Goldenfaf4d9c2014-05-09 18:01:44 +010059 print("The following perl interpreters were found:")
Zachary Ware10c2dba2014-05-09 09:07:50 -050060 for p in perls:
61 print(" ", p)
62 print(" None of these versions appear suitable for building OpenSSL")
63 else:
Tim Goldenfaf4d9c2014-05-09 18:01:44 +010064 print("NO perl interpreters were found on this machine at all!")
Zachary Ware10c2dba2014-05-09 09:07:50 -050065 print(" Please install ActivePerl and ensure it appears on your path")
Zachary Ware10c2dba2014-05-09 09:07:50 -050066
Zachary Ware10c2dba2014-05-09 09:07:50 -050067
Zachary Ware16f164e2016-02-22 04:02:30 -060068def copy_includes(makefile, suffix):
Steve Dower68d663c2017-07-17 11:15:48 +020069 dir = 'inc'+suffix+'\\openssl'
Zachary Ware8c9d99f2016-09-05 12:54:08 -050070 try:
71 os.makedirs(dir)
72 except OSError:
73 pass
Zachary Warea59f9632015-04-09 15:48:32 -050074 copy_if_different = r'$(PERL) $(SRC_D)\util\copy-if-different.pl'
Zachary Ware10c2dba2014-05-09 09:07:50 -050075 with open(makefile) as fin:
Zachary Ware16f164e2016-02-22 04:02:30 -060076 for line in fin:
Zachary Warea59f9632015-04-09 15:48:32 -050077 if copy_if_different in line:
Zachary Ware16f164e2016-02-22 04:02:30 -060078 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 Ware10c2dba2014-05-09 09:07:50 -050087
88def 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 Dower68d663c2017-07-17 11:15:48 +020094def 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 Ware10c2dba2014-05-09 09:07:50 -0500114
115def prep(arch):
Steve Dower68d663c2017-07-17 11:15:48 +0200116 makefile_template = "ms\\ntdll{}.mak"
Zachary Ware16f164e2016-02-22 04:02:30 -0600117 generated_makefile = makefile_template.format('')
Zachary Ware10c2dba2014-05-09 09:07:50 -0500118 if arch == "x86":
119 configure = "VC-WIN32"
120 do_script = "ms\\do_nasm"
Zachary Ware16f164e2016-02-22 04:02:30 -0600121 suffix = "32"
Zachary Ware10c2dba2014-05-09 09:07:50 -0500122 elif arch == "amd64":
123 configure = "VC-WIN64A"
124 do_script = "ms\\do_win64a"
Zachary Ware16f164e2016-02-22 04:02:30 -0600125 suffix = "64"
Zachary Ware10c2dba2014-05-09 09:07:50 -0500126 else:
127 raise ValueError('Unrecognized platform: %s' % arch)
128
Zachary Ware16f164e2016-02-22 04:02:30 -0600129 print("Creating the makefiles...")
130 sys.stdout.flush()
Steve Dower68d663c2017-07-17 11:15:48 +0200131 # run configure, copy includes, patch files
Zachary Ware16f164e2016-02-22 04:02:30 -0600132 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 Ware10c2dba2014-05-09 09:07:50 -0500140
Steve Dower68d663c2017-07-17 11:15:48 +0200141 print('patching ms\\uplink.c...')
142 fix_uplink()
Zachary Ware10c2dba2014-05-09 09:07:50 -0500143
144def 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 Dower68d663c2017-07-17 11:15:48 +0200150 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 Ware10c2dba2014-05-09 09:07:50 -0500155 print("Too many arguments supplied, all we need is the directory",
Steve Dower68d663c2017-07-17 11:15:48 +0200156 "containing OpenSSL sources and optionally the architecture")
Zachary Ware10c2dba2014-05-09 09:07:50 -0500157 sys.exit(1)
158
159 ssl_dir = sys.argv[1]
Steve Dower68d663c2017-07-17 11:15:48 +0200160 arch = sys.argv[2] if len(sys.argv) >= 3 else None
Zachary Ware10c2dba2014-05-09 09:07:50 -0500161
Charles-François Natali6315ffa2014-06-20 22:41:21 +0100162 if not os.path.isdir(ssl_dir):
Zachary Ware10c2dba2014-05-09 09:07:50 -0500163 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 Ware7dfa0942015-04-13 10:53:11 -0500168 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 Ware10c2dba2014-05-09 09:07:50 -0500173 perl = find_working_perl(perls)
174 if perl:
175 print("Found a working perl at '%s'" % (perl,))
176 else:
177 sys.exit(1)
Zachary Ware16f164e2016-02-22 04:02:30 -0600178 if not find_all_on_path('nmake.exe'):
179 print('Could not find nmake.exe, try running env.bat')
180 sys.exit(1)
Steve Dower79993a92016-03-08 12:50:57 -0800181 if not find_all_on_path('nasm.exe'):
182 print('Could not find nasm.exe, please add to PATH')
183 sys.exit(1)
Zachary Ware10c2dba2014-05-09 09:07:50 -0500184 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 Dower68d663c2017-07-17 11:15:48 +0200194 if arch:
Zachary Ware10c2dba2014-05-09 09:07:50 -0500195 prep(arch)
Steve Dower68d663c2017-07-17 11:15:48 +0200196 else:
197 for arch in ['amd64', 'x86']:
198 prep(arch)
Zachary Ware10c2dba2014-05-09 09:07:50 -0500199 finally:
200 os.chdir(old_cwd)
201
202if __name__=='__main__':
203 main()