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;ARM;AArch64" ^
\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;llvm-as" ^
\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 if not exist "%LLVM_INSTALL_DIR%\bin\llvm-dis.exe" (
\r
270 echo Missing LLVM build output, "%LLVM_INSTALL_DIR%\bin\llvm-dis.exe"
\r
274 if not exist "%LLVM_INSTALL_DIR%\bin\llvm-mc.exe" (
\r
275 echo Missing LLVM build output, "%LLVM_INSTALL_DIR%\bin\llvm-mc.exe"
\r
279 if not exist "%LLVM_INSTALL_DIR%\bin\llvm-as.exe" (
\r
280 echo Missing LLVM build output, "%LLVM_INSTALL_DIR%\bin\llvm-as.exe"
\r
284 copy /Y "%LLVM_INSTALL_DIR%\bin\opt.exe" "%MONO_DIST_DIR%" >nul 2>&1
\r
285 copy /Y "%LLVM_INSTALL_DIR%\bin\llc.exe" "%MONO_DIST_DIR%" >nul 2>&1
\r
286 copy /Y "%LLVM_INSTALL_DIR%\bin\llvm-dis.exe" "%MONO_DIST_DIR%" >nul 2>&1
\r
287 copy /Y "%LLVM_INSTALL_DIR%\bin\llvm-mc.exe" "%MONO_DIST_DIR%" >nul 2>&1
\r
288 copy /Y "%LLVM_INSTALL_DIR%\bin\llvm-as.exe" "%MONO_DIST_DIR%" >nul 2>&1
\r
294 if exist "%LLVM_BUILD_DIR%\build.ninja" (
\r
296 cd "%LLVM_BUILD_DIR%"
\r
297 call "%NINJA%" clean
\r
301 if exist "%LLVM_BUILD_DIR%\llvm.sln" (
\r
302 "%MSBUILD%" "%LLVM_BUILD_DIR%\llvm.sln" /p:Configuration=%VS_CONFIGURATION% /p:Platform=%VS_PLATFORM% /t:Clean /v:m /nologo
\r
313 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
316 echo Failed to build LLVM.
\r
320 set PATH=%TEMP_PATH%
\r
321 exit /b %BUILD_RESULT%
\r
323 :: ##############################################################################################################################
\r
326 :: --------------------------------------------------
\r
327 :: Finds a program using environment.
\r
329 :: %1 Existing program to check for.
\r
330 :: %2 Name of binary to locate.
\r
331 :: %3 Output, variable to set if found requested program.
\r
332 :: --------------------------------------------------
\r
335 :: If not set by caller, check environment for program.
\r
340 call where /q "%~2" && (
\r
341 for /f "delims=" %%a in ('where "%~2"') do (
\r
350 :: --------------------------------------------------
\r
351 :: Setup up cmake build environment, including generator, build tools and variables.
\r
352 :: --------------------------------------------------
\r
353 :SETUP_CMAKE_ENVIRONMENT
\r
355 :: If not set by caller, check environment for working cmake.exe.
\r
356 call :FIND_PROGRAM "%CMAKE%" "%CMAKE_BIN_NAME%" CMAKE
\r
357 if "%CMAKE%" == "" (
\r
358 goto _SETUP_CMAKE_ENVIRONMENT_EXIT
\r
361 if /i "%VS_TARGET%" == "build" (
\r
362 echo Found CMake: %CMAKE%
\r
365 :: Check for optional cmake generate and build tools.
\r
366 call :FIND_PROGRAM "%NINJA%" "%NINJA_BIN_NAME%" NINJA
\r
368 if not "%NINJA%" == "" (
\r
369 goto _SETUP_CMAKE_ENVIRONMENT_NINJA_GENERATOR
\r
372 :_SETUP_CMAKE_ENVIRONMENT_VS_GENERATOR
\r
374 if /i "%VS_TARGET%" == "build" (
\r
375 echo Using Visual Studio build generator.
\r
378 :: Detect VS version to use right cmake generator.
\r
379 set CMAKE_GENERATOR=Visual Studio 14 2015
\r
380 if "%VisualStudioVersion%" == "15.0" (
\r
381 set CMAKE_GENERATOR=Visual Studio 15 2017
\r
384 if /i "%VS_PLATFORM%" == "x64" (
\r
385 set CMAKE_GENERATOR=%CMAKE_GENERATOR% Win64
\r
388 set LLVM_BUILD_OUTPUT_DIR=%LLVM_BUILD_DIR%\%VS_CONFIGURATION%
\r
390 goto _SETUP_CMAKE_ENVIRONMENT_EXIT
\r
392 :_SETUP_CMAKE_ENVIRONMENT_NINJA_GENERATOR
\r
394 if /i "%VS_TARGET%" == "build" (
\r
395 echo Found Ninja: %NINJA%
\r
396 echo Using Ninja build generator.
\r
399 set CMAKE_GENERATOR=Ninja
\r
400 set LLVM_BUILD_OUTPUT_DIR=%LLVM_BUILD_DIR%
\r
402 :_SETUP_CMAKE_ENVIRONMENT_EXIT
\r