Skip to content

Commit 88ba56f

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 c4a60dd commit 88ba56f

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
@@ -105,3 +105,22 @@ jobs:
105105
env:
106106
# 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.
107107
IPHONEOS_DEPLOYMENT_TARGET: 16
108+
109+
win_cross_compile_test:
110+
name: Test Cross Compile - ${{ matrix.platform.target }}
111+
needs: [ test ]
112+
runs-on: windows-latest
113+
strategy:
114+
fail-fast: false
115+
matrix:
116+
platform:
117+
- target: aarch64-pc-windows-msvc
118+
steps:
119+
- uses: actions/checkout@v3
120+
- uses: actions-rs/toolchain@v1
121+
with:
122+
toolchain: stable
123+
target: ${{ matrix.platform.target }}
124+
- name: build
125+
run: cargo build -vv --target ${{ matrix.platform.target }}
126+
working-directory: test-crate

src/lib.rs

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

441+
let generator = self
442+
.generator
443+
.clone()
444+
.or_else(|| self.getenv_target_os("CMAKE_GENERATOR"));
445+
446+
let msvc = target.contains("msvc");
447+
441448
// Some decisions later on are made if CMAKE_TOOLCHAIN_FILE is defined,
442449
// so we need to read it from the environment variables from the beginning.
443450
if !self.defined("CMAKE_TOOLCHAIN_FILE") {
@@ -448,7 +455,14 @@ impl Config {
448455
if !self.defined("CMAKE_SYSTEM_NAME") {
449456
self.define("CMAKE_SYSTEM_NAME", "Generic");
450457
}
451-
} else if target != host && !self.defined("CMAKE_SYSTEM_NAME") {
458+
} else if target != host
459+
&& !self.defined("CMAKE_SYSTEM_NAME")
460+
&& !(msvc
461+
&& self
462+
.generator
463+
.as_deref()
464+
.map_or(true, |g| g.to_string_lossy().starts_with("Visual Studio")))
465+
{
452466
// Set CMAKE_SYSTEM_NAME and CMAKE_SYSTEM_PROCESSOR when cross compiling
453467
let os = getenv_unwrap("CARGO_CFG_TARGET_OS");
454468
let arch = getenv_unwrap("CARGO_CFG_TARGET_ARCH");
@@ -495,12 +509,6 @@ impl Config {
495509
}
496510
}
497511

498-
let generator = self
499-
.generator
500-
.clone()
501-
.or_else(|| self.getenv_target_os("CMAKE_GENERATOR"));
502-
503-
let msvc = target.contains("msvc");
504512
let ndk = self.uses_android_ndk();
505513
let mut c_cfg = self.c_cfg.clone().unwrap_or_default();
506514
c_cfg

0 commit comments

Comments
 (0)