A concise guide to building LLVM & Clang on Windows with MSVC

So, let’s say you want to use clang to do some AST analysis on Windows. You did a simple search, and downloaded the installer from LLVM project site. Everthing went well, until you found there is something missing in the installed LLVM headers folder

Perhaps you haven’t noticed anything. In most cases that’s fine, but unfortunately I’m compiling ccls, which needs the AST parsing functionality offered by Clang. Obviously, they decided to exclude this part of the feature in the packed installer.

Then, well, I have no choice but to compile LLVM & Clang by myself.

I have VS2017/2019 on my hand, and here is the official guide: Getting Started with the LLVM System using Microsoft Visual Studio.

Though I’m totally agree that you need to read the documentation (twice), here is basically what you need to do

  • Download & install CMake toolchain. We need cmake-gui and we will generate a VS Solution with 200+ vcxproj files.
  • Download source code as what the documentation says.
    • If you are downloading source code from LLVM download page, please please please ensure to copy Clang source code to LLVM_ROOT_PATH\tools\clang
  • Open cmake-gui. Though it should work properly without VS environment variables, you may try to open cmake-gui explicitly from VS developer commandline (x64) if you find cmake having trouble searching for your MSVC compiler.
  • enter the path of your source code, and the path to place generated VS Solution & built binaries. Then click “Configure”
  • For x64 toolchain, we fill host=x64 in the -T arguments. Click “Finish” to continue
  • Wait a moment until CMake configuration completes
  • In the build parameters list, fill “X86” for LLVM_TARGETS_TO_BUILD. This is actually for x64 architecture. Note the parameter is case-sensitive.
    • For more architectures supported by the source code (ARM, MSP430, WebAssembly, etc.), see the lines starting with “Targeting” in the output box.
  • Click “Generate” to generate Visual Studio projects.
  • Do not click “Open Project” now, because the build is likely to fail due to the absence of VSINSTALLDIR in the environment. Instead, open x64 Native Tools Command Prompt for VS, then enter devenv to open Visual Studio from the console.
  • Ensure you have enough physical memory. Close Chrome if necessary. I have 16GB RAM in total, and ~10GB free at that moment. Nevertheless, the page file is still abused. It definitely helps if your page file is on SSD; so is your LLVM+Clang building directory.
  • Choose a build configuration ( Debug or RelWithDebInfo). On project ALL_BUILD, right-click, then “Build Project”. You do not need to build the whole solution.
    • As an alternative approach (and to save ~1GB RAM), you can build the project with MSBuild directly from the command prompt you just opened. Use -m to explicitly specify the max concurrency level
  • Grab a coffee. Wait for the build. It took me about 15min to build V7.0.1 source code from official downloads, and 35min to build V8.0.0-rc2 from GitHub repo. The build abuses CPU, RAM, and your disk. So get prepared. The intermediate files and built binary took ~70 GB disk for Debug + RelWithDebInfo builds.
  • On project INSTALL, right-click; then “Build Project”. All the binary, library, header stuff should be copied to your C:\Program Files\LLVM folder when it completes. Remember to rename the folder before you INSTALL a different configuration, or the old folder will be replaced.

Yep. Now I have everything I need.

 

Leave a Reply

Your email address will not be published. Required fields are marked *

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.