Skip to content

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

@MarijnS95

Description

@MarijnS95

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions