blob: dc13ef5709b43cbbfef0f5b03cd05b939bf8501e [file] [log] [blame]
Christian Heimes57dddfb2008-01-02 18:30:52 +00001# Script for building the _ssl and _hashlib modules for Windows.
2# Uses Perl to setup the OpenSSL environment correctly
3# and build OpenSSL, then invokes a simple nmake session
4# for the actual _ssl.pyd and _hashlib.pyd DLLs.
5
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.
Hirokazu Yamamoto313dab42010-09-28 18:29:57 +000011# * Run this script from the PC/VS8.0 directory.
Christian Heimes57dddfb2008-01-02 18:30:52 +000012#
13# it should configure and build SSL, then build the _ssl and _hashlib
14# Python extensions without intervention.
15
16# 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
27
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:
Hirokazu Yamamoto313dab42010-09-28 18:29:57 +000049 fh = os.popen('"%s" -e "use Win32;"' % perl)
Christian Heimes57dddfb2008-01-02 18:30:52 +000050 fh.read()
51 rc = fh.close()
52 if rc:
53 continue
54 return perl
55 print("Can not find a suitable PERL:")
56 if perls:
57 print(" the following perl interpreters were found:")
58 for p in perls:
59 print(" ", p)
60 print(" None of these versions appear suitable for building OpenSSL")
61 else:
62 print(" NO perl interpreters were found on this machine at all!")
63 print(" Please install ActivePerl and ensure it appears on your path")
64 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:
71 # note: do not abspath s; the build will fail if any
72 # higher up directory name has spaces in it.
73 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:
92 print("Found an SSL directory at '%s'" % (best_name,))
93 else:
94 print("Could not find an SSL directory in '%s'" % (sources,))
95 sys.stdout.flush()
96 return best_name
97
98def 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
105 with open(m32) as fin:
106 with open(makefile, 'w') as fout:
107 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
124 with open(makefile) as fin:
125 lines = fin.readlines()
126 with open(makefile, 'w') as fout:
127 for line in lines:
128 if line.startswith("PERL="):
129 continue
130 if line.startswith("CP="):
131 line = "CP=copy\n"
132 if line.startswith("MKDIR="):
133 line = "MKDIR=mkdir\n"
134 if line.startswith("CFLAG="):
135 line = line.strip()
136 for algo in ("RC5", "MDC2", "IDEA"):
137 noalgo = " -DOPENSSL_NO_%s" % algo
138 if noalgo not in line:
139 line = line + noalgo
140 line = line + '\n'
141 fout.write(line)
142
143def run_configure(configure, do_script):
Hirokazu Yamamoto2f816e62010-09-21 18:23:05 +0000144 print("perl Configure "+configure+" no-idea no-mdc2")
145 os.system("perl Configure "+configure+" no-idea no-mdc2")
Christian Heimes57dddfb2008-01-02 18:30:52 +0000146 print(do_script)
147 os.system(do_script)
148
Hirokazu Yamamoto5e83da32010-09-24 16:36:34 +0000149def cmp(f1, f2):
150 bufsize = 1024 * 8
151 with open(f1, 'rb') as fp1, open(f2, 'rb') as fp2:
152 while True:
153 b1 = fp1.read(bufsize)
154 b2 = fp2.read(bufsize)
155 if b1 != b2:
156 return False
157 if not b1:
158 return True
159
160def copy(src, dst):
161 if os.path.isfile(dst) and cmp(src, dst):
162 return
163 shutil.copy(src, dst)
164
Christian Heimes57dddfb2008-01-02 18:30:52 +0000165def main():
166 build_all = "-a" in sys.argv
167 if sys.argv[1] == "Release":
168 debug = False
169 elif sys.argv[1] == "Debug":
170 debug = True
171 else:
172 raise ValueError(str(sys.argv))
173
174 if sys.argv[2] == "Win32":
175 arch = "x86"
176 configure = "VC-WIN32"
177 do_script = "ms\\do_nasm"
178 makefile="ms\\nt.mak"
179 m32 = makefile
Hirokazu Yamamoto2f816e62010-09-21 18:23:05 +0000180 dirsuffix = "32"
Christian Heimes57dddfb2008-01-02 18:30:52 +0000181 elif sys.argv[2] == "x64":
182 arch="amd64"
183 configure = "VC-WIN64A"
184 do_script = "ms\\do_win64a"
185 makefile = "ms\\nt64.mak"
186 m32 = makefile.replace('64', '')
Hirokazu Yamamoto2f816e62010-09-21 18:23:05 +0000187 dirsuffix = "64"
Christian Heimes57dddfb2008-01-02 18:30:52 +0000188 #os.environ["VSEXTCOMP_USECL"] = "MS_OPTERON"
189 else:
190 raise ValueError(str(sys.argv))
191
192 make_flags = ""
193 if build_all:
194 make_flags = "-a"
195 # perl should be on the path, but we also look in "\perl" and "c:\\perl"
196 # as "well known" locations
197 perls = find_all_on_path("perl.exe", ["\\perl\\bin", "C:\\perl\\bin"])
198 perl = find_working_perl(perls)
Hirokazu Yamamoto313dab42010-09-28 18:29:57 +0000199 if perl:
200 print("Found a working perl at '%s'" % (perl,))
201 else:
Christian Heimes57dddfb2008-01-02 18:30:52 +0000202 print("No Perl installation was found. Existing Makefiles are used.")
Christian Heimes57dddfb2008-01-02 18:30:52 +0000203 sys.stdout.flush()
Hirokazu Yamamoto313dab42010-09-28 18:29:57 +0000204 # Look for SSL 3 levels up from PC/VS8.0 - ie, same place zlib etc all live.
Christian Heimes57dddfb2008-01-02 18:30:52 +0000205 ssl_dir = find_best_ssl_dir(("..\\..\\..",))
206 if ssl_dir is None:
207 sys.exit(1)
208
209 old_cd = os.getcwd()
210 try:
211 os.chdir(ssl_dir)
212 # rebuild makefile when we do the role over from 32 to 64 build
213 if arch == "amd64" and os.path.isfile(m32) and not os.path.isfile(makefile):
214 os.unlink(m32)
215
216 # If the ssl makefiles do not exist, we invoke Perl to generate them.
217 # Due to a bug in this script, the makefile sometimes ended up empty
218 # Force a regeneration if it is.
219 if not os.path.isfile(makefile) or os.path.getsize(makefile)==0:
220 if perl is None:
221 print("Perl is required to build the makefiles!")
222 sys.exit(1)
223
224 print("Creating the makefiles...")
225 sys.stdout.flush()
226 # Put our working Perl at the front of our path
227 os.environ["PATH"] = os.path.dirname(perl) + \
228 os.pathsep + \
229 os.environ["PATH"]
230 run_configure(configure, do_script)
231 if debug:
232 print("OpenSSL debug builds aren't supported.")
233 #if arch=="x86" and debug:
234 # # the do_masm script in openssl doesn't generate a debug
235 # # build makefile so we generate it here:
236 # os.system("perl util\mk1mf.pl debug "+configure+" >"+makefile)
237
238 if arch == "amd64":
239 create_makefile64(makefile, m32)
240 fix_makefile(makefile)
Hirokazu Yamamoto5e83da32010-09-24 16:36:34 +0000241 copy(r"crypto\buildinf.h", r"crypto\buildinf_%s.h" % arch)
242 copy(r"crypto\opensslconf.h", r"crypto\opensslconf_%s.h" % arch)
Christian Heimes57dddfb2008-01-02 18:30:52 +0000243
Hirokazu Yamamoto2f816e62010-09-21 18:23:05 +0000244 # If the assembler files don't exist in tmpXX, copy them there
Hirokazu Yamamotode5919d2010-11-04 14:11:32 +0000245 if perl is None and os.path.exists("asm"+dirsuffix):
Hirokazu Yamamoto2f816e62010-09-21 18:23:05 +0000246 if not os.path.exists("tmp"+dirsuffix):
247 os.mkdir("tmp"+dirsuffix)
248 for f in os.listdir("asm"+dirsuffix):
249 if not f.endswith(".asm"): continue
250 if os.path.isfile(r"tmp%s\%s" % (dirsuffix, f)): continue
251 shutil.copy(r"asm%s\%s" % (dirsuffix, f), "tmp"+dirsuffix)
252
Christian Heimes57dddfb2008-01-02 18:30:52 +0000253 # Now run make.
254 if arch == "amd64":
Neal Norwitz247bd2e2008-04-05 06:16:50 +0000255 rc = os.system("ml64 -c -Foms\\uptable.obj ms\\uptable.asm")
Christian Heimes57dddfb2008-01-02 18:30:52 +0000256 if rc:
257 print("ml64 assembler has failed.")
258 sys.exit(rc)
259
Hirokazu Yamamoto5e83da32010-09-24 16:36:34 +0000260 copy(r"crypto\buildinf_%s.h" % arch, r"crypto\buildinf.h")
261 copy(r"crypto\opensslconf_%s.h" % arch, r"crypto\opensslconf.h")
Christian Heimes57dddfb2008-01-02 18:30:52 +0000262
263 #makeCommand = "nmake /nologo PERL=\"%s\" -f \"%s\"" %(perl, makefile)
264 makeCommand = "nmake /nologo -f \"%s\"" % makefile
265 print("Executing ssl makefiles:", makeCommand)
266 sys.stdout.flush()
267 rc = os.system(makeCommand)
268 if rc:
269 print("Executing "+makefile+" failed")
270 print(rc)
271 sys.exit(rc)
272 finally:
273 os.chdir(old_cd)
274 sys.exit(rc)
275
276if __name__=='__main__':
277 main()