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 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 PYTHON_BIN_NAME=python.exe
\r
35 set LLVM_BUILD_DIR=%~1
\r
37 set LLVM_INSTALL_DIR=%~1
\r
39 set MONO_DIST_DIR=%~1
\r
43 set LLVM_ADDITIONAL_CMAKE_ARGS=%~1
\r
47 set VS_CONFIGURATION=%~1
\r
51 set MSBUILD_BIN_PATH=%~1
\r
53 set FORCE_MSBUILD=%~1
\r
59 set MSBUILD=%MSBUILD_BIN_PATH%msbuild.exe
\r
61 if "%LLVM_DIR%" == "" (
\r
62 echo Missing LLVM source directory argument.
\r
66 if "%LLVM_BUILD_DIR%" == "" (
\r
67 echo Missing LLVM build directory argument.
\r
71 if "%LLVM_INSTALL_DIR%" == "" (
\r
72 echo Missing LLVM install directory argument.
\r
76 if "%MONO_DIST_DIR%" == "" (
\r
77 echo Missing Mono dist directory argument.
\r
81 if "%VS_CFLAGS%" == "" (
\r
82 echo Missing CFLAGS argument.
\r
86 if "%VS_PLATFORM%" == "" (
\r
90 if "%VS_CONFIGURATION%" == "" (
\r
91 set VS_CONFIGURATION=Release
\r
94 if "%VS_TARGET%" == "" (
\r
98 if "%FORCE_MSBUILD%" == "" (
\r
99 set FORCE_MSBUILD=false
\r
102 if not exist "%LLVM_DIR%" (
\r
103 echo Could not find "%LLVM_DIR%".
\r
107 set LLVM_CFLAGS=%VS_CFLAGS%
\r
108 set LLVM_ARCH=x86_64
\r
109 if /i "%VS_PLATFORM%" == "win32" (
\r
113 :: Check if executed from VS2015/VS2017 build environment.
\r
114 if "%VisualStudioVersion%" == "14.0" (
\r
118 if "%VisualStudioVersion%" == "15.0" (
\r
122 :: Executed outside VS2015/VS2017 build environment, try to locate Visual Studio C/C++ compiler and linker.
\r
123 call :FIND_PROGRAM "" "%CL_BIN_NAME%" CL_PATH
\r
124 if "%CL_PATH%" == "" (
\r
125 goto ON_ENV_WARNING
\r
128 call :FIND_PROGRAM "" "%LINK_BIN_NAME%" LINK_PATH
\r
129 if "%LINK_PATH%" == "" (
\r
130 goto ON_ENV_WARNING
\r
138 set VC_VARS_ALL_FILE=%ProgramFiles(x86)%\Microsoft Visual Studio 14.0\VC\vcvarsall.bat
\r
139 IF EXIST "%VC_VARS_ALL_FILE%" (
\r
140 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
141 echo Setup a "VS2015 x86|x64 Native Tools Command Prompt" command prompt by using "%VC_VARS_ALL_FILE% x86|amd64".
\r
145 set VSWHERE_TOOLS_BIN=%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe
\r
146 if exist "%VSWHERE_TOOLS_BIN%" (
\r
147 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
148 for /f "tokens=*" %%a IN ('"%VSWHERE_TOOLS_BIN%" -latest -property installationPath') do (
\r
149 echo Setup a "x86|x64 Native Tools Command Prompt for VS2017" command prompt by using "%%a\VC\Auxiliary\Build\vcvars32.bat|vcvars64.bat".
\r
153 echo Could not detect Visual Studio build environment. You may experience build problems if wrong toolchain is auto detected.
\r
157 :: Setup all cmake related generator, tools and variables.
\r
158 call :SETUP_CMAKE_ENVIRONMENT
\r
159 if "%CMAKE%" == "" (
\r
160 echo Failed to located working %CMAKE_BIN_NAME%, needs to be accessible in PATH or set using CMAKE environment variable.
\r
164 if "%CMAKE_GENERATOR%" == "" (
\r
165 echo Failed to setup cmake generator.
\r
170 if /i "%VS_TARGET%" == "build" (
\r
174 if /i "%VS_TARGET%" == "install" (
\r
175 goto ON_INSTALL_LLVM
\r
178 if /i "%VS_TARGET%" == "clean" (
\r
184 :: If not set by caller, check environment for working git.exe.
\r
185 call :FIND_PROGRAM "%GIT%" "%GIT_BIN_NAME%" GIT
\r
187 echo Failed to located working %GIT_BIN_NAME%, needs to be accessible in PATH or set using GIT environment variable.
\r
191 :: Make sure llvm submodule is up to date.
\r
194 "%GIT%" submodule update --init
\r
195 if not ERRORLEVEL == 0 (
\r
196 "%GIT%" submodule init
\r
197 "%GIT%" submodule update
\r
198 if not ERRORLEVEL == 0 (
\r
199 echo Git llvm submodules failed to updated. You may experience compilation problems if some submodules are out of date.
\r
204 if not exist "%LLVM_BUILD_DIR%" (
\r
205 mkdir "%LLVM_BUILD_DIR%"
\r
208 cd "%LLVM_BUILD_DIR%"
\r
210 :: Make sure cmake pick up msvc toolchain regardless of selected generator (Visual Studio|Ninja)
\r
211 set CC=%CL_BIN_NAME%
\r
212 set CXX=%CL_BIN_NAME%
\r
214 set CMAKE_GENERATOR_ARGS=
\r
215 if /i "%CMAKE_GENERATOR%" == "ninja" (
\r
216 set CMAKE_GENERATOR_ARGS=-DCMAKE_BUILD_TYPE=%VS_CONFIGURATION%
\r
218 set CMAKE_GENERATOR_ARGS=-Thost=x64
\r
223 -DCMAKE_INSTALL_PREFIX="%LLVM_INSTALL_DIR%" ^
\r
224 -DLLVM_TARGETS_TO_BUILD="X86;ARM;AArch64" ^
\r
225 -DLLVM_BUILD_TESTS=Off ^
\r
226 -DLLVM_INCLUDE_TESTS=Off ^
\r
227 -DLLVM_BUILD_EXAMPLES=Off ^
\r
228 -DLLVM_INCLUDE_EXAMPLES=Off ^
\r
229 -DLLVM_TOOLS_TO_BUILD="opt;llc;llvm-config;llvm-dis;llvm-mc;llvm-as" ^
\r
230 -DLLVM_ENABLE_LIBXML2=Off ^
\r
231 -DCMAKE_SYSTEM_PROCESSOR="%LLVM_ARCH%" ^
\r
232 %LLVM_ADDITIONAL_CMAKE_ARGS% ^
\r
233 %CMAKE_GENERATOR_ARGS% ^
\r
234 -G "%CMAKE_GENERATOR%" ^
\r
237 if not ERRORLEVEL == 0 (
\r
241 if /i "%CMAKE_GENERATOR%" == "ninja" (
\r
242 :: Build LLVM using ninja build system.
\r
243 call "%NINJA%" -j4 || (
\r
247 :: Build LLVM using msbuild build system.
\r
248 call "%MSBUILD%" llvm.sln /p:Configuration=%VS_CONFIGURATION% /p:Platform=%VS_PLATFORM% /t:%VS_TARGET% /v:m /nologo || (
\r
255 :: Make sure build install folder exists.
\r
256 if not exist "%LLVM_INSTALL_DIR%" (
\r
257 echo Could not find "%LLVM_INSTALL_DIR%", creating folder for build output.
\r
258 mkdir "%LLVM_INSTALL_DIR%"
\r
261 :: Make sure Mono dist folder exists.
\r
262 if not exist "%MONO_DIST_DIR%" (
\r
263 echo Could not find "%MONO_DIST_DIR%", creating folder for build output.
\r
264 mkdir "%MONO_DIST_DIR%"
\r
267 if exist "%LLVM_BUILD_DIR%\build.ninja" (
\r
269 cd "%LLVM_BUILD_DIR%"
\r
270 call "%NINJA%" install
\r
274 if exist "%LLVM_BUILD_DIR%\install.vcxproj" (
\r
275 "%MSBUILD%" "%LLVM_BUILD_DIR%\install.vcxproj" /p:Configuration=%VS_CONFIGURATION% /p:Platform=%VS_PLATFORM% /v:m /nologo
\r
278 if not exist "%LLVM_INSTALL_DIR%\bin\opt.exe" (
\r
279 echo Missing LLVM build output, "%LLVM_INSTALL_DIR%\bin\opt.exe"
\r
283 if not exist "%LLVM_INSTALL_DIR%\bin\llc.exe" (
\r
284 echo Missing LLVM build output, "%LLVM_INSTALL_DIR%\bin\llc.exe"
\r
288 if not exist "%LLVM_INSTALL_DIR%\bin\llvm-dis.exe" (
\r
289 echo Missing LLVM build output, "%LLVM_INSTALL_DIR%\bin\llvm-dis.exe"
\r
293 if not exist "%LLVM_INSTALL_DIR%\bin\llvm-mc.exe" (
\r
294 echo Missing LLVM build output, "%LLVM_INSTALL_DIR%\bin\llvm-mc.exe"
\r
298 if not exist "%LLVM_INSTALL_DIR%\bin\llvm-as.exe" (
\r
299 echo Missing LLVM build output, "%LLVM_INSTALL_DIR%\bin\llvm-as.exe"
\r
303 copy /Y "%LLVM_INSTALL_DIR%\bin\opt.exe" "%MONO_DIST_DIR%" >nul 2>&1
\r
304 copy /Y "%LLVM_INSTALL_DIR%\bin\llc.exe" "%MONO_DIST_DIR%" >nul 2>&1
\r
305 copy /Y "%LLVM_INSTALL_DIR%\bin\llvm-dis.exe" "%MONO_DIST_DIR%" >nul 2>&1
\r
306 copy /Y "%LLVM_INSTALL_DIR%\bin\llvm-mc.exe" "%MONO_DIST_DIR%" >nul 2>&1
\r
307 copy /Y "%LLVM_INSTALL_DIR%\bin\llvm-as.exe" "%MONO_DIST_DIR%" >nul 2>&1
\r
313 if exist "%LLVM_BUILD_DIR%\build.ninja" (
\r
315 cd "%LLVM_BUILD_DIR%"
\r
316 call "%NINJA%" clean
\r
320 if exist "%LLVM_BUILD_DIR%\llvm.sln" (
\r
321 "%MSBUILD%" "%LLVM_BUILD_DIR%\llvm.sln" /p:Configuration=%VS_CONFIGURATION% /p:Platform=%VS_PLATFORM% /t:Clean /v:m /nologo
\r
332 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
335 echo Failed to build LLVM.
\r
339 set PATH=%TEMP_PATH%
\r
340 exit /b %BUILD_RESULT%
\r
342 :: ##############################################################################################################################
\r
345 :: --------------------------------------------------
\r
346 :: Finds a program using environment.
\r
348 :: %1 Existing program to check for.
\r
349 :: %2 Name of binary to locate.
\r
350 :: %3 Output, variable to set if found requested program.
\r
351 :: --------------------------------------------------
\r
354 :: If not set by caller, check environment for program.
\r
359 call where /q "%~2" && (
\r
360 for /f "delims=" %%a in ('where "%~2"') do (
\r
369 :: --------------------------------------------------
\r
370 :: Setup up cmake build environment, including generator, build tools and variables.
\r
371 :: --------------------------------------------------
\r
372 :SETUP_CMAKE_ENVIRONMENT
\r
374 :: If not set by caller, check environment for working cmake.exe.
\r
375 call :FIND_PROGRAM "%CMAKE%" "%CMAKE_BIN_NAME%" CMAKE
\r
376 if "%CMAKE%" == "" (
\r
377 goto _SETUP_CMAKE_ENVIRONMENT_EXIT
\r
380 if /i "%VS_TARGET%" == "build" (
\r
381 echo Found CMake: "%CMAKE%"
\r
384 if /i "%FORCE_MSBUILD%" == "true" (
\r
385 goto _SETUP_CMAKE_ENVIRONMENT_VS_GENERATOR
\r
388 :: Check for optional cmake generate and build tools.
\r
389 call :FIND_PROGRAM "%NINJA%" "%NINJA_BIN_NAME%" NINJA
\r
391 if not "%NINJA%" == "" (
\r
392 goto _SETUP_CMAKE_ENVIRONMENT_NINJA_GENERATOR
\r
395 :_SETUP_CMAKE_ENVIRONMENT_VS_GENERATOR
\r
397 if /i "%VS_TARGET%" == "build" (
\r
398 echo Using Visual Studio build generator.
\r
401 :: Detect VS version to use right cmake generator.
\r
402 set CMAKE_GENERATOR=Visual Studio 14 2015
\r
403 if "%VisualStudioVersion%" == "15.0" (
\r
404 set CMAKE_GENERATOR=Visual Studio 15 2017
\r
407 if /i "%VS_PLATFORM%" == "x64" (
\r
408 set CMAKE_GENERATOR=%CMAKE_GENERATOR% Win64
\r
411 set LLVM_BUILD_OUTPUT_DIR=%LLVM_BUILD_DIR%\%VS_CONFIGURATION%
\r
413 goto _SETUP_CMAKE_ENVIRONMENT_EXIT
\r
415 :_SETUP_CMAKE_ENVIRONMENT_NINJA_GENERATOR
\r
417 if /i "%VS_TARGET%" == "build" (
\r
418 echo Found Ninja: "%NINJA%"
\r
419 echo Using Ninja build generator.
\r
422 set CMAKE_GENERATOR=Ninja
\r
423 set LLVM_BUILD_OUTPUT_DIR=%LLVM_BUILD_DIR%
\r
425 :_SETUP_CMAKE_ENVIRONMENT_EXIT
\r