1 :: --------------------------------------------------
\r
2 :: Run full LLVM 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
7 :: %1 LLVM source root directory.
\r
8 :: %2 LLVM build root directory.
\r
9 :: %3 LLVM install root directory.
\r
10 :: %4 Mono distribution root directory.
\r
12 :: %6 Additional CMake arguments.
\r
13 :: %7 VS platform (Win32/x64).
\r
14 :: %8 VS configuration (Debug/Release).
\r
16 :: %9 VS PlatformToolSet, if used.
\r
17 :: %10 Win SDK, if used.
\r
18 :: %11 MsBuild bin path, if used.
\r
19 :: %12 Force MSBuild (true/false), if used.
\r
20 :: --------------------------------------------------
\r
27 set BUILD_EXTERNAL_LLVM_SCRIPT_PATH=%~dp0
\r
29 set CL_BIN_NAME=cl.exe
\r
30 set LINK_BIN_NAME=link.exe
\r
31 set GIT_BIN_NAME=git.exe
\r
32 set CMAKE_BIN_NAME=cmake.exe
\r
33 set NINJA_BIN_NAME=ninja.exe
\r
34 set PYTHON_BIN_NAME=python.exe
\r
38 set LLVM_BUILD_DIR=%~1
\r
40 set LLVM_INSTALL_DIR=%~1
\r
42 set MONO_DIST_DIR=%~1
\r
46 set LLVM_ADDITIONAL_CMAKE_ARGS=%~1
\r
50 set VS_CONFIGURATION=%~1
\r
54 set VS_PLATFORM_TOOL_SET=%~1
\r
56 set VS_WIN_SDK_VERSION=%~1
\r
58 set MSBUILD_BIN_PATH=%~1
\r
60 set FORCE_MSBUILD=%~1
\r
66 set MSBUILD=%MSBUILD_BIN_PATH%msbuild.exe
\r
68 if "%LLVM_DIR%" == "" (
\r
69 echo Missing LLVM source directory argument.
\r
73 if "%LLVM_BUILD_DIR%" == "" (
\r
74 echo Missing LLVM build directory argument.
\r
78 if "%LLVM_INSTALL_DIR%" == "" (
\r
79 echo Missing LLVM install directory argument.
\r
83 if "%MONO_DIST_DIR%" == "" (
\r
84 echo Missing Mono dist directory argument.
\r
88 if "%VS_CFLAGS%" == "" (
\r
89 echo Missing CFLAGS argument.
\r
93 if "%VS_PLATFORM%" == "" (
\r
97 if "%VS_CONFIGURATION%" == "" (
\r
98 set VS_CONFIGURATION=Release
\r
101 if "%VS_TARGET%" == "" (
\r
102 set VS_TARGET=Build
\r
105 if "%VS_PLATFORM_TOOL_SET%" == "" (
\r
106 set VS_PLATFORM_TOOL_SET=v142
\r
109 if "%VS_WIN_SDK_VERSION%" == "" (
\r
110 set VS_WIN_SDK_VERSION=10.0
\r
113 if "%FORCE_MSBUILD%" == "" (
\r
114 set FORCE_MSBUILD=false
\r
117 set LLVM_CFLAGS=%VS_CFLAGS%
\r
118 set LLVM_ARCH=x86_64
\r
119 if /i "%VS_PLATFORM%" == "win32" (
\r
123 :: VS2017/VS2019 includes vswhere.exe that can be used to locate current VS installation.
\r
124 set VSWHERE_TOOLS_BIN=%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe
\r
125 set VS_COMMON_EXTENSION_TOOLS_PATHS=
\r
127 :: Check if executed from VS2015/VS2017/2019 build environment.
\r
128 if "%VisualStudioVersion%" == "14.0" (
\r
129 if /i not "%VS_PLATFORM_TOOL_SET%" == "v140" (
\r
130 echo VisualStudioVersion/PlatformToolchain missmatch, forcing msbuild.
\r
131 set FORCE_MSBUILD=true
\r
136 if "%VisualStudioVersion%" == "15.0" (
\r
137 if /i not "%VS_PLATFORM_TOOL_SET%" == "v141" (
\r
138 echo VisualStudioVersion/PlatformToolchain missmatch, forcing msbuild.
\r
139 set FORCE_MSBUILD=true
\r
144 if "%VisualStudioVersion%" == "16.0" (
\r
145 if /i not "%VS_PLATFORM_TOOL_SET%" == "v142" (
\r
146 echo VisualStudioVersion/PlatformToolchain missmatch, forcing msbuild.
\r
147 set FORCE_MSBUILD=true
\r
152 :: Executed outside VS2015/VS2017/VS2019 build environment, try to locate Visual Studio C/C++ compiler and linker.
\r
153 call :FIND_PROGRAM "" "%CL_BIN_NAME%" CL_PATH
\r
154 if "%CL_PATH%" == "" (
\r
155 goto ON_ENV_WARNING
\r
158 call :FIND_PROGRAM "" "%LINK_BIN_NAME%" LINK_PATH
\r
159 if "%LINK_PATH%" == "" (
\r
160 goto ON_ENV_WARNING
\r
167 set VSWHERE_TOOLS_BIN=%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe
\r
170 if exist "%VSWHERE_TOOLS_BIN%" (
\r
171 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
172 for /f "tokens=*" %%a IN ('"%VSWHERE_TOOLS_BIN%" -version [16.0^,17.0] -property installationPath') do (
\r
173 echo Setup a "x86|x64 Native Tools Command Prompt for VS2019" command prompt by using "%%a\VC\Auxiliary\Build\vcvars32.bat|vcvars64.bat".
\r
174 goto ON_ENV_WARNING_DONE
\r
179 if exist "%VSWHERE_TOOLS_BIN%" (
\r
180 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
181 for /f "tokens=*" %%a IN ('"%VSWHERE_TOOLS_BIN%" -version [15.0^,16.0] -property installationPath') do (
\r
182 echo Setup a "x86|x64 Native Tools Command Prompt for VS2017" command prompt by using "%%a\VC\Auxiliary\Build\vcvars32.bat|vcvars64.bat".
\r
183 goto ON_ENV_WARNING_DONE
\r
188 set VC_VARS_ALL_FILE=%ProgramFiles(x86)%\Microsoft Visual Studio 14.0\VC\vcvarsall.bat
\r
189 IF EXIST "%VC_VARS_ALL_FILE%" (
\r
190 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
191 echo Setup a "VS2015 x86|x64 Native Tools Command Prompt" command prompt by using "%VC_VARS_ALL_FILE% x86|amd64".
\r
192 goto ON_ENV_WARNING_DONE
\r
195 :ON_ENV_WARNING_DONE
\r
197 echo Could not detect Visual Studio build environment. You may experience build problems if wrong toolchain is auto detected.
\r
201 :: Append paths to VS installed common extension tools at end of PATH (Vs2017/VS2019).
\r
202 call :FIND_VS_COMMON_EXTENSION_TOOLS_PATHS VS_COMMON_EXTENSION_TOOLS_PATHS
\r
203 if not "%VS_COMMON_EXTENSION_TOOLS_PATHS%" == "" (
\r
204 set "PATH=%PATH%;%VS_COMMON_EXTENSION_TOOLS_PATHS%"
\r
207 :: Setup all cmake related generator, tools and variables.
\r
208 call :SETUP_CMAKE_ENVIRONMENT
\r
209 if "%CMAKE%" == "" (
\r
210 echo Failed to located working %CMAKE_BIN_NAME%, needs to be accessible in PATH or set using CMAKE environment variable.
\r
214 if "%CMAKE_GENERATOR%" == "" (
\r
215 echo Failed to setup cmake generator.
\r
220 if /i "%VS_TARGET%" == "build" (
\r
224 if /i "%VS_TARGET%" == "install" (
\r
225 goto ON_INSTALL_LLVM
\r
228 if /i "%VS_TARGET%" == "clean" (
\r
234 :: If not set by caller, check environment for working git.exe.
\r
235 call :FIND_PROGRAM "%GIT%" "%GIT_BIN_NAME%" GIT
\r
237 echo Failed to located working %GIT_BIN_NAME%, needs to be accessible in PATH or set using GIT environment variable.
\r
241 :: Make sure llvm submodule is up to date.
\r
242 echo Updating submodule "%LLVM_DIR%"
243 "%GIT%" submodule update --init -- "%LLVM_DIR%"
\r
244 if not ERRORLEVEL == 0 (
\r
245 "%GIT%" submodule init -- "%LLVM_DIR%"
\r
246 "%GIT%" submodule update -- "%LLVM_DIR%"
\r
247 if not ERRORLEVEL == 0 (
\r
248 echo Git llvm submodules failed to updated. You may experience compilation problems if some submodules are out of date.
\r
252 if not exist "%LLVM_DIR%" (
\r
253 echo Could not find "%LLVM_DIR%".
\r
257 if not exist "%LLVM_BUILD_DIR%" (
\r
258 mkdir "%LLVM_BUILD_DIR%"
\r
261 cd "%LLVM_BUILD_DIR%"
\r
263 :: Make sure cmake pick up msvc toolchain regardless of selected generator (Visual Studio|Ninja)
\r
264 set CC=%CL_BIN_NAME%
\r
265 set CXX=%CL_BIN_NAME%
\r
267 set CMAKE_GENERATOR_ARGS=
\r
268 set CMAKE_GENERATOR_TOOLSET=
\r
269 if /i "%CMAKE_GENERATOR%" == "ninja" (
\r
270 set CMAKE_GENERATOR_ARGS=-DCMAKE_BUILD_TYPE=%VS_CONFIGURATION%
\r
272 set CMAKE_GENERATOR_ARGS=-Thost=x64
\r
273 set CMAKE_GENERATOR_TOOLSET=%VS_PLATFORM_TOOL_SET%
\r
276 if not "%CMAKE_GENERATOR_ARCH%" == "" (
\r
277 set CMAKE_GENERATOR_ARCH=-A %CMAKE_GENERATOR_ARCH%
\r
280 :: Check if LLVM_DIR is just repro root or if we should build
281 :: a llvm subfolder within that repository.
282 set "LLVM_SOURCE_DIR=%LLVM_DIR%"
283 if exist "%LLVM_SOURCE_DIR%\llvm\CMakeLists.txt" (
284 set "LLVM_SOURCE_DIR=%LLVM_DIR%\llvm"
289 -DCMAKE_INSTALL_PREFIX="%LLVM_INSTALL_DIR%" ^
\r
290 -DLLVM_TARGETS_TO_BUILD="X86;ARM;AArch64" ^
\r
291 -DLLVM_BUILD_TESTS=Off ^
\r
292 -DLLVM_INCLUDE_TESTS=Off ^
\r
293 -DLLVM_BUILD_EXAMPLES=Off ^
\r
294 -DLLVM_INCLUDE_EXAMPLES=Off ^
\r
295 -DLLVM_TOOLS_TO_BUILD="opt;llc;llvm-config;llvm-dis;llvm-mc;llvm-as" ^
\r
296 -DLLVM_ENABLE_LIBXML2=Off ^
\r
297 -DCMAKE_SYSTEM_PROCESSOR="%LLVM_ARCH%" ^
\r
298 -D CMAKE_SYSTEM_VERSION=%VS_WIN_SDK_VERSION% ^
\r
299 %CMAKE_GENERATOR_TOOLSET% ^
\r
300 %LLVM_ADDITIONAL_CMAKE_ARGS% ^
\r
301 %CMAKE_GENERATOR_ARGS% ^
\r
302 -G "%CMAKE_GENERATOR%" ^
\r
303 %CMAKE_GENERATOR_ARCH% ^
\r
306 if not ERRORLEVEL == 0 (
\r
310 if /i "%CMAKE_GENERATOR%" == "ninja" (
\r
311 :: Build LLVM using ninja build system.
\r
312 call "%NINJA%" -j4 || (
\r
316 :: Build LLVM using msbuild build system.
\r
317 call "%MSBUILD%" llvm.sln /p:Configuration=%VS_CONFIGURATION% /p:Platform=%VS_PLATFORM% /t:%VS_TARGET% /v:m /nologo /m || (
\r
324 :: Make sure build install folder exists.
\r
325 if not exist "%LLVM_INSTALL_DIR%" (
\r
326 echo Could not find "%LLVM_INSTALL_DIR%", creating folder for build output.
\r
327 mkdir "%LLVM_INSTALL_DIR%"
\r
330 :: Make sure Mono dist folder exists.
\r
331 if not exist "%MONO_DIST_DIR%" (
\r
332 echo Could not find "%MONO_DIST_DIR%", creating folder for build output.
\r
333 mkdir "%MONO_DIST_DIR%"
\r
336 if exist "%LLVM_BUILD_DIR%\build.ninja" (
\r
338 cd "%LLVM_BUILD_DIR%"
\r
339 call "%NINJA%" install
\r
343 if exist "%LLVM_BUILD_DIR%\install.vcxproj" (
\r
344 "%MSBUILD%" "%LLVM_BUILD_DIR%\install.vcxproj" /p:Configuration=%VS_CONFIGURATION% /p:Platform=%VS_PLATFORM% /v:m /nologo
\r
347 call "%BUILD_EXTERNAL_LLVM_SCRIPT_PATH%\install-llvm-mono-build.bat" "%LLVM_INSTALL_DIR%" "%MONO_DIST_DIR%" || (
\r
355 if exist "%LLVM_BUILD_DIR%\build.ninja" (
\r
357 cd "%LLVM_BUILD_DIR%"
\r
358 call "%NINJA%" clean
\r
362 if exist "%LLVM_BUILD_DIR%\llvm.sln" (
\r
363 "%MSBUILD%" "%LLVM_BUILD_DIR%\llvm.sln" /p:Configuration=%VS_CONFIGURATION% /p:Platform=%VS_PLATFORM% /t:Clean /v:m /nologo
\r
374 ECHO Usage: build-external-llvm.bat [llvm_src_dir] [llvm_build_dir] [llvm_install_dir] [mono_dist_dir] [vs_cflags] [vs_plaform] [vs_configuration].
\r
377 echo Failed to build LLVM.
\r
381 exit /b %BUILD_RESULT%
\r
383 :: ##############################################################################################################################
\r
386 :: --------------------------------------------------
\r
387 :: Locates PATHS to installed common extension tools.
\r
388 :: %1 Output, variable including paths.
\r
389 :: --------------------------------------------------
\r
390 :FIND_VS_COMMON_EXTENSION_TOOLS_PATHS
\r
392 set VS_COMMON_EXTENSION_PATH=
\r
393 if exist "%VSWHERE_TOOLS_BIN%" (
\r
394 for /f "tokens=*" %%a in ('"%VSWHERE_TOOLS_BIN%" -version [16.0^,17.0] -property installationPath') do (
\r
395 set VS_COMMON_EXTENSION_PATH=%%a\Common7\IDE\CommonExtensions\Microsoft
\r
399 if exist "%VS_COMMON_EXTENSION_PATH%" (
\r
400 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
405 :: --------------------------------------------------
\r
406 :: Finds a program using environment.
\r
408 :: %1 Existing program to check for.
\r
409 :: %2 Name of binary to locate.
\r
410 :: %3 Output, variable to set if found requested program.
\r
411 :: --------------------------------------------------
\r
414 :: If not set by caller, check environment for program.
\r
419 call where /q "%~2" && (
\r
420 for /f "delims=" %%a in ('where "%~2"') do (
\r
429 :: --------------------------------------------------
\r
430 :: Setup up cmake build environment, including generator, build tools and variables.
\r
431 :: --------------------------------------------------
\r
432 :SETUP_CMAKE_ENVIRONMENT
\r
434 :: If not set by caller, check environment for working cmake.exe.
\r
435 call :FIND_PROGRAM "%CMAKE%" "%CMAKE_BIN_NAME%" CMAKE
\r
436 if "%CMAKE%" == "" (
\r
437 goto _SETUP_CMAKE_ENVIRONMENT_EXIT
\r
440 if /i "%VS_TARGET%" == "build" (
\r
441 echo Found CMake: "%CMAKE%"
\r
444 if /i "%FORCE_MSBUILD%" == "true" (
\r
445 goto _SETUP_CMAKE_ENVIRONMENT_VS_GENERATOR
\r
448 :: Check for optional cmake generate and build tools.
\r
449 call :FIND_PROGRAM "%NINJA%" "%NINJA_BIN_NAME%" NINJA
\r
451 if not "%NINJA%" == "" (
\r
452 goto _SETUP_CMAKE_ENVIRONMENT_NINJA_GENERATOR
\r
455 :_SETUP_CMAKE_ENVIRONMENT_VS_GENERATOR
\r
457 if /i "%VS_TARGET%" == "build" (
\r
458 echo Using Visual Studio build generator.
\r
461 set CMAKE_GENERATOR_ARCH=
\r
463 :: Detect VS platform tool set to use matching cmake generator.
\r
464 if /i "%VS_PLATFORM_TOOL_SET%" == "v140" (
\r
465 if /i "%VS_PLATFORM%" == "x64" (
\r
466 set CMAKE_GENERATOR=Visual Studio 14 2015 Win64
\r
468 set CMAKE_GENERATOR=Visual Studio 14 2015
\r
472 if /i "%VS_PLATFORM_TOOL_SET%" == "v141" (
\r
473 if /i "%VS_PLATFORM%" == "x64" (
\r
474 set CMAKE_GENERATOR=Visual Studio 15 2017 Win64
\r
476 set CMAKE_GENERATOR=Visual Studio 15 2017
\r
480 if /i "%VS_PLATFORM_TOOL_SET%" == "v142" (
\r
481 set CMAKE_GENERATOR=Visual Studio 16 2019
\r
482 if /i "%VS_PLATFORM%" == "x64" (
\r
483 set CMAKE_GENERATOR_ARCH=x64
\r
485 set CMAKE_GENERATOR_ARCH=Win32
\r
489 set LLVM_BUILD_OUTPUT_DIR=%LLVM_BUILD_DIR%\%VS_CONFIGURATION%
\r
491 goto _SETUP_CMAKE_ENVIRONMENT_EXIT
\r
493 :_SETUP_CMAKE_ENVIRONMENT_NINJA_GENERATOR
\r
495 if /i "%VS_TARGET%" == "build" (
\r
496 echo Found Ninja: "%NINJA%"
\r
497 echo Using Ninja build generator.
\r
500 set CMAKE_GENERATOR_ARCH=
\r
501 set CMAKE_GENERATOR=Ninja
\r
502 set LLVM_BUILD_OUTPUT_DIR=%LLVM_BUILD_DIR%
\r
504 :_SETUP_CMAKE_ENVIRONMENT_EXIT
\r