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 | ecdfd51 | 2009-03-18 11:39:46 +0000 | [diff] [blame] | 16 | import os, sys, re, shutil |
Tim Peters | f32b027 | 2004-01-04 02:27:33 +0000 | [diff] [blame] | 17 | |
| 18 | # Find all "foo.exe" files on the PATH. |
| 19 | def find_all_on_path(filename, extras = None): |
| 20 | entries = os.environ["PATH"].split(os.pathsep) |
| 21 | ret = [] |
| 22 | for p in entries: |
| 23 | fname = os.path.abspath(os.path.join(p, filename)) |
| 24 | if os.path.isfile(fname) and fname not in ret: |
| 25 | ret.append(fname) |
| 26 | if extras: |
| 27 | for p in extras: |
| 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 | return ret |
| 32 | |
| 33 | # Find a suitable Perl installation for OpenSSL. |
| 34 | # cygwin perl does *not* work. ActivePerl does. |
| 35 | # Being a Perl dummy, the simplest way I can check is if the "Win32" package |
| 36 | # is available. |
| 37 | def find_working_perl(perls): |
| 38 | for perl in perls: |
| 39 | fh = os.popen(perl + ' -e "use Win32;"') |
| 40 | fh.read() |
| 41 | rc = fh.close() |
| 42 | if rc: |
| 43 | continue |
| 44 | return perl |
Hirokazu Yamamoto | ecdfd51 | 2009-03-18 11:39:46 +0000 | [diff] [blame] | 45 | print("Can not find a suitable PERL:") |
Tim Peters | f32b027 | 2004-01-04 02:27:33 +0000 | [diff] [blame] | 46 | if perls: |
Hirokazu Yamamoto | ecdfd51 | 2009-03-18 11:39:46 +0000 | [diff] [blame] | 47 | print(" the following perl interpreters were found:") |
Tim Peters | f32b027 | 2004-01-04 02:27:33 +0000 | [diff] [blame] | 48 | for p in perls: |
Hirokazu Yamamoto | ecdfd51 | 2009-03-18 11:39:46 +0000 | [diff] [blame] | 49 | print(" ", p) |
| 50 | print(" None of these versions appear suitable for building OpenSSL") |
Tim Peters | f32b027 | 2004-01-04 02:27:33 +0000 | [diff] [blame] | 51 | else: |
Hirokazu Yamamoto | ecdfd51 | 2009-03-18 11:39:46 +0000 | [diff] [blame] | 52 | print(" NO perl interpreters were found on this machine at all!") |
| 53 | print(" Please install ActivePerl and ensure it appears on your path") |
Tim Peters | f32b027 | 2004-01-04 02:27:33 +0000 | [diff] [blame] | 54 | return None |
| 55 | |
| 56 | # Locate the best SSL directory given a few roots to look into. |
| 57 | def find_best_ssl_dir(sources): |
| 58 | candidates = [] |
| 59 | for s in sources: |
| 60 | try: |
Hirokazu Yamamoto | ecdfd51 | 2009-03-18 11:39:46 +0000 | [diff] [blame] | 61 | # note: do not abspath s; the build will fail if any |
| 62 | # higher up directory name has spaces in it. |
Tim Peters | f32b027 | 2004-01-04 02:27:33 +0000 | [diff] [blame] | 63 | fnames = os.listdir(s) |
| 64 | except os.error: |
| 65 | fnames = [] |
| 66 | for fname in fnames: |
| 67 | fqn = os.path.join(s, fname) |
| 68 | if os.path.isdir(fqn) and fname.startswith("openssl-"): |
| 69 | candidates.append(fqn) |
| 70 | # Now we have all the candidates, locate the best. |
| 71 | best_parts = [] |
| 72 | best_name = None |
| 73 | for c in candidates: |
| 74 | parts = re.split("[.-]", os.path.basename(c))[1:] |
| 75 | # eg - openssl-0.9.7-beta1 - ignore all "beta" or any other qualifiers |
| 76 | if len(parts) >= 4: |
| 77 | continue |
| 78 | if parts > best_parts: |
| 79 | best_parts = parts |
| 80 | best_name = c |
| 81 | if best_name is not None: |
Hirokazu Yamamoto | ecdfd51 | 2009-03-18 11:39:46 +0000 | [diff] [blame] | 82 | print("Found an SSL directory at '%s'" % (best_name,)) |
Tim Peters | f32b027 | 2004-01-04 02:27:33 +0000 | [diff] [blame] | 83 | else: |
Hirokazu Yamamoto | ecdfd51 | 2009-03-18 11:39:46 +0000 | [diff] [blame] | 84 | print("Could not find an SSL directory in '%s'" % (sources,)) |
| 85 | sys.stdout.flush() |
Tim Peters | f32b027 | 2004-01-04 02:27:33 +0000 | [diff] [blame] | 86 | return best_name |
| 87 | |
Hirokazu Yamamoto | ecdfd51 | 2009-03-18 11:39:46 +0000 | [diff] [blame] | 88 | def fix_makefile(makefile): |
| 89 | """Fix some stuff in all makefiles |
| 90 | """ |
| 91 | if not os.path.isfile(makefile): |
| 92 | return |
| 93 | # 2.4 compatibility |
| 94 | fin = open(makefile) |
| 95 | if 1: # with open(makefile) as fin: |
| 96 | lines = fin.readlines() |
| 97 | fin.close() |
| 98 | fout = open(makefile, 'w') |
| 99 | if 1: # with open(makefile, 'w') as fout: |
| 100 | for line in lines: |
| 101 | if line.startswith("PERL="): |
| 102 | continue |
| 103 | if line.startswith("CP="): |
| 104 | line = "CP=copy\n" |
| 105 | if line.startswith("MKDIR="): |
| 106 | line = "MKDIR=mkdir\n" |
| 107 | if line.startswith("CFLAG="): |
| 108 | line = line.strip() |
| 109 | for algo in ("RC5", "MDC2", "IDEA"): |
| 110 | noalgo = " -DOPENSSL_NO_%s" % algo |
| 111 | if noalgo not in line: |
| 112 | line = line + noalgo |
| 113 | line = line + '\n' |
| 114 | fout.write(line) |
| 115 | fout.close() |
| 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 | |
Tim Peters | f32b027 | 2004-01-04 02:27:33 +0000 | [diff] [blame] | 123 | def main(): |
| 124 | debug = "-d" in sys.argv |
| 125 | build_all = "-a" in sys.argv |
Hirokazu Yamamoto | ecdfd51 | 2009-03-18 11:39:46 +0000 | [diff] [blame] | 126 | if 1: # Win32 |
| 127 | arch = "x86" |
| 128 | configure = "VC-WIN32" |
| 129 | do_script = "ms\\do_nasm" |
| 130 | makefile="ms\\nt.mak" |
| 131 | m32 = makefile |
| 132 | configure += " no-idea no-rc5 no-mdc2" |
Tim Peters | f32b027 | 2004-01-04 02:27:33 +0000 | [diff] [blame] | 133 | make_flags = "" |
| 134 | if build_all: |
| 135 | make_flags = "-a" |
| 136 | # perl should be on the path, but we also look in "\perl" and "c:\\perl" |
| 137 | # as "well known" locations |
| 138 | perls = find_all_on_path("perl.exe", ["\\perl\\bin", "C:\\perl\\bin"]) |
| 139 | perl = find_working_perl(perls) |
| 140 | if perl is None: |
Hirokazu Yamamoto | ecdfd51 | 2009-03-18 11:39:46 +0000 | [diff] [blame] | 141 | print("No Perl installation was found. Existing Makefiles are used.") |
| 142 | else: |
| 143 | print("Found a working perl at '%s'" % (perl,)) |
| 144 | sys.stdout.flush() |
Tim Peters | f32b027 | 2004-01-04 02:27:33 +0000 | [diff] [blame] | 145 | # Look for SSL 3 levels up from pcbuild - ie, same place zlib etc all live. |
Hirokazu Yamamoto | ecdfd51 | 2009-03-18 11:39:46 +0000 | [diff] [blame] | 146 | ssl_dir = find_best_ssl_dir(("..\\..\\..",)) |
Tim Peters | f32b027 | 2004-01-04 02:27:33 +0000 | [diff] [blame] | 147 | if ssl_dir is None: |
| 148 | sys.exit(1) |
| 149 | |
| 150 | old_cd = os.getcwd() |
| 151 | try: |
| 152 | os.chdir(ssl_dir) |
| 153 | # 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] | 154 | # Due to a bug in this script, the makefile sometimes ended up empty |
| 155 | # Force a regeneration if it is. |
| 156 | if not os.path.isfile(makefile) or os.path.getsize(makefile)==0: |
| 157 | if perl is None: |
| 158 | print("Perl is required to build the makefiles!") |
| 159 | sys.exit(1) |
| 160 | |
| 161 | print("Creating the makefiles...") |
| 162 | sys.stdout.flush() |
Tim Peters | f32b027 | 2004-01-04 02:27:33 +0000 | [diff] [blame] | 163 | # Put our working Perl at the front of our path |
Hirokazu Yamamoto | ecdfd51 | 2009-03-18 11:39:46 +0000 | [diff] [blame] | 164 | os.environ["PATH"] = os.path.dirname(perl) + \ |
Tim Peters | f32b027 | 2004-01-04 02:27:33 +0000 | [diff] [blame] | 165 | os.pathsep + \ |
| 166 | os.environ["PATH"] |
Hirokazu Yamamoto | ecdfd51 | 2009-03-18 11:39:46 +0000 | [diff] [blame] | 167 | run_configure(configure, do_script) |
| 168 | if debug: |
| 169 | print("OpenSSL debug builds aren't supported.") |
| 170 | #if arch=="x86" and debug: |
| 171 | # # the do_masm script in openssl doesn't generate a debug |
| 172 | # # build makefile so we generate it here: |
| 173 | # os.system("perl util\mk1mf.pl debug "+configure+" >"+makefile) |
Tim Peters | f32b027 | 2004-01-04 02:27:33 +0000 | [diff] [blame] | 174 | |
Hirokazu Yamamoto | ecdfd51 | 2009-03-18 11:39:46 +0000 | [diff] [blame] | 175 | fix_makefile(makefile) |
| 176 | shutil.copy(r"crypto\buildinf.h", r"crypto\buildinf_%s.h" % arch) |
| 177 | shutil.copy(r"crypto\opensslconf.h", r"crypto\opensslconf_%s.h" % arch) |
Tim Peters | f32b027 | 2004-01-04 02:27:33 +0000 | [diff] [blame] | 178 | |
| 179 | # Now run make. |
Hirokazu Yamamoto | ecdfd51 | 2009-03-18 11:39:46 +0000 | [diff] [blame] | 180 | shutil.copy(r"crypto\buildinf_%s.h" % arch, r"crypto\buildinf.h") |
| 181 | shutil.copy(r"crypto\opensslconf_%s.h" % arch, r"crypto\opensslconf.h") |
| 182 | |
| 183 | #makeCommand = "nmake /nologo PERL=\"%s\" -f \"%s\"" %(perl, makefile) |
| 184 | makeCommand = "nmake /nologo -f \"%s\"" % makefile |
| 185 | print("Executing ssl makefiles:", makeCommand) |
| 186 | sys.stdout.flush() |
| 187 | rc = os.system(makeCommand) |
| 188 | if rc: |
| 189 | print("Executing "+makefile+" failed") |
| 190 | print(rc) |
| 191 | sys.exit(rc) |
Tim Peters | f32b027 | 2004-01-04 02:27:33 +0000 | [diff] [blame] | 192 | finally: |
| 193 | os.chdir(old_cd) |
| 194 | # And finally, we can build the _ssl module itself for Python. |
| 195 | defs = "SSL_DIR=%s" % (ssl_dir,) |
| 196 | if debug: |
| 197 | defs = defs + " " + "DEBUG=1" |
| 198 | rc = os.system('nmake /nologo -f _ssl.mak ' + defs + " " + make_flags) |
| 199 | sys.exit(rc) |
| 200 | |
| 201 | if __name__=='__main__': |
| 202 | main() |