2010-10-01 07:48:47 -03:00
|
|
|
from __future__ import with_statement, print_function
|
2007-11-22 07:21:16 -04:00
|
|
|
# Script for building the _ssl and _hashlib modules for Windows.
|
|
|
|
# Uses Perl to setup the OpenSSL environment correctly
|
|
|
|
# and build OpenSSL, then invokes a simple nmake session
|
|
|
|
# for the actual _ssl.pyd and _hashlib.pyd DLLs.
|
|
|
|
|
|
|
|
# THEORETICALLY, you can:
|
|
|
|
# * Unpack the latest SSL release one level above your main Python source
|
|
|
|
# directory. It is likely you will already find the zlib library and
|
|
|
|
# any other external packages there.
|
|
|
|
# * Install ActivePerl and ensure it is somewhere on your path.
|
|
|
|
# * Run this script from the PCBuild directory.
|
|
|
|
#
|
|
|
|
# it should configure and build SSL, then build the _ssl and _hashlib
|
|
|
|
# Python extensions without intervention.
|
|
|
|
|
2007-11-23 03:05:03 -04:00
|
|
|
# Modified by Christian Heimes
|
|
|
|
# Now this script supports pre-generated makefiles and assembly files.
|
|
|
|
# Developers don't need an installation of Perl anymore to build Python. A svn
|
|
|
|
# checkout from our svn repository is enough.
|
|
|
|
#
|
|
|
|
# In Order to create the files in the case of an update you still need Perl.
|
|
|
|
# Run build_ssl in this order:
|
|
|
|
# python.exe build_ssl.py Release x64
|
|
|
|
# python.exe build_ssl.py Release Win32
|
|
|
|
|
2007-11-22 07:21:16 -04:00
|
|
|
import os, sys, re, shutil
|
|
|
|
|
|
|
|
# Find all "foo.exe" files on the PATH.
|
|
|
|
def find_all_on_path(filename, extras = None):
|
|
|
|
entries = os.environ["PATH"].split(os.pathsep)
|
|
|
|
ret = []
|
|
|
|
for p in entries:
|
|
|
|
fname = os.path.abspath(os.path.join(p, filename))
|
|
|
|
if os.path.isfile(fname) and fname not in ret:
|
|
|
|
ret.append(fname)
|
|
|
|
if extras:
|
|
|
|
for p in extras:
|
|
|
|
fname = os.path.abspath(os.path.join(p, filename))
|
|
|
|
if os.path.isfile(fname) and fname not in ret:
|
|
|
|
ret.append(fname)
|
|
|
|
return ret
|
|
|
|
|
|
|
|
# Find a suitable Perl installation for OpenSSL.
|
|
|
|
# cygwin perl does *not* work. ActivePerl does.
|
|
|
|
# Being a Perl dummy, the simplest way I can check is if the "Win32" package
|
|
|
|
# is available.
|
|
|
|
def find_working_perl(perls):
|
|
|
|
for perl in perls:
|
2010-12-09 04:01:18 -04:00
|
|
|
fh = os.popen('"%s" -e "use Win32;"' % perl)
|
2007-11-22 07:21:16 -04:00
|
|
|
fh.read()
|
|
|
|
rc = fh.close()
|
|
|
|
if rc:
|
|
|
|
continue
|
|
|
|
return perl
|
|
|
|
print("Can not find a suitable PERL:")
|
|
|
|
if perls:
|
|
|
|
print(" the following perl interpreters were found:")
|
|
|
|
for p in perls:
|
|
|
|
print(" ", p)
|
|
|
|
print(" None of these versions appear suitable for building OpenSSL")
|
|
|
|
else:
|
|
|
|
print(" NO perl interpreters were found on this machine at all!")
|
|
|
|
print(" Please install ActivePerl and ensure it appears on your path")
|
|
|
|
return None
|
|
|
|
|
2012-05-18 08:58:30 -03:00
|
|
|
# Fetch SSL directory from VC properties
|
|
|
|
def get_ssl_dir():
|
|
|
|
propfile = (os.path.join(os.path.dirname(__file__), 'pyproject.vsprops'))
|
|
|
|
with open(propfile) as f:
|
|
|
|
m = re.search('openssl-([^"]+)"', f.read())
|
2014-11-02 00:34:09 -03:00
|
|
|
return "..\externals\openssl-"+m.group(1)
|
2012-05-18 08:58:30 -03:00
|
|
|
|
2007-11-22 07:21:16 -04:00
|
|
|
|
2007-11-23 03:05:03 -04:00
|
|
|
def create_makefile64(makefile, m32):
|
|
|
|
"""Create and fix makefile for 64bit
|
2007-11-22 07:21:16 -04:00
|
|
|
|
|
|
|
Replace 32 with 64bit directories
|
|
|
|
"""
|
|
|
|
if not os.path.isfile(m32):
|
|
|
|
return
|
2009-12-21 15:25:56 -04:00
|
|
|
with open(m32) as fin:
|
|
|
|
with open(makefile, 'w') as fout:
|
2007-11-22 07:21:16 -04:00
|
|
|
for line in fin:
|
|
|
|
line = line.replace("=tmp32", "=tmp64")
|
|
|
|
line = line.replace("=out32", "=out64")
|
|
|
|
line = line.replace("=inc32", "=inc64")
|
|
|
|
# force 64 bit machine
|
|
|
|
line = line.replace("MKLIB=lib", "MKLIB=lib /MACHINE:X64")
|
|
|
|
line = line.replace("LFLAGS=", "LFLAGS=/MACHINE:X64 ")
|
|
|
|
# don't link against the lib on 64bit systems
|
|
|
|
line = line.replace("bufferoverflowu.lib", "")
|
|
|
|
fout.write(line)
|
|
|
|
os.unlink(m32)
|
|
|
|
|
2007-11-23 03:05:03 -04:00
|
|
|
def fix_makefile(makefile):
|
|
|
|
"""Fix some stuff in all makefiles
|
|
|
|
"""
|
|
|
|
if not os.path.isfile(makefile):
|
|
|
|
return
|
2008-02-29 14:54:45 -04:00
|
|
|
# 2.4 compatibility
|
|
|
|
fin = open(makefile)
|
|
|
|
if 1: # with open(makefile) as fin:
|
2007-11-23 03:05:03 -04:00
|
|
|
lines = fin.readlines()
|
2008-02-29 14:54:45 -04:00
|
|
|
fin.close()
|
|
|
|
fout = open(makefile, 'w')
|
|
|
|
if 1: # with open(makefile, 'w') as fout:
|
2007-11-23 03:05:03 -04:00
|
|
|
for line in lines:
|
|
|
|
if line.startswith("PERL="):
|
|
|
|
continue
|
|
|
|
if line.startswith("CP="):
|
|
|
|
line = "CP=copy\n"
|
|
|
|
if line.startswith("MKDIR="):
|
|
|
|
line = "MKDIR=mkdir\n"
|
2007-11-23 21:53:59 -04:00
|
|
|
if line.startswith("CFLAG="):
|
|
|
|
line = line.strip()
|
|
|
|
for algo in ("RC5", "MDC2", "IDEA"):
|
|
|
|
noalgo = " -DOPENSSL_NO_%s" % algo
|
|
|
|
if noalgo not in line:
|
|
|
|
line = line + noalgo
|
|
|
|
line = line + '\n'
|
2007-11-23 03:05:03 -04:00
|
|
|
fout.write(line)
|
2008-02-29 14:54:45 -04:00
|
|
|
fout.close()
|
2007-11-23 03:05:03 -04:00
|
|
|
|
2007-11-22 07:21:16 -04:00
|
|
|
def run_configure(configure, do_script):
|
|
|
|
print("perl Configure "+configure)
|
|
|
|
os.system("perl Configure "+configure)
|
|
|
|
print(do_script)
|
|
|
|
os.system(do_script)
|
|
|
|
|
|
|
|
def main():
|
|
|
|
build_all = "-a" in sys.argv
|
|
|
|
if sys.argv[1] == "Release":
|
|
|
|
debug = False
|
|
|
|
elif sys.argv[1] == "Debug":
|
|
|
|
debug = True
|
|
|
|
else:
|
|
|
|
raise ValueError(str(sys.argv))
|
|
|
|
|
|
|
|
if sys.argv[2] == "Win32":
|
|
|
|
arch = "x86"
|
|
|
|
configure = "VC-WIN32"
|
|
|
|
do_script = "ms\\do_nasm"
|
|
|
|
makefile="ms\\nt.mak"
|
|
|
|
m32 = makefile
|
|
|
|
elif sys.argv[2] == "x64":
|
|
|
|
arch="amd64"
|
|
|
|
configure = "VC-WIN64A"
|
|
|
|
do_script = "ms\\do_win64a"
|
|
|
|
makefile = "ms\\nt64.mak"
|
|
|
|
m32 = makefile.replace('64', '')
|
|
|
|
#os.environ["VSEXTCOMP_USECL"] = "MS_OPTERON"
|
|
|
|
else:
|
|
|
|
raise ValueError(str(sys.argv))
|
|
|
|
|
|
|
|
make_flags = ""
|
|
|
|
if build_all:
|
|
|
|
make_flags = "-a"
|
|
|
|
# perl should be on the path, but we also look in "\perl" and "c:\\perl"
|
|
|
|
# as "well known" locations
|
2015-04-13 12:46:40 -03:00
|
|
|
perls = find_all_on_path("perl.exe", [r"\perl\bin",
|
|
|
|
r"C:\perl\bin",
|
|
|
|
r"\perl64\bin",
|
|
|
|
r"C:\perl64\bin",
|
|
|
|
])
|
2007-11-22 07:21:16 -04:00
|
|
|
perl = find_working_perl(perls)
|
2010-12-09 04:01:18 -04:00
|
|
|
if perl:
|
|
|
|
print("Found a working perl at '%s'" % (perl,))
|
|
|
|
else:
|
2007-11-23 03:05:03 -04:00
|
|
|
print("No Perl installation was found. Existing Makefiles are used.")
|
2007-11-22 07:21:16 -04:00
|
|
|
sys.stdout.flush()
|
|
|
|
# Look for SSL 2 levels up from pcbuild - ie, same place zlib etc all live.
|
2012-05-18 08:58:30 -03:00
|
|
|
ssl_dir = get_ssl_dir()
|
2007-11-22 07:21:16 -04:00
|
|
|
if ssl_dir is None:
|
|
|
|
sys.exit(1)
|
|
|
|
|
2014-11-01 19:11:08 -03:00
|
|
|
# add our copy of NASM to PATH. It will be on the same level as openssl
|
|
|
|
for dir in os.listdir(os.path.join(ssl_dir, os.pardir)):
|
|
|
|
if dir.startswith('nasm'):
|
|
|
|
nasm_dir = os.path.join(ssl_dir, os.pardir, dir)
|
|
|
|
nasm_dir = os.path.abspath(nasm_dir)
|
2015-04-09 22:16:05 -03:00
|
|
|
old_path = os.environ['PATH']
|
|
|
|
os.environ['PATH'] = os.pathsep.join([nasm_dir, old_path])
|
2014-11-01 19:11:08 -03:00
|
|
|
break
|
|
|
|
else:
|
|
|
|
print('NASM was not found, make sure it is on PATH')
|
|
|
|
|
|
|
|
|
2007-11-22 07:21:16 -04:00
|
|
|
old_cd = os.getcwd()
|
|
|
|
try:
|
|
|
|
os.chdir(ssl_dir)
|
|
|
|
# rebuild makefile when we do the role over from 32 to 64 build
|
|
|
|
if arch == "amd64" and os.path.isfile(m32) and not os.path.isfile(makefile):
|
|
|
|
os.unlink(m32)
|
|
|
|
|
|
|
|
# If the ssl makefiles do not exist, we invoke Perl to generate them.
|
|
|
|
# Due to a bug in this script, the makefile sometimes ended up empty
|
|
|
|
# Force a regeneration if it is.
|
|
|
|
if not os.path.isfile(makefile) or os.path.getsize(makefile)==0:
|
2007-11-23 03:05:03 -04:00
|
|
|
if perl is None:
|
|
|
|
print("Perl is required to build the makefiles!")
|
|
|
|
sys.exit(1)
|
|
|
|
|
2007-11-22 07:21:16 -04:00
|
|
|
print("Creating the makefiles...")
|
|
|
|
sys.stdout.flush()
|
|
|
|
# Put our working Perl at the front of our path
|
|
|
|
os.environ["PATH"] = os.path.dirname(perl) + \
|
|
|
|
os.pathsep + \
|
|
|
|
os.environ["PATH"]
|
|
|
|
run_configure(configure, do_script)
|
2007-11-23 03:05:03 -04:00
|
|
|
if debug:
|
|
|
|
print("OpenSSL debug builds aren't supported.")
|
|
|
|
#if arch=="x86" and debug:
|
|
|
|
# # the do_masm script in openssl doesn't generate a debug
|
|
|
|
# # build makefile so we generate it here:
|
|
|
|
# os.system("perl util\mk1mf.pl debug "+configure+" >"+makefile)
|
|
|
|
|
|
|
|
if arch == "amd64":
|
|
|
|
create_makefile64(makefile, m32)
|
|
|
|
fix_makefile(makefile)
|
2010-09-21 13:25:21 -03:00
|
|
|
shutil.copy(r"crypto\buildinf.h", r"crypto\buildinf_%s.h" % arch)
|
|
|
|
shutil.copy(r"crypto\opensslconf.h", r"crypto\opensslconf_%s.h" % arch)
|
2007-11-22 07:21:16 -04:00
|
|
|
|
2007-11-23 03:05:03 -04:00
|
|
|
# Now run make.
|
2007-11-22 07:21:16 -04:00
|
|
|
if arch == "amd64":
|
2014-05-31 15:01:37 -03:00
|
|
|
rc = os.system("nasm -f win64 -DNEAR -Ox -g ms\\uptable.asm")
|
2007-11-23 03:05:03 -04:00
|
|
|
if rc:
|
2014-05-31 15:01:37 -03:00
|
|
|
print("nasm assembler has failed.")
|
2007-11-23 03:05:03 -04:00
|
|
|
sys.exit(rc)
|
2007-11-22 07:21:16 -04:00
|
|
|
|
2010-09-21 13:25:21 -03:00
|
|
|
shutil.copy(r"crypto\buildinf_%s.h" % arch, r"crypto\buildinf.h")
|
|
|
|
shutil.copy(r"crypto\opensslconf_%s.h" % arch, r"crypto\opensslconf.h")
|
2007-11-23 03:05:03 -04:00
|
|
|
|
|
|
|
#makeCommand = "nmake /nologo PERL=\"%s\" -f \"%s\"" %(perl, makefile)
|
|
|
|
makeCommand = "nmake /nologo -f \"%s\"" % makefile
|
2007-11-22 07:21:16 -04:00
|
|
|
print("Executing ssl makefiles:", makeCommand)
|
|
|
|
sys.stdout.flush()
|
|
|
|
rc = os.system(makeCommand)
|
|
|
|
if rc:
|
|
|
|
print("Executing "+makefile+" failed")
|
|
|
|
print(rc)
|
|
|
|
sys.exit(rc)
|
|
|
|
finally:
|
|
|
|
os.chdir(old_cd)
|
|
|
|
sys.exit(rc)
|
|
|
|
|
|
|
|
if __name__=='__main__':
|
|
|
|
main()
|