Skip to content

build: Only replace cl.exe with clang-cl for ARM64 Windows builds #2216

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion BUILDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ edition of Visual Studio, like Community, Standard, or Enterprise). The
“Desktop development with C++” workflow must be installed. Visual Studio
2022 Version 17.5 is supported; earlier versions of Visual Studio may work.

### (Cross-)compiling to Windows ARM64

For Windows ARM64 targets (aarch64-pc-windows-msvc), the Visual Studio Build
Tools “VS 2022 C++ ARM64 build tools” and "clang" components must be installed.
Add Microsoft's provided version of `clang` to `%PATH%`, which will allow the
Add Microsoft's provided version of `clang-cl` to `%PATH%`, which will allow the
build to work in GitHub Actions without installing anything:
```
$env:Path += ";C:\Program Files (x86)\Microsoft Visual Studio\2022\Enterprise\VC\Tools\Llvm\x64\bin"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this where clang-cl lives?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's part of the LLVM toolchain. On Linux machines, yes. On Windows: probably?

Expand Down
27 changes: 13 additions & 14 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,8 @@ fn cpp_flags(compiler: &cc::Tool) -> &'static [&'static str] {
NON_MSVC_FLAGS
} else {
static MSVC_FLAGS: &[&str] = &[
"/Gy", // Enable function-level linking.
"/Zc:wchar_t",
"/Zc:forScope",
"/Zc:inline",
// Warnings.
"/Wall",
"/std:c11", "/Gy", // Enable function-level linking.
"/Wall", // Warnings
"/wd4127", // C4127: conditional expression is constant
"/wd4464", // C4464: relative include path contains '..'
"/wd4514", // C4514: <name>: unreferenced inline function has be
Expand Down Expand Up @@ -559,9 +555,15 @@ fn obj_path(out_dir: &Path, src: &Path) -> PathBuf {

fn configure_cc(c: &mut cc::Build, target: &Target, c_root_dir: &Path, include_dir: &Path) {
let compiler = c.get_compiler();
// FIXME: On Windows AArch64 we currently must use Clang to compile C code
let compiler = if target.os == WINDOWS && target.arch == AARCH64 && !compiler.is_like_clang() {
let _ = c.compiler("clang");
let is_msvc_not_clang_cl = compiler.is_like_msvc() && !compiler.is_like_clang_cl();

// FIXME: On Windows AArch64, ring C code cannot be compiled using cl.exe, but must be compiled
// using the LLVM toolchain. Use clang-cl, which is compatible with flags that are already in
// place (i.e. custom CFLAGS that the user provided to cc).
let compiler = if target.os == WINDOWS && target.arch == AARCH64 && is_msvc_not_clang_cl {
// FIXME: This requires clang-cl to be available in PATH, regardless of any explicit
// or custom path that the user might have provided to cc via the CC flag.
c.compiler("clang-cl");
c.get_compiler()
} else {
compiler
Expand All @@ -584,11 +586,8 @@ fn configure_cc(c: &mut cc::Build, target: &Target, c_root_dir: &Path, include_d
let _ = c.define("NDEBUG", None);
}

if target.arch == X86 {
let is_msvc_not_clang_cl = compiler.is_like_msvc() && !compiler.is_like_clang_cl();
if !is_msvc_not_clang_cl {
let _ = c.flag("-msse2");
}
if target.arch == X86 && !is_msvc_not_clang_cl {
let _ = c.flag("-msse2");
}

// Allow cross-compiling without a target sysroot for these targets.
Expand Down