Steve Dower | ac2bd5b | 2015-07-03 09:08:47 -0700 | [diff] [blame] | 1 | #! /usr/bin/env python3 |
Zachary Ware | 10c2dba | 2014-05-09 09:07:50 -0500 | [diff] [blame] | 2 | # Script for preparing OpenSSL for building on Windows. |
| 3 | # Uses Perl to create nmake makefiles and otherwise prepare the way |
| 4 | # for building on 32 or 64 bit platforms. |
| 5 | |
| 6 | # Script originally authored by Mark Hammond. |
| 7 | # Major revisions by: |
| 8 | # Martin v. Löwis |
| 9 | # Christian Heimes |
| 10 | # Zachary Ware |
| 11 | |
| 12 | # THEORETICALLY, you can: |
| 13 | # * Unpack the latest OpenSSL release where $(opensslDir) in |
| 14 | # PCbuild\pyproject.props expects it to be. |
| 15 | # * Install ActivePerl and ensure it is somewhere on your path. |
| 16 | # * Run this script with the OpenSSL source dir as the only argument. |
| 17 | # |
| 18 | # it should configure OpenSSL such that it is ready to be built by |
| 19 | # ssl.vcxproj on 32 or 64 bit platforms. |
| 20 | |
| 21 | import os |
| 22 | import re |
| 23 | import sys |
| 24 | import shutil |
Tim Golden | faf4d9c | 2014-05-09 18:01:44 +0100 | [diff] [blame] | 25 | import subprocess |
Zachary Ware | 10c2dba | 2014-05-09 09:07:50 -0500 | [diff] [blame] | 26 | |
| 27 | # Find all "foo.exe" files on the PATH. |
| 28 | def find_all_on_path(filename, extras = None): |
| 29 | entries = os.environ["PATH"].split(os.pathsep) |
| 30 | ret = [] |
| 31 | for p in entries: |
| 32 | fname = os.path.abspath(os.path.join(p, filename)) |
| 33 | if os.path.isfile(fname) and fname not in ret: |
| 34 | ret.append(fname) |
| 35 | if extras: |
| 36 | for p in extras: |
| 37 | fname = os.path.abspath(os.path.join(p, filename)) |
| 38 | if os.path.isfile(fname) and fname not in ret: |
| 39 | ret.append(fname) |
| 40 | return ret |
| 41 | |
| 42 | # Find a suitable Perl installation for OpenSSL. |
| 43 | # cygwin perl does *not* work. ActivePerl does. |
| 44 | # Being a Perl dummy, the simplest way I can check is if the "Win32" package |
| 45 | # is available. |
| 46 | def find_working_perl(perls): |
| 47 | for perl in perls: |
Tim Golden | faf4d9c | 2014-05-09 18:01:44 +0100 | [diff] [blame] | 48 | try: |
| 49 | subprocess.check_output([perl, "-e", "use Win32;"]) |
| 50 | except subprocess.CalledProcessError: |
Zachary Ware | 10c2dba | 2014-05-09 09:07:50 -0500 | [diff] [blame] | 51 | continue |
Tim Golden | faf4d9c | 2014-05-09 18:01:44 +0100 | [diff] [blame] | 52 | else: |
| 53 | return perl |
| 54 | |
Zachary Ware | 10c2dba | 2014-05-09 09:07:50 -0500 | [diff] [blame] | 55 | if perls: |
Tim Golden | faf4d9c | 2014-05-09 18:01:44 +0100 | [diff] [blame] | 56 | print("The following perl interpreters were found:") |
Zachary Ware | 10c2dba | 2014-05-09 09:07:50 -0500 | [diff] [blame] | 57 | for p in perls: |
| 58 | print(" ", p) |
| 59 | print(" None of these versions appear suitable for building OpenSSL") |
| 60 | else: |
Tim Golden | faf4d9c | 2014-05-09 18:01:44 +0100 | [diff] [blame] | 61 | print("NO perl interpreters were found on this machine at all!") |
Zachary Ware | 10c2dba | 2014-05-09 09:07:50 -0500 | [diff] [blame] | 62 | print(" Please install ActivePerl and ensure it appears on your path") |
Zachary Ware | 10c2dba | 2014-05-09 09:07:50 -0500 | [diff] [blame] | 63 | |
| 64 | def create_makefile64(makefile, m32): |
| 65 | """Create and fix makefile for 64bit |
| 66 | |
| 67 | Replace 32 with 64bit directories |
| 68 | """ |
| 69 | if not os.path.isfile(m32): |
| 70 | return |
| 71 | with open(m32) as fin: |
| 72 | with open(makefile, 'w') as fout: |
| 73 | for line in fin: |
| 74 | line = line.replace("=tmp32", "=tmp64") |
| 75 | line = line.replace("=out32", "=out64") |
| 76 | line = line.replace("=inc32", "=inc64") |
| 77 | # force 64 bit machine |
| 78 | line = line.replace("MKLIB=lib", "MKLIB=lib /MACHINE:X64") |
| 79 | line = line.replace("LFLAGS=", "LFLAGS=/MACHINE:X64 ") |
| 80 | # don't link against the lib on 64bit systems |
| 81 | line = line.replace("bufferoverflowu.lib", "") |
| 82 | fout.write(line) |
| 83 | os.unlink(m32) |
| 84 | |
Zachary Ware | 2897d07 | 2014-08-06 22:47:23 -0500 | [diff] [blame] | 85 | def create_asms(makefile): |
| 86 | #create a custom makefile out of the provided one |
| 87 | asm_makefile = os.path.splitext(makefile)[0] + '.asm.mak' |
| 88 | with open(makefile) as fin: |
| 89 | with open(asm_makefile, 'w') as fout: |
| 90 | for line in fin: |
| 91 | # Keep everything up to the install target (it's convenient) |
| 92 | if line.startswith('install: all'): |
| 93 | break |
| 94 | else: |
| 95 | fout.write(line) |
| 96 | asms = [] |
| 97 | for line in fin: |
| 98 | if '.asm' in line and line.strip().endswith('.pl'): |
| 99 | asms.append(line.split(':')[0]) |
| 100 | while line.strip(): |
| 101 | fout.write(line) |
| 102 | line = next(fin) |
| 103 | fout.write('\n') |
| 104 | |
| 105 | fout.write('asms: $(TMP_D) ') |
| 106 | fout.write(' '.join(asms)) |
| 107 | fout.write('\n') |
| 108 | |
| 109 | os.system('nmake /f {} PERL=perl asms'.format(asm_makefile)) |
| 110 | os.unlink(asm_makefile) |
| 111 | |
| 112 | |
| 113 | |
Zachary Ware | 10c2dba | 2014-05-09 09:07:50 -0500 | [diff] [blame] | 114 | def fix_makefile(makefile): |
| 115 | """Fix some stuff in all makefiles |
| 116 | """ |
| 117 | if not os.path.isfile(makefile): |
| 118 | return |
Zachary Ware | a59f963 | 2015-04-09 15:48:32 -0500 | [diff] [blame] | 119 | copy_if_different = r'$(PERL) $(SRC_D)\util\copy-if-different.pl' |
Zachary Ware | 10c2dba | 2014-05-09 09:07:50 -0500 | [diff] [blame] | 120 | with open(makefile) as fin: |
| 121 | lines = fin.readlines() |
| 122 | with open(makefile, 'w') as fout: |
| 123 | for line in lines: |
| 124 | if line.startswith("PERL="): |
| 125 | continue |
| 126 | if line.startswith("CP="): |
| 127 | line = "CP=copy\n" |
| 128 | if line.startswith("MKDIR="): |
| 129 | line = "MKDIR=mkdir\n" |
| 130 | if line.startswith("CFLAG="): |
| 131 | line = line.strip() |
| 132 | for algo in ("RC5", "MDC2", "IDEA"): |
| 133 | noalgo = " -DOPENSSL_NO_%s" % algo |
| 134 | if noalgo not in line: |
| 135 | line = line + noalgo |
| 136 | line = line + '\n' |
Zachary Ware | a59f963 | 2015-04-09 15:48:32 -0500 | [diff] [blame] | 137 | if copy_if_different in line: |
| 138 | line = line.replace(copy_if_different, 'copy /Y') |
Zachary Ware | 10c2dba | 2014-05-09 09:07:50 -0500 | [diff] [blame] | 139 | fout.write(line) |
| 140 | |
| 141 | def run_configure(configure, do_script): |
| 142 | print("perl Configure "+configure+" no-idea no-mdc2") |
| 143 | os.system("perl Configure "+configure+" no-idea no-mdc2") |
| 144 | print(do_script) |
| 145 | os.system(do_script) |
| 146 | |
| 147 | def cmp(f1, f2): |
| 148 | bufsize = 1024 * 8 |
| 149 | with open(f1, 'rb') as fp1, open(f2, 'rb') as fp2: |
| 150 | while True: |
| 151 | b1 = fp1.read(bufsize) |
| 152 | b2 = fp2.read(bufsize) |
| 153 | if b1 != b2: |
| 154 | return False |
| 155 | if not b1: |
| 156 | return True |
| 157 | |
| 158 | def copy(src, dst): |
| 159 | if os.path.isfile(dst) and cmp(src, dst): |
| 160 | return |
| 161 | shutil.copy(src, dst) |
| 162 | |
| 163 | def prep(arch): |
| 164 | if arch == "x86": |
| 165 | configure = "VC-WIN32" |
| 166 | do_script = "ms\\do_nasm" |
| 167 | makefile="ms\\nt.mak" |
| 168 | m32 = makefile |
| 169 | dirsuffix = "32" |
| 170 | elif arch == "amd64": |
| 171 | configure = "VC-WIN64A" |
| 172 | do_script = "ms\\do_win64a" |
| 173 | makefile = "ms\\nt64.mak" |
| 174 | m32 = makefile.replace('64', '') |
| 175 | dirsuffix = "64" |
| 176 | #os.environ["VSEXTCOMP_USECL"] = "MS_OPTERON" |
| 177 | else: |
| 178 | raise ValueError('Unrecognized platform: %s' % arch) |
| 179 | |
| 180 | # rebuild makefile when we do the role over from 32 to 64 build |
| 181 | if arch == "amd64" and os.path.isfile(m32) and not os.path.isfile(makefile): |
| 182 | os.unlink(m32) |
| 183 | |
| 184 | # If the ssl makefiles do not exist, we invoke Perl to generate them. |
| 185 | # Due to a bug in this script, the makefile sometimes ended up empty |
| 186 | # Force a regeneration if it is. |
| 187 | if not os.path.isfile(makefile) or os.path.getsize(makefile)==0: |
| 188 | print("Creating the makefiles...") |
| 189 | sys.stdout.flush() |
| 190 | run_configure(configure, do_script) |
| 191 | |
| 192 | if arch == "amd64": |
| 193 | create_makefile64(makefile, m32) |
| 194 | fix_makefile(makefile) |
| 195 | copy(r"crypto\buildinf.h", r"crypto\buildinf_%s.h" % arch) |
| 196 | copy(r"crypto\opensslconf.h", r"crypto\opensslconf_%s.h" % arch) |
| 197 | else: |
| 198 | print(makefile, 'already exists!') |
| 199 | |
Zachary Ware | 2897d07 | 2014-08-06 22:47:23 -0500 | [diff] [blame] | 200 | print('creating asms...') |
| 201 | create_asms(makefile) |
Zachary Ware | 10c2dba | 2014-05-09 09:07:50 -0500 | [diff] [blame] | 202 | |
| 203 | def main(): |
| 204 | if len(sys.argv) == 1: |
| 205 | print("Not enough arguments: directory containing OpenSSL", |
| 206 | "sources must be supplied") |
| 207 | sys.exit(1) |
| 208 | |
| 209 | if len(sys.argv) > 2: |
| 210 | print("Too many arguments supplied, all we need is the directory", |
| 211 | "containing OpenSSL sources") |
| 212 | sys.exit(1) |
| 213 | |
| 214 | ssl_dir = sys.argv[1] |
| 215 | |
Charles-François Natali | 6315ffa | 2014-06-20 22:41:21 +0100 | [diff] [blame] | 216 | if not os.path.isdir(ssl_dir): |
Zachary Ware | 10c2dba | 2014-05-09 09:07:50 -0500 | [diff] [blame] | 217 | print(ssl_dir, "is not an existing directory!") |
| 218 | sys.exit(1) |
| 219 | |
| 220 | # perl should be on the path, but we also look in "\perl" and "c:\\perl" |
| 221 | # as "well known" locations |
Zachary Ware | 7dfa094 | 2015-04-13 10:53:11 -0500 | [diff] [blame] | 222 | perls = find_all_on_path("perl.exe", [r"\perl\bin", |
| 223 | r"C:\perl\bin", |
| 224 | r"\perl64\bin", |
| 225 | r"C:\perl64\bin", |
| 226 | ]) |
Zachary Ware | 10c2dba | 2014-05-09 09:07:50 -0500 | [diff] [blame] | 227 | perl = find_working_perl(perls) |
| 228 | if perl: |
| 229 | print("Found a working perl at '%s'" % (perl,)) |
| 230 | else: |
| 231 | sys.exit(1) |
| 232 | sys.stdout.flush() |
| 233 | |
| 234 | # Put our working Perl at the front of our path |
| 235 | os.environ["PATH"] = os.path.dirname(perl) + \ |
| 236 | os.pathsep + \ |
| 237 | os.environ["PATH"] |
| 238 | |
| 239 | old_cwd = os.getcwd() |
| 240 | try: |
| 241 | os.chdir(ssl_dir) |
| 242 | for arch in ['amd64', 'x86']: |
| 243 | prep(arch) |
| 244 | finally: |
| 245 | os.chdir(old_cwd) |
| 246 | |
| 247 | if __name__=='__main__': |
| 248 | main() |