ยปCore Development>Code coverage>PC/VS7.1/build_ssl.py

Python code coverage for PC/VS7.1/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/aimport os, sys, re
17n/a
18n/a# Find all "foo.exe" files on the PATH.
19n/adef find_all_on_path(filename, extras = None):
20n/a entries = os.environ["PATH"].split(os.pathsep)
21n/a ret = []
22n/a for p in entries:
23n/a fname = os.path.abspath(os.path.join(p, filename))
24n/a if os.path.isfile(fname) and fname not in ret:
25n/a ret.append(fname)
26n/a if extras:
27n/a for p in extras:
28n/a fname = os.path.abspath(os.path.join(p, filename))
29n/a if os.path.isfile(fname) and fname not in ret:
30n/a ret.append(fname)
31n/a return ret
32n/a
33n/a# Find a suitable Perl installation for OpenSSL.
34n/a# cygwin perl does *not* work. ActivePerl does.
35n/a# Being a Perl dummy, the simplest way I can check is if the "Win32" package
36n/a# is available.
37n/adef find_working_perl(perls):
38n/a for perl in perls:
39n/a fh = os.popen(perl + ' -e "use Win32;"')
40n/a fh.read()
41n/a rc = fh.close()
42n/a if rc:
43n/a continue
44n/a return perl
45n/a print "Can not find a suitable PERL:"
46n/a if perls:
47n/a print " the following perl interpreters were found:"
48n/a for p in perls:
49n/a print " ", p
50n/a print " None of these versions appear suitable for building OpenSSL"
51n/a else:
52n/a print " NO perl interpreters were found on this machine at all!"
53n/a print " Please install ActivePerl and ensure it appears on your path"
54n/a print "The Python SSL module was not built"
55n/a return None
56n/a
57n/a# Locate the best SSL directory given a few roots to look into.
58n/adef find_best_ssl_dir(sources):
59n/a candidates = []
60n/a for s in sources:
61n/a try:
62n/a # note: do not abspath s; the build will fail if any
63n/a # higher up directory name has spaces in it.
64n/a fnames = os.listdir(s)
65n/a except OSError:
66n/a fnames = []
67n/a for fname in fnames:
68n/a fqn = os.path.join(s, fname)
69n/a if os.path.isdir(fqn) and fname.startswith("openssl-"):
70n/a candidates.append(fqn)
71n/a # Now we have all the candidates, locate the best.
72n/a best_parts = []
73n/a best_name = None
74n/a for c in candidates:
75n/a parts = re.split("[.-]", os.path.basename(c))[1:]
76n/a # eg - openssl-0.9.7-beta1 - ignore all "beta" or any other qualifiers
77n/a if len(parts) >= 4:
78n/a continue
79n/a if parts > best_parts:
80n/a best_parts = parts
81n/a best_name = c
82n/a if best_name is not None:
83n/a print "Found an SSL directory at '%s'" % (best_name,)
84n/a else:
85n/a print "Could not find an SSL directory in '%s'" % (sources,)
86n/a sys.stdout.flush()
87n/a return best_name
88n/a
89n/adef run_configure(configure, do_script):
90n/a os.system("perl Configure "+configure)
91n/a os.system(do_script)
92n/a
93n/adef main():
94n/a build_all = "-a" in sys.argv
95n/a if sys.argv[1] == "Release":
96n/a arch = "x86"
97n/a debug = False
98n/a configure = "VC-WIN32"
99n/a do_script = "ms\\do_masm"
100n/a makefile = "ms\\nt.mak"
101n/a elif sys.argv[1] == "Debug":
102n/a arch = "x86"
103n/a debug = True
104n/a configure = "VC-WIN32"
105n/a do_script = "ms\\do_masm"
106n/a makefile="ms\\d32.mak"
107n/a elif sys.argv[1] == "ReleaseItanium":
108n/a arch = "ia64"
109n/a debug = False
110n/a configure = "VC-WIN64I"
111n/a do_script = "ms\\do_win64i"
112n/a makefile = "ms\\nt.mak"
113n/a os.environ["VSEXTCOMP_USECL"] = "MS_ITANIUM"
114n/a elif sys.argv[1] == "ReleaseAMD64":
115n/a arch="amd64"
116n/a debug=False
117n/a configure = "VC-WIN64A"
118n/a do_script = "ms\\do_win64a"
119n/a makefile = "ms\\nt.mak"
120n/a os.environ["VSEXTCOMP_USECL"] = "MS_OPTERON"
121n/a make_flags = ""
122n/a if build_all:
123n/a make_flags = "-a"
124n/a # perl should be on the path, but we also look in "\perl" and "c:\\perl"
125n/a # as "well known" locations
126n/a perls = find_all_on_path("perl.exe", ["\\perl\\bin", "C:\\perl\\bin"])
127n/a perl = find_working_perl(perls)
128n/a if perl is None:
129n/a sys.exit(1)
130n/a
131n/a print "Found a working perl at '%s'" % (perl,)
132n/a sys.stdout.flush()
133n/a # Look for SSL 2 levels up from pcbuild - ie, same place zlib etc all live.
134n/a ssl_dir = find_best_ssl_dir(("..\\..\\..",))
135n/a if ssl_dir is None:
136n/a sys.exit(1)
137n/a
138n/a old_cd = os.getcwd()
139n/a try:
140n/a os.chdir(ssl_dir)
141n/a # If the ssl makefiles do not exist, we invoke Perl to generate them.
142n/a # Due to a bug in this script, the makefile sometimes ended up empty
143n/a # Force a regeneration if it is.
144n/a if not os.path.isfile(makefile) or os.path.getsize(makefile)==0:
145n/a print "Creating the makefiles..."
146n/a sys.stdout.flush()
147n/a # Put our working Perl at the front of our path
148n/a os.environ["PATH"] = os.path.dirname(perl) + \
149n/a os.pathsep + \
150n/a os.environ["PATH"]
151n/a run_configure(configure, do_script)
152n/a if arch=="x86" and debug:
153n/a # the do_masm script in openssl doesn't generate a debug
154n/a # build makefile so we generate it here:
155n/a os.system("perl util\mk1mf.pl debug "+configure+" >"+makefile)
156n/a
157n/a # Now run make.
158n/a makeCommand = "nmake /nologo PERL=\"%s\" -f \"%s\"" %(perl, makefile)
159n/a print "Executing ssl makefiles:", makeCommand
160n/a sys.stdout.flush()
161n/a rc = os.system(makeCommand)
162n/a if rc:
163n/a print "Executing "+makefile+" failed"
164n/a print rc
165n/a sys.exit(rc)
166n/a finally:
167n/a os.chdir(old_cd)
168n/a # And finally, we can build the _ssl module itself for Python.
169n/a defs = "SSL_DIR=\"%s\"" % (ssl_dir,)
170n/a if debug:
171n/a defs = defs + " " + "DEBUG=1"
172n/a if arch in ('amd64', 'ia64'):
173n/a defs = defs + " EXTRA_CFLAGS=/GS- EXTRA_LIBS=bufferoverflowU.lib"
174n/a makeCommand = 'nmake /nologo -f _ssl.mak ' + defs + " " + make_flags
175n/a print "Executing:", makeCommand
176n/a sys.stdout.flush()
177n/a rc = os.system(makeCommand)
178n/a sys.exit(rc)
179n/a
180n/aif __name__=='__main__':
181n/a main()