blob: 34292545192098922aeef9238bbe684348eceac4 [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
Zachary Ware10c2dba2014-05-09 09:07:50 -050024import sys
Tim Goldenfaf4d9c2014-05-09 18:01:44 +010025import subprocess
Zachary Ware16f164e2016-02-22 04:02:30 -060026from shutil import copy
Zachary Ware10c2dba2014-05-09 09:07:50 -050027
28# Find all "foo.exe" files on the PATH.
Zachary Ware16f164e2016-02-22 04:02:30 -060029def find_all_on_path(filename, extras=None):
Zachary Ware10c2dba2014-05-09 09:07:50 -050030 entries = os.environ["PATH"].split(os.pathsep)
31 ret = []
32 for p in entries:
33 fname = os.path.abspath(os.path.join(p, filename))
34 if os.path.isfile(fname) and fname not in ret:
35 ret.append(fname)
36 if extras:
37 for p in extras:
38 fname = os.path.abspath(os.path.join(p, filename))
39 if os.path.isfile(fname) and fname not in ret:
40 ret.append(fname)
41 return ret
42
Zachary Ware16f164e2016-02-22 04:02:30 -060043
Zachary Ware10c2dba2014-05-09 09:07:50 -050044# 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.
48def find_working_perl(perls):
49 for perl in perls:
Tim Goldenfaf4d9c2014-05-09 18:01:44 +010050 try:
51 subprocess.check_output([perl, "-e", "use Win32;"])
52 except subprocess.CalledProcessError:
Zachary Ware10c2dba2014-05-09 09:07:50 -050053 continue
Tim Goldenfaf4d9c2014-05-09 18:01:44 +010054 else:
55 return perl
56
Zachary Ware10c2dba2014-05-09 09:07:50 -050057 if perls:
Tim Goldenfaf4d9c2014-05-09 18:01:44 +010058 print("The following perl interpreters were found:")
Zachary Ware10c2dba2014-05-09 09:07:50 -050059 for p in perls:
60 print(" ", p)
61 print(" None of these versions appear suitable for building OpenSSL")
62 else:
Tim Goldenfaf4d9c2014-05-09 18:01:44 +010063 print("NO perl interpreters were found on this machine at all!")
Zachary Ware10c2dba2014-05-09 09:07:50 -050064 print(" Please install ActivePerl and ensure it appears on your path")
Zachary Ware10c2dba2014-05-09 09:07:50 -050065
Zachary Ware10c2dba2014-05-09 09:07:50 -050066
Zachary Ware16f164e2016-02-22 04:02:30 -060067def create_asms(makefile, tmp_d):
Zachary Ware2897d072014-08-06 22:47:23 -050068 #create a custom makefile out of the provided one
69 asm_makefile = os.path.splitext(makefile)[0] + '.asm.mak'
Zachary Ware16f164e2016-02-22 04:02:30 -060070 with open(makefile) as fin, open(asm_makefile, 'w') as fout:
71 for line in fin:
72 # Keep everything up to the install target (it's convenient)
73 if line.startswith('install: all'):
74 break
75 fout.write(line)
76 asms = []
77 for line in fin:
78 if '.asm' in line and line.strip().endswith('.pl'):
79 asms.append(line.split(':')[0])
80 while line.strip():
Zachary Ware2897d072014-08-06 22:47:23 -050081 fout.write(line)
Zachary Ware16f164e2016-02-22 04:02:30 -060082 line = next(fin)
83 fout.write('\n')
Zachary Ware2897d072014-08-06 22:47:23 -050084
Zachary Ware16f164e2016-02-22 04:02:30 -060085 fout.write('asms: $(TMP_D) ')
86 fout.write(' '.join(asms))
87 fout.write('\n')
88 os.system('nmake /f {} PERL=perl TMP_D={} asms'.format(asm_makefile, tmp_d))
Zachary Ware2897d072014-08-06 22:47:23 -050089
90
Zachary Ware16f164e2016-02-22 04:02:30 -060091def copy_includes(makefile, suffix):
92 dir = 'include'+suffix+'\\openssl'
Zachary Ware8c9d99f2016-09-05 12:54:08 -050093 try:
94 os.makedirs(dir)
95 except OSError:
96 pass
Zachary Warea59f9632015-04-09 15:48:32 -050097 copy_if_different = r'$(PERL) $(SRC_D)\util\copy-if-different.pl'
Zachary Ware10c2dba2014-05-09 09:07:50 -050098 with open(makefile) as fin:
Zachary Ware16f164e2016-02-22 04:02:30 -060099 for line in fin:
Zachary Warea59f9632015-04-09 15:48:32 -0500100 if copy_if_different in line:
Zachary Ware16f164e2016-02-22 04:02:30 -0600101 perl, script, src, dest = line.split()
102 if not '$(INCO_D)' in dest:
103 continue
104 # We're in the root of the source tree
105 src = src.replace('$(SRC_D)', '.').strip('"')
106 dest = dest.strip('"').replace('$(INCO_D)', dir)
107 print('copying', src, 'to', dest)
108 copy(src, dest)
109
Zachary Ware10c2dba2014-05-09 09:07:50 -0500110
111def run_configure(configure, do_script):
112 print("perl Configure "+configure+" no-idea no-mdc2")
113 os.system("perl Configure "+configure+" no-idea no-mdc2")
114 print(do_script)
115 os.system(do_script)
116
Zachary Ware10c2dba2014-05-09 09:07:50 -0500117
118def prep(arch):
Zachary Ware16f164e2016-02-22 04:02:30 -0600119 makefile_template = "ms\\nt{}.mak"
120 generated_makefile = makefile_template.format('')
Zachary Ware10c2dba2014-05-09 09:07:50 -0500121 if arch == "x86":
122 configure = "VC-WIN32"
123 do_script = "ms\\do_nasm"
Zachary Ware16f164e2016-02-22 04:02:30 -0600124 suffix = "32"
Zachary Ware10c2dba2014-05-09 09:07:50 -0500125 elif arch == "amd64":
126 configure = "VC-WIN64A"
127 do_script = "ms\\do_win64a"
Zachary Ware16f164e2016-02-22 04:02:30 -0600128 suffix = "64"
Zachary Ware10c2dba2014-05-09 09:07:50 -0500129 #os.environ["VSEXTCOMP_USECL"] = "MS_OPTERON"
130 else:
131 raise ValueError('Unrecognized platform: %s' % arch)
132
Zachary Ware16f164e2016-02-22 04:02:30 -0600133 print("Creating the makefiles...")
134 sys.stdout.flush()
135 # run configure, copy includes, create asms
136 run_configure(configure, do_script)
137 makefile = makefile_template.format(suffix)
138 try:
139 os.unlink(makefile)
140 except FileNotFoundError:
141 pass
142 os.rename(generated_makefile, makefile)
143 copy_includes(makefile, suffix)
Zachary Ware10c2dba2014-05-09 09:07:50 -0500144
Zachary Ware2897d072014-08-06 22:47:23 -0500145 print('creating asms...')
Zachary Ware16f164e2016-02-22 04:02:30 -0600146 create_asms(makefile, 'tmp'+suffix)
147
Zachary Ware10c2dba2014-05-09 09:07:50 -0500148
149def main():
150 if len(sys.argv) == 1:
151 print("Not enough arguments: directory containing OpenSSL",
152 "sources must be supplied")
153 sys.exit(1)
154
155 if len(sys.argv) > 2:
156 print("Too many arguments supplied, all we need is the directory",
157 "containing OpenSSL sources")
158 sys.exit(1)
159
160 ssl_dir = sys.argv[1]
161
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)
194 for arch in ['amd64', 'x86']:
195 prep(arch)
196 finally:
197 os.chdir(old_cwd)
198
199if __name__=='__main__':
200 main()