2121import os
2222import re
2323import sys
24- import shutil
2524import subprocess
25+ from shutil import copy
2626
2727# Find all "foo.exe" files on the PATH.
28- def find_all_on_path (filename , extras = None ):
28+ def find_all_on_path (filename , extras = None ):
2929 entries = os .environ ["PATH" ].split (os .pathsep )
3030 ret = []
3131 for p in entries :
@@ -39,6 +39,7 @@ def find_all_on_path(filename, extras = None):
3939 ret .append (fname )
4040 return ret
4141
42+
4243# Find a suitable Perl installation for OpenSSL.
4344# cygwin perl does *not* work. ActivePerl does.
4445# Being a Perl dummy, the simplest way I can check is if the "Win32" package
@@ -61,144 +62,85 @@ def find_working_perl(perls):
6162 print ("NO perl interpreters were found on this machine at all!" )
6263 print (" Please install ActivePerl and ensure it appears on your path" )
6364
64- def create_makefile64 (makefile , m32 ):
65- """Create and fix makefile for 64bit
66-
67- Replace 32 with 64bit directories
68- """
69- if not os .path .isfile (m32 ):
70- return
71- with open (m32 ) as fin :
72- with open (makefile , 'w' ) as fout :
73- for line in fin :
74- line = line .replace ("=tmp32" , "=tmp64" )
75- line = line .replace ("=out32" , "=out64" )
76- line = line .replace ("=inc32" , "=inc64" )
77- # force 64 bit machine
78- line = line .replace ("MKLIB=lib" , "MKLIB=lib /MACHINE:X64" )
79- line = line .replace ("LFLAGS=" , "LFLAGS=/MACHINE:X64 " )
80- # don't link against the lib on 64bit systems
81- line = line .replace ("bufferoverflowu.lib" , "" )
82- fout .write (line )
83- os .unlink (m32 )
84-
85- def create_asms (makefile ):
65+
66+ def create_asms (makefile , tmp_d ):
8667 #create a custom makefile out of the provided one
8768 asm_makefile = os .path .splitext (makefile )[0 ] + '.asm.mak'
88- with open (makefile ) as fin :
89- with open (asm_makefile , 'w' ) as fout :
90- for line in fin :
91- # Keep everything up to the install target (it's convenient)
92- if line .startswith ('install: all' ):
93- break
94- else :
69+ with open (makefile ) as fin , open (asm_makefile , 'w' ) as fout :
70+ for line in fin :
71+ # Keep everything up to the install target (it's convenient)
72+ if line .startswith ('install: all' ):
73+ break
74+ fout .write (line )
75+ asms = []
76+ for line in fin :
77+ if '.asm' in line and line .strip ().endswith ('.pl' ):
78+ asms .append (line .split (':' )[0 ])
79+ while line .strip ():
9580 fout .write (line )
96- asms = []
97- for line in fin :
98- if '.asm' in line and line .strip ().endswith ('.pl' ):
99- asms .append (line .split (':' )[0 ])
100- while line .strip ():
101- fout .write (line )
102- line = next (fin )
103- fout .write ('\n ' )
81+ line = next (fin )
82+ fout .write ('\n ' )
10483
105- fout .write ('asms: $(TMP_D) ' )
106- fout .write (' ' .join (asms ))
107- fout .write ('\n ' )
84+ fout .write ('asms: $(TMP_D) ' )
85+ fout .write (' ' .join (asms ))
86+ fout .write ('\n ' )
87+ os .system ('nmake /f {} PERL=perl TMP_D={} asms' .format (asm_makefile , tmp_d ))
10888
109- os .system ('nmake /f {} PERL=perl asms' .format (asm_makefile ))
110- os .unlink (asm_makefile )
11189
112-
113-
114- def fix_makefile (makefile ):
115- """Fix some stuff in all makefiles
116- """
117- if not os .path .isfile (makefile ):
118- return
90+ def copy_includes (makefile , suffix ):
91+ dir = 'include' + suffix + '\\ openssl'
92+ os .makedirs (dir , exist_ok = True )
11993 copy_if_different = r'$(PERL) $(SRC_D)\util\copy-if-different.pl'
12094 with open (makefile ) as fin :
121- lines = fin .readlines ()
122- with open (makefile , 'w' ) as fout :
123- for line in lines :
124- if line .startswith ("PERL=" ):
125- continue
126- if line .startswith ("CP=" ):
127- line = "CP=copy\n "
128- if line .startswith ("MKDIR=" ):
129- line = "MKDIR=mkdir\n "
130- if line .startswith ("CFLAG=" ):
131- line = line .strip ()
132- for algo in ("RC5" , "MDC2" , "IDEA" ):
133- noalgo = " -DOPENSSL_NO_%s" % algo
134- if noalgo not in line :
135- line = line + noalgo
136- line = line + '\n '
95+ for line in fin :
13796 if copy_if_different in line :
138- line = line .replace (copy_if_different , 'copy /Y' )
139- fout .write (line )
97+ perl , script , src , dest = line .split ()
98+ if not '$(INCO_D)' in dest :
99+ continue
100+ # We're in the root of the source tree
101+ src = src .replace ('$(SRC_D)' , '.' ).strip ('"' )
102+ dest = dest .strip ('"' ).replace ('$(INCO_D)' , dir )
103+ print ('copying' , src , 'to' , dest )
104+ copy (src , dest )
105+
140106
141107def run_configure (configure , do_script ):
142108 print ("perl Configure " + configure + " no-idea no-mdc2" )
143109 os .system ("perl Configure " + configure + " no-idea no-mdc2" )
144110 print (do_script )
145111 os .system (do_script )
146112
147- def cmp (f1 , f2 ):
148- bufsize = 1024 * 8
149- with open (f1 , 'rb' ) as fp1 , open (f2 , 'rb' ) as fp2 :
150- while True :
151- b1 = fp1 .read (bufsize )
152- b2 = fp2 .read (bufsize )
153- if b1 != b2 :
154- return False
155- if not b1 :
156- return True
157-
158- def copy (src , dst ):
159- if os .path .isfile (dst ) and cmp (src , dst ):
160- return
161- shutil .copy (src , dst )
162113
163114def prep (arch ):
115+ makefile_template = "ms\\ nt{}.mak"
116+ generated_makefile = makefile_template .format ('' )
164117 if arch == "x86" :
165118 configure = "VC-WIN32"
166119 do_script = "ms\\ do_nasm"
167- makefile = "ms\\ nt.mak"
168- m32 = makefile
169- dirsuffix = "32"
120+ suffix = "32"
170121 elif arch == "amd64" :
171122 configure = "VC-WIN64A"
172123 do_script = "ms\\ do_win64a"
173- makefile = "ms\\ nt64.mak"
174- m32 = makefile .replace ('64' , '' )
175- dirsuffix = "64"
124+ suffix = "64"
176125 #os.environ["VSEXTCOMP_USECL"] = "MS_OPTERON"
177126 else :
178127 raise ValueError ('Unrecognized platform: %s' % arch )
179128
180- # rebuild makefile when we do the role over from 32 to 64 build
181- if arch == "amd64" and os .path .isfile (m32 ) and not os .path .isfile (makefile ):
182- os .unlink (m32 )
183-
184- # If the ssl makefiles do not exist, we invoke Perl to generate them.
185- # Due to a bug in this script, the makefile sometimes ended up empty
186- # Force a regeneration if it is.
187- if not os .path .isfile (makefile ) or os .path .getsize (makefile )== 0 :
188- print ("Creating the makefiles..." )
189- sys .stdout .flush ()
190- run_configure (configure , do_script )
191-
192- if arch == "amd64" :
193- create_makefile64 (makefile , m32 )
194- fix_makefile (makefile )
195- copy (r"crypto\buildinf.h" , r"crypto\buildinf_%s.h" % arch )
196- copy (r"crypto\opensslconf.h" , r"crypto\opensslconf_%s.h" % arch )
197- else :
198- print (makefile , 'already exists!' )
129+ print ("Creating the makefiles..." )
130+ sys .stdout .flush ()
131+ # run configure, copy includes, create asms
132+ run_configure (configure , do_script )
133+ makefile = makefile_template .format (suffix )
134+ try :
135+ os .unlink (makefile )
136+ except FileNotFoundError :
137+ pass
138+ os .rename (generated_makefile , makefile )
139+ copy_includes (makefile , suffix )
199140
200141 print ('creating asms...' )
201- create_asms (makefile )
142+ create_asms (makefile , 'tmp' + suffix )
143+
202144
203145def main ():
204146 if len (sys .argv ) == 1 :
@@ -229,6 +171,9 @@ def main():
229171 print ("Found a working perl at '%s'" % (perl ,))
230172 else :
231173 sys .exit (1 )
174+ if not find_all_on_path ('nmake.exe' ):
175+ print ('Could not find nmake.exe, try running env.bat' )
176+ sys .exit (1 )
232177 sys .stdout .flush ()
233178
234179 # Put our working Perl at the front of our path
0 commit comments