Skip to content

Commit

Permalink
Merge pull request #817 from messense/zig-macos
Browse files Browse the repository at this point in the history
Add macOS target support for `--zig`
  • Loading branch information
messense authored Feb 25, 2022
2 parents b9d87d1 + 3aee994 commit 05d6b9c
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 22 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,10 @@ jobs:
run: |
rustup target add aarch64-unknown-linux-gnu
rustup target add aarch64-unknown-linux-musl
rustup target add aarch64-apple-darwin
cargo run -- build --no-sdist -i python -m test-crates/pyo3-pure/Cargo.toml --target aarch64-unknown-linux-gnu --zig
cargo run -- build --no-sdist -i python -m test-crates/pyo3-pure/Cargo.toml --target aarch64-unknown-linux-musl --zig
cargo run -- build --no-sdist -i python -m test-crates/pyo3-pure/Cargo.toml --target aarch64-apple-darwin --zig
test-alpine:
name: Test Alpine Linux
Expand Down
20 changes: 11 additions & 9 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ base64 = "0.13.0"
bytesize = "1.0.1"
glob = "0.3.0"
cargo_metadata = "0.14.0"
cargo-zigbuild = "0.5.3"
cargo-zigbuild = "0.5.4"
cbindgen = { version = "0.20.0", default-features = false }
flate2 = "1.0.18"
goblin = "0.5.1"
Expand Down
2 changes: 2 additions & 0 deletions src/build_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ pub struct BuildContext {
pub module_name: String,
/// The path to the Cargo.toml. Required for the cargo invocations
pub manifest_path: PathBuf,
/// Directory for all generated artifacts
pub target_dir: PathBuf,
/// The directory to store the built wheels in. Defaults to a new "wheels"
/// directory in the project's target directory
pub out: PathBuf,
Expand Down
8 changes: 8 additions & 0 deletions src/build_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,13 @@ impl BuildOptions {
);
}

let target_dir = cargo_extra_args
.iter()
.position(|x| x == "--target-dir")
.and_then(|i| cargo_extra_args.get(i + 1))
.map(PathBuf::from)
.unwrap_or_else(|| cargo_metadata.target_directory.clone().into_std_path_buf());

Ok(BuildContext {
target,
bridge,
Expand All @@ -342,6 +349,7 @@ impl BuildOptions {
crate_name: crate_name.to_string(),
module_name,
manifest_path: manifest_file,
target_dir,
out: wheel_dir,
release,
strip,
Expand Down
37 changes: 29 additions & 8 deletions src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,18 @@ fn compile_target(
python_interpreter: Option<&PythonInterpreter>,
bindings_crate: &BridgeModel,
) -> Result<HashMap<String, PathBuf>> {
let (zig_cc, zig_cxx) = if context.zig && context.target.is_linux() {
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))
Expand Down Expand Up @@ -163,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 @@ -190,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 @@ -222,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 Expand Up @@ -324,6 +331,8 @@ fn compile_target(
}

fn prepare_zig_linker(context: &BuildContext) -> Result<(PathBuf, PathBuf)> {
use cargo_zigbuild::macos::LIBICONV_TBD;

let target = &context.target;
let triple = target.target_triple();
let triple = if target.is_linux() {
Expand All @@ -339,6 +348,18 @@ fn prepare_zig_linker(context: &BuildContext) -> Result<(PathBuf, PathBuf)> {
triple.to_string()
};
let (cc, cxx) = cargo_zigbuild::zig::prepare_zig_linker(&triple)?;

if target.is_macos() {
let target_dir = if target.user_specified {
context.target_dir.join(triple)
} else {
context.target_dir.clone()
};
let profile = if context.release { "release" } else { "debug" };
let deps_dir = target_dir.join(profile).join("deps");
fs::create_dir_all(&deps_dir)?;
fs::write(deps_dir.join("libiconv.tbd"), LIBICONV_TBD)?;
}
Ok((cc, cxx))
}

Expand Down
19 changes: 15 additions & 4 deletions src/target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ 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,
}

impl Target {
Expand All @@ -110,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 @@ -157,6 +161,8 @@ impl Target {
arch,
env: platform.environment,
triple,
host_triple,
user_specified: target_triple.is_some(),
cross_compiling: false,
};
target.cross_compiling = is_cross_compiling(&target)?;
Expand Down Expand Up @@ -367,6 +373,11 @@ impl Target {
self.os == Os::Windows
}

/// Returns true if the current environment is msvc
pub fn is_msvc(&self) -> bool {
self.env == Environment::Msvc
}

/// Returns true if the current platform is illumos
pub fn is_illumos(&self) -> bool {
self.os == Os::Illumos
Expand Down

0 comments on commit 05d6b9c

Please sign in to comment.