ยปCore Development>Code coverage>PCbuild/build_ssl.py

Python code coverage for PCbuild/build_ssl.py

#countcontent
1n/a# Script for building the _ssl and _hashlib modules for Windows.
2n/a# Uses Perl to setup the OpenSSL environment correctly
3n/a# and build OpenSSL, then invokes a simple nmake session
4n/a# for the actual _ssl.pyd and _hashlib.pyd DLLs.
5n/a
6n/a# THEORETICALLY, you can:
7n/a# * Unpack the latest SSL release one level above your main Python source
8n/a# directory. It is likely you will already find the zlib library and
9n/a# any other external packages there.
10n/a# * Install ActivePerl and ensure it is somewhere on your path.
11n/a# * Run this script from the PCBuild directory.
12n/a#
13n/a# it should configure and build SSL, then build the _ssl and _hashlib
14n/a# Python extensions without intervention.
15n/a
16n/a# Modified by Christian Heimes
17n/a# Now this script supports pre-generated makefiles and assembly files.
18n/a# Developers don't need an installation of Perl anymore to build Python. A svn
19n/a# checkout from our svn repository is enough.
20n/a#
21n/a# In Order to create the files in the case of an update you still need Perl.
22n/a# Run build_ssl in this order:
23n/a# python.exe build_ssl.py Release x64
24n/a# python.exe build_ssl.py Release Win32
25n/a
26n/aimport os, sys, re, shutil
27n/a
28n/a# Find all "foo.exe" files on the PATH.
29n/adef find_all_on_path(filename, extras = None):
30n/a entries = os.environ["PATH"].split(os.pathsep)
31n/a ret = []
32n/a for p in entries:
33n/a fname = os.path.abspath(os.path.join(p, filename))
34n/a if os.path.isfile(fname) and fname not in ret:
35n/a ret.append(fname)
36n/a if extras:
37n/a for p in extras:
38n/a fname = os.path.abspath(os.path.join(p, filename))
39n/a if os.path.isfile(fname) and fname not in ret:
40n/a ret.append(fname)
41n/a return ret
42n/a
43n/a# Find a suitable Perl installation for OpenSSL.
44n/a# cygwin perl does *not* work. ActivePerl does.
45n/a# Being a Perl dummy, the simplest way I can check is if the "Win32" package
46n/a# is available.
47n/adef find_working_perl(perls):
48n/a for perl in perls:
49n/a fh = os.popen('"%s" -e "use Win32;"' % perl)
50n/a fh.read()
51n/a rc = fh.close()
52n/a if rc:
53n/a continue
54n/a return perl
55n/a print("Can not find a suitable PERL:")
56n/a if perls:
57n/a print(" the following perl interpreters were found:")
58n/a for p in perls:
59n/a print(" ", p)
60n/a print(" None of these versions appear suitable for building OpenSSL")
61n/a else:
62n/a print(" NO perl interpreters were found on this machine at all!")
63n/a print(" Please install ActivePerl and ensure it appears on your path")
64n/a return None
65n/a
66n/a# Fetch SSL directory from VC properties
67n/adef get_ssl_dir():
68n/a propfile = (os.path.join(os.path.dirname(__file__), 'pyproject.props'))
69n/a with open(propfile) as f:
70n/a m = re.search('openssl-([^<]+)<', f.read())
71n/a return "..\..\openssl-"+m.group(1)
72n/a
73n/a
74n/adef create_makefile64(makefile, m32):
75n/a """Create and fix makefile for 64bit
76n/a
77n/a Replace 32 with 64bit directories
78n/a """
79n/a if not os.path.isfile(m32):
80n/a return
81n/a with open(m32) as fin:
82n/a with open(makefile, 'w') as fout:
83n/a for line in fin:
84n/a line = line.replace("=tmp32", "=tmp64")
85n/a line = line.replace("=out32", "=out64")
86n/a line = line.replace("=inc32", "=inc64")
87n/a # force 64 bit machine
88n/a line = line.replace("MKLIB=lib", "MKLIB=lib /MACHINE:X64")
89n/a line = line.replace("LFLAGS=", "LFLAGS=/MACHINE:X64 ")
90n/a # don't link against the lib on 64bit systems
91n/a line = line.replace("bufferoverflowu.lib", "")
92n/a fout.write(line)
93n/a os.unlink(m32)
94n/a
95n/adef fix_makefile(makefile):
96n/a """Fix some stuff in all makefiles
97n/a """
98n/a if not os.path.isfile(makefile):
99n/a return
100n/a with open(makefile) as fin:
101n/a lines = fin.readlines()
102n/a with open(makefile, 'w') as fout:
103n/a for line in lines:
104n/a if line.startswith("PERL="):
105n/a continue
106n/a if line.startswith("CP="):
107n/a line = "CP=copy\n"
108n/a if line.startswith("MKDIR="):
109n/a line = "MKDIR=mkdir\n"
110n/a if line.startswith("CFLAG="):
111n/a line = line.strip()
112n/a for algo in ("RC5", "MDC2", "IDEA"):
113n/a noalgo = " -DOPENSSL_NO_%s" % algo
114n/a if noalgo not in line:
115n/a line = line + noalgo
116n/a line = line + '\n'
117n/a fout.write(line)
118n/a
119n/adef run_configure(configure, do_script):
120n/a print("perl Configure "+configure+" no-idea no-mdc2")
121n/a os.system("perl Configure "+configure+" no-idea no-mdc2")
122n/a print(do_script)
123n/a os.system(do_script)
124n/a
125n/adef cmp(f1, f2):
126n/a bufsize = 1024 * 8
127n/a with open(f1, 'rb') as fp1, open(f2, 'rb') as fp2:
128n/a while True:
129n/a b1 = fp1.read(bufsize)
130n/a b2 = fp2.read(bufsize)
131n/a if b1 != b2:
132n/a return False
133n/a if not b1:
134n/a return True
135n/a
136n/adef copy(src, dst):
137n/a if os.path.isfile(dst) and cmp(src, dst):
138n/a return
139n/a shutil.copy(src, dst)
140n/a
141n/adef main():
142n/a build_all = "-a" in sys.argv
143n/a if sys.argv[1] == "Release":
144n/a debug = False
145n/a elif sys.argv[1] == "Debug":
146n/a debug = True
147n/a else:
148n/a raise ValueError(str(sys.argv))
149n/a
150n/a if sys.argv[2] == "Win32":
151n/a arch = "x86"
152n/a configure = "VC-WIN32"
153n/a do_script = "ms\\do_nasm"
154n/a makefile="ms\\nt.mak"
155n/a m32 = makefile
156n/a dirsuffix = "32"
157n/a elif sys.argv[2] == "x64":
158n/a arch="amd64"
159n/a configure = "VC-WIN64A"
160n/a do_script = "ms\\do_win64a"
161n/a makefile = "ms\\nt64.mak"
162n/a m32 = makefile.replace('64', '')
163n/a dirsuffix = "64"
164n/a #os.environ["VSEXTCOMP_USECL"] = "MS_OPTERON"
165n/a else:
166n/a raise ValueError(str(sys.argv))
167n/a
168n/a make_flags = ""
169n/a if build_all:
170n/a make_flags = "-a"
171n/a # perl should be on the path, but we also look in "\perl" and "c:\\perl"
172n/a # as "well known" locations
173n/a perls = find_all_on_path("perl.exe", ["\\perl\\bin", "C:\\perl\\bin"])
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 print("No Perl installation was found. Existing Makefiles are used.")
179n/a sys.stdout.flush()
180n/a # Look for SSL 2 levels up from pcbuild - ie, same place zlib etc all live.
181n/a ssl_dir = get_ssl_dir()
182n/a if ssl_dir is None:
183n/a sys.exit(1)
184n/a
185n/a old_cd = os.getcwd()
186n/a try:
187n/a os.chdir(ssl_dir)
188n/a # rebuild makefile when we do the role over from 32 to 64 build
189n/a if arch == "amd64" and os.path.isfile(m32) and not os.path.isfile(makefile):
190n/a os.unlink(m32)
191n/a
192n/a # If the ssl makefiles do not exist, we invoke Perl to generate them.
193n/a # Due to a bug in this script, the makefile sometimes ended up empty
194n/a # Force a regeneration if it is.
195n/a if not os.path.isfile(makefile) or os.path.getsize(makefile)==0:
196n/a if perl is None:
197n/a print("Perl is required to build the makefiles!")
198n/a sys.exit(1)
199n/a
200n/a print("Creating the makefiles...")
201n/a sys.stdout.flush()
202n/a # Put our working Perl at the front of our path
203n/a os.environ["PATH"] = os.path.dirname(perl) + \
204n/a os.pathsep + \
205n/a os.environ["PATH"]
206n/a run_configure(configure, do_script)
207n/a if debug:
208n/a print("OpenSSL debug builds aren't supported.")
209n/a #if arch=="x86" and debug:
210n/a # # the do_masm script in openssl doesn't generate a debug
211n/a # # build makefile so we generate it here:
212n/a # os.system("perl util\mk1mf.pl debug "+configure+" >"+makefile)
213n/a
214n/a if arch == "amd64":
215n/a create_makefile64(makefile, m32)
216n/a fix_makefile(makefile)
217n/a copy(r"crypto\buildinf.h", r"crypto\buildinf_%s.h" % arch)
218n/a copy(r"crypto\opensslconf.h", r"crypto\opensslconf_%s.h" % arch)
219n/a
220n/a # If the assembler files don't exist in tmpXX, copy them there
221n/a if perl is None and os.path.exists("asm"+dirsuffix):
222n/a if not os.path.exists("tmp"+dirsuffix):
223n/a os.mkdir("tmp"+dirsuffix)
224n/a for f in os.listdir("asm"+dirsuffix):
225n/a if not f.endswith(".asm"): continue
226n/a if os.path.isfile(r"tmp%s\%s" % (dirsuffix, f)): continue
227n/a shutil.copy(r"asm%s\%s" % (dirsuffix, f), "tmp"+dirsuffix)
228n/a
229n/a # Now run make.
230n/a if arch == "amd64":
231n/a rc = os.system("nasm -f win64 -DNEAR -Ox -g ms\\uptable.asm")
232n/a if rc:
233n/a print("nasm assembler has failed.")
234n/a sys.exit(rc)
235n/a
236n/a copy(r"crypto\buildinf_%s.h" % arch, r"crypto\buildinf.h")
237n/a copy(r"crypto\opensslconf_%s.h" % arch, r"crypto\opensslconf.h")
238n/a
239n/a #makeCommand = "nmake /nologo PERL=\"%s\" -f \"%s\"" %(perl, makefile)
240n/a makeCommand = "nmake /nologo -f \"%s\"" % makefile
241n/a print("Executing ssl makefiles:", makeCommand)
242n/a sys.stdout.flush()
243n/a rc = os.system(makeCommand)
244n/a if rc:
245n/a print("Executing "+makefile+" failed")
246n/a print(rc)
247n/a sys.exit(rc)
248n/a finally:
249n/a os.chdir(old_cd)
250n/a sys.exit(rc)
251n/a
252n/aif __name__=='__main__':
253n/a main()