blob: 95e0e7854edaab6794ebd17cbe0132db26a59a36 [file] [log] [blame]
Hirokazu Yamamotof5e607f2010-10-01 10:48:47 +00001from __future__ import with_statement, print_function
Christian Heimese8954f82007-11-22 11:21:16 +00002# 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 Heimes23361112007-11-23 07:05:03 +000017# 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 Ware10c997a2015-07-15 23:33:15 -050027from __future__ import with_statement
Christian Heimese8954f82007-11-22 11:21:16 +000028import os, sys, re, shutil
29
30# Find all "foo.exe" files on the PATH.
31def 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.
49def find_working_perl(perls):
50 for perl in perls:
Hirokazu Yamamoto59734be2010-12-09 08:01:18 +000051 fh = os.popen('"%s" -e "use Win32;"' % perl)
Christian Heimese8954f82007-11-22 11:21:16 +000052 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 Heimese8954f82007-11-22 11:21:16 +000066 return None
67
Martin v. Löwis0fc2b742012-05-18 13:58:30 +020068# Fetch SSL directory from VC properties
69def 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 Ware47343722015-07-16 00:24:48 -050073 return "..\..\externals\openssl-"+m.group(1)
Martin v. Löwis0fc2b742012-05-18 13:58:30 +020074
Christian Heimese8954f82007-11-22 11:21:16 +000075
Christian Heimes23361112007-11-23 07:05:03 +000076def create_makefile64(makefile, m32):
77 """Create and fix makefile for 64bit
Christian Heimese8954f82007-11-22 11:21:16 +000078
79 Replace 32 with 64bit directories
80 """
81 if not os.path.isfile(m32):
82 return
Martin v. Löwisc3f5ca12009-12-21 19:25:56 +000083 with open(m32) as fin:
84 with open(makefile, 'w') as fout:
Christian Heimese8954f82007-11-22 11:21:16 +000085 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 Heimes23361112007-11-23 07:05:03 +000097def fix_makefile(makefile):
98 """Fix some stuff in all makefiles
99 """
100 if not os.path.isfile(makefile):
101 return
Martin v. Löwis9e051352008-02-29 18:54:45 +0000102 fin = open(makefile)
Zachary Ware10c997a2015-07-15 23:33:15 -0500103 with open(makefile) as fin:
Christian Heimes23361112007-11-23 07:05:03 +0000104 lines = fin.readlines()
Zachary Ware10c997a2015-07-15 23:33:15 -0500105 with open(makefile, 'w') as fout:
Christian Heimes23361112007-11-23 07:05:03 +0000106 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 Heimes3e9ac992007-11-24 01:53:59 +0000113 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 Heimes23361112007-11-23 07:05:03 +0000120 fout.write(line)
121
Christian Heimese8954f82007-11-22 11:21:16 +0000122def run_configure(configure, do_script):
Zachary Ware10c997a2015-07-15 23:33:15 -0500123 print("perl Configure "+configure+" no-idea no-mdc2")
124 os.system("perl Configure "+configure+" no-idea no-mdc2")
Christian Heimese8954f82007-11-22 11:21:16 +0000125 print(do_script)
126 os.system(do_script)
127
128def 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 Ware71d6ed72015-04-13 10:46:40 -0500158 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 Heimese8954f82007-11-22 11:21:16 +0000163 perl = find_working_perl(perls)
Hirokazu Yamamoto59734be2010-12-09 08:01:18 +0000164 if perl:
165 print("Found a working perl at '%s'" % (perl,))
166 else:
Christian Heimes23361112007-11-23 07:05:03 +0000167 print("No Perl installation was found. Existing Makefiles are used.")
Christian Heimese8954f82007-11-22 11:21:16 +0000168 sys.stdout.flush()
169 # Look for SSL 2 levels up from pcbuild - ie, same place zlib etc all live.
Martin v. Löwis0fc2b742012-05-18 13:58:30 +0200170 ssl_dir = get_ssl_dir()
Christian Heimese8954f82007-11-22 11:21:16 +0000171 if ssl_dir is None:
172 sys.exit(1)
173
Zachary Ware247b6442014-11-01 17:11:08 -0500174 # 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 Warec26afcc2015-04-09 20:16:05 -0500179 old_path = os.environ['PATH']
180 os.environ['PATH'] = os.pathsep.join([nasm_dir, old_path])
Zachary Ware247b6442014-11-01 17:11:08 -0500181 break
182 else:
183 print('NASM was not found, make sure it is on PATH')
184
185
Christian Heimese8954f82007-11-22 11:21:16 +0000186 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 Heimes23361112007-11-23 07:05:03 +0000197 if perl is None:
198 print("Perl is required to build the makefiles!")
199 sys.exit(1)
200
Christian Heimese8954f82007-11-22 11:21:16 +0000201 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 Heimes23361112007-11-23 07:05:03 +0000208 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 Heimese8954f82007-11-22 11:21:16 +0000214
Christian Heimes23361112007-11-23 07:05:03 +0000215 if arch == "amd64":
216 create_makefile64(makefile, m32)
217 fix_makefile(makefile)
Hirokazu Yamamoto63e9b502010-09-21 16:25:21 +0000218 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 Heimese8954f82007-11-22 11:21:16 +0000220
221 # Now run make.
Christian Heimes23361112007-11-23 07:05:03 +0000222 if arch == "amd64":
Benjamin Peterson4e33a862014-05-31 11:01:37 -0700223 rc = os.system("nasm -f win64 -DNEAR -Ox -g ms\\uptable.asm")
Christian Heimes23361112007-11-23 07:05:03 +0000224 if rc:
Benjamin Peterson4e33a862014-05-31 11:01:37 -0700225 print("nasm assembler has failed.")
Christian Heimes23361112007-11-23 07:05:03 +0000226 sys.exit(rc)
227
Hirokazu Yamamoto63e9b502010-09-21 16:25:21 +0000228 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 Heimes23361112007-11-23 07:05:03 +0000230
231 #makeCommand = "nmake /nologo PERL=\"%s\" -f \"%s\"" %(perl, makefile)
232 makeCommand = "nmake /nologo -f \"%s\"" % makefile
Christian Heimese8954f82007-11-22 11:21:16 +0000233 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
244if __name__=='__main__':
245 main()