Skip to content

Commit 5908a19

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

File tree

1 file changed

+35
-27
lines changed

1 file changed

+35
-27
lines changed

build.rs

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -151,36 +151,44 @@ 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").is_none()
155+
&& env::var_os("PYO3_CROSS_LIB_DIR").is_none()
156+
&& env::var_os("PYO3_CROSS_INCLUDE_DIR").is_none()
157+
&& env::var_os("PYO3_CROSS_VERSION").is_none()
158+
&& env::var_os("PYO3_CROSS_PYTHON_VERSION").is_none()
159+
{
160+
let target = env::var("TARGET")?;
161+
let host = env::var("HOST")?;
162+
if target == host {
163+
// Not cross-compiling
164+
return Ok(None);
165+
}
160166

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-
}
167+
if target == "i686-pc-windows-msvc" && host == "x86_64-pc-windows-msvc" {
168+
// Not cross-compiling to compile for 32-bit Python from windows 64-bit
169+
return Ok(None);
170+
}
165171

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-
}
172+
if target == "x86_64-apple-darwin" && host == "aarch64-apple-darwin" {
173+
// Not cross-compiling to compile for x86-64 Python from macOS arm64
174+
return Ok(None);
175+
}
174176

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

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

0 commit comments

Comments
 (0)