blob: 3cb4f620fdc19107db1dd770b2d1843976bc3e58 [file] [log] [blame]
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001# Script for building the _ssl and _hashlib modules for Windows.
Mark Hammondf229f9f2002-12-03 05:47:26 +00002# Uses Perl to setup the OpenSSL environment correctly
3# and build OpenSSL, then invokes a simple nmake session
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00004# for the actual _ssl.pyd and _hashlib.pyd DLLs.
Mark Hammondf229f9f2002-12-03 05:47:26 +00005
6# THEORETICALLY, you can:
7# * Unpack the latest SSL release one level above your main Python source
8# directory. It is likely you will already find the zlib library and
9# any other external packages there.
10# * Install ActivePerl and ensure it is somewhere on your path.
11# * Run this script from the PCBuild directory.
12#
Thomas Wouters00ee7ba2006-08-21 19:07:27 +000013# it should configure and build SSL, then build the _ssl and _hashlib
14# Python extensions without intervention.
Mark Hammondf229f9f2002-12-03 05:47:26 +000015
Christian Heimes5b5e81c2007-12-31 16:14:33 +000016# Modified by Christian Heimes
17# Now this script supports pre-generated makefiles and assembly files.
18# Developers don't need an installation of Perl anymore to build Python. A svn
19# checkout from our svn repository is enough.
20#
21# In Order to create the files in the case of an update you still need Perl.
22# Run build_ssl in this order:
23# python.exe build_ssl.py Release x64
24# python.exe build_ssl.py Release Win32
25
26import os, sys, re, shutil
Tim Golden9c18fcf2014-05-09 18:01:19 +010027import subprocess
Mark Hammondf229f9f2002-12-03 05:47:26 +000028
29# Find all "foo.exe" files on the PATH.
30def 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.
48def find_working_perl(perls):
49 for perl in perls:
Tim Golden9c18fcf2014-05-09 18:01:19 +010050 try:
51 subprocess.check_output([perl, "-e", "use Win32;"])
52 except subprocess.CalledProcessError:
Mark Hammondf229f9f2002-12-03 05:47:26 +000053 continue
Tim Golden9c18fcf2014-05-09 18:01:19 +010054 else:
55 return perl
56
Mark Hammondf229f9f2002-12-03 05:47:26 +000057 if perls:
Tim Golden9c18fcf2014-05-09 18:01:19 +010058 print("The following perl interpreters were found:")
Mark Hammondf229f9f2002-12-03 05:47:26 +000059 for p in perls:
Thomas Heller8cef8a82007-08-27 09:42:33 +000060 print(" ", p)
61 print(" None of these versions appear suitable for building OpenSSL")
Mark Hammondf229f9f2002-12-03 05:47:26 +000062 else:
Tim Golden9c18fcf2014-05-09 18:01:19 +010063 print("NO perl interpreters were found on this machine at all!")
Thomas Heller8cef8a82007-08-27 09:42:33 +000064 print(" Please install ActivePerl and ensure it appears on your path")
Mark Hammondf229f9f2002-12-03 05:47:26 +000065
Martin v. Löwis71f3f922012-05-18 14:16:53 +020066# Fetch SSL directory from VC properties
67def get_ssl_dir():
Martin v. Löwisd18c3972012-05-18 14:20:04 +020068 propfile = (os.path.join(os.path.dirname(__file__), 'pyproject.props'))
Zachary Ware90441e82014-06-02 12:09:13 -050069 with open(propfile, encoding='utf-8-sig') as f:
Martin v. Löwisd18c3972012-05-18 14:20:04 +020070 m = re.search('openssl-([^<]+)<', f.read())
Zachary Ware4b2b1de2014-11-01 22:39:21 -050071 return "..\externals\openssl-"+m.group(1)
Martin v. Löwis71f3f922012-05-18 14:16:53 +020072
Mark Hammondf229f9f2002-12-03 05:47:26 +000073
Christian Heimes5b5e81c2007-12-31 16:14:33 +000074def create_makefile64(makefile, m32):
75 """Create and fix makefile for 64bit
76
77 Replace 32 with 64bit directories
78 """
79 if not os.path.isfile(m32):
80 return
Martin v. Löwisb90535f2009-12-22 08:54:52 +000081 with open(m32) as fin:
82 with open(makefile, 'w') as fout:
Christian Heimes5b5e81c2007-12-31 16:14:33 +000083 for line in fin:
84 line = line.replace("=tmp32", "=tmp64")
85 line = line.replace("=out32", "=out64")
86 line = line.replace("=inc32", "=inc64")
87 # force 64 bit machine
88 line = line.replace("MKLIB=lib", "MKLIB=lib /MACHINE:X64")
89 line = line.replace("LFLAGS=", "LFLAGS=/MACHINE:X64 ")
90 # don't link against the lib on 64bit systems
91 line = line.replace("bufferoverflowu.lib", "")
92 fout.write(line)
93 os.unlink(m32)
94
95def fix_makefile(makefile):
96 """Fix some stuff in all makefiles
97 """
98 if not os.path.isfile(makefile):
99 return
Martin v. Löwisf10021d2010-07-30 17:29:39 +0000100 with open(makefile) as fin:
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000101 lines = fin.readlines()
Martin v. Löwisf10021d2010-07-30 17:29:39 +0000102 with open(makefile, 'w') as fout:
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000103 for line in lines:
104 if line.startswith("PERL="):
105 continue
106 if line.startswith("CP="):
107 line = "CP=copy\n"
108 if line.startswith("MKDIR="):
109 line = "MKDIR=mkdir\n"
110 if line.startswith("CFLAG="):
111 line = line.strip()
112 for algo in ("RC5", "MDC2", "IDEA"):
113 noalgo = " -DOPENSSL_NO_%s" % algo
114 if noalgo not in line:
115 line = line + noalgo
116 line = line + '\n'
117 fout.write(line)
118
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000119def run_configure(configure, do_script):
Martin v. Löwisf10021d2010-07-30 17:29:39 +0000120 print("perl Configure "+configure+" no-idea no-mdc2")
121 os.system("perl Configure "+configure+" no-idea no-mdc2")
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000122 print(do_script)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000123 os.system(do_script)
124
Hirokazu Yamamoto5e83da32010-09-24 16:36:34 +0000125def cmp(f1, f2):
126 bufsize = 1024 * 8
127 with open(f1, 'rb') as fp1, open(f2, 'rb') as fp2:
128 while True:
129 b1 = fp1.read(bufsize)
130 b2 = fp2.read(bufsize)
131 if b1 != b2:
132 return False
133 if not b1:
134 return True
135
136def copy(src, dst):
137 if os.path.isfile(dst) and cmp(src, dst):
138 return
139 shutil.copy(src, dst)
140
Mark Hammondf229f9f2002-12-03 05:47:26 +0000141def main():
Mark Hammondf229f9f2002-12-03 05:47:26 +0000142 build_all = "-a" in sys.argv
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000143 if sys.argv[1] == "Release":
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000144 debug = False
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000145 elif sys.argv[1] == "Debug":
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000146 debug = True
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000147 else:
148 raise ValueError(str(sys.argv))
149
150 if sys.argv[2] == "Win32":
151 arch = "x86"
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000152 configure = "VC-WIN32"
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000153 do_script = "ms\\do_nasm"
154 makefile="ms\\nt.mak"
155 m32 = makefile
Martin v. Löwis26d3fc12010-07-31 10:49:53 +0000156 dirsuffix = "32"
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000157 elif sys.argv[2] == "x64":
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000158 arch="amd64"
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000159 configure = "VC-WIN64A"
160 do_script = "ms\\do_win64a"
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000161 makefile = "ms\\nt64.mak"
162 m32 = makefile.replace('64', '')
Martin v. Löwis26d3fc12010-07-31 10:49:53 +0000163 dirsuffix = "64"
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000164 #os.environ["VSEXTCOMP_USECL"] = "MS_OPTERON"
165 else:
166 raise ValueError(str(sys.argv))
167
Mark Hammondf229f9f2002-12-03 05:47:26 +0000168 make_flags = ""
169 if build_all:
170 make_flags = "-a"
171 # perl should be on the path, but we also look in "\perl" and "c:\\perl"
172 # as "well known" locations
173 perls = find_all_on_path("perl.exe", ["\\perl\\bin", "C:\\perl\\bin"])
174 perl = find_working_perl(perls)
Hirokazu Yamamoto313dab42010-09-28 18:29:57 +0000175 if perl:
176 print("Found a working perl at '%s'" % (perl,))
177 else:
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000178 print("No Perl installation was found. Existing Makefiles are used.")
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000179 sys.stdout.flush()
Mark Hammondf229f9f2002-12-03 05:47:26 +0000180 # Look for SSL 2 levels up from pcbuild - ie, same place zlib etc all live.
Martin v. Löwis71f3f922012-05-18 14:16:53 +0200181 ssl_dir = get_ssl_dir()
Mark Hammondf229f9f2002-12-03 05:47:26 +0000182 if ssl_dir is None:
183 sys.exit(1)
184
Zachary Wareaa3ea7e2014-11-01 17:11:08 -0500185 # add our copy of NASM to PATH. It will be on the same level as openssl
186 for dir in os.listdir(os.path.join(ssl_dir, os.pardir)):
187 if dir.startswith('nasm'):
188 nasm_dir = os.path.join(ssl_dir, os.pardir, dir)
189 nasm_dir = os.path.abspath(nasm_dir)
190 os.environ['PATH'] += os.pathsep.join(['', nasm_dir])
191 break
192 else:
193 print('NASM was not found, make sure it is on PATH')
194
195
Mark Hammondf229f9f2002-12-03 05:47:26 +0000196 old_cd = os.getcwd()
197 try:
198 os.chdir(ssl_dir)
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000199 # rebuild makefile when we do the role over from 32 to 64 build
200 if arch == "amd64" and os.path.isfile(m32) and not os.path.isfile(makefile):
201 os.unlink(m32)
202
Mark Hammondf229f9f2002-12-03 05:47:26 +0000203 # If the ssl makefiles do not exist, we invoke Perl to generate them.
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000204 # Due to a bug in this script, the makefile sometimes ended up empty
205 # Force a regeneration if it is.
206 if not os.path.isfile(makefile) or os.path.getsize(makefile)==0:
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000207 if perl is None:
208 print("Perl is required to build the makefiles!")
209 sys.exit(1)
210
Thomas Heller8cef8a82007-08-27 09:42:33 +0000211 print("Creating the makefiles...")
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000212 sys.stdout.flush()
Mark Hammondf229f9f2002-12-03 05:47:26 +0000213 # Put our working Perl at the front of our path
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000214 os.environ["PATH"] = os.path.dirname(perl) + \
Mark Hammondf229f9f2002-12-03 05:47:26 +0000215 os.pathsep + \
216 os.environ["PATH"]
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000217 run_configure(configure, do_script)
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000218 if debug:
219 print("OpenSSL debug builds aren't supported.")
220 #if arch=="x86" and debug:
221 # # the do_masm script in openssl doesn't generate a debug
222 # # build makefile so we generate it here:
223 # os.system("perl util\mk1mf.pl debug "+configure+" >"+makefile)
224
225 if arch == "amd64":
226 create_makefile64(makefile, m32)
227 fix_makefile(makefile)
Hirokazu Yamamoto5e83da32010-09-24 16:36:34 +0000228 copy(r"crypto\buildinf.h", r"crypto\buildinf_%s.h" % arch)
229 copy(r"crypto\opensslconf.h", r"crypto\opensslconf_%s.h" % arch)
Mark Hammondf229f9f2002-12-03 05:47:26 +0000230
Martin v. Löwis26d3fc12010-07-31 10:49:53 +0000231 # If the assembler files don't exist in tmpXX, copy them there
Hirokazu Yamamotode5919d2010-11-04 14:11:32 +0000232 if perl is None and os.path.exists("asm"+dirsuffix):
Hirokazu Yamamoto2f816e62010-09-21 18:23:05 +0000233 if not os.path.exists("tmp"+dirsuffix):
234 os.mkdir("tmp"+dirsuffix)
235 for f in os.listdir("asm"+dirsuffix):
236 if not f.endswith(".asm"): continue
237 if os.path.isfile(r"tmp%s\%s" % (dirsuffix, f)): continue
238 shutil.copy(r"asm%s\%s" % (dirsuffix, f), "tmp"+dirsuffix)
Martin v. Löwis26d3fc12010-07-31 10:49:53 +0000239
Mark Hammondf229f9f2002-12-03 05:47:26 +0000240 # Now run make.
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000241 if arch == "amd64":
Martin v. Löwisdcd1c0c2012-05-18 16:25:04 +0200242 rc = os.system("nasm -f win64 -DNEAR -Ox -g ms\\uptable.asm")
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000243 if rc:
Martin v. Löwisdcd1c0c2012-05-18 16:25:04 +0200244 print("nasm assembler has failed.")
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000245 sys.exit(rc)
246
Hirokazu Yamamoto5e83da32010-09-24 16:36:34 +0000247 copy(r"crypto\buildinf_%s.h" % arch, r"crypto\buildinf.h")
248 copy(r"crypto\opensslconf_%s.h" % arch, r"crypto\opensslconf.h")
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000249
250 #makeCommand = "nmake /nologo PERL=\"%s\" -f \"%s\"" %(perl, makefile)
251 makeCommand = "nmake /nologo -f \"%s\"" % makefile
Thomas Heller8cef8a82007-08-27 09:42:33 +0000252 print("Executing ssl makefiles:", makeCommand)
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000253 sys.stdout.flush()
254 rc = os.system(makeCommand)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000255 if rc:
Thomas Heller8cef8a82007-08-27 09:42:33 +0000256 print("Executing "+makefile+" failed")
257 print(rc)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000258 sys.exit(rc)
Mark Hammondf229f9f2002-12-03 05:47:26 +0000259 finally:
260 os.chdir(old_cd)
Mark Hammondf229f9f2002-12-03 05:47:26 +0000261 sys.exit(rc)
262
263if __name__=='__main__':
264 main()