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 |
Tim Golden | faf4d9c | 2014-05-09 18:01:44 +0100 | [diff] [blame] | 24 | import subprocess |
Zachary Ware | 16f164e | 2016-02-22 04:02:30 -0600 | [diff] [blame] | 25 | from shutil import copy |
Zachary Ware | 10c2dba | 2014-05-09 09:07:50 -0500 | [diff] [blame] | 26 | |
| 27 | # Find all "foo.exe" files on the PATH. |
Zachary Ware | 16f164e | 2016-02-22 04:02:30 -0600 | [diff] [blame] | 28 | def find_all_on_path(filename, extras=None): |
Zachary Ware | 10c2dba | 2014-05-09 09:07:50 -0500 | [diff] [blame] | 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 | |
Zachary Ware | 16f164e | 2016-02-22 04:02:30 -0600 | [diff] [blame] | 42 | |
Zachary Ware | 10c2dba | 2014-05-09 09:07:50 -0500 | [diff] [blame] | 43 | # Find a suitable Perl installation for OpenSSL. |
| 44 | # cygwin perl does *not* work. ActivePerl does. |
| 45 | # Being a Perl dummy, the simplest way I can check is if the "Win32" package |
| 46 | # is available. |
| 47 | def find_working_perl(perls): |
| 48 | for perl in perls: |
Tim Golden | faf4d9c | 2014-05-09 18:01:44 +0100 | [diff] [blame] | 49 | try: |
| 50 | subprocess.check_output([perl, "-e", "use Win32;"]) |
| 51 | except subprocess.CalledProcessError: |
Zachary Ware | 10c2dba | 2014-05-09 09:07:50 -0500 | [diff] [blame] | 52 | continue |
Tim Golden | faf4d9c | 2014-05-09 18:01:44 +0100 | [diff] [blame] | 53 | else: |
| 54 | return perl |
| 55 | |
Zachary Ware | 10c2dba | 2014-05-09 09:07:50 -0500 | [diff] [blame] | 56 | if perls: |
Tim Golden | faf4d9c | 2014-05-09 18:01:44 +0100 | [diff] [blame] | 57 | print("The following perl interpreters were found:") |
Zachary Ware | 10c2dba | 2014-05-09 09:07:50 -0500 | [diff] [blame] | 58 | for p in perls: |
| 59 | print(" ", p) |
| 60 | print(" None of these versions appear suitable for building OpenSSL") |
| 61 | else: |
Tim Golden | faf4d9c | 2014-05-09 18:01:44 +0100 | [diff] [blame] | 62 | print("NO perl interpreters were found on this machine at all!") |
Zachary Ware | 10c2dba | 2014-05-09 09:07:50 -0500 | [diff] [blame] | 63 | print(" Please install ActivePerl and ensure it appears on your path") |
Zachary Ware | 10c2dba | 2014-05-09 09:07:50 -0500 | [diff] [blame] | 64 | |
Zachary Ware | 10c2dba | 2014-05-09 09:07:50 -0500 | [diff] [blame] | 65 | |
Zachary Ware | 16f164e | 2016-02-22 04:02:30 -0600 | [diff] [blame] | 66 | def create_asms(makefile, tmp_d): |
Zachary Ware | 2897d07 | 2014-08-06 22:47:23 -0500 | [diff] [blame] | 67 | #create a custom makefile out of the provided one |
| 68 | asm_makefile = os.path.splitext(makefile)[0] + '.asm.mak' |
Zachary Ware | 16f164e | 2016-02-22 04:02:30 -0600 | [diff] [blame] | 69 | with open(makefile) as fin, open(asm_makefile, 'w') as fout: |
| 70 | for line in fin: |
| 71 | # Keep everything up to the install target (it's convenient) |
| 72 | if line.startswith('install: all'): |
| 73 | break |
| 74 | fout.write(line) |
| 75 | asms = [] |
| 76 | for line in fin: |
| 77 | if '.asm' in line and line.strip().endswith('.pl'): |
| 78 | asms.append(line.split(':')[0]) |
| 79 | while line.strip(): |
Zachary Ware | 2897d07 | 2014-08-06 22:47:23 -0500 | [diff] [blame] | 80 | fout.write(line) |
Zachary Ware | 16f164e | 2016-02-22 04:02:30 -0600 | [diff] [blame] | 81 | line = next(fin) |
| 82 | fout.write('\n') |
Zachary Ware | 2897d07 | 2014-08-06 22:47:23 -0500 | [diff] [blame] | 83 | |
Zachary Ware | 16f164e | 2016-02-22 04:02:30 -0600 | [diff] [blame] | 84 | fout.write('asms: $(TMP_D) ') |
| 85 | fout.write(' '.join(asms)) |
| 86 | fout.write('\n') |
| 87 | os.system('nmake /f {} PERL=perl TMP_D={} asms'.format(asm_makefile, tmp_d)) |
Zachary Ware | 2897d07 | 2014-08-06 22:47:23 -0500 | [diff] [blame] | 88 | |
| 89 | |
Zachary Ware | 16f164e | 2016-02-22 04:02:30 -0600 | [diff] [blame] | 90 | def copy_includes(makefile, suffix): |
| 91 | dir = 'include'+suffix+'\\openssl' |
| 92 | os.makedirs(dir, exist_ok=True) |
Zachary Ware | a59f963 | 2015-04-09 15:48:32 -0500 | [diff] [blame] | 93 | copy_if_different = r'$(PERL) $(SRC_D)\util\copy-if-different.pl' |
Zachary Ware | 10c2dba | 2014-05-09 09:07:50 -0500 | [diff] [blame] | 94 | with open(makefile) as fin: |
Zachary Ware | 16f164e | 2016-02-22 04:02:30 -0600 | [diff] [blame] | 95 | for line in fin: |
Zachary Ware | a59f963 | 2015-04-09 15:48:32 -0500 | [diff] [blame] | 96 | if copy_if_different in line: |
Zachary Ware | 16f164e | 2016-02-22 04:02:30 -0600 | [diff] [blame] | 97 | perl, script, src, dest = line.split() |
| 98 | if not '$(INCO_D)' in dest: |
| 99 | continue |
| 100 | # We're in the root of the source tree |
| 101 | src = src.replace('$(SRC_D)', '.').strip('"') |
| 102 | dest = dest.strip('"').replace('$(INCO_D)', dir) |
| 103 | print('copying', src, 'to', dest) |
| 104 | copy(src, dest) |
| 105 | |
Zachary Ware | 10c2dba | 2014-05-09 09:07:50 -0500 | [diff] [blame] | 106 | |
| 107 | def run_configure(configure, do_script): |
| 108 | print("perl Configure "+configure+" no-idea no-mdc2") |
| 109 | os.system("perl Configure "+configure+" no-idea no-mdc2") |
| 110 | print(do_script) |
| 111 | os.system(do_script) |
| 112 | |
Zachary Ware | 10c2dba | 2014-05-09 09:07:50 -0500 | [diff] [blame] | 113 | |
| 114 | def prep(arch): |
Zachary Ware | 16f164e | 2016-02-22 04:02:30 -0600 | [diff] [blame] | 115 | makefile_template = "ms\\nt{}.mak" |
| 116 | generated_makefile = makefile_template.format('') |
Zachary Ware | 10c2dba | 2014-05-09 09:07:50 -0500 | [diff] [blame] | 117 | if arch == "x86": |
| 118 | configure = "VC-WIN32" |
| 119 | do_script = "ms\\do_nasm" |
Zachary Ware | 16f164e | 2016-02-22 04:02:30 -0600 | [diff] [blame] | 120 | suffix = "32" |
Zachary Ware | 10c2dba | 2014-05-09 09:07:50 -0500 | [diff] [blame] | 121 | elif arch == "amd64": |
| 122 | configure = "VC-WIN64A" |
| 123 | do_script = "ms\\do_win64a" |
Zachary Ware | 16f164e | 2016-02-22 04:02:30 -0600 | [diff] [blame] | 124 | suffix = "64" |
Zachary Ware | 10c2dba | 2014-05-09 09:07:50 -0500 | [diff] [blame] | 125 | #os.environ["VSEXTCOMP_USECL"] = "MS_OPTERON" |
| 126 | else: |
| 127 | raise ValueError('Unrecognized platform: %s' % arch) |
| 128 | |
Zachary Ware | 16f164e | 2016-02-22 04:02:30 -0600 | [diff] [blame] | 129 | print("Creating the makefiles...") |
| 130 | sys.stdout.flush() |
| 131 | # run configure, copy includes, create asms |
| 132 | run_configure(configure, do_script) |
| 133 | makefile = makefile_template.format(suffix) |
| 134 | try: |
| 135 | os.unlink(makefile) |
| 136 | except FileNotFoundError: |
| 137 | pass |
| 138 | os.rename(generated_makefile, makefile) |
| 139 | copy_includes(makefile, suffix) |
Zachary Ware | 10c2dba | 2014-05-09 09:07:50 -0500 | [diff] [blame] | 140 | |
Zachary Ware | 2897d07 | 2014-08-06 22:47:23 -0500 | [diff] [blame] | 141 | print('creating asms...') |
Zachary Ware | 16f164e | 2016-02-22 04:02:30 -0600 | [diff] [blame] | 142 | create_asms(makefile, 'tmp'+suffix) |
| 143 | |
Zachary Ware | 10c2dba | 2014-05-09 09:07:50 -0500 | [diff] [blame] | 144 | |
| 145 | def main(): |
| 146 | if len(sys.argv) == 1: |
| 147 | print("Not enough arguments: directory containing OpenSSL", |
| 148 | "sources must be supplied") |
| 149 | sys.exit(1) |
| 150 | |
| 151 | if len(sys.argv) > 2: |
| 152 | print("Too many arguments supplied, all we need is the directory", |
| 153 | "containing OpenSSL sources") |
| 154 | sys.exit(1) |
| 155 | |
| 156 | ssl_dir = sys.argv[1] |
| 157 | |
Charles-François Natali | 6315ffa | 2014-06-20 22:41:21 +0100 | [diff] [blame] | 158 | if not os.path.isdir(ssl_dir): |
Zachary Ware | 10c2dba | 2014-05-09 09:07:50 -0500 | [diff] [blame] | 159 | print(ssl_dir, "is not an existing directory!") |
| 160 | sys.exit(1) |
| 161 | |
| 162 | # perl should be on the path, but we also look in "\perl" and "c:\\perl" |
| 163 | # as "well known" locations |
Zachary Ware | 7dfa094 | 2015-04-13 10:53:11 -0500 | [diff] [blame] | 164 | perls = find_all_on_path("perl.exe", [r"\perl\bin", |
| 165 | r"C:\perl\bin", |
| 166 | r"\perl64\bin", |
| 167 | r"C:\perl64\bin", |
| 168 | ]) |
Zachary Ware | 10c2dba | 2014-05-09 09:07:50 -0500 | [diff] [blame] | 169 | perl = find_working_perl(perls) |
| 170 | if perl: |
| 171 | print("Found a working perl at '%s'" % (perl,)) |
| 172 | else: |
| 173 | sys.exit(1) |
Zachary Ware | 16f164e | 2016-02-22 04:02:30 -0600 | [diff] [blame] | 174 | if not find_all_on_path('nmake.exe'): |
| 175 | print('Could not find nmake.exe, try running env.bat') |
| 176 | sys.exit(1) |
Steve Dower | 79993a9 | 2016-03-08 12:50:57 -0800 | [diff] [blame^] | 177 | if not find_all_on_path('nasm.exe'): |
| 178 | print('Could not find nasm.exe, please add to PATH') |
| 179 | sys.exit(1) |
Zachary Ware | 10c2dba | 2014-05-09 09:07:50 -0500 | [diff] [blame] | 180 | sys.stdout.flush() |
| 181 | |
| 182 | # Put our working Perl at the front of our path |
| 183 | os.environ["PATH"] = os.path.dirname(perl) + \ |
| 184 | os.pathsep + \ |
| 185 | os.environ["PATH"] |
| 186 | |
| 187 | old_cwd = os.getcwd() |
| 188 | try: |
| 189 | os.chdir(ssl_dir) |
| 190 | for arch in ['amd64', 'x86']: |
| 191 | prep(arch) |
| 192 | finally: |
| 193 | os.chdir(old_cwd) |
| 194 | |
| 195 | if __name__=='__main__': |
| 196 | main() |