-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathbuild-openssl.py
More file actions
executable file
·313 lines (244 loc) · 13.8 KB
/
build-openssl.py
File metadata and controls
executable file
·313 lines (244 loc) · 13.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#!/usr/bin/env python3
import argparse
import sys
import platform
import os
import subprocess as sp
import tempfile
import shutil
import stat
import tarfile
import multiprocessing
from pathlib import Path
def call_shell(cmd):
sp.call(cmd, shell=True)
def parse_args():
parser = argparse.ArgumentParser(
description='Builds the openssl library for multiple platforms')
parser.add_argument('--prefix', required=True, dest='prefix',
help='Where should the binaries be installed to.')
parser.add_argument('--openssl-tar', required=True, dest='openssl_tar',
help='Where is the OpenSSL tar file located.')
parser.add_argument('--force', action='store_true',
help='If binaries already installed to prefix, this flag makes the build overwrite them.')
parser.add_argument('--compile-threads', dest='compile_threads', default=multiprocessing.cpu_count(),
help='Number of processor threads to use during compilation')
subparsers = parser.add_subparsers(dest='command')
parser_android = subparsers.add_parser('android', help='Build for android')
parser_android.add_argument('--arch', required=True, dest='android_arch',
help='Define the android architecture to build, possible values [aarch64, armv7a]')
parser_android.add_argument('--ndk-dir', required=True, dest='ndk_dir',
help='Where is the Android NDK located.')
parser_android = subparsers.add_parser('ios-universal', help='Build a fat lib for ios, containing archs armv7s armv7 and arm64')
parser_android = subparsers.add_parser('ios-sim-universal', help='Build for iOS Simulator x86_64 and ARM64')
parser_android = subparsers.add_parser('mac', help='Build for MacOSX')
parser_android = subparsers.add_parser('mac-universal', help='Build for MacOSX x64 and ARM64 architecture')
parser_android = subparsers.add_parser('windows', help='Build for Windows')
parser_android = subparsers.add_parser('linux', help='Build for Linux')
return parser.parse_args()
# Make a temporary directory for the openssl project extracted from the tar file
# and return the path
def make_openssl_temp_dir(root_folder, openssl_tar):
tempdir = os.path.join(tempfile.gettempdir(), root_folder)
print(f'Using temp dir {tempdir}')
openssl_folder_name = os.path.basename(openssl_tar).split('.tar.gz')[0]
openssl_temp_dir = Path(tempdir, os.path.basename(openssl_tar).split('.tar.gz')[0])
try:
shutil.rmtree(openssl_temp_dir)
except:
None
with tarfile.open(openssl_tar, 'r:gz') as tar:
tar.extractall(tempdir)
config_file = os.path.join(openssl_temp_dir, 'config')
st = os.stat(config_file)
os.chmod(config_file, st.st_mode | stat.S_IEXEC)
configure_file = os.path.join(openssl_temp_dir, 'Configure')
st = os.stat(configure_file)
os.chmod(configure_file, st.st_mode | stat.S_IEXEC)
assert(os.path.exists(openssl_temp_dir))
return openssl_temp_dir
def set_envs(ndk_dir):
toolchain_path = os.path.join(ndk_dir, 'toolchains/llvm/prebuilt/linux-x86_64/bin')
os.environ['ARCH_FLAGS'] = '-march=armv7-a -mfloat-abi=softfp -mfpu=vfpv3-d16'
os.environ['ARCH_LINK'] = '-march=armv7-a -Wl,--fix-cortex-a8'
os.environ['CPPFLAGS'] = '-fPIC -ffunction-sections -funwind-tables -fstack-protector -fno-strict-aliasing '
os.environ['CXXFLAGS'] = '-fPIC -ffunction-sections -funwind-tables -fstack-protector -fno-strict-aliasing -frtti -fexceptions '
os.environ['CFLAGS'] = ' -fPIC -ffunction-sections -funwind-tables -fstack-protector -fno-strict-aliasing '
os.environ['LDFLAGS'] = '-Wl'
os.environ['ANDROID_NDK_HOME'] = ndk_dir
os.environ['PATH'] = '{}:{}'.format(toolchain_path, os.environ['PATH'])
def build(openssl_temp_dir, compile_threads):
print('Building...')
call_shell('cd {} && make -j {} && make install_sw'.format(
openssl_temp_dir, compile_threads))
def android_build(ndk_dir, arch, openssl_temp_dir, prefix, compile_threads):
ndk_dir = os.path.expanduser(ndk_dir)
ndk_dir = os.path.abspath(ndk_dir)
if not os.path.exists(ndk_dir):
print('NDK directory does not exist.')
sys.exit(1)
set_envs(ndk_dir)
openssl_android_arch = ''
if arch == 'armv7a':
openssl_android_arch = 'android-arm'
elif arch == 'aarch64':
openssl_android_arch = 'android-arm64'
else:
print('Invalid android arch selected: {}, choose one from: [aarch64, armv7a]'.format(arch))
sys.exit(1)
call_shell(
'cd {} && ./Configure {} --prefix={}'.format(
openssl_temp_dir, openssl_android_arch, prefix))
build(openssl_temp_dir, compile_threads)
def iossim_build(openssl_arm64_temp_dir, openssl_x64_temp_dir, prefix, compile_threads):
Path(prefix).mkdir(parents=True, exist_ok=True)
Path(prefix).joinpath("lib").mkdir(exist_ok=True)
temp_dir = tempfile.gettempdir()
arm64_prefix = os.path.join(temp_dir, 'arm64')
x64_prefix = os.path.join(temp_dir, 'x64')
os.environ['CFLAGS'] = '-Wno-error=implicit-function-declaration' # fix for compile issue https://github.com/openssl/openssl/issues/18720
print("=======================================================")
print("ARM64 iOS Simulator BUILD")
print("=======================================================")
call_shell(
f'cd {openssl_arm64_temp_dir} && ./Configure iossimulator-xcrun "-arch arm64 -fembed-bitcode" no-asm no-shared no-hw no-async --prefix={arm64_prefix}')
build(openssl_arm64_temp_dir, compile_threads)
print("=======================================================")
print("x86_64 iOS Simulator BUILD")
print("=======================================================")
call_shell(f'cd {openssl_x64_temp_dir} && ./Configure iossimulator-xcrun "-arch x86_64 -fembed-bitcode" no-tests --prefix={x64_prefix}')
build(openssl_x64_temp_dir, compile_threads)
for lib in ["libssl.a", "libcrypto.a"]:
print(f'creating universal binary for {lib}')
call_shell(
f'lipo -create {os.path.join(arm64_prefix, "lib", lib)} {os.path.join(x64_prefix, "lib", lib)} -output {os.path.join(prefix, "lib", lib)}'
)
call_shell(f'cp -r {os.path.join(arm64_prefix, "include")} {os.path.join(prefix, "include")}')
def ios_build(openssl_arm64_temp_dir, openssl_arm64e_temp_dir, openssl_armv7s_temp_dir, openssl_armv7_temp_dir, prefix, compile_threads):
Path(prefix).mkdir(parents=True, exist_ok=True)
Path(prefix).joinpath("lib").mkdir(exist_ok=True)
temp_dir = tempfile.gettempdir()
arm64_prefix = os.path.join(temp_dir, 'arm64')
arm64e_prefix = os.path.join(temp_dir, 'arm64e')
armv7s_prefix = os.path.join(temp_dir, 'armv7s')
armv7_prefix = os.path.join(temp_dir, 'armv7')
os.environ['OPENSSL_LOCAL_CONFIG_DIR'] = f'{os.getcwd()}/openssl-config'
os.environ['CFLAGS'] = '-Wno-error=implicit-function-declaration' # fix for compile issue https://github.com/openssl/openssl/issues/18720
print("=======================================================")
print("ARM64 iOS BUILD")
print("=======================================================")
call_shell(
f'cd {openssl_arm64_temp_dir} && ./Configure ios64-xcrun no-asm no-async no-shared --prefix={arm64_prefix}')
build(openssl_arm64_temp_dir, compile_threads)
print("=======================================================")
print("ARM64e iOS BUILD")
print("=======================================================")
call_shell(
f'cd {openssl_arm64e_temp_dir} && ./Configure ios-xcrun-arm64e no-asm no-async no-shared --prefix={arm64e_prefix}')
build(openssl_arm64e_temp_dir, compile_threads)
print("=======================================================")
print("ARMV7S iOS BUILD")
print("=======================================================")
call_shell(
f'cd {openssl_armv7s_temp_dir} && ./Configure ios-xcrun-armv7s no-asm no-async no-shared --prefix={armv7s_prefix}')
build(openssl_armv7s_temp_dir, compile_threads)
print("=======================================================")
print("ARMV7 iOS BUILD")
print("=======================================================")
call_shell(
f'cd {openssl_armv7_temp_dir} && ./Configure ios-xcrun-armv7 no-asm no-async no-shared --prefix={armv7_prefix}')
build(openssl_armv7_temp_dir, compile_threads)
for lib in ["libssl.a", "libcrypto.a"]:
print(f'creating universal binary for {lib}')
call_shell(
f'lipo -create {os.path.join(arm64_prefix, "lib", lib)} {os.path.join(arm64e_prefix, "lib", lib)} {os.path.join(armv7s_prefix, "lib", lib)} {os.path.join(armv7_prefix, "lib", lib)} -output {os.path.join(prefix, "lib", lib)}'
)
call_shell(f'cp -r {os.path.join(arm64_prefix, "include")} {os.path.join(prefix, "include")}')
def mac_build(openssl_temp_dir, prefix, compile_threads):
min_osx_version = '10.7'
os.environ['CC'] = 'clang -mmacosx-version-min={}'.format(min_osx_version)
os.environ['CFLAGS'] = '-Wno-error=implicit-function-declaration' # fix for compile issue https://github.com/openssl/openssl/issues/18720
os.environ['CROSS_COMPILE'] = ''
call_shell(
'cd {} && ./Configure darwin64-x86_64-cc no-tests --prefix={}'.format(
openssl_temp_dir, prefix))
build(openssl_temp_dir, compile_threads)
def mac_universal_build(openssl_arm64_temp_dir, openssl_x64_temp_dir, prefix, compile_threads):
# Ensure that prefix exists.
Path(prefix).mkdir(parents=True, exist_ok=True)
Path(prefix).joinpath("lib").mkdir(exist_ok=True)
min_osx_version = '12.2'
os.environ['CC'] = f'clang -mmacosx-version-min={min_osx_version}'
os.environ['CROSS_COMPILE'] = ''
temp_dir = tempfile.gettempdir()
arm64_prefix = os.path.join(temp_dir, 'arm64')
x64_prefix = os.path.join(temp_dir, 'x64')
print("=======================================================")
print("ARM 64 BUILD")
print("=======================================================")
call_shell(f'cd {openssl_arm64_temp_dir} && ./Configure darwin64-arm64-cc no-tests --prefix={arm64_prefix}')
build(openssl_arm64_temp_dir, compile_threads)
print("=======================================================")
print("x86_64 BUILD")
print("=======================================================")
call_shell(f'cd {openssl_x64_temp_dir} && ./Configure darwin64-x86_64-cc no-tests --prefix={x64_prefix}')
build(openssl_x64_temp_dir, compile_threads)
for lib in ["libssl.a", "libcrypto.a"]:
print(f'creating universal binary for {lib}')
call_shell(
f'lipo -create {os.path.join(arm64_prefix, "lib", lib)} {os.path.join(x64_prefix, "lib", lib)} -output {os.path.join(prefix, "lib", lib)}'
)
call_shell(f'cp -r {os.path.join(arm64_prefix, "include")} {os.path.join(prefix, "include")}')
def linux_build(openssl_temp_dir, prefix, compile_threads):
os.environ['CROSS_COMPILE'] = ''
os.environ['CC'] = f'{os.environ["CC"]} -fPIC'
call_shell(f'cd {openssl_temp_dir} && ./Configure linux-x86_64 --prefix={prefix}')
build(openssl_temp_dir, compile_threads)
def windows_build(openssl_temp_dir, prefix, compile_threads):
os.environ['CROSS_COMPILE'] = ''
call_shell(f'cd {openssl_temp_dir} && perl Configure VC-WIN64A --prefix={prefix}')
build(openssl_temp_dir, compile_threads)
def main():
args = parse_args()
openssl_tar = os.path.expanduser(args.openssl_tar)
compile_threads = args.compile_threads
prefix = os.path.expanduser(args.prefix)
print('Will compile OpenSSL with {} threads'.format(compile_threads))
if not os.path.exists(openssl_tar):
print('OpenSSL tar file does not exist.')
sys.exit(1)
if os.path.exists(prefix):
if args.force:
shutil.rmtree(prefix)
else:
print('Prefix path already exist, pass --force to overwrite it.')
sys.exit(1)
prefix = os.path.abspath(prefix)
if args.command == 'android':
openssl_temp_dir = make_openssl_temp_dir('android', openssl_tar)
android_build(args.ndk_dir, args.android_arch, openssl_temp_dir, prefix, compile_threads)
elif args.command == 'linux':
openssl_temp_dir = make_openssl_temp_dir('linux', openssl_tar)
linux_build(openssl_temp_dir, prefix, compile_threads)
elif args.command == 'mac':
openssl_temp_dir = make_openssl_temp_dir('mac', openssl_tar)
mac_build(openssl_temp_dir, prefix, compile_threads)
elif args.command == 'ios-universal':
openssl_arm64_temp_dir = make_openssl_temp_dir('ios-arm64', openssl_tar)
openssl_arm64e_temp_dir = make_openssl_temp_dir('ios-arm64e', openssl_tar)
openssl_armv7_temp_dir = make_openssl_temp_dir('ios-armv7', openssl_tar)
openssl_armv7s_temp_dir = make_openssl_temp_dir('ios-armv7s', openssl_tar)
ios_build(openssl_arm64_temp_dir, openssl_arm64e_temp_dir, openssl_armv7s_temp_dir, openssl_armv7_temp_dir, prefix, compile_threads)
elif args.command == 'ios-sim-universal':
openssl_arm64_temp_dir = make_openssl_temp_dir('simulator-arm64', openssl_tar)
openssl_x64_temp_dir = make_openssl_temp_dir('simulator-x64', openssl_tar)
iossim_build(openssl_arm64_temp_dir, openssl_x64_temp_dir, prefix, compile_threads)
elif args.command == 'mac-universal':
openssl_arm64_temp_dir = make_openssl_temp_dir('mac-arm64', openssl_tar)
openssl_x64_temp_dir = make_openssl_temp_dir('mac-x64', openssl_tar)
mac_universal_build(openssl_arm64_temp_dir, openssl_x64_temp_dir, prefix, compile_threads)
elif args.command == 'windows':
openssl_temp_dir = make_openssl_temp_dir('windows', openssl_tar)
windows_build(openssl_temp_dir, prefix, compile_threads)
if __name__ == '__main__':
main()