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