blob: 95af084afe802275b0ead864251721310a3b8e84 [file] [log] [blame]
Tim Petersf32b0272004-01-04 02:27:33 +00001# Script for building the _ssl module for Windows.
2# Uses Perl to setup the OpenSSL environment correctly
3# and build OpenSSL, then invokes a simple nmake session
4# for _ssl.pyd itself.
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.
Christian Heimes4c3eda32008-01-04 15:35:04 +000011# * Run this script from the PC/VC6 directory.
Tim Petersf32b0272004-01-04 02:27:33 +000012#
13# it should configure and build SSL, then build the ssl Python extension
14# without intervention.
15
Hirokazu Yamamoto7405c202010-09-28 18:36:04 +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
Hirokazu Yamamotoecdfd512009-03-18 11:39:46 +000021import os, sys, re, shutil
Tim Petersf32b0272004-01-04 02:27:33 +000022
23# Find all "foo.exe" files on the PATH.
24def find_all_on_path(filename, extras = None):
25 entries = os.environ["PATH"].split(os.pathsep)
26 ret = []
27 for p in entries:
28 fname = os.path.abspath(os.path.join(p, filename))
29 if os.path.isfile(fname) and fname not in ret:
30 ret.append(fname)
31 if extras:
32 for p in extras:
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 return ret
37
38# Find a suitable Perl installation for OpenSSL.
39# cygwin perl does *not* work. ActivePerl does.
40# Being a Perl dummy, the simplest way I can check is if the "Win32" package
41# is available.
42def find_working_perl(perls):
43 for perl in perls:
Hirokazu Yamamoto7405c202010-09-28 18:36:04 +000044 fh = os.popen('"%s" -e "use Win32;"' % perl)
Tim Petersf32b0272004-01-04 02:27:33 +000045 fh.read()
46 rc = fh.close()
47 if rc:
48 continue
49 return perl
Hirokazu Yamamotoecdfd512009-03-18 11:39:46 +000050 print("Can not find a suitable PERL:")
Tim Petersf32b0272004-01-04 02:27:33 +000051 if perls:
Hirokazu Yamamotoecdfd512009-03-18 11:39:46 +000052 print(" the following perl interpreters were found:")
Tim Petersf32b0272004-01-04 02:27:33 +000053 for p in perls:
Hirokazu Yamamotoecdfd512009-03-18 11:39:46 +000054 print(" ", p)
55 print(" None of these versions appear suitable for building OpenSSL")
Tim Petersf32b0272004-01-04 02:27:33 +000056 else:
Hirokazu Yamamotoecdfd512009-03-18 11:39:46 +000057 print(" NO perl interpreters were found on this machine at all!")
58 print(" Please install ActivePerl and ensure it appears on your path")
Tim Petersf32b0272004-01-04 02:27:33 +000059 return None
60
61# Locate the best SSL directory given a few roots to look into.
62def find_best_ssl_dir(sources):
63 candidates = []
64 for s in sources:
65 try:
Hirokazu Yamamotoecdfd512009-03-18 11:39:46 +000066 # note: do not abspath s; the build will fail if any
67 # higher up directory name has spaces in it.
Tim Petersf32b0272004-01-04 02:27:33 +000068 fnames = os.listdir(s)
69 except os.error:
70 fnames = []
71 for fname in fnames:
72 fqn = os.path.join(s, fname)
73 if os.path.isdir(fqn) and fname.startswith("openssl-"):
74 candidates.append(fqn)
75 # Now we have all the candidates, locate the best.
76 best_parts = []
77 best_name = None
78 for c in candidates:
79 parts = re.split("[.-]", os.path.basename(c))[1:]
80 # eg - openssl-0.9.7-beta1 - ignore all "beta" or any other qualifiers
81 if len(parts) >= 4:
82 continue
83 if parts > best_parts:
84 best_parts = parts
85 best_name = c
86 if best_name is not None:
Hirokazu Yamamotoecdfd512009-03-18 11:39:46 +000087 print("Found an SSL directory at '%s'" % (best_name,))
Tim Petersf32b0272004-01-04 02:27:33 +000088 else:
Hirokazu Yamamotoecdfd512009-03-18 11:39:46 +000089 print("Could not find an SSL directory in '%s'" % (sources,))
90 sys.stdout.flush()
Tim Petersf32b0272004-01-04 02:27:33 +000091 return best_name
92
Hirokazu Yamamotoecdfd512009-03-18 11:39:46 +000093def fix_makefile(makefile):
94 """Fix some stuff in all makefiles
95 """
96 if not os.path.isfile(makefile):
97 return
Hirokazu Yamamoto7405c202010-09-28 18:36:04 +000098 with open(makefile) as fin:
Hirokazu Yamamotoecdfd512009-03-18 11:39:46 +000099 lines = fin.readlines()
Hirokazu Yamamoto7405c202010-09-28 18:36:04 +0000100 with open(makefile, 'w') as fout:
Hirokazu Yamamotoecdfd512009-03-18 11:39:46 +0000101 for line in lines:
102 if line.startswith("PERL="):
103 continue
104 if line.startswith("CP="):
105 line = "CP=copy\n"
106 if line.startswith("MKDIR="):
107 line = "MKDIR=mkdir\n"
108 if line.startswith("CFLAG="):
109 line = line.strip()
110 for algo in ("RC5", "MDC2", "IDEA"):
111 noalgo = " -DOPENSSL_NO_%s" % algo
112 if noalgo not in line:
113 line = line + noalgo
114 line = line + '\n'
115 fout.write(line)
Hirokazu Yamamotoecdfd512009-03-18 11:39:46 +0000116
117def run_configure(configure, do_script):
118 print("perl Configure "+configure)
119 os.system("perl Configure "+configure)
120 print(do_script)
121 os.system(do_script)
122
Hirokazu Yamamoto7405c202010-09-28 18:36:04 +0000123def cmp(f1, f2):
124 bufsize = 1024 * 8
125 with open(f1, 'rb') as fp1, open(f2, 'rb') as fp2:
126 while True:
127 b1 = fp1.read(bufsize)
128 b2 = fp2.read(bufsize)
129 if b1 != b2:
130 return False
131 if not b1:
132 return True
133
134def copy(src, dst):
135 if os.path.isfile(dst) and cmp(src, dst):
136 return
137 shutil.copy(src, dst)
138
Tim Petersf32b0272004-01-04 02:27:33 +0000139def main():
140 debug = "-d" in sys.argv
141 build_all = "-a" in sys.argv
Hirokazu Yamamotoecdfd512009-03-18 11:39:46 +0000142 if 1: # Win32
143 arch = "x86"
144 configure = "VC-WIN32"
145 do_script = "ms\\do_nasm"
146 makefile="ms\\nt.mak"
147 m32 = makefile
Hirokazu Yamamoto7405c202010-09-28 18:36:04 +0000148 dirsuffix = "32"
Hirokazu Yamamotoecdfd512009-03-18 11:39:46 +0000149 configure += " no-idea no-rc5 no-mdc2"
Tim Petersf32b0272004-01-04 02:27:33 +0000150 make_flags = ""
151 if build_all:
152 make_flags = "-a"
153 # perl should be on the path, but we also look in "\perl" and "c:\\perl"
154 # as "well known" locations
155 perls = find_all_on_path("perl.exe", ["\\perl\\bin", "C:\\perl\\bin"])
156 perl = find_working_perl(perls)
Hirokazu Yamamoto7405c202010-09-28 18:36:04 +0000157 if perl:
Hirokazu Yamamotoecdfd512009-03-18 11:39:46 +0000158 print("Found a working perl at '%s'" % (perl,))
Hirokazu Yamamoto7405c202010-09-28 18:36:04 +0000159 else:
160 print("No Perl installation was found. Existing Makefiles are used.")
Hirokazu Yamamotoecdfd512009-03-18 11:39:46 +0000161 sys.stdout.flush()
Hirokazu Yamamoto7405c202010-09-28 18:36:04 +0000162 # Look for SSL 3 levels up from PC/VC6 - ie, same place zlib etc all live.
Hirokazu Yamamotoecdfd512009-03-18 11:39:46 +0000163 ssl_dir = find_best_ssl_dir(("..\\..\\..",))
Tim Petersf32b0272004-01-04 02:27:33 +0000164 if ssl_dir is None:
165 sys.exit(1)
166
167 old_cd = os.getcwd()
168 try:
169 os.chdir(ssl_dir)
170 # If the ssl makefiles do not exist, we invoke Perl to generate them.
Hirokazu Yamamotoecdfd512009-03-18 11:39:46 +0000171 # Due to a bug in this script, the makefile sometimes ended up empty
172 # Force a regeneration if it is.
173 if not os.path.isfile(makefile) or os.path.getsize(makefile)==0:
174 if perl is None:
175 print("Perl is required to build the makefiles!")
176 sys.exit(1)
177
178 print("Creating the makefiles...")
179 sys.stdout.flush()
Tim Petersf32b0272004-01-04 02:27:33 +0000180 # Put our working Perl at the front of our path
Hirokazu Yamamotoecdfd512009-03-18 11:39:46 +0000181 os.environ["PATH"] = os.path.dirname(perl) + \
Tim Petersf32b0272004-01-04 02:27:33 +0000182 os.pathsep + \
183 os.environ["PATH"]
Hirokazu Yamamotoecdfd512009-03-18 11:39:46 +0000184 run_configure(configure, do_script)
185 if debug:
186 print("OpenSSL debug builds aren't supported.")
187 #if arch=="x86" and debug:
188 # # the do_masm script in openssl doesn't generate a debug
189 # # build makefile so we generate it here:
190 # os.system("perl util\mk1mf.pl debug "+configure+" >"+makefile)
Tim Petersf32b0272004-01-04 02:27:33 +0000191
Hirokazu Yamamotoecdfd512009-03-18 11:39:46 +0000192 fix_makefile(makefile)
Hirokazu Yamamoto7405c202010-09-28 18:36:04 +0000193 copy(r"crypto\buildinf.h", r"crypto\buildinf_%s.h" % arch)
194 copy(r"crypto\opensslconf.h", r"crypto\opensslconf_%s.h" % arch)
195
196 # If the assembler files don't exist in tmpXX, copy them there
Hirokazu Yamamotode5919d2010-11-04 14:11:32 +0000197 if perl is None and os.path.exists("asm"+dirsuffix):
Hirokazu Yamamoto7405c202010-09-28 18:36:04 +0000198 if not os.path.exists("tmp"+dirsuffix):
199 os.mkdir("tmp"+dirsuffix)
200 for f in os.listdir("asm"+dirsuffix):
201 if not f.endswith(".asm"): continue
202 if os.path.isfile(r"tmp%s\%s" % (dirsuffix, f)): continue
203 shutil.copy(r"asm%s\%s" % (dirsuffix, f), "tmp"+dirsuffix)
Tim Petersf32b0272004-01-04 02:27:33 +0000204
205 # Now run make.
Hirokazu Yamamoto7405c202010-09-28 18:36:04 +0000206 copy(r"crypto\buildinf_%s.h" % arch, r"crypto\buildinf.h")
207 copy(r"crypto\opensslconf_%s.h" % arch, r"crypto\opensslconf.h")
Hirokazu Yamamotoecdfd512009-03-18 11:39:46 +0000208
209 #makeCommand = "nmake /nologo PERL=\"%s\" -f \"%s\"" %(perl, makefile)
210 makeCommand = "nmake /nologo -f \"%s\"" % makefile
211 print("Executing ssl makefiles:", makeCommand)
212 sys.stdout.flush()
213 rc = os.system(makeCommand)
214 if rc:
215 print("Executing "+makefile+" failed")
216 print(rc)
217 sys.exit(rc)
Tim Petersf32b0272004-01-04 02:27:33 +0000218 finally:
219 os.chdir(old_cd)
220 # And finally, we can build the _ssl module itself for Python.
221 defs = "SSL_DIR=%s" % (ssl_dir,)
222 if debug:
223 defs = defs + " " + "DEBUG=1"
224 rc = os.system('nmake /nologo -f _ssl.mak ' + defs + " " + make_flags)
225 sys.exit(rc)
226
227if __name__=='__main__':
228 main()