Skip to content

Commit 1f074cc

Browse files
committed
Ensure we are cross compiling when any cross env variables are set.
1 parent 6e49ba3 commit 1f074cc

File tree

1 file changed

+34
-27
lines changed

1 file changed

+34
-27
lines changed

build.rs

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -151,36 +151,43 @@ impl CrossCompileConfig {
151151
}
152152

153153
fn cross_compiling() -> Result<Option<CrossCompileConfig>> {
154-
let target = env::var("TARGET")?;
155-
let host = env::var("HOST")?;
156-
if target == host {
157-
// Not cross-compiling
158-
return Ok(None);
159-
}
154+
if env::var_os("PYO3_CROSS_LIB_DIR").is_none()
155+
&& env::var_os("PYO3_CROSS_INCLUDE_DIR").is_none()
156+
&& env::var_os("PYO3_CROSS_VERSION").is_none()
157+
&& env::var_os("PYO3_CROSS_PYTHON_VERSION").is_none()
158+
{
159+
let target = env::var("TARGET")?;
160+
let host = env::var("HOST")?;
161+
if target == host {
162+
// Not cross-compiling
163+
return Ok(None);
164+
}
160165

161-
if target == "i686-pc-windows-msvc" && host == "x86_64-pc-windows-msvc" {
162-
// Not cross-compiling to compile for 32-bit Python from windows 64-bit
163-
return Ok(None);
164-
}
166+
if target == "i686-pc-windows-msvc" && host == "x86_64-pc-windows-msvc" {
167+
// Not cross-compiling to compile for 32-bit Python from windows 64-bit
168+
return Ok(None);
169+
}
165170

166-
if target == "x86_64-apple-darwin" && host == "aarch64-apple-darwin" {
167-
// Not cross-compiling to compile for x86-64 Python from macOS arm64
168-
return Ok(None);
169-
}
170-
if target == "aarch64-apple-darwin" && host == "x86_64-apple-darwin" {
171-
// Not cross-compiling to compile for arm64 Python from macOS x86_64
172-
return Ok(None);
173-
}
171+
if target == "x86_64-apple-darwin" && host == "aarch64-apple-darwin" {
172+
// Not cross-compiling to compile for x86-64 Python from macOS arm64
173+
return Ok(None);
174+
}
174175

175-
if host.starts_with(&format!(
176-
"{}-{}-{}",
177-
env::var("CARGO_CFG_TARGET_ARCH")?,
178-
env::var("CARGO_CFG_TARGET_VENDOR")?,
179-
env::var("CARGO_CFG_TARGET_OS")?
180-
)) {
181-
// Not cross-compiling if arch-vendor-os is all the same
182-
// e.g. x86_64-unknown-linux-musl on x86_64-unknown-linux-gnu host
183-
return Ok(None);
176+
if target == "aarch64-apple-darwin" && host == "x86_64-apple-darwin" {
177+
// Not cross-compiling to compile for arm64 Python from macOS x86_64
178+
return Ok(None);
179+
}
180+
181+
if host.starts_with(&format!(
182+
"{}-{}-{}",
183+
env::var("CARGO_CFG_TARGET_ARCH")?,
184+
env::var("CARGO_CFG_TARGET_VENDOR")?,
185+
env::var("CARGO_CFG_TARGET_OS")?
186+
)) {
187+
// Not cross-compiling if arch-vendor-os is all the same
188+
// e.g. x86_64-unknown-linux-musl on x86_64-unknown-linux-gnu host
189+
return Ok(None);
190+
}
184191
}
185192

186193
if env::var("CARGO_CFG_TARGET_FAMILY")? == "windows" {

0 commit comments

Comments
 (0)