Zachary Ware | 91a9c78 | 2016-09-05 11:55:42 -0500 | [diff] [blame] | 1 | #! /usr/bin/env python3 |
Jeremy Kloth | ebbccea | 2017-06-20 14:53:39 -0600 | [diff] [blame] | 2 | # -*- coding: utf8 -*- |
| 3 | # The encoding declaration is required for running PC\VS9.0\build_ssl.py |
| 4 | |
Zachary Ware | 91a9c78 | 2016-09-05 11:55:42 -0500 | [diff] [blame] | 5 | # Script for preparing OpenSSL for building on Windows. |
| 6 | # Uses Perl to create nmake makefiles and otherwise prepare the way |
| 7 | # for building on 32 or 64 bit platforms. |
| 8 | |
| 9 | # Script originally authored by Mark Hammond. |
| 10 | # Major revisions by: |
| 11 | # Martin v. Löwis |
| 12 | # Christian Heimes |
| 13 | # Zachary Ware |
| 14 | |
| 15 | # THEORETICALLY, you can: |
| 16 | # * Unpack the latest OpenSSL release where $(opensslDir) in |
| 17 | # PCbuild\pyproject.props expects it to be. |
| 18 | # * Install ActivePerl and ensure it is somewhere on your path. |
| 19 | # * Run this script with the OpenSSL source dir as the only argument. |
| 20 | # |
| 21 | # it should configure OpenSSL such that it is ready to be built by |
| 22 | # ssl.vcxproj on 32 or 64 bit platforms. |
| 23 | |
Zachary Ware | 118eb65 | 2016-09-05 12:54:08 -0500 | [diff] [blame] | 24 | from __future__ import print_function |
| 25 | |
Zachary Ware | 91a9c78 | 2016-09-05 11:55:42 -0500 | [diff] [blame] | 26 | import os |
| 27 | import re |
| 28 | import sys |
| 29 | import subprocess |
| 30 | from shutil import copy |
| 31 | |
| 32 | # Find all "foo.exe" files on the PATH. |
| 33 | def find_all_on_path(filename, extras=None): |
| 34 | entries = os.environ["PATH"].split(os.pathsep) |
| 35 | ret = [] |
| 36 | for p in entries: |
| 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 | if extras: |
| 41 | for p in extras: |
| 42 | fname = os.path.abspath(os.path.join(p, filename)) |
| 43 | if os.path.isfile(fname) and fname not in ret: |
| 44 | ret.append(fname) |
| 45 | return ret |
| 46 | |
| 47 | |
| 48 | # Find a suitable Perl installation for OpenSSL. |
| 49 | # cygwin perl does *not* work. ActivePerl does. |
| 50 | # Being a Perl dummy, the simplest way I can check is if the "Win32" package |
| 51 | # is available. |
| 52 | def find_working_perl(perls): |
| 53 | for perl in perls: |
| 54 | try: |
| 55 | subprocess.check_output([perl, "-e", "use Win32;"]) |
| 56 | except subprocess.CalledProcessError: |
| 57 | continue |
| 58 | else: |
| 59 | return perl |
| 60 | |
| 61 | if perls: |
| 62 | print("The following perl interpreters were found:") |
| 63 | for p in perls: |
| 64 | print(" ", p) |
| 65 | print(" None of these versions appear suitable for building OpenSSL") |
| 66 | else: |
| 67 | print("NO perl interpreters were found on this machine at all!") |
| 68 | print(" Please install ActivePerl and ensure it appears on your path") |
| 69 | |
| 70 | |
| 71 | def create_asms(makefile, tmp_d): |
| 72 | #create a custom makefile out of the provided one |
| 73 | asm_makefile = os.path.splitext(makefile)[0] + '.asm.mak' |
| 74 | with open(makefile) as fin, open(asm_makefile, 'w') as fout: |
| 75 | for line in fin: |
| 76 | # Keep everything up to the install target (it's convenient) |
| 77 | if line.startswith('install: all'): |
| 78 | break |
| 79 | fout.write(line) |
| 80 | asms = [] |
| 81 | for line in fin: |
| 82 | if '.asm' in line and line.strip().endswith('.pl'): |
| 83 | asms.append(line.split(':')[0]) |
| 84 | while line.strip(): |
| 85 | fout.write(line) |
| 86 | line = next(fin) |
| 87 | fout.write('\n') |
| 88 | |
| 89 | fout.write('asms: $(TMP_D) ') |
| 90 | fout.write(' '.join(asms)) |
| 91 | fout.write('\n') |
| 92 | os.system('nmake /f {} PERL=perl TMP_D={} asms'.format(asm_makefile, tmp_d)) |
| 93 | |
| 94 | |
| 95 | def copy_includes(makefile, suffix): |
| 96 | dir = 'include'+suffix+'\\openssl' |
Zachary Ware | 118eb65 | 2016-09-05 12:54:08 -0500 | [diff] [blame] | 97 | try: |
| 98 | os.makedirs(dir) |
| 99 | except OSError: |
| 100 | pass |
Zachary Ware | 91a9c78 | 2016-09-05 11:55:42 -0500 | [diff] [blame] | 101 | copy_if_different = r'$(PERL) $(SRC_D)\util\copy-if-different.pl' |
| 102 | with open(makefile) as fin: |
| 103 | for line in fin: |
| 104 | if copy_if_different in line: |
| 105 | perl, script, src, dest = line.split() |
| 106 | if not '$(INCO_D)' in dest: |
| 107 | continue |
| 108 | # We're in the root of the source tree |
| 109 | src = src.replace('$(SRC_D)', '.').strip('"') |
| 110 | dest = dest.strip('"').replace('$(INCO_D)', dir) |
| 111 | print('copying', src, 'to', dest) |
| 112 | copy(src, dest) |
| 113 | |
| 114 | |
| 115 | def run_configure(configure, do_script): |
| 116 | print("perl Configure "+configure+" no-idea no-mdc2") |
| 117 | os.system("perl Configure "+configure+" no-idea no-mdc2") |
| 118 | print(do_script) |
| 119 | os.system(do_script) |
| 120 | |
| 121 | |
| 122 | def prep(arch): |
| 123 | makefile_template = "ms\\nt{}.mak" |
| 124 | generated_makefile = makefile_template.format('') |
| 125 | if arch == "x86": |
| 126 | configure = "VC-WIN32" |
| 127 | do_script = "ms\\do_nasm" |
| 128 | suffix = "32" |
| 129 | elif arch == "amd64": |
| 130 | configure = "VC-WIN64A" |
| 131 | do_script = "ms\\do_win64a" |
| 132 | suffix = "64" |
| 133 | #os.environ["VSEXTCOMP_USECL"] = "MS_OPTERON" |
| 134 | else: |
| 135 | raise ValueError('Unrecognized platform: %s' % arch) |
| 136 | |
| 137 | print("Creating the makefiles...") |
| 138 | sys.stdout.flush() |
| 139 | # run configure, copy includes, create asms |
| 140 | run_configure(configure, do_script) |
| 141 | makefile = makefile_template.format(suffix) |
| 142 | try: |
| 143 | os.unlink(makefile) |
| 144 | except FileNotFoundError: |
| 145 | pass |
| 146 | os.rename(generated_makefile, makefile) |
| 147 | copy_includes(makefile, suffix) |
| 148 | |
| 149 | print('creating asms...') |
| 150 | create_asms(makefile, 'tmp'+suffix) |
| 151 | |
| 152 | |
| 153 | def main(): |
| 154 | if len(sys.argv) == 1: |
| 155 | print("Not enough arguments: directory containing OpenSSL", |
| 156 | "sources must be supplied") |
| 157 | sys.exit(1) |
| 158 | |
| 159 | if len(sys.argv) > 2: |
| 160 | print("Too many arguments supplied, all we need is the directory", |
| 161 | "containing OpenSSL sources") |
| 162 | sys.exit(1) |
| 163 | |
| 164 | ssl_dir = sys.argv[1] |
| 165 | |
| 166 | if not os.path.isdir(ssl_dir): |
| 167 | print(ssl_dir, "is not an existing directory!") |
| 168 | sys.exit(1) |
| 169 | |
| 170 | # perl should be on the path, but we also look in "\perl" and "c:\\perl" |
| 171 | # as "well known" locations |
| 172 | perls = find_all_on_path("perl.exe", [r"\perl\bin", |
| 173 | r"C:\perl\bin", |
| 174 | r"\perl64\bin", |
| 175 | r"C:\perl64\bin", |
| 176 | ]) |
| 177 | perl = find_working_perl(perls) |
| 178 | if perl: |
| 179 | print("Found a working perl at '%s'" % (perl,)) |
| 180 | else: |
| 181 | sys.exit(1) |
| 182 | if not find_all_on_path('nmake.exe'): |
| 183 | print('Could not find nmake.exe, try running env.bat') |
| 184 | sys.exit(1) |
| 185 | if not find_all_on_path('nasm.exe'): |
| 186 | print('Could not find nasm.exe, please add to PATH') |
| 187 | sys.exit(1) |
| 188 | sys.stdout.flush() |
| 189 | |
| 190 | # Put our working Perl at the front of our path |
| 191 | os.environ["PATH"] = os.path.dirname(perl) + \ |
| 192 | os.pathsep + \ |
| 193 | os.environ["PATH"] |
| 194 | |
| 195 | old_cwd = os.getcwd() |
| 196 | try: |
| 197 | os.chdir(ssl_dir) |
| 198 | for arch in ['amd64', 'x86']: |
| 199 | prep(arch) |
| 200 | finally: |
| 201 | os.chdir(old_cwd) |
| 202 | |
| 203 | if __name__=='__main__': |
| 204 | main() |