Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Static Linking clang_sys on msvc/windows cannot find libraries, e.g. libclang.lib #184

Open
Cody-Duncan opened this issue Jun 14, 2024 · 0 comments

Comments

@Cody-Duncan
Copy link

Cody-Duncan commented Jun 14, 2024

I ran into a bunch of problems to get clang_sys statically linking with binaries built via msvc on Windows
Documenting the found issues here for a future fix.


Process to get static linking on clang 18.1.7

Stage 1: cmake configuration

Configuring cmake on llvm using the generator "Visual Studio 17 2022",
requires that LLVM_ENABLE_PIC also be turned off. Otherwise, it unsets LIBCLANG_BUILD_STATIC and builds shared DLL libraries.
-DLIBCLANG_BUILD_STATIC=ON
-DLLVM_ENABLE_PIC=OFF

Cmake configure command:
cmake -G "Visual Studio 17 2022" -A x64 -T host=x64 -DLLVM_TARGETS_TO_BUILD=X86 -DLIBCLANG_BUILD_STATIC=ON -DLLVM_ENABLE_PIC=OFF -S <path/to/llvm-project>/llvm -B <build_dir>

Then built the binaries in configuration RelWithDebInfo|x64.
devenv.com LLVM.sln /Build "RelWithDebInfo|x64"

Followed by cmake install command:
cmake --install <build_dir> --prefix <install_dir> --config RelWithDebInfo

Stage 2: Environment Variables

Created .cargo/config.toml containing

[env]
LLVM_CONFIG_PATH = '<install_dir>/bin/llvm-config.exe'
#LIBCLANG_PATH = '<install_dir>/bin/libclang.dll'
LIBCLANG_STATIC_PATH = '<install_dir>/lib/libclang.lib'
CLANG_PATH = '<install_dir>/bin/clang.exe'

Stage 3: fixing build/static.rs link() to find libraries

Problem: Could not find libclang.lib

  • get_clang_libraries() searches for libclang*.a. Libraries on msvc are .lib.

Fix: changed get_clang_libraries() to switch the glob based on platform.

Problem: Could not find clang.lib.

  • get_library_name() strips the lib prefix from the filename, turning libclang -> clang. clang.lib doesn't exist in the install libraries AFAICT. If it does, there's some configuration in cmake I don't know about, but I didn't need a clang.lib in the end.
  • CLANG_LIBRARIES also refers to the nonexistent clang(.a/.lib).

Fix: removed get_library_name() call in get_clang_libraries(). Replaced it so it returned the filename libclang.lib

Problem: Unresolved external symbols (huge list here)

  • get_library_name() wasn't returning any of the other clang libraries that were also dependencies. E.G. clangAST.lib and friends.

Fix: added logic to get_clang_libraries() so it returned libclang.lib and any clang*.lib libraries in the <install>/lib directory.

Problem: Unresolved external symbol GetFileVersionInfoSizeW + 2 more.

  • This is from Version.lib, from C:/Program Files (x86)/Windows Kits/10/Lib/10.0.22621.0/um/x64

Fix: Added the appropriate cargo:rustc-link-search=C:/Program Files (x86)/Windows Kits/10/Lib/10.0.22621.0/um/x64 and cargo:rustc-link-lib=static=Version printlns at the end of link().
Caveat: I just hard-coded just to see if it will work. Need to find a more generic way to find Version.lib.

complete

And now it compiles and tests succeed with static linking.

Workflow Notes:

Need to compile using cargo test --features static -vv

  • cargo build doesn't try to link against the library, so it false-positively succeeds building if it can find a libclang.lib. Use cargo test instead.
  • --features static required to build with static linking from within the clang_sys crate
  • -vv is "very verbose" and will emit the println outputs from build.rs and static.rs.
@Cody-Duncan Cody-Duncan changed the title Static Linking on MSVC is missing dependencies. Static Linking clang_sys on windows cannot find dependencies Jun 14, 2024
@Cody-Duncan Cody-Duncan changed the title Static Linking clang_sys on windows cannot find dependencies Static Linking clang_sys on windows cannot find libraries, e.g. libclang.lib Jun 14, 2024
@Cody-Duncan Cody-Duncan changed the title Static Linking clang_sys on windows cannot find libraries, e.g. libclang.lib Static Linking clang_sys on msvc/windows cannot find libraries, e.g. libclang.lib Jun 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant