Tim Peters | f32b027 | 2004-01-04 02:27:33 +0000 | [diff] [blame] | 1 | # 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 Heimes | 4c3eda3 | 2008-01-04 15:35:04 +0000 | [diff] [blame] | 11 | # * Run this script from the PC/VC6 directory. |
Tim Peters | f32b027 | 2004-01-04 02:27:33 +0000 | [diff] [blame] | 12 | # |
| 13 | # it should configure and build SSL, then build the ssl Python extension |
| 14 | # without intervention. |
| 15 | |
Hirokazu Yamamoto | 7405c20 | 2010-09-28 18:36:04 +0000 | [diff] [blame^] | 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 | |
Hirokazu Yamamoto | ecdfd51 | 2009-03-18 11:39:46 +0000 | [diff] [blame] | 21 | import os, sys, re, shutil |
Tim Peters | f32b027 | 2004-01-04 02:27:33 +0000 | [diff] [blame] | 22 | |
| 23 | # Find all "foo.exe" files on the PATH. |
| 24 | def 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. |
| 42 | def find_working_perl(perls): |
| 43 | for perl in perls: |
Hirokazu Yamamoto | 7405c20 | 2010-09-28 18:36:04 +0000 | [diff] [blame^] | 44 | fh = os.popen('"%s" -e "use Win32;"' % perl) |
Tim Peters | f32b027 | 2004-01-04 02:27:33 +0000 | [diff] [blame] | 45 | fh.read() |
| 46 | rc = fh.close() |
| 47 | if rc: |
| 48 | continue |
| 49 | return perl |
Hirokazu Yamamoto | ecdfd51 | 2009-03-18 11:39:46 +0000 | [diff] [blame] | 50 | print("Can not find a suitable PERL:") |
Tim Peters | f32b027 | 2004-01-04 02:27:33 +0000 | [diff] [blame] | 51 | if perls: |
Hirokazu Yamamoto | ecdfd51 | 2009-03-18 11:39:46 +0000 | [diff] [blame] | 52 | print(" the following perl interpreters were found:") |
Tim Peters | f32b027 | 2004-01-04 02:27:33 +0000 | [diff] [blame] | 53 | for p in perls: |
Hirokazu Yamamoto | ecdfd51 | 2009-03-18 11:39:46 +0000 | [diff] [blame] | 54 | print(" ", p) |
| 55 | print(" None of these versions appear suitable for building OpenSSL") |
Tim Peters | f32b027 | 2004-01-04 02:27:33 +0000 | [diff] [blame] | 56 | else: |
Hirokazu Yamamoto | ecdfd51 | 2009-03-18 11:39:46 +0000 | [diff] [blame] | 57 | print(" NO perl interpreters were found on this machine at all!") |
| 58 | print(" Please install ActivePerl and ensure it appears on your path") |
Tim Peters | f32b027 | 2004-01-04 02:27:33 +0000 | [diff] [blame] | 59 | return None |
| 60 | |
| 61 | # Locate the best SSL directory given a few roots to look into. |
| 62 | def find_best_ssl_dir(sources): |
| 63 | candidates = [] |
| 64 | for s in sources: |
| 65 | try: |
Hirokazu Yamamoto | ecdfd51 | 2009-03-18 11:39:46 +0000 | [diff] [blame] | 66 | # note: do not abspath s; the build will fail if any |
| 67 | # higher up directory name has spaces in it. |
Tim Peters | f32b027 | 2004-01-04 02:27:33 +0000 | [diff] [blame] | 68 | 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 Yamamoto | ecdfd51 | 2009-03-18 11:39:46 +0000 | [diff] [blame] | 87 | print("Found an SSL directory at '%s'" % (best_name,)) |
Tim Peters | f32b027 | 2004-01-04 02:27:33 +0000 | [diff] [blame] | 88 | else: |
Hirokazu Yamamoto | ecdfd51 | 2009-03-18 11:39:46 +0000 | [diff] [blame] | 89 | print("Could not find an SSL directory in '%s'" % (sources,)) |
| 90 | sys.stdout.flush() |
Tim Peters | f32b027 | 2004-01-04 02:27:33 +0000 | [diff] [blame] | 91 | return best_name |
| 92 | |
Hirokazu Yamamoto | ecdfd51 | 2009-03-18 11:39:46 +0000 | [diff] [blame] | 93 | def fix_makefile(makefile): |
| 94 | """Fix some stuff in all makefiles |
| 95 | """ |
| 96 | if not os.path.isfile(makefile): |
| 97 | return |
Hirokazu Yamamoto | 7405c20 | 2010-09-28 18:36:04 +0000 | [diff] [blame^] | 98 | with open(makefile) as fin: |
Hirokazu Yamamoto | ecdfd51 | 2009-03-18 11:39:46 +0000 | [diff] [blame] | 99 | lines = fin.readlines() |
Hirokazu Yamamoto | 7405c20 | 2010-09-28 18:36:04 +0000 | [diff] [blame^] | 100 | with open(makefile, 'w') as fout: |
Hirokazu Yamamoto | ecdfd51 | 2009-03-18 11:39:46 +0000 | [diff] [blame] | 101 | 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 Yamamoto | ecdfd51 | 2009-03-18 11:39:46 +0000 | [diff] [blame] | 116 | |
| 117 | def 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 Yamamoto | 7405c20 | 2010-09-28 18:36:04 +0000 | [diff] [blame^] | 123 | def 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 | |
| 134 | def copy(src, dst): |
| 135 | if os.path.isfile(dst) and cmp(src, dst): |
| 136 | return |
| 137 | shutil.copy(src, dst) |
| 138 | |
Tim Peters | f32b027 | 2004-01-04 02:27:33 +0000 | [diff] [blame] | 139 | def main(): |
| 140 | debug = "-d" in sys.argv |
| 141 | build_all = "-a" in sys.argv |
Hirokazu Yamamoto | ecdfd51 | 2009-03-18 11:39:46 +0000 | [diff] [blame] | 142 | 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 Yamamoto | 7405c20 | 2010-09-28 18:36:04 +0000 | [diff] [blame^] | 148 | dirsuffix = "32" |
Hirokazu Yamamoto | ecdfd51 | 2009-03-18 11:39:46 +0000 | [diff] [blame] | 149 | configure += " no-idea no-rc5 no-mdc2" |
Tim Peters | f32b027 | 2004-01-04 02:27:33 +0000 | [diff] [blame] | 150 | 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 Yamamoto | 7405c20 | 2010-09-28 18:36:04 +0000 | [diff] [blame^] | 157 | if perl: |
Hirokazu Yamamoto | ecdfd51 | 2009-03-18 11:39:46 +0000 | [diff] [blame] | 158 | print("Found a working perl at '%s'" % (perl,)) |
Hirokazu Yamamoto | 7405c20 | 2010-09-28 18:36:04 +0000 | [diff] [blame^] | 159 | else: |
| 160 | print("No Perl installation was found. Existing Makefiles are used.") |
Hirokazu Yamamoto | ecdfd51 | 2009-03-18 11:39:46 +0000 | [diff] [blame] | 161 | sys.stdout.flush() |
Hirokazu Yamamoto | 7405c20 | 2010-09-28 18:36:04 +0000 | [diff] [blame^] | 162 | # Look for SSL 3 levels up from PC/VC6 - ie, same place zlib etc all live. |
Hirokazu Yamamoto | ecdfd51 | 2009-03-18 11:39:46 +0000 | [diff] [blame] | 163 | ssl_dir = find_best_ssl_dir(("..\\..\\..",)) |
Tim Peters | f32b027 | 2004-01-04 02:27:33 +0000 | [diff] [blame] | 164 | 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 Yamamoto | ecdfd51 | 2009-03-18 11:39:46 +0000 | [diff] [blame] | 171 | # 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 Peters | f32b027 | 2004-01-04 02:27:33 +0000 | [diff] [blame] | 180 | # Put our working Perl at the front of our path |
Hirokazu Yamamoto | ecdfd51 | 2009-03-18 11:39:46 +0000 | [diff] [blame] | 181 | os.environ["PATH"] = os.path.dirname(perl) + \ |
Tim Peters | f32b027 | 2004-01-04 02:27:33 +0000 | [diff] [blame] | 182 | os.pathsep + \ |
| 183 | os.environ["PATH"] |
Hirokazu Yamamoto | ecdfd51 | 2009-03-18 11:39:46 +0000 | [diff] [blame] | 184 | 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 Peters | f32b027 | 2004-01-04 02:27:33 +0000 | [diff] [blame] | 191 | |
Hirokazu Yamamoto | ecdfd51 | 2009-03-18 11:39:46 +0000 | [diff] [blame] | 192 | fix_makefile(makefile) |
Hirokazu Yamamoto | 7405c20 | 2010-09-28 18:36:04 +0000 | [diff] [blame^] | 193 | 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 |
| 197 | if perl is None: |
| 198 | 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 Peters | f32b027 | 2004-01-04 02:27:33 +0000 | [diff] [blame] | 204 | |
| 205 | # Now run make. |
Hirokazu Yamamoto | 7405c20 | 2010-09-28 18:36:04 +0000 | [diff] [blame^] | 206 | copy(r"crypto\buildinf_%s.h" % arch, r"crypto\buildinf.h") |
| 207 | copy(r"crypto\opensslconf_%s.h" % arch, r"crypto\opensslconf.h") |
Hirokazu Yamamoto | ecdfd51 | 2009-03-18 11:39:46 +0000 | [diff] [blame] | 208 | |
| 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 Peters | f32b027 | 2004-01-04 02:27:33 +0000 | [diff] [blame] | 218 | 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 | |
| 227 | if __name__=='__main__': |
| 228 | main() |