inkwell/llvm-sys not able to compile on windows

126 Views Asked by At

I am working with LLVM to build a small compiler in Rust using inkwell (inkwell uses llvm-sys, so it is relevant to it too).

First of all, compiled windows binaries on llvm's github do not include needed tools for building on top of llvm. Also after compiling llvm from sources, I was having an issues of not able to compile said crates, having different errors saying things like "Files/LLVM/Target.h not found!", etc.

First of all some solutions that worked for some people:

  1. Cleaning the project (cargo clean)

  2. Adding "<installation path>/LLVM/bin" to env Path.

  3. If you are using inkwell, you still need to reference appropriate llvm-sys version in Cargo.toml:

    llvm-sys-160 = { package="llvm-sys", version = "160.1.4" } # For LLVM 16.x
    
1

There are 1 best solutions below

0
MORAS On

Turns out llvm-sys doesn't understand spaces in the path to LLVM (default installation path for LLVM is C:\Program Files\LLVM\). So the whole solution will be install it into the directory with no spaces:

(Delete LLVM, if you already have one, -> install cmake and visual studio (I used 2022))

(Everything is done from VS Developer Console)

  1. From Visual Studio Installer you need make sure you have Visual C++ ATL v141-143 or C++ ATL for latest v141-143
  2. git clone needed version of llvm using: git clone https://github.com/llvm/llvm-project.git --branch release/16.x (change the release version to one you need, it has to have format of release/<major>.x)
  3. cd llvm-project -> cd llvm -> mkdir build -> cd build
  4. cmake .. "-DCMAKE_INSTALL_PREFIX=C:\LLVM\" (add more cmake flags if you want)
  5. after the config is done (do not exit build directory): cmake --build . --target install --config Release
  6. wait until the build is fully done, and then check if C:\LLVM\bin is in your PATH, if not add it. Done, this should allows the llvm-sys/inkwell to build.