Move offsets-tool into mono/tools (#18830)
[mono-project.git] / msvc / build-external-btls.bat
blob216134cd3b503cb87b7c192b77b9dce35db0689e
1 :: --------------------------------------------------\r
2 :: Run full BTLS build using msvc toolchain and available cmake generator.\r
3 :: Script needs to be run from within a matching build environment, x86|x64.\r
4 :: When executed from withing Visual Studio build environment current\r
5 :: build environment will be inherited by script.\r
6 ::\r
7 :: %1 Mono BTLS source root directory.\r
8 :: %2 BTLS source root directory.\r
9 :: %3 BTLS build root directory.\r
10 :: %4 Mono distribution root directory.\r
11 :: %5 VS CFLAGS.\r
12 :: %6 VS platform (Win32/x64)\r
13 :: %7 VS configuration (Debug/Release)\r
14 :: %8 VS target\r
15 :: %9 VS PlatformToolSet, if used.\r
16 :: %10 Win SDK, if used.\r
17 :: %11 MsBuild bin path, if used.\r
18 :: %12 Force MSBuild (true/false), if used.\r
19 :: --------------------------------------------------\r
21 @echo off\r
22 setlocal\r
24 set BUILD_RESULT=1\r
26 set CL_BIN_NAME=cl.exe\r
27 set LINK_BIN_NAME=link.exe\r
28 set GIT_BIN_NAME=git.exe\r
29 set CMAKE_BIN_NAME=cmake.exe\r
30 set NINJA_BIN_NAME=ninja.exe\r
31 set PERL_BIN_NAME=perl.exe\r
32 set YASM_BIN_NAME=yasm.exe\r
34 set MONO_BTLS_DIR=%~1\r
35 shift\r
36 set BTLS_DIR=%~1\r
37 shift\r
38 set BTLS_BUILD_DIR=%~1\r
39 shift\r
40 set MONO_DIST_DIR=%~1\r
41 shift\r
42 set VS_CFLAGS=%~1\r
43 shift\r
44 set VS_PLATFORM=%~1\r
45 shift\r
46 set VS_CONFIGURATION=%~1\r
47 shift\r
48 set VS_TARGET=%~1\r
49 shift\r
50 set VS_PLATFORM_TOOL_SET=%~1\r
51 shift\r
52 set VS_WIN_SDK_VERSION=%~1\r
53 shift\r
54 set MSBUILD_BIN_PATH=%~1\r
55 shift\r
56 set FORCE_MSBUILD=%~1\r
58 :: Setup toolchain.\r
59 :: set GIT=\r
60 :: set CMAKE=\r
61 :: set NINJA=\r
62 set MSBUILD=%MSBUILD_BIN_PATH%msbuild.exe\r
64 if "%MONO_BTLS_DIR%" == "" (\r
65     echo Missing mono BTLS source directory argument.\r
66     goto ECHO_USAGE\r
67 )\r
69 if "%BTLS_DIR%" == "" (\r
70     echo Missing BTLS source directory argument.\r
71     goto ECHO_USAGE\r
72 )\r
74 if "%BTLS_BUILD_DIR%" == "" (\r
75     echo Missing BTLS build directory argument.\r
76     goto ECHO_USAGE\r
77 )\r
79 if "%MONO_DIST_DIR%" == "" (\r
80     echo Missing mono install directory argument.\r
81     goto ECHO_USAGE\r
82 )\r
84 if "%VS_CFLAGS%" == "" (\r
85     echo Missing CFLAGS argument.\r
86     goto ECHO_USAGE\r
87 )\r
89 if "%VS_PLATFORM%" == "" (\r
90     set VS_PLATFORM=x64\r
91 )\r
93 if "%VS_CONFIGURATION%" == "" (\r
94     set VS_CONFIGURATION=Release\r
95 )\r
97 if "%VS_TARGET%" == "" (\r
98     set VS_TARGET=Build\r
99 )\r
101 if "%VS_PLATFORM_TOOL_SET%" == "" (\r
102     set VS_PLATFORM_TOOL_SET=v142\r
105 if "%VS_WIN_SDK_VERSION%" == "" (\r
106     set VS_WIN_SDK_VERSION=10.0\r
109 if "%FORCE_MSBUILD%" == "" (\r
110     set FORCE_MSBUILD=false\r
113 set BTLS_CFLAGS=%VS_CFLAGS%\r
114 set BTLS_ARCH=x86_64\r
115 if /i "%VS_PLATFORM%" == "win32" (\r
116     set BTLS_ARCH=i386\r
119 set BTLS_NO_ASM_SUPPORT=1\r
121 :: VS2017/VS2019 includes vswhere.exe that can be used to locate current VS installation.\r
122 set VSWHERE_TOOLS_BIN=%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe\r
123 set VS_COMMON_EXTENSION_TOOLS_PATHS=\r
125 :: Check if executed from VS2015/VS2017/VS2019 build environment.\r
126 if "%VisualStudioVersion%" == "14.0" (\r
127     if /i not "%VS_PLATFORM_TOOL_SET%" == "v140" (\r
128         echo VisualStudioVersion/PlatformToolchain missmatch, forcing msbuild.\r
129         set FORCE_MSBUILD=true\r
130     )\r
131     goto ON_ENV_OK\r
134 if "%VisualStudioVersion%" == "15.0" (\r
135     if /i not "%VS_PLATFORM_TOOL_SET%" == "v141" (\r
136         echo VisualStudioVersion/PlatformToolchain missmatch, forcing msbuild.\r
137         set FORCE_MSBUILD=true\r
138     )\r
139     goto ON_ENV_OK\r
142 if "%VisualStudioVersion%" == "16.0" (\r
143     if /i not "%VS_PLATFORM_TOOL_SET%" == "v142" (\r
144         echo VisualStudioVersion/PlatformToolchain missmatch, forcing msbuild.\r
145         set FORCE_MSBUILD=true\r
146     )\r
147     goto ON_ENV_OK\r
150 :: Executed outside VS2015/VS2017/VS2019 build environment, try to locate Visual Studio C/C++ compiler and linker.\r
151 call :FIND_PROGRAM "" "%CL_BIN_NAME%" CL_PATH\r
152 if "%CL_PATH%" == "" (\r
153     goto ON_ENV_WARNING\r
156 call :FIND_PROGRAM "" "%LINK_BIN_NAME%" LINK_PATH\r
157 if "%LINK_PATH%" == "" (\r
158     goto ON_ENV_WARNING\r
161 goto ON_ENV_OK\r
163 :ON_ENV_WARNING\r
165 :: VS 2019.\r
166 if exist "%VSWHERE_TOOLS_BIN%" (\r
167     echo For VS2019 builds, make sure to run this from within Visual Studio build or using "x86|x64 Native Tools Command Prompt for VS2019" command prompt.\r
168     for /f "tokens=*" %%a IN ('"%VSWHERE_TOOLS_BIN%" -version [16.0^,17.0] -property installationPath') do (\r
169         echo Setup a "x86|x64 Native Tools Command Prompt for VS2019" command prompt by using "%%a\VC\Auxiliary\Build\vcvars32.bat|vcvars64.bat".\r
170         goto ON_ENV_WARNING_DONE\r
171     )\r
174 :: VS 2017.\r
175 if exist "%VSWHERE_TOOLS_BIN%" (\r
176     echo For VS2017 builds, make sure to run this from within Visual Studio build or using "x86|x64 Native Tools Command Prompt for VS2017" command prompt.\r
177     for /f "tokens=*" %%a IN ('"%VSWHERE_TOOLS_BIN%" -version [15.0^,16.0] -property installationPath') do (\r
178         echo Setup a "x86|x64 Native Tools Command Prompt for VS2017" command prompt by using "%%a\VC\Auxiliary\Build\vcvars32.bat|vcvars64.bat".\r
179         goto ON_ENV_WARNING_DONE\r
180     )\r
183 :: VS 2015.\r
184 set VC_VARS_ALL_FILE=%ProgramFiles(x86)%\Microsoft Visual Studio 14.0\VC\vcvarsall.bat\r
185 IF EXIST "%VC_VARS_ALL_FILE%" (\r
186     echo For VS2015 builds, make sure to run this from within Visual Studio build or using "VS2015 x86|x64 Native Tools Command Prompt" command prompt.\r
187     echo Setup a "VS2015 x86|x64 Native Tools Command Prompt" command prompt by using "%VC_VARS_ALL_FILE% x86|amd64".\r
188     goto ON_ENV_WARNING_DONE\r
191 :ON_ENV_WARNING_DONE\r
193 echo Could not detect Visual Studio build environment. You may experience build problems if wrong toolchain is auto detected.\r
195 :ON_ENV_OK\r
197 :: Append paths to VS installed common extension tools at end of PATH (Vs2017/VS2019).\r
198 call :FIND_VS_COMMON_EXTENSION_TOOLS_PATHS VS_COMMON_EXTENSION_TOOLS_PATHS\r
199 if not "%VS_COMMON_EXTENSION_TOOLS_PATHS%" == "" (\r
200     set "PATH=%PATH%;%VS_COMMON_EXTENSION_TOOLS_PATHS%"\r
203 :: Setup all cmake related generator, tools and variables.\r
204 call :SETUP_CMAKE_ENVIRONMENT\r
205 if "%CMAKE%" == "" (\r
206     echo Failed to located working %CMAKE_BIN_NAME%, needs to be accessible in PATH or set using CMAKE environment variable.\r
207     goto ON_ERROR\r
210 if "%CMAKE_GENERATOR%" == "" (\r
211     echo Failed to setup cmake generator.\r
212     goto ON_ERROR\r
215 :: Check target.\r
216 if /i "%VS_TARGET%" == "build" (\r
217     goto ON_BUILD_BTLS\r
220 if /i "%VS_TARGET%" == "install" (\r
221     goto ON_INSTALL_BTLS\r
224 if /i "%VS_TARGET%" == "clean" (\r
225     goto ON_CLEAN_BTLS\r
228 :ON_BUILD_BTLS\r
230 :: If not set by caller, check environment for working git.exe.\r
231 call :FIND_PROGRAM "%GIT%" "%GIT_BIN_NAME%" GIT\r
232 if "%GIT%" == "" (\r
233     echo Failed to located working %GIT_BIN_NAME%, needs to be accessible in PATH or set using GIT environment variable.\r
234     goto ON_ERROR\r
237 :: Make sure boringssl submodule is up to date.\r
238 echo Updating submodule "%BTLS_DIR%"
239 "%GIT%" submodule update --init -- "%BTLS_DIR%"\r
240 if not ERRORLEVEL == 0 (\r
241     "%GIT%" submodule init -- "%BTLS_DIR%"\r
242     "%GIT%" submodule update -- "%BTLS_DIR%"\r
243     if not ERRORLEVEL == 0 (\r
244         echo Git boringssl submodules failed to updated. You may experience compilation problems if some submodules are out of date.\r
245     )\r
248 if not exist "%BTLS_DIR%" (\r
249     echo Could not find "%BTLS_DIR%".\r
250     goto ON_ERROR\r
253 if not exist "%BTLS_BUILD_DIR%" (\r
254     mkdir "%BTLS_BUILD_DIR%"\r
257 cd "%BTLS_BUILD_DIR%"\r
259 :: Make sure cmake pick up msvc toolchain regardless of selected generator (Visual Studio|Ninja)\r
260 set CC=%CL_BIN_NAME%\r
261 set CXX=%CL_BIN_NAME%\r
263 set BTLS_BUILD_TYPE=\r
264 set CMAKE_GENERATOR_TOOLSET=\r
265 if /i "%CMAKE_GENERATOR%" == "ninja" (\r
266     set BTLS_BUILD_TYPE=-D CMAKE_BUILD_TYPE=%VS_CONFIGURATION%\r
267 ) else (\r
268     set CMAKE_GENERATOR_TOOLSET=%VS_PLATFORM_TOOL_SET%\r
271 if not "%CMAKE_GENERATOR_ARCH%" == "" (\r
272     set CMAKE_GENERATOR_ARCH=-A %CMAKE_GENERATOR_ARCH%\r
275 :: Run cmake.\r
276 "%CMAKE%" ^\r
277 -D BTLS_ROOT:PATH="%BTLS_DIR%" ^\r
278 -D SRC_DIR:PATH="%MONO_BTLS_DIR%" ^\r
279 -D BTLS_CFLAGS="%BTLS_CFLAGS%" ^\r
280 -D OPENSSL_NO_ASM=%BTLS_NO_ASM_SUPPORT% ^\r
281 -D BTLS_ARCH="%BTLS_ARCH%" ^\r
282 -D BUILD_SHARED_LIBS=1 ^\r
283 -D CMAKE_SYSTEM_VERSION=%VS_WIN_SDK_VERSION% ^\r
284 %CMAKE_GENERATOR_TOOLSET% ^\r
285 %BTLS_BUILD_TYPE% ^\r
286 -G "%CMAKE_GENERATOR%" ^\r
287 %CMAKE_GENERATOR_ARCH% ^\r
288 "%MONO_BTLS_DIR%"\r
290 if not ERRORLEVEL == 0 (\r
291     goto ON_ERROR\r
294 if /i "%CMAKE_GENERATOR%" == "ninja" (\r
295     :: Build BTLS using ninja build system.\r
296     call "%NINJA%" -j4 || (\r
297         goto ON_ERROR\r
298     )\r
299 ) else (\r
300     :: Build BTLS using msbuild build system.\r
301     call "%MSBUILD%" mono-btls.sln /p:Configuration=%VS_CONFIGURATION% /p:Platform=%VS_PLATFORM% /t:%VS_TARGET% /v:m /nologo /m || (\r
302         goto ON_ERROR\r
303     )\r
306 :ON_INSTALL_BTLS\r
308 if not exist "%BTLS_BUILD_OUTPUT_DIR%\libmono-btls-shared.dll" (\r
309     echo Missing btls build output, "%BTLS_BUILD_OUTPUT_DIR%\libmono-btls-shared.dll"\r
310     goto ON_ERROR\r
313 :: Make sure build output folder exists.\r
314 if not exist "%MONO_DIST_DIR%" (\r
315     echo Could not find "%MONO_DIST_DIR%", creating folder for build output.\r
316     mkdir "%MONO_DIST_DIR%"\r
319 :: Copy files into distribution directory.\r
320 copy /Y "%BTLS_BUILD_OUTPUT_DIR%\libmono-btls-shared.dll" "%MONO_DIST_DIR%" >nul 2>&1\r
322 if exist "%BTLS_BUILD_OUTPUT_DIR%\libmono-btls-shared.pdb" (\r
323     copy /Y "%BTLS_BUILD_OUTPUT_DIR%\libmono-btls-shared.pdb" "%MONO_DIST_DIR%" >nul 2>&1\r
326 goto ON_SUCCESS\r
328 :ON_CLEAN_BTLS\r
330 if exist "%BTLS_BUILD_DIR%\build.ninja" (\r
331     pushd\r
332     cd "%BTLS_BUILD_DIR%"\r
333     call "%NINJA%" clean\r
334     popd\r
337 if exist "%BTLS_BUILD_DIR%\mono-btls.sln" (\r
338     "%MSBUILD%" "%BTLS_BUILD_DIR%\mono-btls.sln" /p:Configuration=%VS_CONFIGURATION% /p:Platform=%VS_PLATFORM% /t:Clean /v:m /nologo\r
341 goto ON_SUCCESS\r
343 :ON_SUCCESS\r
345 set BUILD_RESULT=0\r
346 goto ON_EXIT\r
348 :ECHO_USAGE:\r
349     ECHO Usage: build-btls.bat [mono_btls_src_dir] [btls_src_dir] [btls_build_dir] [mono_dist_dir] [vs_cflags] [vs_plaform] [vs_configuration].\r
351 :ON_ERROR\r
352     echo Failed to build BTLS.\r
353     goto ON_EXIT\r
355 :ON_EXIT\r
356     exit /b %BUILD_RESULT%\r
358 :: ##############################################################################################################################\r
359 :: Functions\r
361 :: --------------------------------------------------\r
362 :: Locates PATHS to installed common extension tools.\r
363 :: %1 Output, variable including paths.\r
364 :: --------------------------------------------------\r
365 :FIND_VS_COMMON_EXTENSION_TOOLS_PATHS\r
367 set VS_COMMON_EXTENSION_PATH=\r
368 if exist "%VSWHERE_TOOLS_BIN%" (\r
369     for /f "tokens=*" %%a in ('"%VSWHERE_TOOLS_BIN%" -version [16.0^,17.0] -property installationPath') do (\r
370         set VS_COMMON_EXTENSION_PATH=%%a\Common7\IDE\CommonExtensions\Microsoft\r
371     )\r
374 if exist "%VS_COMMON_EXTENSION_PATH%" (\r
375     set "%~1=%VS_COMMON_EXTENSION_PATH%\TeamFoundation\Team Explorer\Git\cmd;%VS_COMMON_EXTENSION_PATH%\CMake\CMake\bin;%VS_COMMON_EXTENSION_PATH%\CMake\Ninja"\r
378 goto :EOF\r
380 :: --------------------------------------------------\r
381 :: Finds a program using environment.\r
382 ::\r
383 :: %1 Existing program to check for.\r
384 :: %2 Name of binary to locate.\r
385 :: %3 Output, variable to set if found requested program.\r
386 :: --------------------------------------------------\r
387 :FIND_PROGRAM\r
389 :: If not set by caller, check environment for program.\r
390 if exist "%~1" (\r
391     goto :EOF\r
394 call where /q "%~2" && (\r
395     for /f "delims=" %%a in ('where "%~2"') do (\r
396         set "%~3=%%a"\r
397     )\r
398 ) || (\r
399     set "%~3="\r
402 goto :EOF\r
404 :: --------------------------------------------------\r
405 :: Setup up cmake build environment, including generator, build tools and variables.\r
406 :: --------------------------------------------------\r
407 :SETUP_CMAKE_ENVIRONMENT\r
409 :: If not set by caller, check environment for working cmake.exe.\r
410 call :FIND_PROGRAM "%CMAKE%" "%CMAKE_BIN_NAME%" CMAKE\r
411 if "%CMAKE%" == "" (\r
412     goto _SETUP_CMAKE_ENVIRONMENT_EXIT\r
415 if /i "%VS_TARGET%" == "build" (\r
416     echo Found CMake: "%CMAKE%"\r
419 if /i "%FORCE_MSBUILD%" == "true" (\r
420     goto _SETUP_CMAKE_ENVIRONMENT_VS_GENERATOR\r
423 :: Check for optional cmake generate and build tools for full BTLS assembler supported build. NOTE, currently BTLS assembler build\r
424 :: can't be done using Visual Studio and must use ninja build generator + yasm and perl.\r
425 call :FIND_PROGRAM "%NINJA%" "%NINJA_BIN_NAME%" NINJA\r
426 call :FIND_PROGRAM "%YASM%" "%YASM_BIN_NAME%" YASM\r
427 call :FIND_PROGRAM "%PERL%" "%PERL_BIN_NAME%" PERL\r
429 if not "%NINJA%" == "" if not "%YASM%" == "" if not "%PERL%" == "" (\r
430     goto _SETUP_CMAKE_ENVIRONMENT_NINJA_GENERATOR\r
433 :_SETUP_CMAKE_ENVIRONMENT_VS_GENERATOR\r
435 if /i "%VS_TARGET%" == "build" (\r
436     echo Using Visual Studio build generator, disabling full assembler build.\r
439 set CMAKE_GENERATOR_ARCH=\r
441 :: Detect VS platform tool set to use matching cmake generator.\r
442 if /i "%VS_PLATFORM_TOOL_SET%" == "v140" (\r
443     if /i "%VS_PLATFORM%" == "x64" (\r
444         set CMAKE_GENERATOR=Visual Studio 14 2015 Win64\r
445     ) else (\r
446         set CMAKE_GENERATOR=Visual Studio 14 2015\r
447     )\r
450 if /i "%VS_PLATFORM_TOOL_SET%" == "v141" (\r
451     if /i "%VS_PLATFORM%" == "x64" (\r
452         set CMAKE_GENERATOR=Visual Studio 15 2017 Win64\r
453     ) else (\r
454         set CMAKE_GENERATOR=Visual Studio 15 2017\r
455     )\r
458 if /i "%VS_PLATFORM_TOOL_SET%" == "v142" (\r
459     set CMAKE_GENERATOR=Visual Studio 16 2019\r
460     if /i "%VS_PLATFORM%" == "x64" (\r
461         set CMAKE_GENERATOR_ARCH=x64\r
462     ) else (\r
463         set CMAKE_GENERATOR_ARCH=Win32\r
464     )\r
467 set BTLS_BUILD_OUTPUT_DIR=%BTLS_BUILD_DIR%\%VS_CONFIGURATION%\r
469 goto _SETUP_CMAKE_ENVIRONMENT_EXIT\r
471 :_SETUP_CMAKE_ENVIRONMENT_NINJA_GENERATOR\r
473 if /i "%VS_TARGET%" == "build" (\r
474     echo Found Ninja: "%NINJA%"\r
475     echo Using Ninja build generator, enabling full assembler build.\r
478 set CMAKE_GENERATOR_ARCH=\r
479 set CMAKE_GENERATOR=Ninja\r
480 set BTLS_BUILD_OUTPUT_DIR=%BTLS_BUILD_DIR%\r
481 set BTLS_NO_ASM_SUPPORT=0\r
483 :_SETUP_CMAKE_ENVIRONMENT_EXIT\r
485 goto :EOF\r
487 @echo on\r