Thursday, November 11, 2021

[SOLVED] Unable to compile llvm pass on Windows using Visual studio

Issue

Im learning how to use the llvm toolchain on Windows. I compiled and installed llvm using the following cmake command

cmake -S llvm -B build -DLLVM_ENABLE_PROJECTS="clang;lld" -DLLVM_USE_LINKER=lld -DLLVM_TARGETS_TO_BUILD=X86 -DCMAKE_INSTALL_PREFIX="C:\llvm" -Thost=x64

Running the INSTALL project in the solution created in the build folder installed llvm in c:\llvm. To test my llvm installation i created a new Visual Studio project and placed a Directory.build.props file in the project root with the following contents:

<Project>
  <PropertyGroup>
    <LLVMInstallDir>C:\llvm</LLVMInstallDir>
    <LLVMToolsVersion>14.0.0</LLVMToolsVersion>
  </PropertyGroup>
</Project>

I set the project toolchain to LLVM (clang-cl) in project settings and successfully compiled a hello world program.
Now i wanna use llvm for it's intended use and play around with some passes. So i downloaded https://github.com/tsarpaul/llvm-string-obfuscator.
Running cmake -Bbuild -DLLVM_DIR=C:\llvm\lib\cmake\llvm\ generated a Visual Studio solution in .\build.
So far so good i tought. This is similar to the steps required to compile llvm itself.
But running the BUILD_ALL project in the generated solution gave me 105 linker errors.
Errors

I have pasted the entire error output here: https://pastebin.com/TAizbAEi.
I have a feeling the required libraries in C:\llvm\lib are not visible to Visual Studio. But i have no idea how to proceed. Could anyone point me in the right direction on how to use llvm with visual studio correctly?


Solution

Solved by adding to the cmake file:

llvm_map_components_to_libnames(llvm_libs core)
target_link_libraries(LLVMStringObfuscator ${llvm_libs})

Now the dll build. I still have problems using opt.exe

PS C:\Users\Administrator\source\repos> C:\llvm\bin\opt.exe -load-pass-plugin=.\11philip22\llvm-string-obfuscator\build\StringObfuscator\Release\LLVMStringObfuscator.dll -passes="string-obfuscator-pass"
C:\llvm\bin\opt.exe: .\11philip22\llvm-string-obfuscator\build\StringObfuscator\Release\LLVMStringObfuscator.dll:1:1: error: expected top-level entity
MZÉ           ╕       @                                   ║ ┤  ═!╕L═!This program cannot be run in DOS mode.

But that's a new problem for another thread :)



Answered By - woldgrep