-
Notifications
You must be signed in to change notification settings - Fork 93
/
build_3rdparty.py
executable file
·262 lines (240 loc) · 11 KB
/
build_3rdparty.py
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
#!/usr/bin/env python3
# --------------------------------------------------------------- CONFIGURATION
DEPENDENT_LIBS = {
'openssl': {
'order' : 1,
'url' : 'https://github.com/openssl/openssl/releases/download/openssl-3.4.0/openssl-3.4.0.tar.gz',
'sha256' : 'e15dda82fe2fe8139dc2ac21a36d4ca01d5313c75f99f46c4e8a27709b7294bf',
'target': {
'mingw-w64': {
'result': ['include/openssl/ssl.h', 'lib/libssl.a', 'lib/libcrypto.a'],
'commands': [
'perl Configure --prefix=%(dest)s --openssldir=%(dest)s --cross-compile-prefix=%(prefix)s- no-asm mingw',
'make', 'make install_sw'
]
},
'mingw-w64_x64': {
'result': ['include/openssl/ssl.h', 'lib/libssl.a', 'lib/libcrypto.a'],
'commands': [
'perl Configure --prefix=%(dest)s --openssldir=%(dest)s --libdir=lib --cross-compile-prefix=%(prefix)s- no-asm mingw64',
'make', 'make install_sw'
]
},
'msvc': {
'result': ['include/openssl/ssl.h', 'lib/libssl.lib', 'lib/libcrypto.lib'],
'commands': [
'perl Configure --prefix=%(dest)s --openssldir=%(dest)s no-shared no-asm no-capieng VC-WIN32 -wd4005',
'nmake',
'nmake install_sw'
]
},
'msvc_x64': {
'result': ['include/openssl/ssl.h', 'lib/libssl.lib', 'lib/libcrypto.lib'],
'commands': [
'perl Configure --prefix=%(dest)s --openssldir=%(dest)s no-shared no-asm no-capieng VC-WIN64A',
'nmake',
'nmake install_sw'
]
}
}
},
'zlib': {
'order' : 2,
'url' : 'https://zlib.net/fossils/zlib-1.3.1.tar.gz',
'sha256' : '9a93b2b7dfdac77ceba5a558a580e74667dd6fede4585b91eefb60f03b72df23',
'target': {
'mingw-w64': {
'result': ['include/zlib.h', 'include/zconf.h', 'lib/libz.a'],
'commands': [
'make -f win32/Makefile.gcc PREFIX=%(prefix)s-',
'cp zlib.h zconf.h %(dest)s/include',
'cp libz.a %(dest)s/lib'
]
},
'mingw-w64_x64': {
'result': ['include/zlib.h', 'include/zconf.h', 'lib/libz.a'],
'commands': [
'make -f win32/Makefile.gcc PREFIX=%(prefix)s-',
'cp zlib.h zconf.h %(dest)s/include',
'cp libz.a %(dest)s/lib'
]
},
'msvc': {
'result': ['include/zlib.h', 'include/zconf.h', 'lib/zlib.lib'],
'replace': [('win32/Makefile.msc', '-MD', '-MT')],
'commands': [
'nmake /f win32/Makefile.msc zlib.lib',
'copy /Y zlib.h %(dest)s\\include >nul',
'copy /Y zconf.h %(dest)s\\include >nul',
'copy /Y zlib.lib %(dest)s\\lib >nul'
]
},
'msvc_x64': {
'result': ['include/zlib.h', 'include/zconf.h', 'lib/zlib.lib'],
'replace': [('win32/Makefile.msc', '-MD', '-MT')],
'commands': [
'nmake /f win32/Makefile.msc zlib.lib',
'copy /Y zlib.h %(dest)s\\include >nul',
'copy /Y zconf.h %(dest)s\\include >nul',
'copy /Y zlib.lib %(dest)s\\lib >nul'
]
}
}
},
'libssh': {
'order' : 3,
'shadow': True,
'url' : 'https://www.libssh.org/files/0.11/libssh-0.11.1.tar.xz',
'sha256' : '14b7dcc72e91e08151c58b981a7b570ab2663f630e7d2837645d5a9c612c1b79',
'target': {
'mingw-w64': {
'result': ['include/libssh/libssh.h', 'lib/libssh.a'],
'replace': [('src/CMakeLists.txt', 'set_target_properties(ssh PROPERTIES COMPILE_FLAGS "-D_POSIX_SOURCE")', '')],
'commands': [
'cmake -DCMAKE_SYSTEM_NAME=Windows \
-DCMAKE_C_COMPILER=%(prefix)s-gcc -DCMAKE_CXX_COMPILER=%(prefix)s-g++ \
-DOPENSSL_INCLUDE_DIRS=%(dest)s/include -DOPENSSL_CRYPTO_LIBRARY=%(dest)s/lib/libcrypto.a \
-DBUILD_STATIC_LIB=ON -DBUILD_SHARED_LIBS=OFF -DWITH_EXAMPLES=OFF -DWITH_SERVER=OFF -DCMAKE_INSTALL_PREFIX=%(dest)s -DCMAKE_PREFIX_PATH=%(dest)s %(src)s',
'make',
'make install'
]
},
'mingw-w64_x64': {
'result': ['include/libssh/libssh.h', 'lib/libssh.a'],
'replace': [('src/CMakeLists.txt', 'set_target_properties(ssh PROPERTIES COMPILE_FLAGS "-D_POSIX_SOURCE")', '')],
'commands': [
'cmake -DCMAKE_SYSTEM_NAME=Windows \
-DCMAKE_C_COMPILER=%(prefix)s-gcc -DCMAKE_CXX_COMPILER=%(prefix)s-g++ \
-DOPENSSL_INCLUDE_DIRS=%(dest)s/include -DOPENSSL_CRYPTO_LIBRARY=%(dest)s/lib/libcrypto.a \
-DBUILD_STATIC_LIB=ON -DBUILD_SHARED_LIBS=OFF -DWITH_EXAMPLES=OFF -DWITH_SERVER=OFF -DCMAKE_INSTALL_PREFIX=%(dest)s -DCMAKE_PREFIX_PATH=%(dest)s %(src)s',
'make',
'make install'
]
},
'msvc': {
'result': ['include/libssh/libssh.h', 'lib/ssh.lib'],
'commands': [
'cmake -G "NMake Makefiles" -DBUILD_STATIC_LIB=ON -DBUILD_SHARED_LIBS=OFF -DWITH_EXAMPLES=OFF -DWITH_SERVER=OFF -DCMAKE_BUILD_TYPE=Release \
"-DCMAKE_C_FLAGS_RELEASE=/MP /MT /O2 /Ob2 /D NDEBUG" "-DCMAKE_CXX_FLAGS_RELEASE=/MP /MT /O2 /Ob2 /D NDEBUG" \
-DOPENSSL_INCLUDE_DIRS=%(dest)s\\include -DOPENSSL_CRYPTO_LIBRARY=%(dest)s\\lib\\libcrypto.lib \
-DCMAKE_INSTALL_PREFIX=%(dest)s -DCMAKE_PREFIX_PATH=%(dest)s %(src)s',
'nmake install'
]
},
'msvc_x64': {
'result': ['include/libssh/libssh.h', 'lib/ssh.lib'],
'commands': [
'cmake -G "NMake Makefiles" -DBUILD_STATIC_LIB=ON -DBUILD_SHARED_LIBS=OFF -DWITH_EXAMPLES=OFF -DWITH_SERVER=OFF -DCMAKE_BUILD_TYPE=Release \
"-DCMAKE_C_FLAGS_RELEASE=/MP /MT /O2 /Ob2 /D NDEBUG" "-DCMAKE_CXX_FLAGS_RELEASE=/MP /MT /O2 /Ob2 /D NDEBUG" \
-DOPENSSL_INCLUDE_DIRS=%(dest)s\\include -DOPENSSL_CRYPTO_LIBRARY=%(dest)s\\lib\\libcrypto.lib \
-DCMAKE_INSTALL_PREFIX=%(dest)s -DCMAKE_PREFIX_PATH=%(dest)s %(src)s',
'nmake install'
]
}
}
}
}
# --------------------------------------------------------------- HELPERS
import os, sys, platform, shutil, urllib.request, hashlib, tarfile, subprocess
def join_path(*p):
return os.path.abspath(os.path.join(*p))
def message(msg):
sys.stdout.write(msg)
sys.stdout.flush()
def error(msg):
message(msg+'\n')
sys.exit(1)
def shell(cmd):
message(' %s\n' % cmd)
try:
subprocess.check_output(cmd, stderr=subprocess.STDOUT, shell=True)
except subprocess.CalledProcessError as e:
error("\n%s\ncommand failed: exit code %d" % (e.output.decode('utf-8'), e.returncode))
def rmdir(path):
if os.path.exists(path):
if platform.system() == 'Windows':
shell('attrib -R %s\\* /S' % path)
shutil.rmtree(path)
def mkdir_p(*paths):
path = join_path(*paths)
if not os.path.exists(path):
os.makedirs(path)
def download_file(url, sha256, dir):
name = url.split('/')[-1]
loc = join_path(dir, name)
if os.path.exists(loc):
hash = hashlib.sha256(open(loc, 'rb').read()).hexdigest()
if hash == sha256:
return loc
os.remove(loc)
message('Checksum mismatch for %s, re-downloading.\n' % name)
def hook(cnt, bs, total):
pct = int(cnt*bs*100/total)
message("\rDownloading: %s [%d%%]" % (name, pct))
urllib.request.urlretrieve(url, loc, reporthook=hook)
message("\r")
hash = hashlib.sha256(open(loc, 'rb').read()).hexdigest()
if hash != sha256:
os.remove(loc)
error('Checksum mismatch for %s, aborting.' % name)
message("\rDownloaded: %s [checksum OK]\n" % name)
return loc
def download_tarball(url, sha256, dir, name):
loc = download_file(url, sha256, dir)
tar = tarfile.open(loc)
sub = tar.getnames()[0]
if '/' in sub:
sub = sub[:sub.index('/')]
src = join_path(dir, sub)
tgt = join_path(dir, name)
rmdir(src)
tar.extractall(dir)
rmdir(tgt)
os.rename(src, tgt)
return tgt
# --------------------------------------------------------------- BUILDING
def main(outdir, prefix):
build = os.path.abspath(os.path.join(outdir, 'obj'))
dest = os.path.abspath(os.path.join(outdir, '3rdparty'))
mkdir_p(build)
mkdir_p(dest, 'include')
mkdir_p(dest, 'lib')
if(prefix == 'msvc_x64'):
target = 'msvc_x64'
elif (prefix == 'x86_64-w64-mingw32'):
target = 'mingw-w64_x64'
else:
target = platform.system() == 'Windows' and 'msvc' or 'mingw-w64'
for library in sorted(DEPENDENT_LIBS, key=lambda x: DEPENDENT_LIBS[x]['order']):
if target not in DEPENDENT_LIBS[library]['target']:
print('%s: skipping (not available)' % library)
continue
cfg = DEPENDENT_LIBS[library]['target'][target]
built = True
for path in cfg['result']:
built = built and os.path.exists(join_path(dest, path))
if built:
print('%s: skipping (already built)' % library)
continue
print('-' * 60, library)
download_tarball(DEPENDENT_LIBS[library]['url'],
DEPENDENT_LIBS[library]['sha256'], build, library)
if DEPENDENT_LIBS[library].get('shadow'):
rmdir(join_path(build, library+'-build'))
mkdir_p(join_path(build, library+'-build'))
os.chdir(join_path(build, library+'-build'))
else:
os.chdir(join_path(build, library))
for location, src, tgt in cfg.get('replace', []):
data = open(join_path(build, library, location), 'r').read()
open(join_path(build, library, location), 'w').write(data.replace(src, tgt))
for cmd in cfg['commands']:
shell(cmd % { 'dest' : dest, 'src': join_path(build, library), 'prefix': prefix })
os.chdir(dest)
for path in cfg['result']:
if not os.path.exists(path):
error('Unable to build %s, missing: %s' % (library, path))
if __name__ == '__main__':
outdir = sys.argv[1] if len(sys.argv) in (2,3) else 'x86'
prefix = sys.argv[2] if len(sys.argv) == 3 else ''
main(outdir, prefix)