KyleMayes / clang-sys

Rust bindings for libclang.
Apache License 2.0
128 stars 65 forks source link

clang_sys::load tries to load ARM64 library on x64 Windows #170

Closed IvanPashchenko closed 1 month ago

IvanPashchenko commented 3 months ago

I have LLVM installed via MS Build Tools Installer (components C++ Clang Compiler for Windows (16.0.5) and MSBuild support for LLVM (clang-cl) toolset) and have both ARM64 and x64 versions present:

 Directory of C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Tools\Llvm

01/17/2024  06:45 PM    <DIR>          .
01/17/2024  06:44 PM    <DIR>          ..
01/17/2024  06:45 PM    <DIR>          ARM64
01/17/2024  06:45 PM    <DIR>          bin
01/17/2024  06:45 PM    <DIR>          lib
01/17/2024  06:45 PM    <DIR>          x64
               0 File(s)              0 bytes
               6 Dir(s)  176,173,752,320 bytes free

Following paths to load the libclang.dll are available:

C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Tools\Llvm\ARM64\bin\libclang.dll
C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Tools\Llvm\bin\libclang.dll
C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Tools\Llvm\x64\bin\libclang.dll

A pretty simple project with

clang-sys = { version = "1.7.0", features = ["runtime"] }

And

fn main() {
  clang_sys::load().unwrap();
}

Will fail with the following error:

called `Result::unwrap()` on an `Err` value: "the `libclang` shared library at C:\\Program Files (x86)\\Microsoft Visual Studio\\2022\\BuildTools\\VC\\Tools\\Llvm\\ARM64\\bin\\libclang.dll could not be opened: LoadLibraryExW failed"

It is indeed an ARM64 library and it's not possible to load it in x64 Windows:

Dump of file C:\\Program Files (x86)\\Microsoft Visual Studio\\2022\\BuildTools\\VC\\Tools\\Llvm\\ARM64\\bin\\libclang.dll

PE signature found

File Type: DLL

FILE HEADER VALUES
            AA64 machine (ARM64)
[...]

I'm not really familiary with the clang/LLVM ecosystem, but I'm happy to try and provide a fix. 1) What would be the proper place for the fix? dynamic.rs fn validate_library? 2) Is there a way to determine the target architecture (like target_os macro)?

KyleMayes commented 1 month ago

I've improved the library detection on Windows to take into account the machine type of the library, so ARM64 DLLs should get ignored when targeting x86-64 and vice-versa (released in v1.8.1).