Skip to content

Commit

Permalink
Only setup zig as linker when host target isn't the same as cross target
Browse files Browse the repository at this point in the history
  • Loading branch information
messense committed Feb 24, 2022
1 parent 336dd08 commit fb07377
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 19 deletions.
36 changes: 21 additions & 15 deletions src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,24 @@ fn compile_target(
python_interpreter: Option<&PythonInterpreter>,
bindings_crate: &BridgeModel,
) -> Result<HashMap<String, PathBuf>> {
let (zig_cc, zig_cxx) =
if context.zig && context.target.user_specified && !context.target.is_msvc() {
let (cc, cxx) =
prepare_zig_linker(context).context("Failed to create zig linker wrapper")?;
(Some(cc), Some(cxx))
} else {
(None, None)
};
let target = &context.target;
let target_triple = target.target_triple();
let zig_triple = if target.is_linux() {
match context.platform_tag {
Some(PlatformTag::Manylinux { x, y }) => format!("{}.{}.{}", target_triple, x, y),
_ => target_triple.to_string(),
}
} else {
target_triple.to_string()
};
let (zig_cc, zig_cxx) = if context.zig && !target.is_msvc() && target.host_triple != zig_triple
{
let (cc, cxx) =
prepare_zig_linker(context).context("Failed to create zig linker wrapper")?;
(Some(cc), Some(cxx))
} else {
(None, None)
};

let mut shared_args = vec!["--manifest-path", context.manifest_path.to_str().unwrap()];

Expand Down Expand Up @@ -164,7 +174,7 @@ fn compile_target(
let macos_dylib_install_name = format!("link-args=-Wl,-install_name,@rpath/{}", so_filename);

// https://github.com/PyO3/pyo3/issues/88#issuecomment-337744403
if context.target.is_macos() {
if target.is_macos() {
if let BridgeModel::Bindings(_) | BridgeModel::BindingsAbi3(_, _) = bindings_crate {
let mac_args = &[
"-C",
Expand All @@ -191,7 +201,7 @@ fn compile_target(
// however, we get an exit code 0xc0000005 if we try the same with
// `/FORCE:UNDEFINED`, so we still look up the python interpreter
// and pass the location of the lib with the definitions.
if context.target.is_windows() {
if target.is_windows() {
let python_interpreter = python_interpreter
.expect("Must have a python interpreter for building abi3 on windows");
pythonxy_lib_folder = format!("native={}", python_interpreter.libs_dir.display());
Expand Down Expand Up @@ -223,11 +233,7 @@ fn compile_target(

// Also set TARGET_CC and TARGET_CXX for cc-rs and cmake-rs
if let Some(zig_cc) = zig_cc {
let env_target = context
.target
.target_triple()
.to_uppercase()
.replace("-", "_");
let env_target = target_triple.to_uppercase().replace("-", "_");
build_command.env("TARGET_CC", &zig_cc);
build_command.env(format!("CARGO_TARGET_{}_LINKER", env_target), &zig_cc);
}
Expand Down
11 changes: 7 additions & 4 deletions src/target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ pub struct Target {
env: Environment,
triple: String,
cross_compiling: bool,
/// Host machine target triple
pub(crate) host_triple: String,
/// Is user specified `--target`
pub(crate) user_specified: bool,
}
Expand All @@ -112,17 +114,17 @@ impl Target {
///
/// Fails if the target triple isn't supported
pub fn from_target_triple(target_triple: Option<String>) -> Result<Self> {
let host_triple = get_host_target()?;
let (platform, triple) = if let Some(ref target_triple) = target_triple {
let platform: Triple = target_triple
.parse()
.map_err(|_| format_err!("Unknown target triple {}", target_triple))?;
(platform, target_triple.to_string())
} else {
let target_triple = get_host_target()?;
let platform: Triple = target_triple
let platform: Triple = host_triple
.parse()
.map_err(|_| format_err!("Unknown target triple {}", target_triple))?;
(platform, target_triple)
.map_err(|_| format_err!("Unknown target triple {}", host_triple))?;
(platform, host_triple.clone())
};

let os = match platform.operating_system {
Expand Down Expand Up @@ -159,6 +161,7 @@ impl Target {
arch,
env: platform.environment,
triple,
host_triple,
user_specified: target_triple.is_some(),
cross_compiling: false,
};
Expand Down

0 comments on commit fb07377

Please sign in to comment.