Commands for building LLVM/Clang on Windows.
-
Create a directory to sync the LLVM Project repo to.
cd ${SRC_ROOT} # root folder for LLVM repo git clone --depth 1 https://github.com/llvm/llvm-project .
-
From the root folder, configure the build for LLVM.
cmake -S llvm/ -B build/ -G "Visual Studio 16 2019"^ -DCMAKE_GENERATOR_PLATFORM=x64^ -Thost=x64^ -DLLVM_INCLUDE_TESTS=OFF^ -DLLVM_BUILD_TOOLS=ON^ -DLLVM_INCLUDE_UTILS=OFF^ -DLLVM_TARGETS_TO_BUILD=X86^ -DCLANG_ENABLE_STATIC_ANALYZER=OFF^ -DCLANG_ENABLE_ARCMT=OFF^ -DLLVM_ENABLE_PROJECTS="clang;clang-tools-extra;lld"
-
Build LLVM/Clang.
cmake --build build/ --target install --config Release
- The
--target install
command will copy built files (.lib, .exe etc.) toC:\Program Files (x86)\LLVM
by default. This location can be customized by setting the-DCMAKE_INSTALL_PREFIX
variable when running the configure step.- If a custom install location isn't set and a Debug version of the library is built/installed, the files will overwrite the previous Release files (whichever you built last will win). Please see the example files in examples/llvm-build-scripts for an example of how to install both Debug and Release versions using
-DCMAKE_INSTALL_PREFIX
.
- If a custom install location isn't set and a Debug version of the library is built/installed, the files will overwrite the previous Release files (whichever you built last will win). Please see the example files in examples/llvm-build-scripts for an example of how to install both Debug and Release versions using
- The
-
More information can be found here on using LLVM and CMake.
It is possible to use the Ninja build system on Windows (even when using the MSVC (cl.exe
) compiler) as opposed to MSBuild
. This is useful if you want to generate a compile_commands.json
file for your project.
- If you wish to use your normal terminal (e.g. cmd.exe, cmder, Windows Terminal etc.) Ninja must be added to your path.
- Potential locations include:
C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\Common7\IDE\CommonExtensions\Microsoft\CMake\Ninja\ninja.exe
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\IDE\CommonExtensions\Microsoft\CMake\Ninja\ninja.exe
- Potential locations include:
- To use the Ninja generator from your normal terminal with MSVC or Clang you must also run
vcvars64.bat
. This is so CMake knows the location of the compiler (cl.exe
) and libraries (kernel32.lib
etc.).- Potential locations include:
"C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Auxiliary\Build\vcvars64.bat"
"C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvars64.bat"
- Potential locations include:
- It is also possible to simply launch the Developer Command Prompt for Visual Studio to have these options added automatically to the environment.
The exact location of
ninja.exe
andvcvars64.bat
may differ depending on which version of Visual Studio you have installed (year/edition etc.).
There are several options available when building your project.
-
If you would like to use the full Visual Studio tool-chain then you can download C++ Clang Compiler for Windows (9.0.0) from the Visual Studio Installer (Modify - Individual Components - (search 'llvm')).
-
To build using MSBuild and a custom Clang build, download C++ Clang-cl for v142 build tools (x64/x86) (Modify - Individual Components - (search 'llvm')).
# configure with MSBuild generator and Clang compiler cmake -S . -B build/ -G "Visual Studio 16 2019" -A x64 -T clangcl^ -DCMAKE_CXX_COMPILER="C:/Program Files (x86)/LLVM/bin/clang++.exe" # optional - custom Clang location # build with MSBuild cmake --build build --config Release
- For the above to work, you must add the location where you installed
clang
to your Path, or have the previously mentioned C++ Clang Compiler for Windows (9.0.0) downloaded. By default Visual Studio looks for Clang in two places (source):-
(Windows) The internally installed copy of Clang/LLVM that comes with the Visual Studio installer.
-
(Windows and Linux) The PATH environment variable.
If neither of these are present than the CMake configure step will fail. Afterwards you can still customize the location of the compiler with
CMAKE_CXX_COMPILER
once the default has been located.# configure with Ninja and clang compiler cmake -S . -B build-rel/ -G Ninja^ -DCMAKE_BUILD_TYPE=Release^ -DCMAKE_CXX_COMPILER="C:/Program Files (x86)/LLVM/bin/ clang-cl.exe"^ # build with Ninja cmake --build build
-
- For the above to work, you must add the location where you installed
-
To verify which compiler is being used to build, pass
-v
(--verbose
) to thecmake --build <folder>
command.cmake --build build -v
In addition to being able to use Clang to compile, with Clang/LLVM installed, it is possible to build projects making using of libTooling
. It is possible to use the libTooling
libraries with MSVC or Clang.
Please refer to use-llvm-simple/CMakeLists.txt for an example of how to setup a project that uses Clang and LLVM as dependencies and the use-llvm-simple/configure-debug.bat for how to configure the project.
With the LLVM and Clang libraries installed it's possible to build include-what-you-use
to analyse C and C++ source files.
Please see examples/iwyu-build-scripts for commands to build the library.
Once iwyu
is built, add the location of include-what-you-use.exe
to your path and then invoke iwyu_tool.py
in the directory of a compile_commands.json
file of a project you've already built.
# analyse includes, write output to file
python C:\path\to\iwyu\iwyu_tool.py -p . > iwyu.out
# apply fixes
python C:\path\to\iwyu\fix_includes.py < iwyu.out --blank_lines --nocomments --reorder
# optional --dry_run
Please find more information on include-what-you-use
here.