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

Fix building bin bindings on Apple Silicon #404

Merged
merged 1 commit into from
Feb 10, 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
1 change: 1 addition & 0 deletions src/build_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ pub fn find_bridge(cargo_metadata: &Metadata, bridge: Option<&str>) -> Result<Br
if bindings == "cffi" {
BridgeModel::Cffi
} else if bindings == "bin" {
println!("🔗 Found bin bindings");
BridgeModel::Bin
} else {
if !deps.contains_key(bindings) {
Expand Down
40 changes: 26 additions & 14 deletions src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,28 @@ pub fn compile(
bindings_crate: &BridgeModel,
) -> Result<HashMap<String, PathBuf>> {
if context.target.is_macos_universal2() {
let arm64_artifact = compile_target(
let build_type = match bindings_crate {
BridgeModel::Bin => "bin",
_ => "cdylib",
};
let aarch64_artifact = compile_target(
context,
python_interpreter,
bindings_crate,
Some("aarch64-apple-darwin"),
)
.context("Failed to build a arm64 library through cargo")?
.get("cdylib")
.get(build_type)
.cloned()
.ok_or_else(|| {
anyhow!(
"Cargo didn't build a cdylib. Did you miss crate-type = [\"cdylib\"] \
if build_type == "cdylib" {
anyhow!(
"Cargo didn't build an aarch64 cdylib. Did you miss crate-type = [\"cdylib\"] \
in the lib section of your Cargo.toml?",
)
)
} else {
anyhow!("Cargo didn't build an aarch64 bin.")
}
})?;
let x86_64_artifact = compile_target(
context,
Expand All @@ -40,24 +48,28 @@ pub fn compile(
Some("x86_64-apple-darwin"),
)
.context("Failed to build a x86_64 library through cargo")?
.get("cdylib")
.get(build_type)
.cloned()
.ok_or_else(|| {
anyhow!(
"Cargo didn't build a cdylib. Did you miss crate-type = [\"cdylib\"] \
if build_type == "cdylib" {
anyhow!(
"Cargo didn't build a x86_64 cdylib. Did you miss crate-type = [\"cdylib\"] \
in the lib section of your Cargo.toml?",
)
)
} else {
anyhow!("Cargo didn't build a x86_64 bin.")
}
})?;
// Use lipo to create an universal dylib
let cdylib_path = arm64_artifact
// Use lipo to create an universal dylib/bin
let output_path = aarch64_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(&output_path)
.arg(aarch64_artifact)
.arg(x86_64_artifact);
let lipo = cmd.spawn().context("Failed to run lipo")?;
let output = lipo
Expand All @@ -72,7 +84,7 @@ pub fn compile(
)
}
let mut result = HashMap::new();
result.insert("cdylib".to_string(), PathBuf::from(cdylib_path));
result.insert(build_type.to_string(), PathBuf::from(output_path));
Ok(result)
} else {
compile_target(context, python_interpreter, bindings_crate, None)
Expand Down