Skip to content

Commit

Permalink
Set CMAKE_OSX_ARCHITECTURES when building for Apple targets
Browse files Browse the repository at this point in the history
Running
```
$ cargo build --target aarch64-apple-darwin
```
was failing with link errors because the ObjC code in SDL was being
built for x86_64 for some reason. Setting this CMake variable keeps
everything aligned with the right architecture though! Hurray!
  • Loading branch information
Chris--B committed Aug 8, 2022
1 parent ce84869 commit 96a0c3d
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ fn main() {
let target = env::var("TARGET").expect("Could not read `TARGET`!");
println!("target:{}", target);

let target_arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap();
println!("target_arch:{}", target_arch);

let target_vendor = env::var("CARGO_CFG_TARGET_VENDOR").unwrap();
println!("target_vendor:{}", target_vendor);

let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap();
println!("target_os:{}", target_os);

let out_dir = env::var("OUT_DIR").expect("Could not read `OUT_DIR`!");
println!("out_dir:{}", out_dir);

Expand Down Expand Up @@ -57,6 +66,26 @@ fn main() {
cm.define("SDL_SHARED", "ON");
cm.define("SDL_STATIC", "ON");
cm.define("HIDAPI", "ON");

// We need to set extra CMake options when building for Apple platforms.
if target_vendor == "apple" {
// CMake can handle the x86_64/aarch64 duality of Apple platforms, but
// needs to be told which architecture(s) we want. Since rust doesn't support
// fat binaries, we'll only set the one architecture requested.
// See: https://github.com/rust-lang/cargo/issues/8875
match target_arch.as_str() {
"aarch64" => {
cm.define("CMAKE_OSX_ARCHITECTURES", "arm64");
}
"x86_86" => {
cm.define("CMAKE_OSX_ARCHITECTURES", "x86_86");
}
arch => {
println!("Unrecognized architecture for Apple platform \"{}\", not setting CMAKE_OSX_ARCHITECTURES", arch);
}
}
}

cm.profile("Release");
let build_output_path = cm.build();
println!("build_output_path: {}", build_output_path.display());
Expand Down

0 comments on commit 96a0c3d

Please sign in to comment.