1 :: --------------------------------------------------
2 :: Run full BTLS build using msvc toolchain and available cmake generator.
3 :: Script needs to be run from within a matching build environment, x86|x64.
4 :: When executed from withing Visual Studio build environment current
5 :: build environment will be inherited by script.
7 :: %1 Mono BTLS source root directory.
8 :: %2 BTLS source root directory.
9 :: %3 BTLS build root directory.
10 :: %4 Mono distribution root directory.
12 :: %6 VS platform (Win32/x64)
13 :: %7 VS configuration (Debug/Release)
15 :: %9 VS PlatformToolSet, if used.
16 :: %10 Win SDK, if used.
17 :: %11 MsBuild bin path, if used.
18 :: %12 Force MSBuild (true/false), if used.
19 :: --------------------------------------------------
26 set CL_BIN_NAME=cl.exe
27 set LINK_BIN_NAME=link.exe
28 set GIT_BIN_NAME=git.exe
29 set CMAKE_BIN_NAME=cmake.exe
30 set NINJA_BIN_NAME=ninja.exe
31 set PERL_BIN_NAME=perl.exe
32 set YASM_BIN_NAME=yasm.exe
38 set BTLS_BUILD_DIR=%~1
46 set VS_CONFIGURATION=%~1
50 set VS_PLATFORM_TOOL_SET=%~1
52 set VS_WIN_SDK_VERSION=%~1
54 set MSBUILD_BIN_PATH=%~1
62 set MSBUILD=%MSBUILD_BIN_PATH%msbuild.exe
64 if "%MONO_BTLS_DIR%" == "" (
65 echo Missing mono BTLS source directory argument.
69 if "%BTLS_DIR%" == "" (
70 echo Missing BTLS source directory argument.
74 if "%BTLS_BUILD_DIR%" == "" (
75 echo Missing BTLS build directory argument.
79 if "%MONO_DIST_DIR%" == "" (
80 echo Missing mono install directory argument.
84 if "%VS_CFLAGS%" == "" (
85 echo Missing CFLAGS argument.
89 if "%VS_PLATFORM%" == "" (
93 if "%VS_CONFIGURATION%" == "" (
94 set VS_CONFIGURATION=Release
97 if "%VS_TARGET%" == "" (
101 if "%VS_PLATFORM_TOOL_SET%" == "" (
102 set VS_PLATFORM_TOOL_SET=v142
105 if "%VS_WIN_SDK_VERSION%" == "" (
106 set VS_WIN_SDK_VERSION=10.0
109 if "%FORCE_MSBUILD%" == "" (
110 set FORCE_MSBUILD=false
113 set BTLS_CFLAGS=%VS_CFLAGS%
115 if /i "%VS_PLATFORM%" == "win32" (
119 set BTLS_NO_ASM_SUPPORT=1
121 :: VS2017/VS2019 includes vswhere.exe that can be used to locate current VS installation.
122 set VSWHERE_TOOLS_BIN=%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe
123 set VS_COMMON_EXTENSION_TOOLS_PATHS=
125 :: Check if executed from VS2015/VS2017/VS2019 build environment.
126 if "%VisualStudioVersion%" == "14.0" (
127 if /i not "%VS_PLATFORM_TOOL_SET%" == "v140" (
128 echo VisualStudioVersion/PlatformToolchain missmatch, forcing msbuild.
129 set FORCE_MSBUILD=true
134 if "%VisualStudioVersion%" == "15.0" (
135 if /i not "%VS_PLATFORM_TOOL_SET%" == "v141" (
136 echo VisualStudioVersion/PlatformToolchain missmatch, forcing msbuild.
137 set FORCE_MSBUILD=true
142 if "%VisualStudioVersion%" == "16.0" (
143 if /i not "%VS_PLATFORM_TOOL_SET%" == "v142" (
144 echo VisualStudioVersion/PlatformToolchain missmatch, forcing msbuild.
145 set FORCE_MSBUILD=true
150 :: Executed outside VS2015/VS2017/VS2019 build environment, try to locate Visual Studio C/C++ compiler and linker.
151 call :FIND_PROGRAM "" "%CL_BIN_NAME%" CL_PATH
152 if "%CL_PATH%" == "" (
156 call :FIND_PROGRAM "" "%LINK_BIN_NAME%" LINK_PATH
157 if "%LINK_PATH%" == "" (
166 if exist "%VSWHERE_TOOLS_BIN%" (
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.
168 for /f "tokens=*" %%a IN ('"%VSWHERE_TOOLS_BIN%" -version [16.0^,17.0] -prerelease -property installationPath') do (
169 echo Setup a "x86|x64 Native Tools Command Prompt for VS2019" command prompt by using "%%a\VC\Auxiliary\Build\vcvars32.bat|vcvars64.bat".
170 goto ON_ENV_WARNING_DONE
175 if exist "%VSWHERE_TOOLS_BIN%" (
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.
177 for /f "tokens=*" %%a IN ('"%VSWHERE_TOOLS_BIN%" -version [15.0^,16.0] -property installationPath') do (
178 echo Setup a "x86|x64 Native Tools Command Prompt for VS2017" command prompt by using "%%a\VC\Auxiliary\Build\vcvars32.bat|vcvars64.bat".
179 goto ON_ENV_WARNING_DONE
184 set VC_VARS_ALL_FILE=%ProgramFiles(x86)%\Microsoft Visual Studio 14.0\VC\vcvarsall.bat
185 IF EXIST "%VC_VARS_ALL_FILE%" (
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.
187 echo Setup a "VS2015 x86|x64 Native Tools Command Prompt" command prompt by using "%VC_VARS_ALL_FILE% x86|amd64".
188 goto ON_ENV_WARNING_DONE
193 echo Could not detect Visual Studio build environment. You may experience build problems if wrong toolchain is auto detected.
197 :: Append paths to VS installed common extension tools at end of PATH (Vs2017/VS2019).
198 call :FIND_VS_COMMON_EXTENSION_TOOLS_PATHS VS_COMMON_EXTENSION_TOOLS_PATHS
199 if not "%VS_COMMON_EXTENSION_TOOLS_PATHS%" == "" (
200 set "PATH=%PATH%;%VS_COMMON_EXTENSION_TOOLS_PATHS%"
203 :: Setup all cmake related generator, tools and variables.
204 call :SETUP_CMAKE_ENVIRONMENT
206 echo Failed to located working %CMAKE_BIN_NAME%, needs to be accessible in PATH or set using CMAKE environment variable.
210 if "%CMAKE_GENERATOR%" == "" (
211 echo Failed to setup cmake generator.
216 if /i "%VS_TARGET%" == "build" (
220 if /i "%VS_TARGET%" == "install" (
224 if /i "%VS_TARGET%" == "clean" (
230 :: If not set by caller, check environment for working git.exe.
231 call :FIND_PROGRAM "%GIT%" "%GIT_BIN_NAME%" GIT
233 echo Failed to located working %GIT_BIN_NAME%, needs to be accessible in PATH or set using GIT environment variable.
237 :: Make sure boringssl submodule is up to date.
238 echo Updating submodule "%BTLS_DIR%"
239 "%GIT%" submodule update --init -- "%BTLS_DIR%"
240 if not ERRORLEVEL == 0 (
241 "%GIT%" submodule init -- "%BTLS_DIR%"
242 "%GIT%" submodule update -- "%BTLS_DIR%"
243 if not ERRORLEVEL == 0 (
244 echo Git boringssl submodules failed to updated. You may experience compilation problems if some submodules are out of date.
248 if not exist "%BTLS_DIR%" (
249 echo Could not find "%BTLS_DIR%".
253 if not exist "%BTLS_BUILD_DIR%" (
254 mkdir "%BTLS_BUILD_DIR%"
257 cd "%BTLS_BUILD_DIR%"
259 :: Make sure cmake pick up msvc toolchain regardless of selected generator (Visual Studio|Ninja)
261 set CXX=%CL_BIN_NAME%
264 set CMAKE_GENERATOR_TOOLSET=
265 if /i "%CMAKE_GENERATOR%" == "ninja" (
266 set BTLS_BUILD_TYPE=-D CMAKE_BUILD_TYPE=%VS_CONFIGURATION%
268 set CMAKE_GENERATOR_TOOLSET=%VS_PLATFORM_TOOL_SET%
271 if not "%CMAKE_GENERATOR_ARCH%" == "" (
272 set CMAKE_GENERATOR_ARCH=-A %CMAKE_GENERATOR_ARCH%
277 -D BTLS_ROOT:PATH="%BTLS_DIR%" ^
278 -D SRC_DIR:PATH="%MONO_BTLS_DIR%" ^
279 -D BTLS_CFLAGS="%BTLS_CFLAGS%" ^
280 -D OPENSSL_NO_ASM=%BTLS_NO_ASM_SUPPORT% ^
281 -D BTLS_ARCH="%BTLS_ARCH%" ^
282 -D BUILD_SHARED_LIBS=1 ^
283 -D CMAKE_SYSTEM_VERSION=%VS_WIN_SDK_VERSION% ^
284 %CMAKE_GENERATOR_TOOLSET% ^
286 -G "%CMAKE_GENERATOR%" ^
287 %CMAKE_GENERATOR_ARCH% ^
290 if not ERRORLEVEL == 0 (
294 if /i "%CMAKE_GENERATOR%" == "ninja" (
295 :: Build BTLS using ninja build system.
296 call "%NINJA%" -j4 || (
300 :: Build BTLS using msbuild build system.
301 call "%MSBUILD%" mono-btls.sln /p:Configuration=%VS_CONFIGURATION% /p:Platform=%VS_PLATFORM% /t:%VS_TARGET% /v:m /nologo /m || (
308 if not exist "%BTLS_BUILD_OUTPUT_DIR%\libmono-btls-shared.dll" (
309 echo Missing btls build output, "%BTLS_BUILD_OUTPUT_DIR%\libmono-btls-shared.dll"
313 :: Make sure build output folder exists.
314 if not exist "%MONO_DIST_DIR%" (
315 echo Could not find "%MONO_DIST_DIR%", creating folder for build output.
316 mkdir "%MONO_DIST_DIR%"
319 :: Copy files into distribution directory.
320 copy /Y "%BTLS_BUILD_OUTPUT_DIR%\libmono-btls-shared.dll" "%MONO_DIST_DIR%" >nul 2>&1
322 if exist "%BTLS_BUILD_OUTPUT_DIR%\libmono-btls-shared.pdb" (
323 copy /Y "%BTLS_BUILD_OUTPUT_DIR%\libmono-btls-shared.pdb" "%MONO_DIST_DIR%" >nul 2>&1
330 if exist "%BTLS_BUILD_DIR%\build.ninja" (
332 cd "%BTLS_BUILD_DIR%"
337 if exist "%BTLS_BUILD_DIR%\mono-btls.sln" (
338 "%MSBUILD%" "%BTLS_BUILD_DIR%\mono-btls.sln" /p:Configuration=%VS_CONFIGURATION% /p:Platform=%VS_PLATFORM% /t:Clean /v:m /nologo
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].
352 echo Failed to build BTLS.
356 exit /b %BUILD_RESULT%
358 :: ##############################################################################################################################
361 :: --------------------------------------------------
362 :: Locates PATHS to installed common extension tools.
363 :: %1 Output, variable including paths.
364 :: --------------------------------------------------
365 :FIND_VS_COMMON_EXTENSION_TOOLS_PATHS
367 set VS_COMMON_EXTENSION_PATH=
368 if exist "%VSWHERE_TOOLS_BIN%" (
369 for /f "tokens=*" %%a in ('"%VSWHERE_TOOLS_BIN%" -version [16.0^,17.0] -property installationPath') do (
370 set VS_COMMON_EXTENSION_PATH=%%a\Common7\IDE\CommonExtensions\Microsoft
374 if exist "%VS_COMMON_EXTENSION_PATH%" (
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"
380 :: --------------------------------------------------
381 :: Finds a program using environment.
383 :: %1 Existing program to check for.
384 :: %2 Name of binary to locate.
385 :: %3 Output, variable to set if found requested program.
386 :: --------------------------------------------------
389 :: If not set by caller, check environment for program.
394 call where /q "%~2" && (
395 for /f "delims=" %%a in ('where "%~2"') do (
404 :: --------------------------------------------------
405 :: Setup up cmake build environment, including generator, build tools and variables.
406 :: --------------------------------------------------
407 :SETUP_CMAKE_ENVIRONMENT
409 :: If not set by caller, check environment for working cmake.exe.
410 call :FIND_PROGRAM "%CMAKE%" "%CMAKE_BIN_NAME%" CMAKE
412 goto _SETUP_CMAKE_ENVIRONMENT_EXIT
415 if /i "%VS_TARGET%" == "build" (
416 echo Found CMake: "%CMAKE%"
419 if /i "%FORCE_MSBUILD%" == "true" (
420 goto _SETUP_CMAKE_ENVIRONMENT_VS_GENERATOR
423 :: Check for optional cmake generate and build tools for full BTLS assembler supported build. NOTE, currently BTLS assembler build
424 :: can't be done using Visual Studio and must use ninja build generator + yasm and perl.
425 call :FIND_PROGRAM "%NINJA%" "%NINJA_BIN_NAME%" NINJA
426 call :FIND_PROGRAM "%YASM%" "%YASM_BIN_NAME%" YASM
427 call :FIND_PROGRAM "%PERL%" "%PERL_BIN_NAME%" PERL
429 if not "%NINJA%" == "" if not "%YASM%" == "" if not "%PERL%" == "" (
430 goto _SETUP_CMAKE_ENVIRONMENT_NINJA_GENERATOR
433 :_SETUP_CMAKE_ENVIRONMENT_VS_GENERATOR
435 if /i "%VS_TARGET%" == "build" (
436 echo Using Visual Studio build generator, disabling full assembler build.
439 set CMAKE_GENERATOR_ARCH=
441 :: Detect VS platform tool set to use matching cmake generator.
442 if /i "%VS_PLATFORM_TOOL_SET%" == "v140" (
443 if /i "%VS_PLATFORM%" == "x64" (
444 set CMAKE_GENERATOR=Visual Studio 14 2015 Win64
446 set CMAKE_GENERATOR=Visual Studio 14 2015
450 if /i "%VS_PLATFORM_TOOL_SET%" == "v141" (
451 if /i "%VS_PLATFORM%" == "x64" (
452 set CMAKE_GENERATOR=Visual Studio 15 2017 Win64
454 set CMAKE_GENERATOR=Visual Studio 15 2017
458 if /i "%VS_PLATFORM_TOOL_SET%" == "v142" (
459 set CMAKE_GENERATOR=Visual Studio 16 2019
460 if /i "%VS_PLATFORM%" == "x64" (
461 set CMAKE_GENERATOR_ARCH=x64
463 set CMAKE_GENERATOR_ARCH=Win32
467 set BTLS_BUILD_OUTPUT_DIR=%BTLS_BUILD_DIR%\%VS_CONFIGURATION%
469 goto _SETUP_CMAKE_ENVIRONMENT_EXIT
471 :_SETUP_CMAKE_ENVIRONMENT_NINJA_GENERATOR
473 if /i "%VS_TARGET%" == "build" (
474 echo Found Ninja: "%NINJA%"
475 echo Using Ninja build generator, enabling full assembler build.
478 set CMAKE_GENERATOR_ARCH=
479 set CMAKE_GENERATOR=Ninja
480 set BTLS_BUILD_OUTPUT_DIR=%BTLS_BUILD_DIR%
481 set BTLS_NO_ASM_SUPPORT=0
483 :_SETUP_CMAKE_ENVIRONMENT_EXIT