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 :: %10 MsBuild bin path, if used.
\r
17 :: %11 Force MSBuild (true/false), if used.
\r
18 :: --------------------------------------------------
\r
23 set TEMP_PATH=%PATH%
\r
26 set BUILD_EXTERNAL_LLVM_SCRIPT_PATH=%~dp0
\r
28 set CL_BIN_NAME=cl.exe
\r
29 set LINK_BIN_NAME=link.exe
\r
30 set GIT_BIN_NAME=git.exe
\r
31 set CMAKE_BIN_NAME=cmake.exe
\r
32 set NINJA_BIN_NAME=ninja.exe
\r
33 set PYTHON_BIN_NAME=python.exe
\r
37 set LLVM_BUILD_DIR=%~1
\r
39 set LLVM_INSTALL_DIR=%~1
\r
41 set MONO_DIST_DIR=%~1
\r
45 set LLVM_ADDITIONAL_CMAKE_ARGS=%~1
\r
49 set VS_CONFIGURATION=%~1
\r
53 set MSBUILD_BIN_PATH=%~1
\r
55 set FORCE_MSBUILD=%~1
\r
61 set MSBUILD=%MSBUILD_BIN_PATH%msbuild.exe
\r
63 if "%LLVM_DIR%" == "" (
\r
64 echo Missing LLVM source directory argument.
\r
68 if "%LLVM_BUILD_DIR%" == "" (
\r
69 echo Missing LLVM build directory argument.
\r
73 if "%LLVM_INSTALL_DIR%" == "" (
\r
74 echo Missing LLVM install directory argument.
\r
78 if "%MONO_DIST_DIR%" == "" (
\r
79 echo Missing Mono dist directory argument.
\r
83 if "%VS_CFLAGS%" == "" (
\r
84 echo Missing CFLAGS argument.
\r
88 if "%VS_PLATFORM%" == "" (
\r
92 if "%VS_CONFIGURATION%" == "" (
\r
93 set VS_CONFIGURATION=Release
\r
96 if "%VS_TARGET%" == "" (
\r
100 if "%FORCE_MSBUILD%" == "" (
\r
101 set FORCE_MSBUILD=false
\r
104 if not exist "%LLVM_DIR%" (
\r
105 echo Could not find "%LLVM_DIR%".
\r
109 set LLVM_CFLAGS=%VS_CFLAGS%
\r
110 set LLVM_ARCH=x86_64
\r
111 if /i "%VS_PLATFORM%" == "win32" (
\r
115 :: Check if executed from VS2015/VS2017 build environment.
\r
116 if "%VisualStudioVersion%" == "14.0" (
\r
120 if "%VisualStudioVersion%" == "15.0" (
\r
124 :: Executed outside VS2015/VS2017 build environment, try to locate Visual Studio C/C++ compiler and linker.
\r
125 call :FIND_PROGRAM "" "%CL_BIN_NAME%" CL_PATH
\r
126 if "%CL_PATH%" == "" (
\r
127 goto ON_ENV_WARNING
\r
130 call :FIND_PROGRAM "" "%LINK_BIN_NAME%" LINK_PATH
\r
131 if "%LINK_PATH%" == "" (
\r
132 goto ON_ENV_WARNING
\r
140 set VC_VARS_ALL_FILE=%ProgramFiles(x86)%\Microsoft Visual Studio 14.0\VC\vcvarsall.bat
\r
141 IF EXIST "%VC_VARS_ALL_FILE%" (
\r
142 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
143 echo Setup a "VS2015 x86|x64 Native Tools Command Prompt" command prompt by using "%VC_VARS_ALL_FILE% x86|amd64".
\r
147 set VSWHERE_TOOLS_BIN=%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe
\r
148 if exist "%VSWHERE_TOOLS_BIN%" (
\r
149 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
150 for /f "tokens=*" %%a IN ('"%VSWHERE_TOOLS_BIN%" -latest -property installationPath') do (
\r
151 echo Setup a "x86|x64 Native Tools Command Prompt for VS2017" command prompt by using "%%a\VC\Auxiliary\Build\vcvars32.bat|vcvars64.bat".
\r
155 echo Could not detect Visual Studio build environment. You may experience build problems if wrong toolchain is auto detected.
\r
159 :: Setup all cmake related generator, tools and variables.
\r
160 call :SETUP_CMAKE_ENVIRONMENT
\r
161 if "%CMAKE%" == "" (
\r
162 echo Failed to located working %CMAKE_BIN_NAME%, needs to be accessible in PATH or set using CMAKE environment variable.
\r
166 if "%CMAKE_GENERATOR%" == "" (
\r
167 echo Failed to setup cmake generator.
\r
172 if /i "%VS_TARGET%" == "build" (
\r
176 if /i "%VS_TARGET%" == "install" (
\r
177 goto ON_INSTALL_LLVM
\r
180 if /i "%VS_TARGET%" == "clean" (
\r
186 :: If not set by caller, check environment for working git.exe.
\r
187 call :FIND_PROGRAM "%GIT%" "%GIT_BIN_NAME%" GIT
\r
189 echo Failed to located working %GIT_BIN_NAME%, needs to be accessible in PATH or set using GIT environment variable.
\r
193 :: Make sure llvm submodule is up to date.
\r
196 "%GIT%" submodule update --init
\r
197 if not ERRORLEVEL == 0 (
\r
198 "%GIT%" submodule init
\r
199 "%GIT%" submodule update
\r
200 if not ERRORLEVEL == 0 (
\r
201 echo Git llvm submodules failed to updated. You may experience compilation problems if some submodules are out of date.
\r
206 if not exist "%LLVM_BUILD_DIR%" (
\r
207 mkdir "%LLVM_BUILD_DIR%"
\r
210 cd "%LLVM_BUILD_DIR%"
\r
212 :: Make sure cmake pick up msvc toolchain regardless of selected generator (Visual Studio|Ninja)
\r
213 set CC=%CL_BIN_NAME%
\r
214 set CXX=%CL_BIN_NAME%
\r
216 set CMAKE_GENERATOR_ARGS=
\r
217 if /i "%CMAKE_GENERATOR%" == "ninja" (
\r
218 set CMAKE_GENERATOR_ARGS=-DCMAKE_BUILD_TYPE=%VS_CONFIGURATION%
\r
220 set CMAKE_GENERATOR_ARGS=-Thost=x64
\r
225 -DCMAKE_INSTALL_PREFIX="%LLVM_INSTALL_DIR%" ^
\r
226 -DLLVM_TARGETS_TO_BUILD="X86;ARM;AArch64" ^
\r
227 -DLLVM_BUILD_TESTS=Off ^
\r
228 -DLLVM_INCLUDE_TESTS=Off ^
\r
229 -DLLVM_BUILD_EXAMPLES=Off ^
\r
230 -DLLVM_INCLUDE_EXAMPLES=Off ^
\r
231 -DLLVM_TOOLS_TO_BUILD="opt;llc;llvm-config;llvm-dis;llvm-mc;llvm-as" ^
\r
232 -DLLVM_ENABLE_LIBXML2=Off ^
\r
233 -DCMAKE_SYSTEM_PROCESSOR="%LLVM_ARCH%" ^
\r
234 %LLVM_ADDITIONAL_CMAKE_ARGS% ^
\r
235 %CMAKE_GENERATOR_ARGS% ^
\r
236 -G "%CMAKE_GENERATOR%" ^
\r
239 if not ERRORLEVEL == 0 (
\r
243 if /i "%CMAKE_GENERATOR%" == "ninja" (
\r
244 :: Build LLVM using ninja build system.
\r
245 call "%NINJA%" -j4 || (
\r
249 :: Build LLVM using msbuild build system.
\r
250 call "%MSBUILD%" llvm.sln /p:Configuration=%VS_CONFIGURATION% /p:Platform=%VS_PLATFORM% /t:%VS_TARGET% /v:m /nologo || (
\r
257 :: Make sure build install folder exists.
\r
258 if not exist "%LLVM_INSTALL_DIR%" (
\r
259 echo Could not find "%LLVM_INSTALL_DIR%", creating folder for build output.
\r
260 mkdir "%LLVM_INSTALL_DIR%"
\r
263 :: Make sure Mono dist folder exists.
\r
264 if not exist "%MONO_DIST_DIR%" (
\r
265 echo Could not find "%MONO_DIST_DIR%", creating folder for build output.
\r
266 mkdir "%MONO_DIST_DIR%"
\r
269 if exist "%LLVM_BUILD_DIR%\build.ninja" (
\r
271 cd "%LLVM_BUILD_DIR%"
\r
272 call "%NINJA%" install
\r
276 if exist "%LLVM_BUILD_DIR%\install.vcxproj" (
\r
277 "%MSBUILD%" "%LLVM_BUILD_DIR%\install.vcxproj" /p:Configuration=%VS_CONFIGURATION% /p:Platform=%VS_PLATFORM% /v:m /nologo
\r
280 call "%BUILD_EXTERNAL_LLVM_SCRIPT_PATH%\install-llvm-mono-build.bat" "%LLVM_INSTALL_DIR%" "%MONO_DIST_DIR%" || (
\r
288 if exist "%LLVM_BUILD_DIR%\build.ninja" (
\r
290 cd "%LLVM_BUILD_DIR%"
\r
291 call "%NINJA%" clean
\r
295 if exist "%LLVM_BUILD_DIR%\llvm.sln" (
\r
296 "%MSBUILD%" "%LLVM_BUILD_DIR%\llvm.sln" /p:Configuration=%VS_CONFIGURATION% /p:Platform=%VS_PLATFORM% /t:Clean /v:m /nologo
\r
307 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
310 echo Failed to build LLVM.
\r
314 set PATH=%TEMP_PATH%
\r
315 exit /b %BUILD_RESULT%
\r
317 :: ##############################################################################################################################
\r
320 :: --------------------------------------------------
\r
321 :: Finds a program using environment.
\r
323 :: %1 Existing program to check for.
\r
324 :: %2 Name of binary to locate.
\r
325 :: %3 Output, variable to set if found requested program.
\r
326 :: --------------------------------------------------
\r
329 :: If not set by caller, check environment for program.
\r
334 call where /q "%~2" && (
\r
335 for /f "delims=" %%a in ('where "%~2"') do (
\r
344 :: --------------------------------------------------
\r
345 :: Setup up cmake build environment, including generator, build tools and variables.
\r
346 :: --------------------------------------------------
\r
347 :SETUP_CMAKE_ENVIRONMENT
\r
349 :: If not set by caller, check environment for working cmake.exe.
\r
350 call :FIND_PROGRAM "%CMAKE%" "%CMAKE_BIN_NAME%" CMAKE
\r
351 if "%CMAKE%" == "" (
\r
352 goto _SETUP_CMAKE_ENVIRONMENT_EXIT
\r
355 if /i "%VS_TARGET%" == "build" (
\r
356 echo Found CMake: "%CMAKE%"
\r
359 if /i "%FORCE_MSBUILD%" == "true" (
\r
360 goto _SETUP_CMAKE_ENVIRONMENT_VS_GENERATOR
\r
363 :: Check for optional cmake generate and build tools.
\r
364 call :FIND_PROGRAM "%NINJA%" "%NINJA_BIN_NAME%" NINJA
\r
366 if not "%NINJA%" == "" (
\r
367 goto _SETUP_CMAKE_ENVIRONMENT_NINJA_GENERATOR
\r
370 :_SETUP_CMAKE_ENVIRONMENT_VS_GENERATOR
\r
372 if /i "%VS_TARGET%" == "build" (
\r
373 echo Using Visual Studio build generator.
\r
376 :: Detect VS version to use right cmake generator.
\r
377 set CMAKE_GENERATOR=Visual Studio 14 2015
\r
378 if "%VisualStudioVersion%" == "15.0" (
\r
379 set CMAKE_GENERATOR=Visual Studio 15 2017
\r
382 if /i "%VS_PLATFORM%" == "x64" (
\r
383 set CMAKE_GENERATOR=%CMAKE_GENERATOR% Win64
\r
386 set LLVM_BUILD_OUTPUT_DIR=%LLVM_BUILD_DIR%\%VS_CONFIGURATION%
\r
388 goto _SETUP_CMAKE_ENVIRONMENT_EXIT
\r
390 :_SETUP_CMAKE_ENVIRONMENT_NINJA_GENERATOR
\r
392 if /i "%VS_TARGET%" == "build" (
\r
393 echo Found Ninja: "%NINJA%"
\r
394 echo Using Ninja build generator.
\r
397 set CMAKE_GENERATOR=Ninja
\r
398 set LLVM_BUILD_OUTPUT_DIR=%LLVM_BUILD_DIR%
\r
400 :_SETUP_CMAKE_ENVIRONMENT_EXIT
\r