blob: 5858d5db43ff3f5bed0859b49c74006fa15d596f [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:
49 fh = os.popen(perl + ' -e "use Win32;"')
50 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
66# Locate the best SSL directory given a few roots to look into.
67def find_best_ssl_dir(sources):
68 candidates = []
69 for s in sources:
70 try:
Thomas Wouters00ee7ba2006-08-21 19:07:27 +000071 # note: do not abspath s; the build will fail if any
72 # higher up directory name has spaces in it.
Mark Hammondf229f9f2002-12-03 05:47:26 +000073 fnames = os.listdir(s)
74 except os.error:
75 fnames = []
76 for fname in fnames:
77 fqn = os.path.join(s, fname)
78 if os.path.isdir(fqn) and fname.startswith("openssl-"):
79 candidates.append(fqn)
80 # Now we have all the candidates, locate the best.
81 best_parts = []
82 best_name = None
83 for c in candidates:
84 parts = re.split("[.-]", os.path.basename(c))[1:]
85 # eg - openssl-0.9.7-beta1 - ignore all "beta" or any other qualifiers
86 if len(parts) >= 4:
87 continue
88 if parts > best_parts:
89 best_parts = parts
90 best_name = c
91 if best_name is not None:
Thomas Heller8cef8a82007-08-27 09:42:33 +000092 print("Found an SSL directory at '%s'" % (best_name,))
Mark Hammondf229f9f2002-12-03 05:47:26 +000093 else:
Thomas Heller8cef8a82007-08-27 09:42:33 +000094 print("Could not find an SSL directory in '%s'" % (sources,))
Thomas Wouters00ee7ba2006-08-21 19:07:27 +000095 sys.stdout.flush()
Mark Hammondf229f9f2002-12-03 05:47:26 +000096 return best_name
97
Christian Heimes5b5e81c2007-12-31 16:14:33 +000098def create_makefile64(makefile, m32):
99 """Create and fix makefile for 64bit
100
101 Replace 32 with 64bit directories
102 """
103 if not os.path.isfile(m32):
104 return
Martin v. Löwisb90535f2009-12-22 08:54:52 +0000105 with open(m32) as fin:
106 with open(makefile, 'w') as fout:
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000107 for line in fin:
108 line = line.replace("=tmp32", "=tmp64")
109 line = line.replace("=out32", "=out64")
110 line = line.replace("=inc32", "=inc64")
111 # force 64 bit machine
112 line = line.replace("MKLIB=lib", "MKLIB=lib /MACHINE:X64")
113 line = line.replace("LFLAGS=", "LFLAGS=/MACHINE:X64 ")
114 # don't link against the lib on 64bit systems
115 line = line.replace("bufferoverflowu.lib", "")
116 fout.write(line)
117 os.unlink(m32)
118
119def fix_makefile(makefile):
120 """Fix some stuff in all makefiles
121 """
122 if not os.path.isfile(makefile):
123 return
Martin v. Löwis1561bab2008-02-29 19:39:25 +0000124 # 2.4 compatibility
125 fin = open(makefile)
126 if 1: # with open(makefile) as fin:
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000127 lines = fin.readlines()
Martin v. Löwis1561bab2008-02-29 19:39:25 +0000128 fin.close()
129 fout = open(makefile, 'w')
130 if 1: # with open(makefile, 'w') as fout:
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000131 for line in lines:
132 if line.startswith("PERL="):
133 continue
134 if line.startswith("CP="):
135 line = "CP=copy\n"
136 if line.startswith("MKDIR="):
137 line = "MKDIR=mkdir\n"
138 if line.startswith("CFLAG="):
139 line = line.strip()
140 for algo in ("RC5", "MDC2", "IDEA"):
141 noalgo = " -DOPENSSL_NO_%s" % algo
142 if noalgo not in line:
143 line = line + noalgo
144 line = line + '\n'
145 fout.write(line)
Martin v. Löwis1561bab2008-02-29 19:39:25 +0000146 fout.close()
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000147
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000148def run_configure(configure, do_script):
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000149 print("perl Configure "+configure)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000150 os.system("perl Configure "+configure)
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000151 print(do_script)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000152 os.system(do_script)
153
Mark Hammondf229f9f2002-12-03 05:47:26 +0000154def main():
Mark Hammondf229f9f2002-12-03 05:47:26 +0000155 build_all = "-a" in sys.argv
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000156 if sys.argv[1] == "Release":
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000157 debug = False
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000158 elif sys.argv[1] == "Debug":
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000159 debug = True
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000160 else:
161 raise ValueError(str(sys.argv))
162
163 if sys.argv[2] == "Win32":
164 arch = "x86"
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000165 configure = "VC-WIN32"
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000166 do_script = "ms\\do_nasm"
167 makefile="ms\\nt.mak"
168 m32 = makefile
169 elif sys.argv[2] == "x64":
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000170 arch="amd64"
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000171 configure = "VC-WIN64A"
172 do_script = "ms\\do_win64a"
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000173 makefile = "ms\\nt64.mak"
174 m32 = makefile.replace('64', '')
175 #os.environ["VSEXTCOMP_USECL"] = "MS_OPTERON"
176 else:
177 raise ValueError(str(sys.argv))
178
Mark Hammondf229f9f2002-12-03 05:47:26 +0000179 make_flags = ""
180 if build_all:
181 make_flags = "-a"
182 # perl should be on the path, but we also look in "\perl" and "c:\\perl"
183 # as "well known" locations
184 perls = find_all_on_path("perl.exe", ["\\perl\\bin", "C:\\perl\\bin"])
185 perl = find_working_perl(perls)
186 if perl is None:
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000187 print("No Perl installation was found. Existing Makefiles are used.")
Mark Hammondf229f9f2002-12-03 05:47:26 +0000188
Thomas Heller8cef8a82007-08-27 09:42:33 +0000189 print("Found a working perl at '%s'" % (perl,))
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000190 sys.stdout.flush()
Mark Hammondf229f9f2002-12-03 05:47:26 +0000191 # Look for SSL 2 levels up from pcbuild - ie, same place zlib etc all live.
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000192 ssl_dir = find_best_ssl_dir(("..\\..",))
Mark Hammondf229f9f2002-12-03 05:47:26 +0000193 if ssl_dir is None:
194 sys.exit(1)
195
196 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)
228 shutil.copy(r"crypto\buildinf.h", r"crypto\buildinf_%s.h" % arch)
229 shutil.copy(r"crypto\opensslconf.h", r"crypto\opensslconf_%s.h" % arch)
Mark Hammondf229f9f2002-12-03 05:47:26 +0000230
231 # Now run make.
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000232 if arch == "amd64":
Neal Norwitz247bd2e2008-04-05 06:16:50 +0000233 rc = os.system("ml64 -c -Foms\\uptable.obj ms\\uptable.asm")
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000234 if rc:
235 print("ml64 assembler has failed.")
236 sys.exit(rc)
237
238 shutil.copy(r"crypto\buildinf_%s.h" % arch, r"crypto\buildinf.h")
239 shutil.copy(r"crypto\opensslconf_%s.h" % arch, r"crypto\opensslconf.h")
240
241 #makeCommand = "nmake /nologo PERL=\"%s\" -f \"%s\"" %(perl, makefile)
242 makeCommand = "nmake /nologo -f \"%s\"" % makefile
Thomas Heller8cef8a82007-08-27 09:42:33 +0000243 print("Executing ssl makefiles:", makeCommand)
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000244 sys.stdout.flush()
245 rc = os.system(makeCommand)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000246 if rc:
Thomas Heller8cef8a82007-08-27 09:42:33 +0000247 print("Executing "+makefile+" failed")
248 print(rc)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000249 sys.exit(rc)
Mark Hammondf229f9f2002-12-03 05:47:26 +0000250 finally:
251 os.chdir(old_cd)
Mark Hammondf229f9f2002-12-03 05:47:26 +0000252 sys.exit(rc)
253
254if __name__=='__main__':
255 main()