Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for macOS universal2 wheel #403

Merged
merged 1 commit into from
Jan 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 73 additions & 1 deletion src/compile.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::build_context::BridgeModel;
use crate::BuildContext;
use crate::PythonInterpreter;
use anyhow::{bail, Context, Result};
use anyhow::{anyhow, bail, Context, Result};
use fs_err::File;
use std::collections::HashMap;
use std::io::{BufReader, Read};
Expand All @@ -16,6 +16,74 @@ pub fn compile(
context: &BuildContext,
python_interpreter: Option<&PythonInterpreter>,
bindings_crate: &BridgeModel,
) -> Result<HashMap<String, PathBuf>> {
if context.target.is_macos_universal2() {
let arm64_artifact = compile_target(
context,
python_interpreter,
bindings_crate,
Some("aarch64-apple-darwin"),
)
.context("Failed to build a arm64 library through cargo")?
.get("cdylib")
.cloned()
.ok_or_else(|| {
anyhow!(
"Cargo didn't build a cdylib. Did you miss crate-type = [\"cdylib\"] \
in the lib section of your Cargo.toml?",
)
})?;
let x86_64_artifact = compile_target(
context,
python_interpreter,
bindings_crate,
Some("x86_64-apple-darwin"),
)
.context("Failed to build a x86_64 library through cargo")?
.get("cdylib")
.cloned()
.ok_or_else(|| {
anyhow!(
"Cargo didn't build a cdylib. Did you miss crate-type = [\"cdylib\"] \
in the lib section of your Cargo.toml?",
)
})?;
// Use lipo to create an universal dylib
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently Cargo does have builtin support creating macOS Universal/fat binaries: rust-lang/cargo#8875

let cdylib_path = arm64_artifact
.display()
.to_string()
.replace("aarch64-apple-darwin/", "");
let mut cmd = Command::new("lipo");
cmd.arg("-create")
.arg("-output")
.arg(&cdylib_path)
.arg(arm64_artifact)
.arg(x86_64_artifact);
let lipo = cmd.spawn().context("Failed to run lipo")?;
let output = lipo
.wait_with_output()
.expect("Failed to wait on lipo child process");

if !output.status.success() {
bail!(
r#"lipo finished with "{}": {}"#,
output.status,
String::from_utf8_lossy(&output.stderr)
)
}
let mut result = HashMap::new();
result.insert("cdylib".to_string(), PathBuf::from(cdylib_path));
Ok(result)
} else {
compile_target(context, python_interpreter, bindings_crate, None)
}
}

fn compile_target(
context: &BuildContext,
python_interpreter: Option<&PythonInterpreter>,
bindings_crate: &BridgeModel,
target: Option<&str>,
) -> Result<HashMap<String, PathBuf>> {
let mut shared_args = vec!["--manifest-path", context.manifest_path.to_str().unwrap()];

Expand All @@ -33,6 +101,10 @@ pub fn compile(
if context.release {
shared_args.push("--release");
}
if let Some(target) = target {
shared_args.push("--target");
shared_args.push(target);
}

let cargo_args = vec!["rustc", "--message-format", "json"];

Expand Down
25 changes: 23 additions & 2 deletions src/target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ enum Arch {
ARMV7L,
POWERPC64LE,
POWERPC64,
Universal2,
X86,
X86_64,
}
Expand All @@ -69,6 +70,7 @@ impl fmt::Display for Arch {
Arch::ARMV7L => write!(f, "armv7l"),
Arch::POWERPC64LE => write!(f, "ppc64le"),
Arch::POWERPC64 => write!(f, "ppc64"),
Arch::Universal2 => write!(f, "universal2"),
Arch::X86 => write!(f, "i686"),
Arch::X86_64 => write!(f, "x86_64"),
}
Expand Down Expand Up @@ -108,7 +110,13 @@ impl Target {
platforms::target::Arch::X86_64 => Arch::X86_64,
platforms::target::Arch::X86 => Arch::X86,
platforms::target::Arch::ARM => Arch::ARMV7L,
platforms::target::Arch::AARCH64 => Arch::AARCH64,
platforms::target::Arch::AARCH64 => {
if os == OS::Macos {
Arch::Universal2 // macOS with Apple Silicon
} else {
Arch::AARCH64
}
}
platforms::target::Arch::POWERPC64
if platform.target_triple.starts_with("powerpc64-") =>
{
Expand All @@ -126,6 +134,7 @@ impl Target {
match (&os, &arch) {
(OS::FreeBSD, Arch::AARCH64) => bail!("aarch64 is not supported for FreeBSD"),
(OS::FreeBSD, Arch::ARMV7L) => bail!("armv7l is not supported for FreeBSD"),
(OS::FreeBSD, Arch::Universal2) => bail!("universal2 is not supported for FreeBSD"),
(OS::FreeBSD, Arch::X86) => bail!("32-bit wheels are not supported for FreeBSD"),
(OS::FreeBSD, Arch::X86_64) => {
match PlatformInfo::new() {
Expand All @@ -137,6 +146,7 @@ impl Target {
(OS::Macos, Arch::X86) => bail!("32-bit wheels are not supported for macOS"),
(OS::Windows, Arch::AARCH64) => bail!("aarch64 is not supported for Windows"),
(OS::Windows, Arch::ARMV7L) => bail!("armv7l is not supported for Windows"),
(OS::Windows, Arch::Universal2) => bail!("universal2 is not supported for Windows"),
(_, _) => {}
}
Ok(Target { os, arch })
Expand All @@ -149,6 +159,7 @@ impl Target {
Arch::ARMV7L => 32,
Arch::POWERPC64 => 64,
Arch::POWERPC64LE => 64,
Arch::Universal2 => 64,
Arch::X86 => 32,
Arch::X86_64 => 64,
}
Expand All @@ -174,6 +185,11 @@ impl Target {
self.os == OS::Macos
}

/// Returns true if the current platform is mac os and arch is universal2
pub fn is_macos_universal2(&self) -> bool {
self.os == OS::Macos && self.arch == Arch::Universal2
}

/// Returns true if the current platform is windows
pub fn is_windows(&self) -> bool {
self.os == OS::Windows
Expand All @@ -193,6 +209,7 @@ impl Target {
(OS::Linux, _) => format!("{}_{}", manylinux, self.arch),
(OS::Macos, Arch::X86_64) => "macosx_10_7_x86_64".to_string(),
(OS::Macos, Arch::AARCH64) => "macosx_11_0_arm64".to_string(),
(OS::Macos, Arch::Universal2) => "macosx_10_9_universal2".to_string(),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(OS::Windows, Arch::X86) => "win32".to_string(),
(OS::Windows, Arch::X86_64) => "win_amd64".to_string(),
(_, _) => panic!("unsupported target should not have reached get_platform_tag()"),
Expand All @@ -204,7 +221,7 @@ impl Target {
vec![format!("py3-none-{}", self.get_platform_tag(&manylinux))]
}

/// Returns the platform for the tag in the shared libaries file name
/// Returns the platform for the tag in the shared libraries file name
pub fn get_shared_platform_tag(&self) -> &'static str {
match (&self.os, &self.arch) {
(OS::FreeBSD, _) => "", // according imp.get_suffixes(), there are no such
Expand All @@ -216,8 +233,12 @@ impl Target {
(OS::Linux, Arch::X86_64) => "x86_64-linux-gnu",
(OS::Macos, Arch::X86_64) => "darwin",
(OS::Macos, Arch::AARCH64) => "darwin",
(OS::Macos, Arch::Universal2) => "darwin",
(OS::Windows, Arch::X86) => "win32",
(OS::Windows, Arch::X86_64) => "win_amd64",
(OS::Linux, _) => {
panic!("unsupported Linux Arch should not have reached get_shared_platform_tag()")
}
(OS::Macos, _) => {
panic!("unsupported macOS Arch should not have reached get_shared_platform_tag()")
}
Expand Down