Skip to content

Commit 897845f

Browse files
committed
Don't set CMAKE_SYSTEM_NAME when using the Visual Studio generator
Setting CMAKE_SYSTEM_NAME and CMAKE_SYSTEM_PROCESSOR is often enough for CMake to handle cross-compilation even without a toolchain file, but it gets in the way of generators that handle cross-compilation on their own (in Visual Studio's case, using the -T "toolset" option).
1 parent 08af05c commit 897845f

File tree

2 files changed

+34
-7
lines changed

2 files changed

+34
-7
lines changed

.github/workflows/main.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,25 @@ jobs:
114114
# If this isn't specified the default is iOS 7, for which zlib-ng will not compile due to the lack of thread-local storage.
115115
IPHONEOS_DEPLOYMENT_TARGET: 16
116116

117+
win_cross_compile_test:
118+
name: Test Cross Compile - ${{ matrix.platform.target }}
119+
needs: [ test ]
120+
runs-on: windows-latest
121+
strategy:
122+
fail-fast: false
123+
matrix:
124+
platform:
125+
- target: aarch64-pc-windows-msvc
126+
steps:
127+
- uses: actions/checkout@v3
128+
- uses: actions-rs/toolchain@v1
129+
with:
130+
toolchain: stable
131+
target: ${{ matrix.platform.target }}
132+
- name: build
133+
run: cargo build -vv --target ${{ matrix.platform.target }}
134+
working-directory: test-crate
135+
117136
rustfmt:
118137
name: Rustfmt
119138
runs-on: ubuntu-latest

src/lib.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,13 @@ impl Config {
445445
};
446446
let host = self.host.clone().unwrap_or_else(|| getenv_unwrap("HOST"));
447447

448+
let generator = self
449+
.generator
450+
.clone()
451+
.or_else(|| self.getenv_target_os("CMAKE_GENERATOR"));
452+
453+
let msvc = target.contains("msvc");
454+
448455
// Some decisions later on are made if CMAKE_TOOLCHAIN_FILE is defined,
449456
// so we need to read it from the environment variables from the beginning.
450457
if !self.defined("CMAKE_TOOLCHAIN_FILE") {
@@ -454,7 +461,14 @@ impl Config {
454461
if !self.defined("CMAKE_SYSTEM_NAME") {
455462
self.define("CMAKE_SYSTEM_NAME", "Generic");
456463
}
457-
} else if target != host && !self.defined("CMAKE_SYSTEM_NAME") {
464+
} else if target != host
465+
&& !self.defined("CMAKE_SYSTEM_NAME")
466+
&& !(msvc
467+
&& self
468+
.generator
469+
.as_deref()
470+
.map_or(true, |g| g.to_string_lossy().starts_with("Visual Studio")))
471+
{
458472
// Set CMAKE_SYSTEM_NAME and CMAKE_SYSTEM_PROCESSOR when cross compiling
459473
let os = getenv_unwrap("CARGO_CFG_TARGET_OS");
460474
let arch = getenv_unwrap("CARGO_CFG_TARGET_ARCH");
@@ -507,12 +521,6 @@ impl Config {
507521
}
508522
}
509523

510-
let generator = self
511-
.generator
512-
.clone()
513-
.or_else(|| self.getenv_target_os("CMAKE_GENERATOR"));
514-
515-
let msvc = target.contains("msvc");
516524
let ndk = self.uses_android_ndk();
517525
let mut c_cfg = self.c_cfg.clone().unwrap_or_default();
518526
c_cfg

0 commit comments

Comments
 (0)