Commit ebbccea9 authored by Jeremy Kloth's avatar Jeremy Kloth Committed by Victor Stinner

bpo-30368: Update build_ssl.py to restore Perl-less building (#1805)

* bpo-30368: Update build_ssl.py to restore Perl-less building

OpenSSL 1.0.2 releases changed how files are copied in the makefile,
thus causing Perl to be required even for Python's "prepared" OpenSSL.
Now build_ssl.py does the requisite copies before running nmake.

* bpo-30368: Update build_ssl.py to use prepared OpenSSL

* Updates SSL-linking projects to use the new include{suffix} directory
* build_ssl.py now only copies those files not handled by prepare_ssl.py

* * bpo-30368: Update build_ssl.py to use prepared OpenSSL

* Update SSL-linking projects to use the new include{suffix} directory

* Move comment to following line
parent d4324bac
...@@ -43,7 +43,7 @@ ...@@ -43,7 +43,7 @@
/> />
<Tool <Tool
Name="VCCLCompilerTool" Name="VCCLCompilerTool"
AdditionalIncludeDirectories="$(opensslDir)\inc32" AdditionalIncludeDirectories="$(opensslDir)\include32"
/> />
<Tool <Tool
Name="VCManagedResourceCompilerTool" Name="VCManagedResourceCompilerTool"
...@@ -106,7 +106,7 @@ ...@@ -106,7 +106,7 @@
/> />
<Tool <Tool
Name="VCCLCompilerTool" Name="VCCLCompilerTool"
AdditionalIncludeDirectories="$(opensslDir)\inc64" AdditionalIncludeDirectories="$(opensslDir)\include64"
/> />
<Tool <Tool
Name="VCManagedResourceCompilerTool" Name="VCManagedResourceCompilerTool"
...@@ -169,7 +169,7 @@ ...@@ -169,7 +169,7 @@
/> />
<Tool <Tool
Name="VCCLCompilerTool" Name="VCCLCompilerTool"
AdditionalIncludeDirectories="$(opensslDir)\inc32" AdditionalIncludeDirectories="$(opensslDir)\include32"
/> />
<Tool <Tool
Name="VCManagedResourceCompilerTool" Name="VCManagedResourceCompilerTool"
...@@ -233,7 +233,7 @@ ...@@ -233,7 +233,7 @@
/> />
<Tool <Tool
Name="VCCLCompilerTool" Name="VCCLCompilerTool"
AdditionalIncludeDirectories="$(opensslDir)\inc64" AdditionalIncludeDirectories="$(opensslDir)\include64"
/> />
<Tool <Tool
Name="VCManagedResourceCompilerTool" Name="VCManagedResourceCompilerTool"
...@@ -296,7 +296,7 @@ ...@@ -296,7 +296,7 @@
/> />
<Tool <Tool
Name="VCCLCompilerTool" Name="VCCLCompilerTool"
AdditionalIncludeDirectories="$(opensslDir)\inc32" AdditionalIncludeDirectories="$(opensslDir)\include32"
/> />
<Tool <Tool
Name="VCManagedResourceCompilerTool" Name="VCManagedResourceCompilerTool"
...@@ -360,7 +360,7 @@ ...@@ -360,7 +360,7 @@
/> />
<Tool <Tool
Name="VCCLCompilerTool" Name="VCCLCompilerTool"
AdditionalIncludeDirectories="$(opensslDir)\inc64" AdditionalIncludeDirectories="$(opensslDir)\include64"
/> />
<Tool <Tool
Name="VCManagedResourceCompilerTool" Name="VCManagedResourceCompilerTool"
...@@ -424,7 +424,7 @@ ...@@ -424,7 +424,7 @@
/> />
<Tool <Tool
Name="VCCLCompilerTool" Name="VCCLCompilerTool"
AdditionalIncludeDirectories="$(opensslDir)\inc32" AdditionalIncludeDirectories="$(opensslDir)\include32"
/> />
<Tool <Tool
Name="VCManagedResourceCompilerTool" Name="VCManagedResourceCompilerTool"
...@@ -488,7 +488,7 @@ ...@@ -488,7 +488,7 @@
/> />
<Tool <Tool
Name="VCCLCompilerTool" Name="VCCLCompilerTool"
AdditionalIncludeDirectories="$(opensslDir)\inc64" AdditionalIncludeDirectories="$(opensslDir)\include64"
/> />
<Tool <Tool
Name="VCManagedResourceCompilerTool" Name="VCManagedResourceCompilerTool"
......
...@@ -43,7 +43,7 @@ ...@@ -43,7 +43,7 @@
/> />
<Tool <Tool
Name="VCCLCompilerTool" Name="VCCLCompilerTool"
AdditionalIncludeDirectories="$(opensslDir)\inc32" AdditionalIncludeDirectories="$(opensslDir)\include32"
/> />
<Tool <Tool
Name="VCManagedResourceCompilerTool" Name="VCManagedResourceCompilerTool"
...@@ -106,7 +106,7 @@ ...@@ -106,7 +106,7 @@
/> />
<Tool <Tool
Name="VCCLCompilerTool" Name="VCCLCompilerTool"
AdditionalIncludeDirectories="$(opensslDir)\inc64" AdditionalIncludeDirectories="$(opensslDir)\include64"
/> />
<Tool <Tool
Name="VCManagedResourceCompilerTool" Name="VCManagedResourceCompilerTool"
...@@ -169,7 +169,7 @@ ...@@ -169,7 +169,7 @@
/> />
<Tool <Tool
Name="VCCLCompilerTool" Name="VCCLCompilerTool"
AdditionalIncludeDirectories="$(opensslDir)\inc32" AdditionalIncludeDirectories="$(opensslDir)\include32"
/> />
<Tool <Tool
Name="VCManagedResourceCompilerTool" Name="VCManagedResourceCompilerTool"
...@@ -233,7 +233,7 @@ ...@@ -233,7 +233,7 @@
/> />
<Tool <Tool
Name="VCCLCompilerTool" Name="VCCLCompilerTool"
AdditionalIncludeDirectories="$(opensslDir)\inc64" AdditionalIncludeDirectories="$(opensslDir)\include64"
/> />
<Tool <Tool
Name="VCManagedResourceCompilerTool" Name="VCManagedResourceCompilerTool"
...@@ -296,7 +296,7 @@ ...@@ -296,7 +296,7 @@
/> />
<Tool <Tool
Name="VCCLCompilerTool" Name="VCCLCompilerTool"
AdditionalIncludeDirectories="$(opensslDir)\inc32" AdditionalIncludeDirectories="$(opensslDir)\include32"
/> />
<Tool <Tool
Name="VCManagedResourceCompilerTool" Name="VCManagedResourceCompilerTool"
...@@ -360,7 +360,7 @@ ...@@ -360,7 +360,7 @@
/> />
<Tool <Tool
Name="VCCLCompilerTool" Name="VCCLCompilerTool"
AdditionalIncludeDirectories="$(opensslDir)\inc64" AdditionalIncludeDirectories="$(opensslDir)\include64"
/> />
<Tool <Tool
Name="VCManagedResourceCompilerTool" Name="VCManagedResourceCompilerTool"
...@@ -424,7 +424,7 @@ ...@@ -424,7 +424,7 @@
/> />
<Tool <Tool
Name="VCCLCompilerTool" Name="VCCLCompilerTool"
AdditionalIncludeDirectories="$(opensslDir)\inc32" AdditionalIncludeDirectories="$(opensslDir)\include32"
/> />
<Tool <Tool
Name="VCManagedResourceCompilerTool" Name="VCManagedResourceCompilerTool"
...@@ -488,7 +488,7 @@ ...@@ -488,7 +488,7 @@
/> />
<Tool <Tool
Name="VCCLCompilerTool" Name="VCCLCompilerTool"
AdditionalIncludeDirectories="$(opensslDir)\inc64" AdditionalIncludeDirectories="$(opensslDir)\include64"
/> />
<Tool <Tool
Name="VCManagedResourceCompilerTool" Name="VCManagedResourceCompilerTool"
......
from __future__ import with_statement, print_function
# Script for building the _ssl and _hashlib modules for Windows. # Script for building the _ssl and _hashlib modules for Windows.
# Uses Perl to setup the OpenSSL environment correctly # Uses Perl to setup the OpenSSL environment correctly
# and build OpenSSL, then invokes a simple nmake session # and build OpenSSL, then invokes a simple nmake session
...@@ -24,221 +23,212 @@ from __future__ import with_statement, print_function ...@@ -24,221 +23,212 @@ from __future__ import with_statement, print_function
# python.exe build_ssl.py Release x64 # python.exe build_ssl.py Release x64
# python.exe build_ssl.py Release Win32 # python.exe build_ssl.py Release Win32
from __future__ import with_statement from __future__ import with_statement, print_function
import os, sys, re, shutil import os
import re
import sys
import time
import subprocess import subprocess
from shutil import copy
# Find all "foo.exe" files on the PATH. from distutils import log
def find_all_on_path(filename, extras = None): from distutils.spawn import find_executable
entries = os.environ["PATH"].split(os.pathsep) from distutils.file_util import copy_file
ret = [] from distutils.sysconfig import parse_makefile, expand_makefile_vars
for p in entries:
fname = os.path.abspath(os.path.join(p, filename)) # The mk1mf.pl output filename template
if os.path.isfile(fname) and fname not in ret: # !!! This must match what is used in prepare_ssl.py
ret.append(fname) MK1MF_FMT = 'ms\\nt{}.mak'
if extras:
for p in extras: # The header files output directory name template
fname = os.path.abspath(os.path.join(p, filename)) # !!! This must match what is used in prepare_ssl.py
if os.path.isfile(fname) and fname not in ret: INCLUDE_FMT = 'include{}'
ret.append(fname)
return ret # Fetch all the directory definitions from VC properties
def get_project_properties(propfile):
# Find a suitable Perl installation for OpenSSL. macro_pattern = r'<UserMacro\s+Name="([^"]+?)"\s+Value="([^"]*?)"\s*/>'
# cygwin perl does *not* work. ActivePerl does. with open(propfile) as fin:
# Being a Perl dummy, the simplest way I can check is if the "Win32" package items = re.findall(macro_pattern, fin.read(), re.MULTILINE)
# is available. props = dict(items)
def find_working_perl(perls): for name, value in items:
for perl in perls:
try: try:
subprocess.check_output([perl, "-e", "use win32;"]) props[name] = expand_makefile_vars(value, props)
except Subprocess.CalledProcessError: except TypeError:
continue # value contains undefined variable reference, drop it
else: del props[name]
return perl return props
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
# 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())
return "..\..\externals\openssl-"+m.group(1)
_variable_rx = re.compile(r"([a-zA-Z][a-zA-Z0-9_]+)\s*=\s*(.*)")
def create_makefile64(makefile, m32): def fix_makefile(makefile, platform_makefile, suffix):
"""Create and fix makefile for 64bit """Fix some stuff in all makefiles
Replace 32 with 64bit directories
""" """
if not os.path.isfile(m32): subs = {
return 'PERL': 'rem', # just in case
with open(m32) as fin: 'CP': 'copy',
with open(makefile, 'w') as fout: 'MKDIR': 'mkdir',
'OUT_D': 'out' + suffix,
'TMP_D': 'tmp' + suffix,
'INC_D': INCLUDE_FMT.format(suffix),
'INCO_D': '$(INC_D)\\openssl',
}
with open(platform_makefile) as fin, open(makefile, 'w') as fout:
for line in fin: for line in fin:
line = line.replace("=tmp32", "=tmp64") m = _variable_rx.match(line)
line = line.replace("=out32", "=out64") if m:
line = line.replace("=inc32", "=inc64") name = m.group(1)
# force 64 bit machine if name in subs:
line = line.replace("MKLIB=lib", "MKLIB=lib /MACHINE:X64") line = '%s=%s\n' % (name, subs[name])
line = line.replace("LFLAGS=", "LFLAGS=/MACHINE:X64 ")
# don't link against the lib on 64bit systems
line = line.replace("bufferoverflowu.lib", "")
fout.write(line) fout.write(line)
os.unlink(m32)
def fix_makefile(makefile):
"""Fix some stuff in all makefiles _copy_rx = re.compile(r'\t\$\(PERL\) '
""" r'\$\(SRC_D\)\\util\\copy-if-different.pl '
if not os.path.isfile(makefile): r'"([^"]+)"\s+"([^"]+)"')
return def copy_files(makefile, makevars):
fin = open(makefile) # Create the destination directories (see 'init' rule in nt.dll)
for varname in ('TMP_D', 'LIB_D', 'INC_D', 'INCO_D'):
dirname = makevars[varname]
if not os.path.isdir(dirname):
os.mkdir(dirname)
# Process the just local library headers (HEADER) as installed headers
# (EXHEADER) are handled by prepare_ssl.py (see 'headers' rule in nt.dll)
headers = set(makevars['HEADER'].split())
with open(makefile) as fin: with open(makefile) as fin:
lines = fin.readlines() for line in fin:
with open(makefile, 'w') as fout: m = _copy_rx.match(line)
for line in lines: if m:
if line.startswith("PERL="): src, dst = m.groups()
continue src = expand_makefile_vars(src, makevars)
if line.startswith("CP="): dst = expand_makefile_vars(dst, makevars)
line = "CP=copy\n" if dst in headers:
if line.startswith("MKDIR="): copy_file(src, dst, preserve_times=False, update=True)
line = "MKDIR=mkdir\n"
if line.startswith("CFLAG="):
line = line.strip() # Update buildinf.h for the build platform.
for algo in ("RC5", "MDC2", "IDEA"): def fix_buildinf(makevars):
noalgo = " -DOPENSSL_NO_%s" % algo platform_cpp_symbol = 'MK1MF_PLATFORM_'
if noalgo not in line: platform_cpp_symbol += makevars['PLATFORM'].replace('-', '_')
line = line + noalgo fn = expand_makefile_vars('$(INCL_D)\\buildinf.h', makevars)
line = line + '\n' with open(fn, 'w') as f:
fout.write(line) # sanity check
f.write(('#ifndef {}\n'
' #error "Windows build (PLATFORM={PLATFORM}) only"\n'
'#endif\n').format(platform_cpp_symbol, **makevars))
buildinf = (
'#define CFLAGS "compiler: cl {CFLAG}"\n'
'#define PLATFORM "{PLATFORM}"\n'
'#define DATE "{}"\n'
).format(time.asctime(time.gmtime()),
**makevars)
f.write(buildinf)
print('Updating buildinf:')
print(buildinf)
sys.stdout.flush()
def run_configure(configure, do_script):
print("perl Configure "+configure+" no-idea no-mdc2")
os.system("perl Configure "+configure+" no-idea no-mdc2")
print(do_script)
os.system(do_script)
def main(): def main():
build_all = "-a" in sys.argv if sys.argv[1] == "Debug":
if sys.argv[1] == "Release": print("OpenSSL debug builds aren't supported.")
debug = False elif sys.argv[1] != "Release":
elif sys.argv[1] == "Debug": raise ValueError('Unrecognized configuration: %s' % sys.argv[1])
debug = True
else:
raise ValueError(str(sys.argv))
if sys.argv[2] == "Win32": if sys.argv[2] == "Win32":
arch = "x86" platform = "VC-WIN32"
configure = "VC-WIN32" suffix = '32'
do_script = "ms\\do_nasm"
makefile="ms\\nt.mak"
m32 = makefile
elif sys.argv[2] == "x64": elif sys.argv[2] == "x64":
arch="amd64" platform = "VC-WIN64A"
configure = "VC-WIN64A" suffix = '64'
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
perls = find_all_on_path("perl.exe", [r"\perl\bin",
r"C:\perl\bin",
r"\perl64\bin",
r"C:\perl64\bin",
])
perl = find_working_perl(perls)
if perl:
print("Found a working perl at '%s'" % (perl,))
# Set PERL for the makefile to find it
os.environ["PERL"] = perl
else: else:
print("No Perl installation was found. Existing Makefiles are used.") raise ValueError('Unrecognized platform: %s' % sys.argv[2])
sys.stdout.flush()
# Look for SSL 2 levels up from pcbuild - ie, same place zlib etc all live. # Have the distutils functions display information output
ssl_dir = get_ssl_dir() log.set_verbosity(1)
if ssl_dir is None:
# Use the same properties that are used in the VS projects
solution_dir = os.path.dirname(__file__)
propfile = os.path.join(solution_dir, 'pyproject.vsprops')
props = get_project_properties(propfile)
# Ensure we have the necessary external depenedencies
ssl_dir = os.path.join(solution_dir, props['opensslDir'])
if not os.path.isdir(ssl_dir):
print("Could not find the OpenSSL sources, try running "
"'build.bat -e'")
sys.exit(1)
# Ensure the executables used herein are available.
if not find_executable('nmake.exe'):
print('Could not find nmake.exe, try running env.bat')
sys.exit(1) sys.exit(1)
# add our copy of NASM to PATH. It will be on the same level as openssl # 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)): externals_dir = os.path.join(solution_dir, props['externalsDir'])
for dir in os.listdir(externals_dir):
if dir.startswith('nasm'): if dir.startswith('nasm'):
nasm_dir = os.path.join(ssl_dir, os.pardir, dir) nasm_dir = os.path.join(externals_dir, dir)
nasm_dir = os.path.abspath(nasm_dir) nasm_dir = os.path.abspath(nasm_dir)
old_path = os.environ['PATH'] old_path = os.environ['PATH']
os.environ['PATH'] = os.pathsep.join([nasm_dir, old_path]) os.environ['PATH'] = os.pathsep.join([nasm_dir, old_path])
break break
else: else:
print('NASM was not found, make sure it is on PATH') if not find_executable('nasm.exe'):
print('Could not find nasm.exe, please add to PATH')
sys.exit(1)
# If the ssl makefiles do not exist, we invoke PCbuild/prepare_ssl.py
# to generate them.
platform_makefile = MK1MF_FMT.format(suffix)
if not os.path.isfile(os.path.join(ssl_dir, platform_makefile)):
pcbuild_dir = os.path.join(os.path.dirname(externals_dir), 'PCbuild')
prepare_ssl = os.path.join(pcbuild_dir, 'prepare_ssl.py')
rc = subprocess.call([sys.executable, prepare_ssl, ssl_dir])
if rc:
print('Executing', prepare_ssl, 'failed (error %d)' % rc)
sys.exit(rc)
old_cd = os.getcwd() old_cd = os.getcwd()
try: try:
os.chdir(ssl_dir) 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:
if perl is None:
print("Perl is required to build the makefiles!")
sys.exit(1)
print("Creating the makefiles...") # Get the variables defined in the current makefile, if it exists.
makefile = MK1MF_FMT.format('')
try:
makevars = parse_makefile(makefile)
except EnvironmentError:
makevars = {'PLATFORM': None}
# Rebuild the makefile when building for different a platform than
# the last run.
if makevars['PLATFORM'] != platform:
print("Updating the makefile...")
sys.stdout.flush() sys.stdout.flush()
# Put our working Perl at the front of our path # Firstly, apply the changes for the platform makefile into
os.environ["PATH"] = os.path.dirname(perl) + \ # a temporary file to prevent any errors from this script
os.pathsep + \ # causing false positives on subsequent runs.
os.environ["PATH"] new_makefile = makefile + '.new'
run_configure(configure, do_script) fix_makefile(new_makefile, platform_makefile, suffix)
if debug: makevars = parse_makefile(new_makefile)
print("OpenSSL debug builds aren't supported.")
#if arch=="x86" and debug: # Secondly, perform the make recipes that use Perl
# # the do_masm script in openssl doesn't generate a debug copy_files(new_makefile, makevars)
# # build makefile so we generate it here:
# os.system("perl util\mk1mf.pl debug "+configure+" >"+makefile) # Set our build information in buildinf.h.
# XXX: This isn't needed for a properly "prepared" SSL, but
if arch == "amd64": # it fixes the current checked-in external (as of 2017-05).
create_makefile64(makefile, m32) fix_buildinf(makevars)
fix_makefile(makefile)
shutil.copy(r"crypto\buildinf.h", r"crypto\buildinf_%s.h" % arch) # Finally, move the temporary file to its real destination.
shutil.copy(r"crypto\opensslconf.h", r"crypto\opensslconf_%s.h" % arch) if os.path.exists(makefile):
os.remove(makefile)
os.rename(new_makefile, makefile)
# Now run make. # Now run make.
if arch == "amd64": makeCommand = "nmake /nologo /f \"%s\" lib" % makefile
rc = os.system("nasm -f win64 -DNEAR -Ox -g ms\\uptable.asm")
if rc:
print("nasm assembler has failed.")
sys.exit(rc)
shutil.copy(r"crypto\buildinf_%s.h" % arch, r"crypto\buildinf.h")
shutil.copy(r"crypto\opensslconf_%s.h" % arch, r"crypto\opensslconf.h")
#makeCommand = "nmake /nologo PERL=\"%s\" -f \"%s\"" %(perl, makefile)
makeCommand = "nmake /nologo -f \"%s\"" % makefile
print("Executing ssl makefiles:", makeCommand) print("Executing ssl makefiles:", makeCommand)
sys.stdout.flush() sys.stdout.flush()
rc = os.system(makeCommand) rc = os.system(makeCommand)
if rc: if rc:
print("Executing "+makefile+" failed") print("Executing", makefile, "failed (error %d)" % rc)
print(rc)
sys.exit(rc) sys.exit(rc)
finally: finally:
os.chdir(old_cd) os.chdir(old_cd)
......
#! /usr/bin/env python3 #! /usr/bin/env python3
# -*- coding: utf8 -*-
# The encoding declaration is required for running PC\VS9.0\build_ssl.py
# Script for preparing OpenSSL for building on Windows. # Script for preparing OpenSSL for building on Windows.
# Uses Perl to create nmake makefiles and otherwise prepare the way # Uses Perl to create nmake makefiles and otherwise prepare the way
# for building on 32 or 64 bit platforms. # for building on 32 or 64 bit platforms.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment