»Core Development>Code coverage>PCbuild/prepare_ssl.py

Python code coverage for PCbuild/prepare_ssl.py

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