blob: f81e0bc9f2e0cf2cd86264e58ef7a427dda25bb5 [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
Christian Heimese8954f82007-11-22 11:21:16 +000027import os, sys, re, shutil
28
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:
Hirokazu Yamamoto59734be2010-12-09 08:01:18 +000050 fh = os.popen('"%s" -e "use Win32;"' % perl)
Christian Heimese8954f82007-11-22 11:21:16 +000051 fh.read()
52 rc = fh.close()
53 if rc:
54 continue
55 return perl
56 print("Can not find a suitable PERL:")
57 if perls:
58 print(" the following perl interpreters were found:")
59 for p in perls:
60 print(" ", p)
61 print(" None of these versions appear suitable for building OpenSSL")
62 else:
63 print(" NO perl interpreters were found on this machine at all!")
64 print(" Please install ActivePerl and ensure it appears on your path")
Christian Heimese8954f82007-11-22 11:21:16 +000065 return None
66
67# Locate the best SSL directory given a few roots to look into.
68def find_best_ssl_dir(sources):
69 candidates = []
70 for s in sources:
71 try:
72 # note: do not abspath s; the build will fail if any
73 # higher up directory name has spaces in it.
74 fnames = os.listdir(s)
75 except os.error:
76 fnames = []
77 for fname in fnames:
78 fqn = os.path.join(s, fname)
79 if os.path.isdir(fqn) and fname.startswith("openssl-"):
80 candidates.append(fqn)
81 # Now we have all the candidates, locate the best.
82 best_parts = []
83 best_name = None
84 for c in candidates:
85 parts = re.split("[.-]", os.path.basename(c))[1:]
86 # eg - openssl-0.9.7-beta1 - ignore all "beta" or any other qualifiers
87 if len(parts) >= 4:
88 continue
89 if parts > best_parts:
90 best_parts = parts
91 best_name = c
92 if best_name is not None:
93 print("Found an SSL directory at '%s'" % (best_name,))
94 else:
95 print("Could not find an SSL directory in '%s'" % (sources,))
96 sys.stdout.flush()
97 return best_name
98
Christian Heimes23361112007-11-23 07:05:03 +000099def create_makefile64(makefile, m32):
100 """Create and fix makefile for 64bit
Christian Heimese8954f82007-11-22 11:21:16 +0000101
102 Replace 32 with 64bit directories
103 """
104 if not os.path.isfile(m32):
105 return
Martin v. Löwisc3f5ca12009-12-21 19:25:56 +0000106 with open(m32) as fin:
107 with open(makefile, 'w') as fout:
Christian Heimese8954f82007-11-22 11:21:16 +0000108 for line in fin:
109 line = line.replace("=tmp32", "=tmp64")
110 line = line.replace("=out32", "=out64")
111 line = line.replace("=inc32", "=inc64")
112 # force 64 bit machine
113 line = line.replace("MKLIB=lib", "MKLIB=lib /MACHINE:X64")
114 line = line.replace("LFLAGS=", "LFLAGS=/MACHINE:X64 ")
115 # don't link against the lib on 64bit systems
116 line = line.replace("bufferoverflowu.lib", "")
117 fout.write(line)
118 os.unlink(m32)
119
Christian Heimes23361112007-11-23 07:05:03 +0000120def fix_makefile(makefile):
121 """Fix some stuff in all makefiles
122 """
123 if not os.path.isfile(makefile):
124 return
Martin v. Löwis9e051352008-02-29 18:54:45 +0000125 # 2.4 compatibility
126 fin = open(makefile)
127 if 1: # with open(makefile) as fin:
Christian Heimes23361112007-11-23 07:05:03 +0000128 lines = fin.readlines()
Martin v. Löwis9e051352008-02-29 18:54:45 +0000129 fin.close()
130 fout = open(makefile, 'w')
131 if 1: # with open(makefile, 'w') as fout:
Christian Heimes23361112007-11-23 07:05:03 +0000132 for line in lines:
133 if line.startswith("PERL="):
134 continue
135 if line.startswith("CP="):
136 line = "CP=copy\n"
137 if line.startswith("MKDIR="):
138 line = "MKDIR=mkdir\n"
Christian Heimes3e9ac992007-11-24 01:53:59 +0000139 if line.startswith("CFLAG="):
140 line = line.strip()
141 for algo in ("RC5", "MDC2", "IDEA"):
142 noalgo = " -DOPENSSL_NO_%s" % algo
143 if noalgo not in line:
144 line = line + noalgo
145 line = line + '\n'
Christian Heimes23361112007-11-23 07:05:03 +0000146 fout.write(line)
Martin v. Löwis9e051352008-02-29 18:54:45 +0000147 fout.close()
Christian Heimes23361112007-11-23 07:05:03 +0000148
Christian Heimese8954f82007-11-22 11:21:16 +0000149def run_configure(configure, do_script):
150 print("perl Configure "+configure)
151 os.system("perl Configure "+configure)
152 print(do_script)
153 os.system(do_script)
154
155def main():
156 build_all = "-a" in sys.argv
157 if sys.argv[1] == "Release":
158 debug = False
159 elif sys.argv[1] == "Debug":
160 debug = True
161 else:
162 raise ValueError(str(sys.argv))
163
164 if sys.argv[2] == "Win32":
165 arch = "x86"
166 configure = "VC-WIN32"
167 do_script = "ms\\do_nasm"
168 makefile="ms\\nt.mak"
169 m32 = makefile
170 elif sys.argv[2] == "x64":
171 arch="amd64"
172 configure = "VC-WIN64A"
173 do_script = "ms\\do_win64a"
174 makefile = "ms\\nt64.mak"
175 m32 = makefile.replace('64', '')
176 #os.environ["VSEXTCOMP_USECL"] = "MS_OPTERON"
177 else:
178 raise ValueError(str(sys.argv))
179
180 make_flags = ""
181 if build_all:
182 make_flags = "-a"
183 # perl should be on the path, but we also look in "\perl" and "c:\\perl"
184 # as "well known" locations
185 perls = find_all_on_path("perl.exe", ["\\perl\\bin", "C:\\perl\\bin"])
186 perl = find_working_perl(perls)
Hirokazu Yamamoto59734be2010-12-09 08:01:18 +0000187 if perl:
188 print("Found a working perl at '%s'" % (perl,))
189 else:
Christian Heimes23361112007-11-23 07:05:03 +0000190 print("No Perl installation was found. Existing Makefiles are used.")
Christian Heimese8954f82007-11-22 11:21:16 +0000191 sys.stdout.flush()
192 # Look for SSL 2 levels up from pcbuild - ie, same place zlib etc all live.
193 ssl_dir = find_best_ssl_dir(("..\\..",))
194 if ssl_dir is None:
195 sys.exit(1)
196
197 old_cd = os.getcwd()
198 try:
199 os.chdir(ssl_dir)
200 # rebuild makefile when we do the role over from 32 to 64 build
201 if arch == "amd64" and os.path.isfile(m32) and not os.path.isfile(makefile):
202 os.unlink(m32)
203
204 # If the ssl makefiles do not exist, we invoke Perl to generate them.
205 # Due to a bug in this script, the makefile sometimes ended up empty
206 # Force a regeneration if it is.
207 if not os.path.isfile(makefile) or os.path.getsize(makefile)==0:
Christian Heimes23361112007-11-23 07:05:03 +0000208 if perl is None:
209 print("Perl is required to build the makefiles!")
210 sys.exit(1)
211
Christian Heimese8954f82007-11-22 11:21:16 +0000212 print("Creating the makefiles...")
213 sys.stdout.flush()
214 # Put our working Perl at the front of our path
215 os.environ["PATH"] = os.path.dirname(perl) + \
216 os.pathsep + \
217 os.environ["PATH"]
218 run_configure(configure, do_script)
Christian Heimes23361112007-11-23 07:05:03 +0000219 if debug:
220 print("OpenSSL debug builds aren't supported.")
221 #if arch=="x86" and debug:
222 # # the do_masm script in openssl doesn't generate a debug
223 # # build makefile so we generate it here:
224 # os.system("perl util\mk1mf.pl debug "+configure+" >"+makefile)
Christian Heimese8954f82007-11-22 11:21:16 +0000225
Christian Heimes23361112007-11-23 07:05:03 +0000226 if arch == "amd64":
227 create_makefile64(makefile, m32)
228 fix_makefile(makefile)
Hirokazu Yamamoto63e9b502010-09-21 16:25:21 +0000229 shutil.copy(r"crypto\buildinf.h", r"crypto\buildinf_%s.h" % arch)
230 shutil.copy(r"crypto\opensslconf.h", r"crypto\opensslconf_%s.h" % arch)
Christian Heimese8954f82007-11-22 11:21:16 +0000231
232 # Now run make.
Christian Heimes23361112007-11-23 07:05:03 +0000233 if arch == "amd64":
234 rc = os.system(r"ml64 -c -Foms\uptable.obj ms\uptable.asm")
235 if rc:
236 print("ml64 assembler has failed.")
237 sys.exit(rc)
238
Hirokazu Yamamoto63e9b502010-09-21 16:25:21 +0000239 shutil.copy(r"crypto\buildinf_%s.h" % arch, r"crypto\buildinf.h")
240 shutil.copy(r"crypto\opensslconf_%s.h" % arch, r"crypto\opensslconf.h")
Christian Heimes23361112007-11-23 07:05:03 +0000241
242 #makeCommand = "nmake /nologo PERL=\"%s\" -f \"%s\"" %(perl, makefile)
243 makeCommand = "nmake /nologo -f \"%s\"" % makefile
Christian Heimese8954f82007-11-22 11:21:16 +0000244 print("Executing ssl makefiles:", makeCommand)
245 sys.stdout.flush()
246 rc = os.system(makeCommand)
247 if rc:
248 print("Executing "+makefile+" failed")
249 print(rc)
250 sys.exit(rc)
251 finally:
252 os.chdir(old_cd)
253 sys.exit(rc)
254
255if __name__=='__main__':
256 main()