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

Conflicting C(XX)FLAGS when cross-compiling to aarch64-pc-windows-msvc #2215

Open
MarijnS95 opened this issue Jan 10, 2025 · 0 comments · May be fixed by #2216
Open

Conflicting C(XX)FLAGS when cross-compiling to aarch64-pc-windows-msvc #2215

MarijnS95 opened this issue Jan 10, 2025 · 0 comments · May be fixed by #2216

Comments

@MarijnS95
Copy link

First of all thanks for landing #1339 in some crates.io release! Would be great if this repository had git tags to follow along where commits are reachable 😅

Setup

When cross-compiling (currently from a Linux setup) to Windows, it's common to use https://jake-shadle.github.io/xwin/ / https://github.com/Jake-Shadle/xwin to set up headers and libraries.

When running this in my home directory (/home/marijn):

$ xwin --arch x86_64,aarch64 --sdk-version 10.0.22621 splat

I add the following to my ~/.cargo/config.toml to support cross-compiling C(++) sources in our Rust tree, and ultimately linking a binary:

[target.aarch64-pc-windows-msvc]
linker = "lld-link"
rustflags = [
    "-Lnative=/home/marijn/.xwin-cache/splat/crt/lib/aarch64",
    "-Lnative=/home/marijn/.xwin-cache/splat/sdk/lib/um/aarch64",
    "-Lnative=/home/marijn/.xwin-cache/splat/sdk/lib/ucrt/aarch64",
]

[env]
CC_aarch64-pc-windows-msvc = "clang-cl"
CXX_aarch64-pc-windows-msvc = "clang-cl"
AR_aarch64-pc-windows-msvc = "llvm-lib"

CFLAGS_aarch64-pc-windows-msvc = "-Wno-unused-command-line-argument -fuse-ld=lld-link -vctoolsdir/home/marijn/.xwin-cache/splat/crt -winsdkdir/home/marijn/.xwin-cache/splat/sdk"
CXXFLAGS_aarch64-pc-windows-msvc = "-Wno-unused-command-line-argument -fuse-ld=lld-link -vctoolsdir/home/marijn/.xwin-cache/splat/crt -winsdkdir/home/marijn/.xwin-cache/splat/sdk"

Problem statement

Unfortunately the Windows ARM64 support PR (#1339) overwrites the compiler with clang:

ring/build.rs

Lines 549 to 556 in d2e401f

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");
c.get_compiler()
} else {
compiler
};

This code doesn't modify the flags, and leaves what my environment has in CFLAGS_aarch64-pc-windows-msvc in place. Without using the clang-cl driver, clang doesn't understand the above commands and compiling ring errors out with a bunch of errors like:

warning: [email protected]: clang: error: unknown argument: '-vctoolsdir/home/marijn/.xwin-cache/splat/crt'
warning: [email protected]: clang: error: unknown argument: '-winsdkdir/home/marijn/.xwin-cache/splat/sdk'
warning: [email protected]: ToolExecError: Command "clang" "-O3" "--target=aarch64-pc-windows-msvc" "-ffunction-sections" "-fdata-sections" "-g" "-fno-omit-frame-pointer" "--target=aarch64-pc-windows-msvc" "-I" "ring/include" "-I" "myproject/target/aarch64-pc-windows-msvc/debug/build/ring-81f4360e84b9e6a3/out" "-Wno-unused-command-line-argument" "-fuse-ld=lld-link" "-vctoolsdir/home/marijn/.xwin-cache/splat/crt" "-winsdkdir/home/marijn/.xwin-cache/splat/sdk" "-fvisibility=hidden" "-std=c1x" "-Wall" "-Wbad-function-cast" "-Wcast-align" "-Wcast-qual" "-Wconversion" "-Wmissing-field-initializers" "-Wmissing-include-dirs" "-Wnested-externs" "-Wredundant-decls" "-Wshadow" "-Wsign-compare" "-Wsign-conversion" "-Wstrict-prototypes" "-Wundef" "-Wuninitialized" "-g3" "-Werror" "-o" "myproject/target/aarch64-pc-windows-msvc/debug/build/ring-81f4360e84b9e6a3/out/8551bf18c762c50d-curve25519.o" "-c" "ring/crypto/curve25519/curve25519.c" with args clang did not execute successfully (status code exit status: 1).

Proposed solution

As mentioned at #1339 (comment) clang-cl works, albeit currently (on LLVM 18.1.8) with lots of warnings of the form:

error: mixing declarations and code is incompatible with standards before C99 [-Werror,-Wdeclaration-after-statement]
error: unsafe buffer access [-Werror,-Wunsafe-buffer-usage]

I'd be happy to PR a change to not overwrite the compiler when it is clang-cl like, next to the existing ... && !compiler.is_clang_like(). cc-rs already tracks if it's clang_cl but doesn't currently expose that publicly, so we'll have to add that first.

Thanks!

MarijnS95 added a commit to MarijnS95/cc-rs that referenced this issue Jan 10, 2025
In `ring` we need to check if the compiler is `clang-cl`
to prevent overwriting it with `clang` (see all details in
briansmith/ring#2215), but `cc-rs` does not
currently expose this despite already tracking it.  By adding this
getter we can steer that logic to not overwrite the compiler when it
is `clang-cl`.

Perhaps, since `ToolFamily` is `pub` (but not yet reexported from
`lib.rs`), could we have a public `Tool::family()` getter to not have
to extend these `is_xxx()` getters whenever a downstream crate wants to
know something new?
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

Successfully merging a pull request may close this issue.

1 participant