├── README.md ├── build-llvm.bat └── build-mingw.sh /README.md: -------------------------------------------------------------------------------- 1 | # LLVM-Build-Windows 2 | 3 | This is a helpful batch script to automate building the LLVM toolchain on Windows using MSVC or Clang itself! 4 | 5 | Credits to MSYS project and [Matthew Oliver](https://github.com/Sibras) 6 | 7 | ## Usage 8 | 9 | NOTE: If you want to build clang with clang itself, use the `Ninja` generator instead. 10 | 11 | 1. Open a command prompt (cmd.exe) and change directory to the root of where you want the root of your LLVM directory tree. 12 | 13 | >cd C:\llvm-7.0.0 14 | 15 | 2. Place the build scripts at the root of the LLVM directory tree. 16 | 17 | >git clone https://github.com/ajkhoury/LLVM-Build-Windows . 18 | 19 | 3. See here on how to obtain the LLVM source code for building: 20 | 21 | https://llvm.org/docs/GettingStarted.html#getting-started-quickly-a-summary 22 | 23 | 4. Use the `-help` argument for the script for more information: 24 | 25 | >build-llvm.bat -help 26 | Detected 64 bit system... 27 | Usage: 28 | build-llvm.bat [options] 29 | 30 | Options: 31 | -configure Configure the project 32 | -build Build the project 33 | -static Configure and build the project statically 34 | -debug Build project with debug configuration 35 | -target ARCH Set the target architecture 36 | -directory DIRECTORY Use specified DIRECTORY for build directory 37 | -help | --help | -? | /? Display this help and exit 38 | 39 | 4. Run the build script for the target of your choice: 40 | 41 | x64 42 | 43 | >build-llvm.bat -configure -build -static -target x64 -directory build-release-x64 44 | 45 | x86 46 | 47 | >build-llvm.bat -configure -build -static -target x86 -directory build-release-x86 48 | -------------------------------------------------------------------------------- /build-llvm.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | setlocal 3 | 4 | REM 5 | REM Convert backslashes to forward slashes for peace of mind. 6 | REM 7 | set CWD=%~dp0 8 | set "CWD=%CWD:\=/%" 9 | 10 | REM 11 | REM Check what architecture we are running on 12 | REM 13 | if "%PROCESSOR_ARCHITECTURE%"=="AMD64" ( 14 | echo Detected 64 bit system... 15 | set SYSARCH=64 16 | ) else if "%PROCESSOR_ARCHITECTURE%"=="x86" ( 17 | if "%PROCESSOR_ARCHITEW6432%"=="AMD64" ( 18 | echo Detected 64 bit system running 32 bit shell... 19 | set SYSARCH=64 20 | ) else ( 21 | echo Detected 32 bit system... 22 | set SYSARCH=32 23 | ) 24 | ) else ( 25 | echo Error: Could not detect current platform architecture!" 26 | exit /b 5 27 | ) 28 | 29 | REM 30 | REM Iterate command line parameters 31 | REM 32 | :checkparams 33 | REM Help option 34 | if "x%~1" == "x-help" ( 35 | call :printhelp "%~nx0" 36 | exit /b %ERRORLEVEL% 37 | ) 38 | if "x%~1" == "x--help" ( 39 | call :printhelp "%~nx0" 40 | exit /b %ERRORLEVEL% 41 | ) 42 | if "x%~1" == "x-?" ( 43 | call :printhelp "%~nx0" 44 | exit /b %ERRORLEVEL% 45 | ) 46 | if "x%~1" == "x/?" ( 47 | call :printhelp "%~nx0" 48 | exit /b %ERRORLEVEL% 49 | ) 50 | if "x%~1" == "x-here" shift& set CHERE_INVOKING=enabled_from_arguments& goto :checkparams 51 | if "x%~1" == "x-target" ( 52 | if "x%~2" == "x" ( 53 | echo Architecture was not defined for -target parameter. 1>&2 54 | exit /b 2 55 | ) 56 | set TARGET_ARCH=%~2 57 | )& shift& shift& goto :checkparams 58 | if "x%~1" == "x-directory" ( 59 | if "x%~2" == "x" ( 60 | echo Build directory is not specified for -builddir parameter. 1>&2 61 | exit /b 2 62 | ) 63 | if defined BUILD_DIR ( 64 | echo Build directory cannot be defined more than once. 1>&2 65 | exit /b 3 66 | ) 67 | if exist %~2 goto skip1 68 | echo Making new build directory "%~2" 69 | mkdir %~2 70 | :skip1 71 | cd /d "%~2" || ( 72 | echo Cannot set specified build directory "%~2". 1>&2 73 | exit /b 2 74 | ) 75 | set BUILD_DIR=%~2 76 | )& shift& shift& goto :checkparams 77 | if "x%~1" == "x-configure" shift& set CONFIGURE=yes& goto :checkparams 78 | if "x%~1" == "x-build" shift& set BUILD=yes& goto :checkparams 79 | if "x%~1" == "x-static" shift& set STATIC=yes& goto :checkparams 80 | if "x%~1" == "x-debug" shift& set DEBUG=yes& goto :checkparams 81 | 82 | :next 83 | if not defined CONFIGURE ( 84 | echo CONFIGURE=no 85 | goto nextparam1 86 | ) else ( 87 | echo CONFIGURE=%CONFIGURE% 88 | goto nextparam1 89 | ) 90 | :nextparam1 91 | if not defined BUILD ( 92 | echo BUILD=no 93 | goto nextparam2 94 | ) else ( 95 | echo BUILD=%BUILD% 96 | goto nextparam2 97 | ) 98 | :nextparam2 99 | if not defined STATIC ( 100 | echo STATIC=no 101 | goto nextparam3 102 | ) else ( 103 | echo STATIC=%STATIC% 104 | goto nextparam3 105 | ) 106 | :nextparam3 107 | if not defined DEBUG ( 108 | echo DEBUG=no 109 | goto nextparam4 110 | ) else ( 111 | echo DEBUG=%DEBUG% 112 | goto nextparam4 113 | ) 114 | :nextparam4 115 | if not defined TARGET_ARCH ( 116 | set TARGET_ARCH=amd64 117 | echo Defaulted TARGET_ARCH to amd64 118 | goto nextparam5 119 | ) else ( 120 | echo TARGET_ARCH=%TARGET_ARCH% 121 | goto nextparam5 122 | ) 123 | :nextparam5 124 | echo BUILD_DIR=%BUILD_DIR% 125 | 126 | REM 127 | REM Check if already running in an environment with VS setup 128 | REM 129 | if defined VCINSTALLDIR ( 130 | if defined VisualStudioVersion ( 131 | echo Existing Visual Studio environment version %VisualStudioVersion% detected... 132 | if "%VisualStudioVersion%"=="15.0" ( 133 | set MSVC_VER=15 134 | goto msvcdone 135 | ) else if "%VisualStudioVersion%"=="14.0" ( 136 | set MSVC_VER=14 137 | goto msvcdone 138 | ) else if "%VisualStudioVersion%"=="12.0" ( 139 | set MSVC_VER=12 140 | goto msvcdone 141 | ) else ( 142 | echo Unknown Visual Studio environment detected '%VisualStudioVersion%', Creating a new one... 143 | ) 144 | ) 145 | ) 146 | REM 147 | REM Check for default install locations based on current system architecture 148 | REM 149 | if "%SYSARCH%"=="32" ( 150 | set MSVCVARSDIR= 151 | set WOWNODE= 152 | ) else if "%SYSARCH%"=="64" ( 153 | set MSVCVARSDIR=\amd64 154 | set WOWNODE=\WOW6432Node 155 | ) else ( 156 | echo Error: Running on invalid architecture! 157 | exit /b 5 158 | ) 159 | echo TARGET_ARCH=%TARGET_ARCH% 160 | if "%TARGET_ARCH%" == "64" ( 161 | set TARGET_ARCH_BITS=64 162 | ) else if "%TARGET_ARCH%" == "amd64" ( 163 | set TARGET_ARCH_BITS=64 164 | ) else if "%TARGET_ARCH%" == "x64" ( 165 | set TARGET_ARCH_BITS=64 166 | ) else if "%TARGET_ARCH%" == "32" ( 167 | set TARGET_ARCH_BITS=32 168 | ) else if "%TARGET_ARCH%" == "Win32" ( 169 | set TARGET_ARCH_BITS=32 170 | ) else if "%TARGET_ARCH%" == "x86" ( 171 | set TARGET_ARCH_BITS=32 172 | ) else ( 173 | echo Error: Invalid target architecture specified! 174 | exit /b 5 175 | ) 176 | reg.exe query "HKLM\SOFTWARE%WOWNODE%\Microsoft\VisualStudio\SxS\VS7" /v "15.0" >nul 2>&1 177 | if not ERRORLEVEL 1 ( 178 | echo Visual Studio 2017 installation detected... 179 | for /f "skip=2 tokens=2,*" %%a in ('reg.exe query "HKLM\SOFTWARE%WOWNODE%\Microsoft\VisualStudio\SxS\VS7" /v "15.0"') do ( 180 | if exist "%%bVC\Auxiliary\Build\vcvars%TARGET_ARCH_BITS%.bat" ( 181 | set VSINSTALLDIR=%%b 182 | echo Found vcvars file at "%%bVC\Auxiliary\Build\vcvars%TARGET_ARCH_BITS%.bat" 183 | call "%%bVC\Auxiliary\Build\vcvars%TARGET_ARCH_BITS%.bat" >nul 2>&1 184 | goto breakoutmsvc15 185 | ) else ( 186 | echo Error: vcvars "%%bVC\Auxiliary\Build\vcvars%TARGET_ARCH_BITS%.bat" not found! 187 | ) 188 | ) 189 | exit /b 5 190 | :breakoutmsvc15 191 | set MSVC_VER=15 192 | goto msvcdone 193 | ) 194 | reg.exe query "HKLM\Software%WOWNODE%\Microsoft\VisualStudio\14.0" /v "InstallDir" >nul 2>&1 195 | if not ERRORLEVEL 1 ( 196 | echo Visual Studio 2015 installation detected... 197 | for /f "skip=2 tokens=2,*" %%a in ('reg.exe query "HKLM\Software%WOWNODE%\Microsoft\VisualStudio\14.0" /v "InstallDir"') do ( 198 | if exist "%%b..\..\VC\bin%MSVCVARSDIR%\vcvars%TARGET_ARCH_BITS%.bat" ( 199 | set VSINSTALLDIR=%%b..\..\ 200 | echo Found vcvars file at "%%b..\..\VC\bin%MSVCVARSDIR%\vcvars%TARGET_ARCH_BITS%.bat" 201 | call "%%b..\..\VC\bin%MSVCVARSDIR%\vcvars%TARGET_ARCH_BITS%.bat" >nul 2>&1 202 | goto breakoutmsvc14 203 | ) else ( 204 | echo Error: vcvars "%%b..\..\VC\bin%MSVCVARSDIR%\vcvars%TARGET_ARCH_BITS%.bat" not found! 205 | ) 206 | ) 207 | exit /b 5 208 | :breakoutmsvc14 209 | set MSVC_VER=14 210 | goto msvcdone 211 | ) 212 | reg.exe query "HKLM\Software%WOWNODE%\Microsoft\VisualStudio\12.0" /v "InstallDir" >nul 2>&1 213 | if not ERRORLEVEL 1 ( 214 | echo Visual Studio 2013 installation detected... 215 | for /f "skip=2 tokens=2,*" %%a in ('reg.exe query "HKLM\Software%WOWNODE%\Microsoft\VisualStudio\12.0" /v "InstallDir"') do ( 216 | if exist "%%b..\..\VC\bin%MSVCVARSDIR%\vcvars%TARGET_ARCH_BITS%.bat" ( 217 | set VSINSTALLDIR=%%b..\..\ 218 | echo Found vcvars file at "%%b..\..\VC\bin%MSVCVARSDIR%\vcvars%TARGET_ARCH_BITS%.bat" 219 | call "%%b..\..\VC\bin%MSVCVARSDIR%\vcvars%TARGET_ARCH_BITS%.bat" >nul 2>&1 220 | goto breakoutmsvc12 221 | ) else ( 222 | echo Error: vcvars "%%b..\..\VC\bin%MSVCVARSDIR%\vcvars%TARGET_ARCH_BITS%.bat" not found! 223 | ) 224 | ) 225 | exit /b 5 226 | :breakoutmsvc12 227 | set MSVC_VER=12 228 | goto msvcdone 229 | ) 230 | reg.exe query "HKLM\Software%WOWNODE%\Microsoft\VisualStudio\9.0" /v "InstallDir" >nul 2>&1 231 | if not ERRORLEVEL 1 ( 232 | echo Visual Studio 2008 installation detected... 233 | for /f "skip=2 tokens=2,*" %%a in ('reg.exe query "HKLM\Software%WOWNODE%\Microsoft\VisualStudio\12.0" /v "InstallDir"') do ( 234 | if exist "%%b..\..\VC\bin%MSVCVARSDIR%\vcvars%TARGET_ARCH_BITS%.bat" ( 235 | set VSINSTALLDIR=%%b..\..\ 236 | echo Found vcvars file at "%%b..\..\VC\bin%MSVCVARSDIR%\vcvars%TARGET_ARCH_BITS%.bat" 237 | call "%%b..\..\VC\bin%MSVCVARSDIR%\vcvars%TARGET_ARCH_BITS%.bat" >nul 2>&1 238 | goto breakoutmsvc9 239 | ) else ( 240 | echo Error: vcvars "%%b..\..\VC\bin%MSVCVARSDIR%\vcvars%TARGET_ARCH_BITS%.bat" not found! 241 | ) 242 | ) 243 | exit /b 5 244 | :breakoutmsvc9 245 | set MSVC_VER=9 246 | goto msvcdone 247 | ) 248 | REM 249 | REM If none were found check for an environment variable to help locate the VS installation 250 | REM 251 | if defined VS140COMNTOOLS ( 252 | if exist "%VS140COMNTOOLS%\..\..\VC\vcvars%TARGET_ARCH_BITS%.bat" ( 253 | set VSINSTALLDIR=%VS140COMNTOOLS%\..\..\ 254 | echo Visual Studio 2015 environment detected through environment variable... 255 | call "%VS140COMNTOOLS%\..\..\VC\vcvars%TARGET_ARCH_BITS%.bat" >nul 2>&1 256 | set MSVC_VER=14 257 | goto msvcdone 258 | ) 259 | ) 260 | if defined VS120COMNTOOLS ( 261 | if exist "%VS120COMNTOOLS%\..\..\VC\vcvars%TARGET_ARCH_BITS%.bat" ( 262 | set VSINSTALLDIR=%VS120COMNTOOLS%\..\..\ 263 | echo Visual Studio 2013 environment detected through environment variable... 264 | call "%VS120COMNTOOLS%\..\..\VC\vcvars%TARGET_ARCH_BITS%.bat" >nul 2>&1 265 | set MSVC_VER=12 266 | goto msvcdone 267 | ) 268 | ) 269 | echo Error: Could not find valid Visual Studio installation! 270 | exit /b 6 271 | 272 | :msvcdone 273 | 274 | echo VSINSTALLDIR=%VSINSTALLDIR% 275 | 276 | :checkconfigure 277 | if defined CONFIGURE ( 278 | echo Configuring... 279 | goto doconfigure 280 | ) 281 | :checkbuild 282 | if defined BUILD ( 283 | echo Building... 284 | goto dobuild 285 | ) 286 | 287 | :doconfigure 288 | if not defined DEBUG (set BUILD_TYPE=Release) else (set BUILD_TYPE=Debug) 289 | if not defined STATIC (set CRT_TYPE_RELEASE=MD& set CRT_TYPE_DEBUG=MDd) else (set CRT_TYPE_RELEASE=MT& set CRT_TYPE_DEBUG=MTd) 290 | if "x%TARGET_ARCH_BITS%" == "x64" (set ARCH=x64) else (set ARCH=x86) 291 | if "x%ARCH%" == "xx64" (set LLVM_BIN_PATH=C:/PROGRA~1/LLVM/bin) else (set LLVM_BIN_PATH=C:/PROGRA~2/LLVM/bin) 292 | if "x%ARCH%" == "xx64" (set GENERATOR_PLATFORM=x64) else (set GENERATOR_PLATFORM=) 293 | 294 | set ZLIB_PATH=%CWD%third-party/zlib 295 | set ZLIB_LIBRARY_PATH=%ZLIB_PATH%/%ARCH% 296 | set ZLIB_INCLUDE_PATH=%ZLIB_PATH% 297 | echo ZLIB_PATH is %ZLIB_PATH% 298 | 299 | cmake -H. -G"Visual Studio 15 2017" -Bbuild^ 300 | -DBUILD_SHARED_LIBS=Off^ 301 | -DLLVM_INCLUDE_TESTS=Off^ 302 | -DLLVM_ENABLE_RTTI=0^ 303 | -DLLVM_ENABLE_DIA_SDK=1^ 304 | -DLLVM_USE_CRT_RELEASE=%CRT_TYPE_RELEASE%^ 305 | -DLLVM_USE_CRT_DEBUG=%CRT_TYPE_DEBUG%^ 306 | -DLLVM_TARGETS_TO_BUILD="X86"^ 307 | -DHAVE_LIBZ=1^ 308 | -DCMAKE_C_FLAGS="/I%ZLIB_INCLUDE_PATH% /DLLVM_ENABLE_DIA_SDK"^ 309 | -DCMAKE_CXX_FLAGS="/I%ZLIB_INCLUDE_PATH% /DLLVM_ENABLE_DIA_SDK"^ 310 | -DCMAKE_EXE_LINKER_FLAGS="%ZLIB_LIBRARY_PATH%/zlibstatic.lib"^ 311 | -DCMAKE_STATIC_LINKER_FLAGS="%ZLIB_LIBRARY_PATH%/zlibstatic.lib"^ 312 | -DCMAKE_SHARED_LINKER_FLAGS="%ZLIB_LIBRARY_PATH%/zlibstatic.lib"^ 313 | -DCMAKE_GENERATOR_PLATFORM=%GENERATOR_PLATFORM%^ 314 | -DCMAKE_BUILD_TYPE=%BUILD_TYPE%^ 315 | %CWD% 316 | 317 | if defined BUILD goto checkbuild 318 | exit /b 0 319 | 320 | :dobuild 321 | cmake --build . --config %BUILD_TYPE% 322 | exit /b 0 323 | 324 | :printhelp 325 | echo Usage: 326 | echo %~1 [options] 327 | echo. 328 | echo Options: 329 | echo -configure Configure the project 330 | echo -build Build the project 331 | echo -static Configure and build the project statically 332 | echo -debug Build project with debug configuration 333 | echo -target ARCH Set the target architecture: [ 64 ^| amd64 ^| x64 ^| 32 ^| Win32 ^| x86 ] 334 | echo -directory DIRECTORY Use specified DIRECTORY for build directory 335 | echo -help ^| --help ^| -? ^| /? Display this help and exit 336 | echo. 337 | echo Examples: 338 | echo build-llvm.bat -configure -build -static -target x64 -directory build-release-x64 339 | echo build-llvm.bat -configure -build -static -target x86 -directory build-release-x32 340 | echo. 341 | exit /b 0 342 | 343 | pause>nul 344 | -------------------------------------------------------------------------------- /build-mingw.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # For building using MINGW toolchain 3 | 4 | cmake -H. -G"Ninja" -Bbuild \ 5 | -DCMAKE_BUILD_TYPE=Release \ 6 | -DBUILD_SHARED_LIBS=false \ 7 | -DLLVM_BUILD_STATIC=true \ 8 | -DLLVM_ENABLE_RTTI=0 \ 9 | -DLLVM_USE_CRT_RELEASE=MT \ 10 | -DLLVM_USE_CRT_DEBUG=MTd \ 11 | -DLIBCXX_INSTALL_HEADERS=ON \ 12 | -DLIBCXX_ENABLE_EXCEPTIONS=ON \ 13 | -DLIBCXX_ENABLE_THREADS=ON \ 14 | -DLIBCXX_HAS_WIN32_THREAD_API=ON \ 15 | -DLIBCXX_ENABLE_MONOTONIC_CLOCK=ON \ 16 | -DLIBCXX_ENABLE_SHARED=OFF \ 17 | -DLIBCXX_SUPPORTS_STD_EQ_CXX11_FLAG=TRUE \ 18 | -DLIBCXX_HAVE_CXX_ATOMICS_WITHOUT_LIB=TRUE \ 19 | -DLIBCXX_ENABLE_EXPERIMENTAL_LIBRARY=OFF \ 20 | -DLIBCXX_ENABLE_FILESYSTEM=OFF \ 21 | -DLIBCXX_ENABLE_STATIC_ABI_LIBRARY=TRUE \ 22 | -DLIBCXX_CXX_ABI=libcxxabi \ 23 | -DCMAKE_C_FLAGS_RELEASE="-s -Wl,--strip-all" \ 24 | -DCMAKE_CXX_FLAGS_RELEASE="-s -Wl,--strip-all" \ 25 | -DCMAKE_EXE_LINKER_FLAGS="-static" \ 26 | .. --------------------------------------------------------------------------------