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 VS platform (Win32/x64)
\r
13 :: %7 VS configuration (Debug/Release)
\r
15 :: %9 MsBuild bin path, if used.
\r
16 :: --------------------------------------------------
\r
21 set TEMP_PATH=%PATH%
\r
24 set CL_BIN_NAME=cl.exe
\r
25 set LINK_BIN_NAME=link.exe
\r
26 set GIT_BIN_NAME=git.exe
\r
27 set CMAKE_BIN_NAME=cmake.exe
\r
28 set NINJA_BIN_NAME=ninja.exe
\r
29 set PYTHON_BIN_NAME=python.exe
\r
32 set LLVM_BUILD_DIR=%~2
\r
33 set LLVM_INSTALL_DIR=%~3
\r
34 set MONO_DIST_DIR=%~4
\r
37 set VS_CONFIGURATION=%~7
\r
39 set MSBUILD_BIN_PATH=%~9
\r
45 set MSBUILD=%MSBUILD_BIN_PATH%msbuild.exe
\r
47 if "%LLVM_DIR%" == "" (
\r
48 echo Missing LLVM source directory argument.
\r
52 if "%LLVM_BUILD_DIR%" == "" (
\r
53 echo Missing LLVM build directory argument.
\r
57 if "%LLVM_INSTALL_DIR%" == "" (
\r
58 echo Missing LLVM install directory argument.
\r
62 if "%MONO_DIST_DIR%" == "" (
\r
63 echo Missing Mono dist directory argument.
\r
67 if "%VS_CFLAGS%" == "" (
\r
68 echo Missing CFLAGS argument.
\r
72 if "%VS_PLATFORM%" == "" (
\r
76 if "%VS_CONFIGURATION%" == "" (
\r
77 set VS_CONFIGURATION=Release
\r
80 if "%VS_TARGET%" == "" (
\r
84 if not exist "%LLVM_DIR%" (
\r
85 echo Could not find "%LLVM_DIR%".
\r
89 set LLVM_CFLAGS=%VS_CFLAGS%
\r
90 set LLVM_ARCH=x86_64
\r
91 if /i "%VS_PLATFORM%" == "win32" (
\r
95 :: Check if executed from VS2015/VS2017 build environment.
\r
96 if "%VisualStudioVersion%" == "14.0" (
\r
100 if "%VisualStudioVersion%" == "15.0" (
\r
104 :: Executed outside VS2015/VS2017 build environment, try to locate Visual Studio C/C++ compiler and linker.
\r
105 call :FIND_PROGRAM "" "%CL_BIN_NAME%" CL_PATH
\r
106 if "%CL_PATH%" == "" (
\r
107 goto ON_ENV_WARNING
\r
110 call :FIND_PROGRAM "" "%LINK_BIN_NAME%" LINK_PATH
\r
111 if "%LINK_PATH%" == "" (
\r
112 goto ON_ENV_WARNING
\r
120 set VC_VARS_ALL_FILE=%ProgramFiles(x86)%\Microsoft Visual Studio 14.0\VC\vcvarsall.bat
\r
121 IF EXIST "%VC_VARS_ALL_FILE%" (
\r
122 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
123 echo Setup a "VS2015 x86|x64 Native Tools Command Prompt" command prompt by using "%VC_VARS_ALL_FILE% x86|amd64".
\r
127 set VSWHERE_TOOLS_BIN=%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe
\r
128 if exist "%VSWHERE_TOOLS_BIN%" (
\r
129 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
130 for /f "tokens=*" %%a IN ('"%VSWHERE_TOOLS_BIN%" -latest -property installationPath') do (
\r
131 echo Setup a "x86|x64 Native Tools Command Prompt for VS2017" command prompt by using "%%a\VC\Auxiliary\Build\vcvars32.bat|vcvars64.bat".
\r
135 echo Could not detect Visual Studio build environment. You may experience build problems if wrong toolchain is auto detected.
\r
139 :: Setup all cmake related generator, tools and variables.
\r
140 call :SETUP_CMAKE_ENVIRONMENT
\r
141 if "%CMAKE%" == "" (
\r
142 echo Failed to located working %CMAKE_BIN_NAME%, needs to be accessible in PATH or set using CMAKE environment variable.
\r
146 if "%CMAKE_GENERATOR%" == "" (
\r
147 echo Failed to setup cmake generator.
\r
152 if /i "%VS_TARGET%" == "build" (
\r
156 if /i "%VS_TARGET%" == "install" (
\r
157 goto ON_INSTALL_LLVM
\r
160 if /i "%VS_TARGET%" == "clean" (
\r
166 :: If not set by caller, check environment for working git.exe.
\r
167 call :FIND_PROGRAM "%GIT%" "%GIT_BIN_NAME%" GIT
\r
169 echo Failed to located working %GIT_BIN_NAME%, needs to be accessible in PATH or set using GIT environment variable.
\r
173 :: Make sure llvm submodule is up to date.
\r
176 "%GIT%" submodule update --init
\r
177 if not ERRORLEVEL == 0 (
\r
178 "%GIT%" submodule init
\r
179 "%GIT%" submodule update
\r
180 if not ERRORLEVEL == 0 (
\r
181 echo Git llvm submodules failed to updated. You may experience compilation problems if some submodules are out of date.
\r
186 if not exist "%LLVM_BUILD_DIR%" (
\r
187 mkdir "%LLVM_BUILD_DIR%"
\r
190 cd "%LLVM_BUILD_DIR%"
\r
192 :: Make sure cmake pick up msvc toolchain regardless of selected generator (Visual Studio|Ninja)
\r
193 set CC=%CL_BIN_NAME%
\r
194 set CXX=%CL_BIN_NAME%
\r
196 set CMAKE_GENERATOR_ARGS=
\r
197 if /i "%CMAKE_GENERATOR%" == "ninja" (
\r
198 set CMAKE_GENERATOR_ARGS=-DCMAKE_BUILD_TYPE=%VS_CONFIGURATION%
\r
200 set CMAKE_GENERATOR_ARGS=-Thost=x64
\r
205 -DCMAKE_INSTALL_PREFIX="%LLVM_INSTALL_DIR%" ^
\r
206 -DLLVM_TARGETS_TO_BUILD="X86" ^
\r
207 -DLLVM_BUILD_TESTS=Off ^
\r
208 -DLLVM_INCLUDE_TESTS=Off ^
\r
209 -DLLVM_BUILD_EXAMPLES=Off ^
\r
210 -DLLVM_INCLUDE_EXAMPLES=Off ^
\r
211 -DLLVM_TOOLS_TO_BUILD="opt;llc;llvm-config;llvm-dis;llvm-mc" ^
\r
212 -DLLVM_ENABLE_LIBXML2=Off ^
\r
213 -DCMAKE_SYSTEM_PROCESSOR="%LLVM_ARCH%" ^
\r
214 %CMAKE_GENERATOR_ARGS% ^
\r
215 -G "%CMAKE_GENERATOR%" ^
\r
218 if not ERRORLEVEL == 0 (
\r
222 if /i "%CMAKE_GENERATOR%" == "ninja" (
\r
223 :: Build LLVM using ninja build system.
\r
224 call "%NINJA%" -j4 || (
\r
228 :: Build LLVM using msbuild build system.
\r
229 call "%MSBUILD%" llvm.sln /p:Configuration=%VS_CONFIGURATION% /p:Platform=%VS_PLATFORM% /t:%VS_TARGET% /v:m /nologo || (
\r
236 :: Make sure build install folder exists.
\r
237 if not exist "%LLVM_INSTALL_DIR%" (
\r
238 echo Could not find "%LLVM_INSTALL_DIR%", creating folder for build output.
\r
239 mkdir "%LLVM_INSTALL_DIR%"
\r
242 :: Make sure Mono dist folder exists.
\r
243 if not exist "%MONO_DIST_DIR%" (
\r
244 echo Could not find "%MONO_DIST_DIR%", creating folder for build output.
\r
245 mkdir "%MONO_DIST_DIR%"
\r
248 if exist "%LLVM_BUILD_DIR%\build.ninja" (
\r
250 cd "%LLVM_BUILD_DIR%"
\r
251 call "%NINJA%" install
\r
255 if exist "%LLVM_BUILD_DIR%\install.vcxproj" (
\r
256 "%MSBUILD%" "%LLVM_BUILD_DIR%\install.vcxproj" /p:Configuration=%VS_CONFIGURATION% /p:Platform=%VS_PLATFORM% /v:m /nologo
\r
259 if not exist "%LLVM_INSTALL_DIR%\bin\opt.exe" (
\r
260 echo Missing LLVM build output, "%LLVM_INSTALL_DIR%\bin\opt.exe"
\r
264 if not exist "%LLVM_INSTALL_DIR%\bin\llc.exe" (
\r
265 echo Missing LLVM build output, "%LLVM_INSTALL_DIR%\bin\llc.exe"
\r
269 copy /Y "%LLVM_INSTALL_DIR%\bin\opt.exe" "%MONO_DIST_DIR%" >nul 2>&1
\r
270 copy /Y "%LLVM_INSTALL_DIR%\bin\llc.exe" "%MONO_DIST_DIR%" >nul 2>&1
\r
276 if exist "%LLVM_BUILD_DIR%\build.ninja" (
\r
278 cd "%LLVM_BUILD_DIR%"
\r
279 call "%NINJA%" clean
\r
283 if exist "%LLVM_BUILD_DIR%\llvm.sln" (
\r
284 "%MSBUILD%" "%LLVM_BUILD_DIR%\llvm.sln" /p:Configuration=%VS_CONFIGURATION% /p:Platform=%VS_PLATFORM% /t:Clean /v:m /nologo
\r
295 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
298 echo Failed to build LLVM.
\r
302 set PATH=%TEMP_PATH%
\r
303 exit /b %BUILD_RESULT%
\r
305 :: ##############################################################################################################################
\r
308 :: --------------------------------------------------
\r
309 :: Finds a program using environment.
\r
311 :: %1 Existing program to check for.
\r
312 :: %2 Name of binary to locate.
\r
313 :: %3 Output, variable to set if found requested program.
\r
314 :: --------------------------------------------------
\r
317 :: If not set by caller, check environment for program.
\r
322 call where /q "%~2" && (
\r
323 for /f "delims=" %%a in ('where "%~2"') do (
\r
332 :: --------------------------------------------------
\r
333 :: Setup up cmake build environment, including generator, build tools and variables.
\r
334 :: --------------------------------------------------
\r
335 :SETUP_CMAKE_ENVIRONMENT
\r
337 :: If not set by caller, check environment for working cmake.exe.
\r
338 call :FIND_PROGRAM "%CMAKE%" "%CMAKE_BIN_NAME%" CMAKE
\r
339 if "%CMAKE%" == "" (
\r
340 goto _SETUP_CMAKE_ENVIRONMENT_EXIT
\r
343 if /i "%VS_TARGET%" == "build" (
\r
344 echo Found CMake: %CMAKE%
\r
347 :: Check for optional cmake generate and build tools.
\r
348 call :FIND_PROGRAM "%NINJA%" "%NINJA_BIN_NAME%" NINJA
\r
350 if not "%NINJA%" == "" (
\r
351 goto _SETUP_CMAKE_ENVIRONMENT_NINJA_GENERATOR
\r
354 :_SETUP_CMAKE_ENVIRONMENT_VS_GENERATOR
\r
356 if /i "%VS_TARGET%" == "build" (
\r
357 echo Using Visual Studio build generator.
\r
360 :: Detect VS version to use right cmake generator.
\r
361 set CMAKE_GENERATOR=Visual Studio 14 2015
\r
362 if "%VisualStudioVersion%" == "15.0" (
\r
363 set CMAKE_GENERATOR=Visual Studio 15 2017
\r
366 if /i "%VS_PLATFORM%" == "x64" (
\r
367 set CMAKE_GENERATOR=%CMAKE_GENERATOR% Win64
\r
370 set LLVM_BUILD_OUTPUT_DIR=%LLVM_BUILD_DIR%\%VS_CONFIGURATION%
\r
372 goto _SETUP_CMAKE_ENVIRONMENT_EXIT
\r
374 :_SETUP_CMAKE_ENVIRONMENT_NINJA_GENERATOR
\r
376 if /i "%VS_TARGET%" == "build" (
\r
377 echo Found Ninja: %NINJA%
\r
378 echo Using Ninja build generator.
\r
381 set CMAKE_GENERATOR=Ninja
\r
382 set LLVM_BUILD_OUTPUT_DIR=%LLVM_BUILD_DIR%
\r
384 :_SETUP_CMAKE_ENVIRONMENT_EXIT
\r