Skip to content

Commit

Permalink
Don't set CMAKE_SYSTEM_NAME when using the Visual Studio generator
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
jrose-signal committed Nov 13, 2023
1 parent c4a60dd commit 88ba56f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
19 changes: 19 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,22 @@ jobs:
env:
# 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.
IPHONEOS_DEPLOYMENT_TARGET: 16

win_cross_compile_test:
name: Test Cross Compile - ${{ matrix.platform.target }}
needs: [ test ]
runs-on: windows-latest
strategy:
fail-fast: false
matrix:
platform:
- target: aarch64-pc-windows-msvc
steps:
- uses: actions/checkout@v3
- uses: actions-rs/toolchain@v1
with:
toolchain: stable
target: ${{ matrix.platform.target }}
- name: build
run: cargo build -vv --target ${{ matrix.platform.target }}
working-directory: test-crate
22 changes: 15 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,13 @@ impl Config {
};
let host = self.host.clone().unwrap_or_else(|| getenv_unwrap("HOST"));

let generator = self
.generator
.clone()
.or_else(|| self.getenv_target_os("CMAKE_GENERATOR"));

let msvc = target.contains("msvc");

// Some decisions later on are made if CMAKE_TOOLCHAIN_FILE is defined,
// so we need to read it from the environment variables from the beginning.
if !self.defined("CMAKE_TOOLCHAIN_FILE") {
Expand All @@ -448,7 +455,14 @@ impl Config {
if !self.defined("CMAKE_SYSTEM_NAME") {
self.define("CMAKE_SYSTEM_NAME", "Generic");
}
} else if target != host && !self.defined("CMAKE_SYSTEM_NAME") {
} else if target != host
&& !self.defined("CMAKE_SYSTEM_NAME")
&& !(msvc
&& self
.generator
.as_deref()
.map_or(true, |g| g.to_string_lossy().starts_with("Visual Studio")))
{
// Set CMAKE_SYSTEM_NAME and CMAKE_SYSTEM_PROCESSOR when cross compiling
let os = getenv_unwrap("CARGO_CFG_TARGET_OS");
let arch = getenv_unwrap("CARGO_CFG_TARGET_ARCH");
Expand Down Expand Up @@ -495,12 +509,6 @@ impl Config {
}
}

let generator = self
.generator
.clone()
.or_else(|| self.getenv_target_os("CMAKE_GENERATOR"));

let msvc = target.contains("msvc");
let ndk = self.uses_android_ndk();
let mut c_cfg = self.c_cfg.clone().unwrap_or_default();
c_cfg
Expand Down

0 comments on commit 88ba56f

Please sign in to comment.