Issue #1523: Remove deprecated overflow masking in struct module, and
[python.git] / PCbuild / build_ssl.py
blobf24c9068ae0db9614cdd5548b717a4da5d859335
1 # 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.
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.
13 # it should configure and build SSL, then build the _ssl and _hashlib
14 # Python extensions without intervention.
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.
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
26 import os, sys, re, shutil
28 # Find all "foo.exe" files on the PATH.
29 def 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
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.
47 def 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
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
66 # Locate the best SSL directory given a few roots to look into.
67 def 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
98 def create_makefile64(makefile, m32):
99 """Create and fix makefile for 64bit
101 Replace 32 with 64bit directories
103 if not os.path.isfile(m32):
104 return
105 # 2.4 compatibility
106 fin = open(m32)
107 if 1: # with open(m32) as fin:
108 fout = open(makefile, 'w')
109 if 1: # with open(makefile, 'w') as fout:
110 for line in fin:
111 line = line.replace("=tmp32", "=tmp64")
112 line = line.replace("=out32", "=out64")
113 line = line.replace("=inc32", "=inc64")
114 # force 64 bit machine
115 line = line.replace("MKLIB=lib", "MKLIB=lib /MACHINE:X64")
116 line = line.replace("LFLAGS=", "LFLAGS=/MACHINE:X64 ")
117 # don't link against the lib on 64bit systems
118 line = line.replace("bufferoverflowu.lib", "")
119 fout.write(line)
120 os.unlink(m32)
122 def fix_makefile(makefile):
123 """Fix some stuff in all makefiles
125 if not os.path.isfile(makefile):
126 return
127 # 2.4 compatibility
128 fin = open(makefile)
129 if 1: # with open(makefile) as fin:
130 lines = fin.readlines()
131 fin.close()
132 fout = open(makefile, 'w')
133 if 1: # with open(makefile, 'w') as fout:
134 for line in lines:
135 if line.startswith("PERL="):
136 continue
137 if line.startswith("CP="):
138 line = "CP=copy\n"
139 if line.startswith("MKDIR="):
140 line = "MKDIR=mkdir\n"
141 if line.startswith("CFLAG="):
142 line = line.strip()
143 for algo in ("RC5", "MDC2", "IDEA"):
144 noalgo = " -DOPENSSL_NO_%s" % algo
145 if noalgo not in line:
146 line = line + noalgo
147 line = line + '\n'
148 fout.write(line)
149 fout.close()
151 def run_configure(configure, do_script):
152 print("perl Configure "+configure)
153 os.system("perl Configure "+configure)
154 print(do_script)
155 os.system(do_script)
157 def main():
158 build_all = "-a" in sys.argv
159 if sys.argv[1] == "Release":
160 debug = False
161 elif sys.argv[1] == "Debug":
162 debug = True
163 else:
164 raise ValueError(str(sys.argv))
166 if sys.argv[2] == "Win32":
167 arch = "x86"
168 configure = "VC-WIN32"
169 do_script = "ms\\do_nasm"
170 makefile="ms\\nt.mak"
171 m32 = makefile
172 elif sys.argv[2] == "x64":
173 arch="amd64"
174 configure = "VC-WIN64A"
175 do_script = "ms\\do_win64a"
176 makefile = "ms\\nt64.mak"
177 m32 = makefile.replace('64', '')
178 #os.environ["VSEXTCOMP_USECL"] = "MS_OPTERON"
179 else:
180 raise ValueError(str(sys.argv))
182 make_flags = ""
183 if build_all:
184 make_flags = "-a"
185 # perl should be on the path, but we also look in "\perl" and "c:\\perl"
186 # as "well known" locations
187 perls = find_all_on_path("perl.exe", ["\\perl\\bin", "C:\\perl\\bin"])
188 perl = find_working_perl(perls)
189 if perl is None:
190 print("No Perl installation was found. Existing Makefiles are used.")
192 print("Found a working perl at '%s'" % (perl,))
193 sys.stdout.flush()
194 # Look for SSL 2 levels up from pcbuild - ie, same place zlib etc all live.
195 ssl_dir = find_best_ssl_dir(("..\\..",))
196 if ssl_dir is None:
197 sys.exit(1)
199 old_cd = os.getcwd()
200 try:
201 os.chdir(ssl_dir)
202 # rebuild makefile when we do the role over from 32 to 64 build
203 if arch == "amd64" and os.path.isfile(m32) and not os.path.isfile(makefile):
204 os.unlink(m32)
206 # If the ssl makefiles do not exist, we invoke Perl to generate them.
207 # Due to a bug in this script, the makefile sometimes ended up empty
208 # Force a regeneration if it is.
209 if not os.path.isfile(makefile) or os.path.getsize(makefile)==0:
210 if perl is None:
211 print("Perl is required to build the makefiles!")
212 sys.exit(1)
214 print("Creating the makefiles...")
215 sys.stdout.flush()
216 # Put our working Perl at the front of our path
217 os.environ["PATH"] = os.path.dirname(perl) + \
218 os.pathsep + \
219 os.environ["PATH"]
220 run_configure(configure, do_script)
221 if debug:
222 print("OpenSSL debug builds aren't supported.")
223 #if arch=="x86" and debug:
224 # # the do_masm script in openssl doesn't generate a debug
225 # # build makefile so we generate it here:
226 # os.system("perl util\mk1mf.pl debug "+configure+" >"+makefile)
228 if arch == "amd64":
229 create_makefile64(makefile, m32)
230 fix_makefile(makefile)
231 shutil.copy(r"crypto\buildinf.h", r"crypto\buildinf_%s.h" % arch)
232 shutil.copy(r"crypto\opensslconf.h", r"crypto\opensslconf_%s.h" % arch)
234 # Now run make.
235 if arch == "amd64":
236 rc = os.system(r"ml64 -c -Foms\uptable.obj ms\uptable.asm")
237 if rc:
238 print("ml64 assembler has failed.")
239 sys.exit(rc)
241 shutil.copy(r"crypto\buildinf_%s.h" % arch, r"crypto\buildinf.h")
242 shutil.copy(r"crypto\opensslconf_%s.h" % arch, r"crypto\opensslconf.h")
244 #makeCommand = "nmake /nologo PERL=\"%s\" -f \"%s\"" %(perl, makefile)
245 makeCommand = "nmake /nologo -f \"%s\"" % makefile
246 print("Executing ssl makefiles:", makeCommand)
247 sys.stdout.flush()
248 rc = os.system(makeCommand)
249 if rc:
250 print("Executing "+makefile+" failed")
251 print(rc)
252 sys.exit(rc)
253 finally:
254 os.chdir(old_cd)
255 sys.exit(rc)
257 if __name__=='__main__':
258 main()