blob: 5a7a89e4ec9bb6d9a3719df90ecfc8e1b356f26e [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
Mark Hammondf229f9f2002-12-03 05:47:26 +000027
28# Find all "foo.exe" files on the PATH.
29def find_all_on_path(filename, extras = None):
30 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
43# Find a suitable Perl installation for OpenSSL.
44# cygwin perl does *not* work. ActivePerl does.
45# Being a Perl dummy, the simplest way I can check is if the "Win32" package
46# is available.
47def find_working_perl(perls):
48 for perl in perls:
Martin v. Löwisb15d1a72012-05-18 15:28:43 +020049 fh = os.popen('"%s" -e "use Win32;"' % perl)
Mark Hammondf229f9f2002-12-03 05:47:26 +000050 fh.read()
51 rc = fh.close()
52 if rc:
53 continue
54 return perl
Thomas Heller8cef8a82007-08-27 09:42:33 +000055 print("Can not find a suitable PERL:")
Mark Hammondf229f9f2002-12-03 05:47:26 +000056 if perls:
Thomas Heller8cef8a82007-08-27 09:42:33 +000057 print(" the following perl interpreters were found:")
Mark Hammondf229f9f2002-12-03 05:47:26 +000058 for p in perls:
Thomas Heller8cef8a82007-08-27 09:42:33 +000059 print(" ", p)
60 print(" None of these versions appear suitable for building OpenSSL")
Mark Hammondf229f9f2002-12-03 05:47:26 +000061 else:
Thomas Heller8cef8a82007-08-27 09:42:33 +000062 print(" NO perl interpreters were found on this machine at all!")
63 print(" Please install ActivePerl and ensure it appears on your path")
Mark Hammondf229f9f2002-12-03 05:47:26 +000064 return None
65
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'))
Martin v. Löwis71f3f922012-05-18 14:16:53 +020069 with open(propfile) as f:
Martin v. Löwisd18c3972012-05-18 14:20:04 +020070 m = re.search('openssl-([^<]+)<', f.read())
Martin v. Löwis71f3f922012-05-18 14:16:53 +020071 return "..\..\openssl-"+m.group(1)
72
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
185 old_cd = os.getcwd()
186 try:
187 os.chdir(ssl_dir)
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000188 # rebuild makefile when we do the role over from 32 to 64 build
189 if arch == "amd64" and os.path.isfile(m32) and not os.path.isfile(makefile):
190 os.unlink(m32)
191
Mark Hammondf229f9f2002-12-03 05:47:26 +0000192 # If the ssl makefiles do not exist, we invoke Perl to generate them.
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000193 # Due to a bug in this script, the makefile sometimes ended up empty
194 # Force a regeneration if it is.
195 if not os.path.isfile(makefile) or os.path.getsize(makefile)==0:
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000196 if perl is None:
197 print("Perl is required to build the makefiles!")
198 sys.exit(1)
199
Thomas Heller8cef8a82007-08-27 09:42:33 +0000200 print("Creating the makefiles...")
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000201 sys.stdout.flush()
Mark Hammondf229f9f2002-12-03 05:47:26 +0000202 # Put our working Perl at the front of our path
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000203 os.environ["PATH"] = os.path.dirname(perl) + \
Mark Hammondf229f9f2002-12-03 05:47:26 +0000204 os.pathsep + \
205 os.environ["PATH"]
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000206 run_configure(configure, do_script)
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000207 if debug:
208 print("OpenSSL debug builds aren't supported.")
209 #if arch=="x86" and debug:
210 # # the do_masm script in openssl doesn't generate a debug
211 # # build makefile so we generate it here:
212 # os.system("perl util\mk1mf.pl debug "+configure+" >"+makefile)
213
214 if arch == "amd64":
215 create_makefile64(makefile, m32)
216 fix_makefile(makefile)
Hirokazu Yamamoto5e83da32010-09-24 16:36:34 +0000217 copy(r"crypto\buildinf.h", r"crypto\buildinf_%s.h" % arch)
218 copy(r"crypto\opensslconf.h", r"crypto\opensslconf_%s.h" % arch)
Mark Hammondf229f9f2002-12-03 05:47:26 +0000219
Martin v. Löwis26d3fc12010-07-31 10:49:53 +0000220 # If the assembler files don't exist in tmpXX, copy them there
Hirokazu Yamamotode5919d2010-11-04 14:11:32 +0000221 if perl is None and os.path.exists("asm"+dirsuffix):
Hirokazu Yamamoto2f816e62010-09-21 18:23:05 +0000222 if not os.path.exists("tmp"+dirsuffix):
223 os.mkdir("tmp"+dirsuffix)
224 for f in os.listdir("asm"+dirsuffix):
225 if not f.endswith(".asm"): continue
226 if os.path.isfile(r"tmp%s\%s" % (dirsuffix, f)): continue
227 shutil.copy(r"asm%s\%s" % (dirsuffix, f), "tmp"+dirsuffix)
Martin v. Löwis26d3fc12010-07-31 10:49:53 +0000228
Mark Hammondf229f9f2002-12-03 05:47:26 +0000229 # Now run make.
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000230 if arch == "amd64":
Martin v. Löwisdcd1c0c2012-05-18 16:25:04 +0200231 rc = os.system("nasm -f win64 -DNEAR -Ox -g ms\\uptable.asm")
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000232 if rc:
Martin v. Löwisdcd1c0c2012-05-18 16:25:04 +0200233 print("nasm assembler has failed.")
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000234 sys.exit(rc)
235
Hirokazu Yamamoto5e83da32010-09-24 16:36:34 +0000236 copy(r"crypto\buildinf_%s.h" % arch, r"crypto\buildinf.h")
237 copy(r"crypto\opensslconf_%s.h" % arch, r"crypto\opensslconf.h")
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000238
239 #makeCommand = "nmake /nologo PERL=\"%s\" -f \"%s\"" %(perl, makefile)
240 makeCommand = "nmake /nologo -f \"%s\"" % makefile
Thomas Heller8cef8a82007-08-27 09:42:33 +0000241 print("Executing ssl makefiles:", makeCommand)
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000242 sys.stdout.flush()
243 rc = os.system(makeCommand)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000244 if rc:
Thomas Heller8cef8a82007-08-27 09:42:33 +0000245 print("Executing "+makefile+" failed")
246 print(rc)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000247 sys.exit(rc)
Mark Hammondf229f9f2002-12-03 05:47:26 +0000248 finally:
249 os.chdir(old_cd)
Mark Hammondf229f9f2002-12-03 05:47:26 +0000250 sys.exit(rc)
251
252if __name__=='__main__':
253 main()