-
Notifications
You must be signed in to change notification settings - Fork 50
/
build_win_deps.bat
429 lines (363 loc) · 14.6 KB
/
build_win_deps.bat
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
@echo off
setlocal
pushd "%~dp0"
set "deps_dir=build\deps\win"
set "third_party_dir=third-party"
set "third_party_deps_dir=%third_party_dir%\deps\win"
set "third_party_common_dir=%third_party_dir%\deps\common"
set "extractor=%third_party_deps_dir%\7za\7za.exe"
set "mycmake=%~dp0%third_party_deps_dir%\cmake\bin\cmake.exe"
set /a last_code=0
if not exist "%extractor%" (
call :err_msg "Couldn't find extraction tool"
set /a last_code=1
goto :end_script
)
if not exist "%mycmake%" (
call :err_msg "Couldn't find cmake"
set /a last_code=1
goto :end_script
)
:: < 0: deduce, > 1: force
set /a PARALLEL_THREADS_OVERRIDE=-1
set "VERBOSITY="
:: get args
:args_loop
if "%~1"=="" (
goto :args_loop_end
) else if "%~1"=="-j" (
call :get_parallel_threads_count %~2 || (
call :err_msg "Invalid arg after -j, expected a number"
set /a last_code=1
goto :end_script
)
shift /1
) else if "%~1"=="-verbose" (
set "VERBOSITY=-v"
) else (
call :err_msg "Invalid arg: %~1"
set /a last_code=1
goto :end_script
)
shift /1
goto :args_loop
:args_loop_end
:: use 70%
if defined NUMBER_OF_PROCESSORS (
set /a jobs_count=NUMBER_OF_PROCESSORS*70/100
) else (
set /a jobs_count=2
)
if %PARALLEL_THREADS_OVERRIDE% gtr 0 (
set /a jobs_count=PARALLEL_THREADS_OVERRIDE
)
if %jobs_count% lss 2 (
set /a jobs_count=2
)
call :extract_all_deps || (
set /a last_code=1
goto :end_script
)
:: ############## common CMAKE args ##############
:: https://cmake.org/cmake/help/latest/variable/CMAKE_LANG_FLAGS_CONFIG.html#variable:CMAKE_%3CLANG%3E_FLAGS_%3CCONFIG%3E
set cmake_common_args=-G "Visual Studio 17 2022" -S .
set cmake_common_defs=-DCMAKE_BUILD_TYPE=Release -DCMAKE_C_STANDARD_REQUIRED=ON -DCMAKE_CXX_STANDARD_REQUIRED=ON -DCMAKE_C_STANDARD=17 -DCMAKE_CXX_STANDARD=17 -DCMAKE_POSITION_INDEPENDENT_CODE=True -DBUILD_SHARED_LIBS=OFF -DCMAKE_MSVC_RUNTIME_LIBRARY=MultiThreaded "-DCMAKE_C_FLAGS_RELEASE=/MT /std:c17 /D_MT" "-DCMAKE_CXX_FLAGS_RELEASE=/MT /std:c++17 /D_MT"
set "recreate_32=rmdir /s /q build32\ 1>nul 2>&1 & rmdir /s /q install32\ 1>nul 2>&1 & mkdir build32\ && mkdir install32\"
set "recreate_64=rmdir /s /q build64\ 1>nul 2>&1 & rmdir /s /q install64\ 1>nul 2>&1 & mkdir build64\ && mkdir install64\"
set cmake_gen32="%mycmake%" %cmake_common_args% -A Win32 -B build32 -DCMAKE_INSTALL_PREFIX=install32 %cmake_common_defs%
set cmake_gen64="%mycmake%" %cmake_common_args% -A x64 -B build64 -DCMAKE_INSTALL_PREFIX=install64 %cmake_common_defs%
set cmake_build32="%mycmake%" --build build32 --config Release --parallel %jobs_count% %VERBOSITY%
set cmake_build64="%mycmake%" --build build64 --config Release --parallel %jobs_count% %VERBOSITY%
set "clean_gen32=if exist build32\ rmdir /s /q build32"
set "clean_gen64=if exist build64\ rmdir /s /q build64"
:: "-DCMAKE_C_STANDARD_LIBRARIES=kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib" "-DCMAKE_CXX_STANDARD_LIBRARIES=kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib"
:: -DCMAKE_MSVC_RUNTIME_LIBRARY=MultiThreaded" "-DMSVC_RUNTIME_LIBRARY=MultiThreaded"
:: "-DCMAKE_C_STANDARD_LIBRARIES=kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib"
:: "-DCMAKE_C_FLAGS_RELEASE=/MT /O2 /Ob2 /DNDEBUG"
:: "-DCMAKE_CXX_FLAGS_RELEASE=/MT /O2 /Ob2 /DNDEBUG"
:: "-DCMAKE_CXX_STANDARD_LIBRARIES=kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib"
:: "-DCMAKE_C_STANDARD_LIBRARIES=kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib" "-DCMAKE_C_FLAGS_RELEASE=/MT /O2 /Ob2 /DNDEBUG"
:: "-DCMAKE_CXX_STANDARD_LIBRARIES=kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib"
:: "-DCMAKE_CXX_FLAGS_RELEASE=/MT /O2 /Ob2 /DNDEBUG"
:: "-DCMAKE_C_STANDARD_LIBRARIES=kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib" "-DCMAKE_CXX_STANDARD_LIBRARIES=kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib"
echo // [?] All CMAKE builds will use %jobs_count% parallel jobs
:: ############## build ssq ##############
echo // building ssq lib
pushd "%deps_dir%\libssq"
setlocal
call "%~dp0build_win_set_env.bat" 32 || (
endlocal
popd
call :err_msg "Couldn't find Visual Studio or build tools - 32"
set /a last_code=1
goto :end_script
)
%recreate_32%
%cmake_gen32%
set /a _exit=%errorlevel%
%cmake_build32%
set /a _exit+=%errorlevel%
endlocal & set /a last_code+=%_exit%
setlocal
call "%~dp0build_win_set_env.bat" 64 || (
endlocal
popd
call :err_msg "Couldn't find Visual Studio or build tools - 64"
set /a last_code=1
goto :end_script
)
%recreate_64%
%cmake_gen64%
set /a _exit=%errorlevel%
%cmake_build64%
set /a _exit+=%errorlevel%
endlocal & set /a last_code+=%_exit%
popd
echo: & echo:
:: ############## build zlib ##############
echo // building zlib lib
pushd "%deps_dir%\zlib"
setlocal
call "%~dp0build_win_set_env.bat" 32 || (
endlocal
popd
call :err_msg "Couldn't find Visual Studio or build tools - 32"
set /a last_code=1
goto :end_script
)
%recreate_32%
%cmake_gen32%
set /a _exit=%errorlevel%
%cmake_build32% --target install
set /a _exit+=%errorlevel%
%clean_gen32%
endlocal & set /a last_code+=%_exit%
setlocal
call "%~dp0build_win_set_env.bat" 64 || (
endlocal
popd
call :err_msg "Couldn't find Visual Studio or build tools - 64"
set /a last_code=1
goto :end_script
)
%recreate_64%
%cmake_gen64%
set /a _exit=%errorlevel%
%cmake_build64% --target install
set /a _exit+=%errorlevel%
%clean_gen64%
endlocal & set /a last_code+=%_exit%
popd
echo: & echo:
:: ############## zlib is painful ##############
:: lib curl uses the default search paths, even when ZLIB_INCLUDE_DIR and ZLIB_LIBRARY_RELEASE are defined
:: check thir CMakeLists.txt line #573
:: optional_dependency(ZLIB)
:: if(ZLIB_FOUND)
:: set(HAVE_LIBZ ON)
:: set(USE_ZLIB ON)
::
:: # Depend on ZLIB via imported targets if supported by the running
:: # version of CMake. This allows our dependents to get our dependencies
:: # transitively.
:: if(NOT CMAKE_VERSION VERSION_LESS 3.4)
:: list(APPEND CURL_LIBS ZLIB::ZLIB) <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< evil
:: else()
:: list(APPEND CURL_LIBS ${ZLIB_LIBRARIES})
:: include_directories(${ZLIB_INCLUDE_DIRS})
:: endif()
:: list(APPEND CMAKE_REQUIRED_INCLUDES ${ZLIB_INCLUDE_DIRS})
:: endif()
:: we have to set the ZLIB_ROOT so that it is prepended to the search list
:: we have to set ZLIB_LIBRARY NOT ZLIB_LIBRARY_RELEASE in order to override the FindZlib module
:: we also should set ZLIB_USE_STATIC_LIBS since we want to force static builds
:: https://github.com/Kitware/CMake/blob/a6853135f569f0b040a34374a15a8361bb73901b/Modules/FindZLIB.cmake#L98C4-L98C13
set wild_zlib_32=-DZLIB_USE_STATIC_LIBS=ON "-DZLIB_ROOT=%~dp0%deps_dir%\zlib\install32" "-DZLIB_INCLUDE_DIR=%~dp0%deps_dir%\zlib\install32\include" "-DZLIB_LIBRARY=%~dp0%deps_dir%\zlib\install32\lib\zlibstatic.lib"
set wild_zlib_64=-DZLIB_USE_STATIC_LIBS=ON "-DZLIB_ROOT=%~dp0%deps_dir%\zlib\install64" "-DZLIB_INCLUDE_DIR=%~dp0%deps_dir%\zlib\install64\include" "-DZLIB_LIBRARY=%~dp0%deps_dir%\zlib\install64\lib\zlibstatic.lib"
:: ############## build curl ##############
echo // building curl lib
pushd "%deps_dir%\curl"
:: CURL_STATICLIB
set "curl_common_defs=-DBUILD_CURL_EXE=OFF -DBUILD_SHARED_LIBS=OFF -DBUILD_STATIC_CURL=OFF -DBUILD_STATIC_LIBS=ON -DCURL_USE_OPENSSL=OFF -DCURL_ZLIB=ON -DENABLE_UNICODE=ON -DCURL_STATIC_CRT=ON"
setlocal
call "%~dp0build_win_set_env.bat" 32 || (
endlocal
popd
call :err_msg "Couldn't find Visual Studio or build tools - 32"
set /a last_code=1
goto :end_script
)
%recreate_32%
%cmake_gen32% %curl_common_defs% %wild_zlib_32% "-DCMAKE_SHARED_LINKER_FLAGS_INIT=%~dp0%deps_dir%\zlib\install32\lib\zlibstatic.lib" "-DCMAKE_MODULE_LINKER_FLAGS_INIT=%~dp0%deps_dir%\zlib\install32\lib\zlibstatic.lib" "-DCMAKE_EXE_LINKER_FLAGS_INIT=%~dp0%deps_dir%\zlib\install32\lib\zlibstatic.lib"
set /a _exit=%errorlevel%
%cmake_build32% --target install
set /a _exit+=%errorlevel%
%clean_gen32%
endlocal & set /a last_code+=%_exit%
setlocal
call "%~dp0build_win_set_env.bat" 64 || (
endlocal
popd
call :err_msg "Couldn't find Visual Studio or build tools - 64"
set /a last_code=1
goto :end_script
)
%recreate_64%
%cmake_gen64% %curl_common_defs% %wild_zlib_64% "-DCMAKE_SHARED_LINKER_FLAGS_INIT=%~dp0%deps_dir%\zlib\install64\lib\zlibstatic.lib" "-DCMAKE_MODULE_LINKER_FLAGS_INIT=%~dp0%deps_dir%\zlib\install64\lib\zlibstatic.lib" "-DCMAKE_EXE_LINKER_FLAGS_INIT=%~dp0%deps_dir%\zlib\install64\lib\zlibstatic.lib"
set /a _exit=%errorlevel%
%cmake_build64% --target install
set /a _exit+=%errorlevel%
%clean_gen64%
endlocal & set /a last_code+=%_exit%
popd
echo: & echo:
:: ############## build protobuf ##############
echo // building protobuf lib
pushd "%deps_dir%\protobuf"
set "proto_common_defs=-Dprotobuf_BUILD_TESTS=OFF -Dprotobuf_BUILD_SHARED_LIBS=OFF -Dprotobuf_WITH_ZLIB=ON"
setlocal
call "%~dp0build_win_set_env.bat" 32 || (
endlocal
popd
call :err_msg "Couldn't find Visual Studio or build tools - 32"
set /a last_code=1
goto :end_script
)
%recreate_32%
%cmake_gen32% %proto_common_defs% %wild_zlib_32% "-DCMAKE_SHARED_LINKER_FLAGS_INIT=%~dp0%deps_dir%\zlib\install32\lib\zlibstatic.lib" "-DCMAKE_MODULE_LINKER_FLAGS_INIT=%~dp0%deps_dir%\zlib\install32\lib\zlibstatic.lib" "-DCMAKE_EXE_LINKER_FLAGS_INIT=%~dp0%deps_dir%\zlib\install32\lib\zlibstatic.lib"
set /a _exit=%errorlevel%
%cmake_build32% --target install
set /a _exit+=%errorlevel%
%clean_gen32%
endlocal & set /a last_code+=%_exit%
setlocal
call "%~dp0build_win_set_env.bat" 64 || (
endlocal
popd
call :err_msg "Couldn't find Visual Studio or build tools - 64"
set /a last_code=1
goto :end_script
)
%recreate_64%
%cmake_gen64% %proto_common_defs% %wild_zlib_64% "-DCMAKE_SHARED_LINKER_FLAGS_INIT=%~dp0%deps_dir%\zlib\install64\lib\zlibstatic.lib" "-DCMAKE_MODULE_LINKER_FLAGS_INIT=%~dp0%deps_dir%\zlib\install64\lib\zlibstatic.lib" "-DCMAKE_EXE_LINKER_FLAGS_INIT=%~dp0%deps_dir%\zlib\install64\lib\zlibstatic.lib"
set /a _exit=%errorlevel%
%cmake_build64% --target install
set /a _exit+=%errorlevel%
%clean_gen64%
endlocal & set /a last_code+=%_exit%
popd
echo: & echo:
:: ############## build mbedtls ##############
echo // building mbedtls lib
pushd "%deps_dir%\mbedtls"
set "mbedtls_common_defs=-DUSE_STATIC_MBEDTLS_LIBRARY=ON -DUSE_SHARED_MBEDTLS_LIBRARY=OFF -DMSVC_STATIC_RUNTIME=ON -DENABLE_TESTING=OFF -DENABLE_PROGRAMS=OFF"
setlocal
call "%~dp0build_win_set_env.bat" 32 || (
endlocal
popd
call :err_msg "Couldn't find Visual Studio or build tools - 32"
set /a last_code=1
goto :end_script
)
%recreate_32%
%cmake_gen32% %mbedtls_common_defs%
set /a _exit=%errorlevel%
%cmake_build32% --target install
set /a _exit+=%errorlevel%
%clean_gen32%
endlocal & set /a last_code+=%_exit%
setlocal
call "%~dp0build_win_set_env.bat" 64 || (
endlocal
popd
call :err_msg "Couldn't find Visual Studio or build tools - 64"
set /a last_code=1
goto :end_script
)
%recreate_64%
%cmake_gen64% %mbedtls_common_defs%
set /a _exit=%errorlevel%
%cmake_build64% --target install
set /a _exit+=%errorlevel%
%clean_gen64%
endlocal & set /a last_code+=%_exit%
popd
echo: & echo:
goto :end_script
:extract_all_deps
set /a list=-1
for /f "tokens=1 delims=:" %%A in ('findstr /B /L /N /C:"deps_to_extract=[" "%~f0" 2^>nul') do (
set /a "list=%%~A"
)
if "%list%"=="-1" (
call :err_msg "Couldn't find list of tools to extract inside thif batch script"
exit /b 1
)
echo // Recreating dir...
rmdir /s /q "%deps_dir%"
mkdir "%deps_dir%"
for /f "usebackq eol=; skip=%list% tokens=1,* delims=\" %%A in ("%~f0") do (
if "%%~A"=="]" (
goto :extract_all_deps_end
)
echo // Extracting archive "%%~B" to "%deps_dir%\%%~A"
if not exist "%third_party_common_dir%\%%~A\%%~B" (
call :err_msg "File not found"
exit /b 1
)
for /f "usebackq tokens=* delims=" %%Z in ('"%%~nB"') do (
if /i "%%~xZ%%~xB"==".tar.gz" (
"%extractor%" x "%third_party_common_dir%\%%~A\%%~B" -so | "%extractor%" x -si -ttar -y -aoa -o"%deps_dir%\%%~A" || (
call :err_msg "Extraction failed"
exit /b 1
)
) else (
"%extractor%" x "%third_party_common_dir%\%%~A\%%~B" -y -aoa -o"%deps_dir%\%%~A" || (
call :err_msg "Extraction failed"
exit /b 1
)
)
)
for /f "tokens=* delims=" %%C in ('dir /b /a:d "%deps_dir%\%%~A\" 2^>nul') do (
echo // Flattening dir "%deps_dir%\%%~A\%%~C" by moving everything inside it to "%deps_dir%\%%~A"
robocopy /E /MOVE /MT4 /NS /NC /NFL /NDL /NP /NJH /NJS "%deps_dir%\%%~A\%%~C" "%deps_dir%\%%~A"
if ERRORLEVEL 8 (
call :err_msg "Failed to flatten dir of dep"
exit /b 1
)
if exist "%deps_dir%\%%~A\%%~C" (
echo // Removing nested dir "%deps_dir%\%%~A\%%~C"
rmdir /s /q "%deps_dir%\%%~A\%%~C"
)
)
)
:extract_all_deps_end
exit /b 0
:err_msg
1>&2 echo [X] %~1
exit /b
:get_parallel_threads_count
for /f "tokens=* delims=" %%A in ('echo %~1^| findstr /B /R /X "^[0-9][0-9]*$" 2^>nul') do (
set /a PARALLEL_THREADS_OVERRIDE=%~1
rem echo [?] Overriding parralel build jobs count with %~1
exit /b 0
)
exit /b 1
:end_script
echo:
if %last_code% equ 0 (
echo [GG] no failures
) else (
1>&2 echo [XX] general failure
)
popd
endlocal & (
exit /b %last_code%
)
deps_to_extract=[
libssq\libssq.tar.gz
zlib\zlib.tar.gz
curl\curl.tar.gz
protobuf\protobuf.tar.gz
mbedtls\mbedtls.tar.gz
]