Contributing: fixing some typos (#18453)
[mono-project.git] / msvc / build-external-btls.bat
blob479a3d5653575f5a16d33df8a986c028308770d2
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.
6 ::
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.
11 :: %5 VS CFLAGS.
12 :: %6 VS platform (Win32/x64)
13 :: %7 VS configuration (Debug/Release)
14 :: %8 VS target
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 :: --------------------------------------------------
21 @echo off
22 setlocal
24 set BUILD_RESULT=1
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
34 set MONO_BTLS_DIR=%~1
35 shift
36 set BTLS_DIR=%~1
37 shift
38 set BTLS_BUILD_DIR=%~1
39 shift
40 set MONO_DIST_DIR=%~1
41 shift
42 set VS_CFLAGS=%~1
43 shift
44 set VS_PLATFORM=%~1
45 shift
46 set VS_CONFIGURATION=%~1
47 shift
48 set VS_TARGET=%~1
49 shift
50 set VS_PLATFORM_TOOL_SET=%~1
51 shift
52 set VS_WIN_SDK_VERSION=%~1
53 shift
54 set MSBUILD_BIN_PATH=%~1
55 shift
56 set FORCE_MSBUILD=%~1
58 :: Setup toolchain.
59 :: set GIT=
60 :: set CMAKE=
61 :: set NINJA=
62 set MSBUILD=%MSBUILD_BIN_PATH%msbuild.exe
64 if "%MONO_BTLS_DIR%" == "" (
65     echo Missing mono BTLS source directory argument.
66     goto ECHO_USAGE
69 if "%BTLS_DIR%" == "" (
70     echo Missing BTLS source directory argument.
71     goto ECHO_USAGE
74 if "%BTLS_BUILD_DIR%" == "" (
75     echo Missing BTLS build directory argument.
76     goto ECHO_USAGE
79 if "%MONO_DIST_DIR%" == "" (
80     echo Missing mono install directory argument.
81     goto ECHO_USAGE
84 if "%VS_CFLAGS%" == "" (
85     echo Missing CFLAGS argument.
86     goto ECHO_USAGE
89 if "%VS_PLATFORM%" == "" (
90     set VS_PLATFORM=x64
93 if "%VS_CONFIGURATION%" == "" (
94     set VS_CONFIGURATION=Release
97 if "%VS_TARGET%" == "" (
98     set VS_TARGET=Build
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%
114 set BTLS_ARCH=x86_64
115 if /i "%VS_PLATFORM%" == "win32" (
116     set BTLS_ARCH=i386
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
130     )
131     goto ON_ENV_OK
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
138     )
139     goto ON_ENV_OK
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
146     )
147     goto ON_ENV_OK
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%" == "" (
153     goto ON_ENV_WARNING
156 call :FIND_PROGRAM "" "%LINK_BIN_NAME%" LINK_PATH
157 if "%LINK_PATH%" == "" (
158     goto ON_ENV_WARNING
161 goto ON_ENV_OK
163 :ON_ENV_WARNING
165 :: VS 2019.
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
171     )
174 :: VS 2017.
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
180     )
183 :: VS 2015.
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
191 :ON_ENV_WARNING_DONE
193 echo Could not detect Visual Studio build environment. You may experience build problems if wrong toolchain is auto detected.
195 :ON_ENV_OK
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
205 if "%CMAKE%" == "" (
206     echo Failed to located working %CMAKE_BIN_NAME%, needs to be accessible in PATH or set using CMAKE environment variable.
207     goto ON_ERROR
210 if "%CMAKE_GENERATOR%" == "" (
211     echo Failed to setup cmake generator.
212     goto ON_ERROR
215 :: Check target.
216 if /i "%VS_TARGET%" == "build" (
217     goto ON_BUILD_BTLS
220 if /i "%VS_TARGET%" == "install" (
221     goto ON_INSTALL_BTLS
224 if /i "%VS_TARGET%" == "clean" (
225     goto ON_CLEAN_BTLS
228 :ON_BUILD_BTLS
230 :: If not set by caller, check environment for working git.exe.
231 call :FIND_PROGRAM "%GIT%" "%GIT_BIN_NAME%" GIT
232 if "%GIT%" == "" (
233     echo Failed to located working %GIT_BIN_NAME%, needs to be accessible in PATH or set using GIT environment variable.
234     goto ON_ERROR
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.
245     )
248 if not exist "%BTLS_DIR%" (
249     echo Could not find "%BTLS_DIR%".
250     goto ON_ERROR
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)
260 set CC=%CL_BIN_NAME%
261 set CXX=%CL_BIN_NAME%
263 set BTLS_BUILD_TYPE=
264 set CMAKE_GENERATOR_TOOLSET=
265 if /i "%CMAKE_GENERATOR%" == "ninja" (
266     set BTLS_BUILD_TYPE=-D CMAKE_BUILD_TYPE=%VS_CONFIGURATION%
267 ) else (
268     set CMAKE_GENERATOR_TOOLSET=%VS_PLATFORM_TOOL_SET%
271 if not "%CMAKE_GENERATOR_ARCH%" == "" (
272     set CMAKE_GENERATOR_ARCH=-A %CMAKE_GENERATOR_ARCH%
275 :: Run cmake.
276 "%CMAKE%" ^
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% ^
285 %BTLS_BUILD_TYPE% ^
286 -G "%CMAKE_GENERATOR%" ^
287 %CMAKE_GENERATOR_ARCH% ^
288 "%MONO_BTLS_DIR%"
290 if not ERRORLEVEL == 0 (
291     goto ON_ERROR
294 if /i "%CMAKE_GENERATOR%" == "ninja" (
295     :: Build BTLS using ninja build system.
296     call "%NINJA%" -j4 || (
297         goto ON_ERROR
298     )
299 ) else (
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 || (
302         goto ON_ERROR
303     )
306 :ON_INSTALL_BTLS
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"
310     goto ON_ERROR
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
326 goto ON_SUCCESS
328 :ON_CLEAN_BTLS
330 if exist "%BTLS_BUILD_DIR%\build.ninja" (
331     pushd
332     cd "%BTLS_BUILD_DIR%"
333     call "%NINJA%" clean
334     popd
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
341 goto ON_SUCCESS
343 :ON_SUCCESS
345 set BUILD_RESULT=0
346 goto ON_EXIT
348 :ECHO_USAGE:
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].
351 :ON_ERROR
352     echo Failed to build BTLS.
353     goto ON_EXIT
355 :ON_EXIT
356     exit /b %BUILD_RESULT%
358 :: ##############################################################################################################################
359 :: Functions
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
371     )
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"
378 goto :EOF
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 :: --------------------------------------------------
387 :FIND_PROGRAM
389 :: If not set by caller, check environment for program.
390 if exist "%~1" (
391     goto :EOF
394 call where /q "%~2" && (
395     for /f "delims=" %%a in ('where "%~2"') do (
396         set "%~3=%%a"
397     )
398 ) || (
399     set "%~3="
402 goto :EOF
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
411 if "%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
445     ) else (
446         set CMAKE_GENERATOR=Visual Studio 14 2015
447     )
450 if /i "%VS_PLATFORM_TOOL_SET%" == "v141" (
451     if /i "%VS_PLATFORM%" == "x64" (
452         set CMAKE_GENERATOR=Visual Studio 15 2017 Win64
453     ) else (
454         set CMAKE_GENERATOR=Visual Studio 15 2017
455     )
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
462     ) else (
463         set CMAKE_GENERATOR_ARCH=Win32
464     )
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
485 goto :EOF
487 @echo on