| 1 | n/a | # Script for building the _ssl and _hashlib modules for Windows. |
|---|
| 2 | n/a | # Uses Perl to setup the OpenSSL environment correctly |
|---|
| 3 | n/a | # and build OpenSSL, then invokes a simple nmake session |
|---|
| 4 | n/a | # for the actual _ssl.pyd and _hashlib.pyd DLLs. |
|---|
| 5 | n/a | |
|---|
| 6 | n/a | # THEORETICALLY, you can: |
|---|
| 7 | n/a | # * Unpack the latest SSL release one level above your main Python source |
|---|
| 8 | n/a | # directory. It is likely you will already find the zlib library and |
|---|
| 9 | n/a | # any other external packages there. |
|---|
| 10 | n/a | # * Install ActivePerl and ensure it is somewhere on your path. |
|---|
| 11 | n/a | # * Run this script from the PCBuild directory. |
|---|
| 12 | n/a | # |
|---|
| 13 | n/a | # it should configure and build SSL, then build the _ssl and _hashlib |
|---|
| 14 | n/a | # Python extensions without intervention. |
|---|
| 15 | n/a | |
|---|
| 16 | n/a | # Modified by Christian Heimes |
|---|
| 17 | n/a | # Now this script supports pre-generated makefiles and assembly files. |
|---|
| 18 | n/a | # Developers don't need an installation of Perl anymore to build Python. A svn |
|---|
| 19 | n/a | # checkout from our svn repository is enough. |
|---|
| 20 | n/a | # |
|---|
| 21 | n/a | # In Order to create the files in the case of an update you still need Perl. |
|---|
| 22 | n/a | # Run build_ssl in this order: |
|---|
| 23 | n/a | # python.exe build_ssl.py Release x64 |
|---|
| 24 | n/a | # python.exe build_ssl.py Release Win32 |
|---|
| 25 | n/a | |
|---|
| 26 | n/a | import os, sys, re, shutil |
|---|
| 27 | n/a | |
|---|
| 28 | n/a | # Find all "foo.exe" files on the PATH. |
|---|
| 29 | n/a | def find_all_on_path(filename, extras = None): |
|---|
| 30 | n/a | entries = os.environ["PATH"].split(os.pathsep) |
|---|
| 31 | n/a | ret = [] |
|---|
| 32 | n/a | for p in entries: |
|---|
| 33 | n/a | fname = os.path.abspath(os.path.join(p, filename)) |
|---|
| 34 | n/a | if os.path.isfile(fname) and fname not in ret: |
|---|
| 35 | n/a | ret.append(fname) |
|---|
| 36 | n/a | if extras: |
|---|
| 37 | n/a | for p in extras: |
|---|
| 38 | n/a | fname = os.path.abspath(os.path.join(p, filename)) |
|---|
| 39 | n/a | if os.path.isfile(fname) and fname not in ret: |
|---|
| 40 | n/a | ret.append(fname) |
|---|
| 41 | n/a | return ret |
|---|
| 42 | n/a | |
|---|
| 43 | n/a | # Find a suitable Perl installation for OpenSSL. |
|---|
| 44 | n/a | # cygwin perl does *not* work. ActivePerl does. |
|---|
| 45 | n/a | # Being a Perl dummy, the simplest way I can check is if the "Win32" package |
|---|
| 46 | n/a | # is available. |
|---|
| 47 | n/a | def find_working_perl(perls): |
|---|
| 48 | n/a | for perl in perls: |
|---|
| 49 | n/a | fh = os.popen('"%s" -e "use Win32;"' % perl) |
|---|
| 50 | n/a | fh.read() |
|---|
| 51 | n/a | rc = fh.close() |
|---|
| 52 | n/a | if rc: |
|---|
| 53 | n/a | continue |
|---|
| 54 | n/a | return perl |
|---|
| 55 | n/a | print("Can not find a suitable PERL:") |
|---|
| 56 | n/a | if perls: |
|---|
| 57 | n/a | print(" the following perl interpreters were found:") |
|---|
| 58 | n/a | for p in perls: |
|---|
| 59 | n/a | print(" ", p) |
|---|
| 60 | n/a | print(" None of these versions appear suitable for building OpenSSL") |
|---|
| 61 | n/a | else: |
|---|
| 62 | n/a | print(" NO perl interpreters were found on this machine at all!") |
|---|
| 63 | n/a | print(" Please install ActivePerl and ensure it appears on your path") |
|---|
| 64 | n/a | return None |
|---|
| 65 | n/a | |
|---|
| 66 | n/a | # Fetch SSL directory from VC properties |
|---|
| 67 | n/a | def get_ssl_dir(): |
|---|
| 68 | n/a | propfile = (os.path.join(os.path.dirname(__file__), 'pyproject.props')) |
|---|
| 69 | n/a | with open(propfile) as f: |
|---|
| 70 | n/a | m = re.search('openssl-([^<]+)<', f.read()) |
|---|
| 71 | n/a | return "..\..\openssl-"+m.group(1) |
|---|
| 72 | n/a | |
|---|
| 73 | n/a | |
|---|
| 74 | n/a | def create_makefile64(makefile, m32): |
|---|
| 75 | n/a | """Create and fix makefile for 64bit |
|---|
| 76 | n/a | |
|---|
| 77 | n/a | Replace 32 with 64bit directories |
|---|
| 78 | n/a | """ |
|---|
| 79 | n/a | if not os.path.isfile(m32): |
|---|
| 80 | n/a | return |
|---|
| 81 | n/a | with open(m32) as fin: |
|---|
| 82 | n/a | with open(makefile, 'w') as fout: |
|---|
| 83 | n/a | for line in fin: |
|---|
| 84 | n/a | line = line.replace("=tmp32", "=tmp64") |
|---|
| 85 | n/a | line = line.replace("=out32", "=out64") |
|---|
| 86 | n/a | line = line.replace("=inc32", "=inc64") |
|---|
| 87 | n/a | # force 64 bit machine |
|---|
| 88 | n/a | line = line.replace("MKLIB=lib", "MKLIB=lib /MACHINE:X64") |
|---|
| 89 | n/a | line = line.replace("LFLAGS=", "LFLAGS=/MACHINE:X64 ") |
|---|
| 90 | n/a | # don't link against the lib on 64bit systems |
|---|
| 91 | n/a | line = line.replace("bufferoverflowu.lib", "") |
|---|
| 92 | n/a | fout.write(line) |
|---|
| 93 | n/a | os.unlink(m32) |
|---|
| 94 | n/a | |
|---|
| 95 | n/a | def fix_makefile(makefile): |
|---|
| 96 | n/a | """Fix some stuff in all makefiles |
|---|
| 97 | n/a | """ |
|---|
| 98 | n/a | if not os.path.isfile(makefile): |
|---|
| 99 | n/a | return |
|---|
| 100 | n/a | with open(makefile) as fin: |
|---|
| 101 | n/a | lines = fin.readlines() |
|---|
| 102 | n/a | with open(makefile, 'w') as fout: |
|---|
| 103 | n/a | for line in lines: |
|---|
| 104 | n/a | if line.startswith("PERL="): |
|---|
| 105 | n/a | continue |
|---|
| 106 | n/a | if line.startswith("CP="): |
|---|
| 107 | n/a | line = "CP=copy\n" |
|---|
| 108 | n/a | if line.startswith("MKDIR="): |
|---|
| 109 | n/a | line = "MKDIR=mkdir\n" |
|---|
| 110 | n/a | if line.startswith("CFLAG="): |
|---|
| 111 | n/a | line = line.strip() |
|---|
| 112 | n/a | for algo in ("RC5", "MDC2", "IDEA"): |
|---|
| 113 | n/a | noalgo = " -DOPENSSL_NO_%s" % algo |
|---|
| 114 | n/a | if noalgo not in line: |
|---|
| 115 | n/a | line = line + noalgo |
|---|
| 116 | n/a | line = line + '\n' |
|---|
| 117 | n/a | fout.write(line) |
|---|
| 118 | n/a | |
|---|
| 119 | n/a | def run_configure(configure, do_script): |
|---|
| 120 | n/a | print("perl Configure "+configure+" no-idea no-mdc2") |
|---|
| 121 | n/a | os.system("perl Configure "+configure+" no-idea no-mdc2") |
|---|
| 122 | n/a | print(do_script) |
|---|
| 123 | n/a | os.system(do_script) |
|---|
| 124 | n/a | |
|---|
| 125 | n/a | def cmp(f1, f2): |
|---|
| 126 | n/a | bufsize = 1024 * 8 |
|---|
| 127 | n/a | with open(f1, 'rb') as fp1, open(f2, 'rb') as fp2: |
|---|
| 128 | n/a | while True: |
|---|
| 129 | n/a | b1 = fp1.read(bufsize) |
|---|
| 130 | n/a | b2 = fp2.read(bufsize) |
|---|
| 131 | n/a | if b1 != b2: |
|---|
| 132 | n/a | return False |
|---|
| 133 | n/a | if not b1: |
|---|
| 134 | n/a | return True |
|---|
| 135 | n/a | |
|---|
| 136 | n/a | def copy(src, dst): |
|---|
| 137 | n/a | if os.path.isfile(dst) and cmp(src, dst): |
|---|
| 138 | n/a | return |
|---|
| 139 | n/a | shutil.copy(src, dst) |
|---|
| 140 | n/a | |
|---|
| 141 | n/a | def main(): |
|---|
| 142 | n/a | build_all = "-a" in sys.argv |
|---|
| 143 | n/a | if sys.argv[1] == "Release": |
|---|
| 144 | n/a | debug = False |
|---|
| 145 | n/a | elif sys.argv[1] == "Debug": |
|---|
| 146 | n/a | debug = True |
|---|
| 147 | n/a | else: |
|---|
| 148 | n/a | raise ValueError(str(sys.argv)) |
|---|
| 149 | n/a | |
|---|
| 150 | n/a | if sys.argv[2] == "Win32": |
|---|
| 151 | n/a | arch = "x86" |
|---|
| 152 | n/a | configure = "VC-WIN32" |
|---|
| 153 | n/a | do_script = "ms\\do_nasm" |
|---|
| 154 | n/a | makefile="ms\\nt.mak" |
|---|
| 155 | n/a | m32 = makefile |
|---|
| 156 | n/a | dirsuffix = "32" |
|---|
| 157 | n/a | elif sys.argv[2] == "x64": |
|---|
| 158 | n/a | arch="amd64" |
|---|
| 159 | n/a | configure = "VC-WIN64A" |
|---|
| 160 | n/a | do_script = "ms\\do_win64a" |
|---|
| 161 | n/a | makefile = "ms\\nt64.mak" |
|---|
| 162 | n/a | m32 = makefile.replace('64', '') |
|---|
| 163 | n/a | dirsuffix = "64" |
|---|
| 164 | n/a | #os.environ["VSEXTCOMP_USECL"] = "MS_OPTERON" |
|---|
| 165 | n/a | else: |
|---|
| 166 | n/a | raise ValueError(str(sys.argv)) |
|---|
| 167 | n/a | |
|---|
| 168 | n/a | make_flags = "" |
|---|
| 169 | n/a | if build_all: |
|---|
| 170 | n/a | make_flags = "-a" |
|---|
| 171 | n/a | # perl should be on the path, but we also look in "\perl" and "c:\\perl" |
|---|
| 172 | n/a | # as "well known" locations |
|---|
| 173 | n/a | perls = find_all_on_path("perl.exe", ["\\perl\\bin", "C:\\perl\\bin"]) |
|---|
| 174 | n/a | perl = find_working_perl(perls) |
|---|
| 175 | n/a | if perl: |
|---|
| 176 | n/a | print("Found a working perl at '%s'" % (perl,)) |
|---|
| 177 | n/a | else: |
|---|
| 178 | n/a | print("No Perl installation was found. Existing Makefiles are used.") |
|---|
| 179 | n/a | sys.stdout.flush() |
|---|
| 180 | n/a | # Look for SSL 2 levels up from pcbuild - ie, same place zlib etc all live. |
|---|
| 181 | n/a | ssl_dir = get_ssl_dir() |
|---|
| 182 | n/a | if ssl_dir is None: |
|---|
| 183 | n/a | sys.exit(1) |
|---|
| 184 | n/a | |
|---|
| 185 | n/a | old_cd = os.getcwd() |
|---|
| 186 | n/a | try: |
|---|
| 187 | n/a | os.chdir(ssl_dir) |
|---|
| 188 | n/a | # rebuild makefile when we do the role over from 32 to 64 build |
|---|
| 189 | n/a | if arch == "amd64" and os.path.isfile(m32) and not os.path.isfile(makefile): |
|---|
| 190 | n/a | os.unlink(m32) |
|---|
| 191 | n/a | |
|---|
| 192 | n/a | # If the ssl makefiles do not exist, we invoke Perl to generate them. |
|---|
| 193 | n/a | # Due to a bug in this script, the makefile sometimes ended up empty |
|---|
| 194 | n/a | # Force a regeneration if it is. |
|---|
| 195 | n/a | if not os.path.isfile(makefile) or os.path.getsize(makefile)==0: |
|---|
| 196 | n/a | if perl is None: |
|---|
| 197 | n/a | print("Perl is required to build the makefiles!") |
|---|
| 198 | n/a | sys.exit(1) |
|---|
| 199 | n/a | |
|---|
| 200 | n/a | print("Creating the makefiles...") |
|---|
| 201 | n/a | sys.stdout.flush() |
|---|
| 202 | n/a | # Put our working Perl at the front of our path |
|---|
| 203 | n/a | os.environ["PATH"] = os.path.dirname(perl) + \ |
|---|
| 204 | n/a | os.pathsep + \ |
|---|
| 205 | n/a | os.environ["PATH"] |
|---|
| 206 | n/a | run_configure(configure, do_script) |
|---|
| 207 | n/a | if debug: |
|---|
| 208 | n/a | print("OpenSSL debug builds aren't supported.") |
|---|
| 209 | n/a | #if arch=="x86" and debug: |
|---|
| 210 | n/a | # # the do_masm script in openssl doesn't generate a debug |
|---|
| 211 | n/a | # # build makefile so we generate it here: |
|---|
| 212 | n/a | # os.system("perl util\mk1mf.pl debug "+configure+" >"+makefile) |
|---|
| 213 | n/a | |
|---|
| 214 | n/a | if arch == "amd64": |
|---|
| 215 | n/a | create_makefile64(makefile, m32) |
|---|
| 216 | n/a | fix_makefile(makefile) |
|---|
| 217 | n/a | copy(r"crypto\buildinf.h", r"crypto\buildinf_%s.h" % arch) |
|---|
| 218 | n/a | copy(r"crypto\opensslconf.h", r"crypto\opensslconf_%s.h" % arch) |
|---|
| 219 | n/a | |
|---|
| 220 | n/a | # If the assembler files don't exist in tmpXX, copy them there |
|---|
| 221 | n/a | if perl is None and os.path.exists("asm"+dirsuffix): |
|---|
| 222 | n/a | if not os.path.exists("tmp"+dirsuffix): |
|---|
| 223 | n/a | os.mkdir("tmp"+dirsuffix) |
|---|
| 224 | n/a | for f in os.listdir("asm"+dirsuffix): |
|---|
| 225 | n/a | if not f.endswith(".asm"): continue |
|---|
| 226 | n/a | if os.path.isfile(r"tmp%s\%s" % (dirsuffix, f)): continue |
|---|
| 227 | n/a | shutil.copy(r"asm%s\%s" % (dirsuffix, f), "tmp"+dirsuffix) |
|---|
| 228 | n/a | |
|---|
| 229 | n/a | # Now run make. |
|---|
| 230 | n/a | if arch == "amd64": |
|---|
| 231 | n/a | rc = os.system("nasm -f win64 -DNEAR -Ox -g ms\\uptable.asm") |
|---|
| 232 | n/a | if rc: |
|---|
| 233 | n/a | print("nasm assembler has failed.") |
|---|
| 234 | n/a | sys.exit(rc) |
|---|
| 235 | n/a | |
|---|
| 236 | n/a | copy(r"crypto\buildinf_%s.h" % arch, r"crypto\buildinf.h") |
|---|
| 237 | n/a | copy(r"crypto\opensslconf_%s.h" % arch, r"crypto\opensslconf.h") |
|---|
| 238 | n/a | |
|---|
| 239 | n/a | #makeCommand = "nmake /nologo PERL=\"%s\" -f \"%s\"" %(perl, makefile) |
|---|
| 240 | n/a | makeCommand = "nmake /nologo -f \"%s\"" % makefile |
|---|
| 241 | n/a | print("Executing ssl makefiles:", makeCommand) |
|---|
| 242 | n/a | sys.stdout.flush() |
|---|
| 243 | n/a | rc = os.system(makeCommand) |
|---|
| 244 | n/a | if rc: |
|---|
| 245 | n/a | print("Executing "+makefile+" failed") |
|---|
| 246 | n/a | print(rc) |
|---|
| 247 | n/a | sys.exit(rc) |
|---|
| 248 | n/a | finally: |
|---|
| 249 | n/a | os.chdir(old_cd) |
|---|
| 250 | n/a | sys.exit(rc) |
|---|
| 251 | n/a | |
|---|
| 252 | n/a | if __name__=='__main__': |
|---|
| 253 | n/a | main() |
|---|