1#! /usr/bin/env python3
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
21from __future__ import print_function
22
23import os
24import re
25import sys
26import subprocess
27from shutil import copy
28
29# Find all "foo.exe" files on the PATH.
30def find_all_on_path(filename, extras=None):
31    entries = os.environ["PATH"].split(os.pathsep)
32    ret = []
33    for p in entries:
34        fname = os.path.abspath(os.path.join(p, filename))
35        if os.path.isfile(fname) and fname not in ret:
36            ret.append(fname)
37    if extras:
38        for p in extras:
39            fname = os.path.abspath(os.path.join(p, filename))
40            if os.path.isfile(fname) and fname not in ret:
41                ret.append(fname)
42    return ret
43
44
45# Find a suitable Perl installation for OpenSSL.
46# cygwin perl does *not* work.  ActivePerl does.
47# Being a Perl dummy, the simplest way I can check is if the "Win32" package
48# is available.
49def find_working_perl(perls):
50    for perl in perls:
51        try:
52            subprocess.check_output([perl, "-e", "use Win32;"])
53        except subprocess.CalledProcessError:
54            continue
55        else:
56            return perl
57
58    if perls:
59        print("The following perl interpreters were found:")
60        for p in perls:
61            print(" ", p)
62        print(" None of these versions appear suitable for building OpenSSL")
63    else:
64        print("NO perl interpreters were found on this machine at all!")
65    print(" Please install ActivePerl and ensure it appears on your path")
66
67
68def copy_includes(makefile, suffix):
69    dir = 'inc'+suffix+'\\openssl'
70    try:
71        os.makedirs(dir)
72    except OSError:
73        pass
74    copy_if_different = r'$(PERL) $(SRC_D)\util\copy-if-different.pl'
75    with open(makefile) as fin:
76        for line in fin:
77            if copy_if_different in line:
78                perl, script, src, dest = line.split()
79                if not '$(INCO_D)' in dest:
80                    continue
81                # We're in the root of the source tree
82                src = src.replace('$(SRC_D)', '.').strip('"')
83                dest = dest.strip('"').replace('$(INCO_D)', dir)
84                print('copying', src, 'to', dest)
85                copy(src, dest)
86
87
88def run_configure(configure, do_script):
89    print("perl Configure "+configure+" no-idea no-mdc2")
90    os.system("perl Configure "+configure+" no-idea no-mdc2")
91    print(do_script)
92    os.system(do_script)
93
94def fix_uplink():
95    # uplink.c tries to find the OPENSSL_Applink function exported from the current
96    # executable. However, we export it from _ssl[_d].pyd instead. So we update the
97    # module name here before building.
98    with open('ms\\uplink.c', 'r', encoding='utf-8') as f1:
99        code = list(f1)
100    os.replace('ms\\uplink.c', 'ms\\uplink.c.orig')
101    already_patched = False
102    with open('ms\\uplink.c', 'w', encoding='utf-8') as f2:
103        for line in code:
104            if not already_patched:
105                if re.search('MODIFIED FOR CPYTHON _ssl MODULE', line):
106                    already_patched = True
107                elif re.match(r'^\s+if\s*\(\(h\s*=\s*GetModuleHandle[AW]?\(NULL\)\)\s*==\s*NULL\)', line):
108                    f2.write("/* MODIFIED FOR CPYTHON _ssl MODULE */\n")
109                    f2.write('if ((h = GetModuleHandleW(L"_ssl.pyd")) == NULL) if ((h = GetModuleHandleW(L"_ssl_d.pyd")) == NULL)\n')
110                    already_patched = True
111            f2.write(line)
112    if not already_patched:
113        print("WARN: failed to patch ms\\uplink.c")
114
115def prep(arch):
116    makefile_template = "ms\\ntdll{}.mak"
117    generated_makefile = makefile_template.format('')
118    if arch == "x86":
119        configure = "VC-WIN32"
120        do_script = "ms\\do_nasm"
121        suffix = "32"
122    elif arch == "amd64":
123        configure = "VC-WIN64A"
124        do_script = "ms\\do_win64a"
125        suffix = "64"
126    else:
127        raise ValueError('Unrecognized platform: %s' % arch)
128
129    print("Creating the makefiles...")
130    sys.stdout.flush()
131    # run configure, copy includes, patch files
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)
140
141    print('patching ms\\uplink.c...')
142    fix_uplink()
143
144def main():
145    if len(sys.argv) == 1:
146        print("Not enough arguments: directory containing OpenSSL",
147              "sources must be supplied")
148        sys.exit(1)
149
150    if len(sys.argv) == 3 and sys.argv[2] not in ('x86', 'amd64'):
151        print("Second argument must be x86 or amd64")
152        sys.exit(1)
153
154    if len(sys.argv) > 3:
155        print("Too many arguments supplied, all we need is the directory",
156              "containing OpenSSL sources and optionally the architecture")
157        sys.exit(1)
158
159    ssl_dir = sys.argv[1]
160    arch = sys.argv[2] if len(sys.argv) >= 3 else None
161
162    if not os.path.isdir(ssl_dir):
163        print(ssl_dir, "is not an existing directory!")
164        sys.exit(1)
165
166    # perl should be on the path, but we also look in "\perl" and "c:\\perl"
167    # as "well known" locations
168    perls = find_all_on_path("perl.exe", [r"\perl\bin",
169                                          r"C:\perl\bin",
170                                          r"\perl64\bin",
171                                          r"C:\perl64\bin",
172                                         ])
173    perl = find_working_perl(perls)
174    if perl:
175        print("Found a working perl at '%s'" % (perl,))
176    else:
177        sys.exit(1)
178    if not find_all_on_path('nmake.exe'):
179        print('Could not find nmake.exe, try running env.bat')
180        sys.exit(1)
181    if not find_all_on_path('nasm.exe'):
182        print('Could not find nasm.exe, please add to PATH')
183        sys.exit(1)
184    sys.stdout.flush()
185
186    # Put our working Perl at the front of our path
187    os.environ["PATH"] = os.path.dirname(perl) + \
188                                os.pathsep + \
189                                os.environ["PATH"]
190
191    old_cwd = os.getcwd()
192    try:
193        os.chdir(ssl_dir)
194        if arch:
195            prep(arch)
196        else:
197            for arch in ['amd64', 'x86']:
198                prep(arch)
199    finally:
200        os.chdir(old_cwd)
201
202if __name__=='__main__':
203    main()
204