Skip to content

Commit faeadb3

Browse files
authored
Add upstream patch to allow building on aarch64-apple-darwin (#74)
* Use fs module convenience methods for MUSL patch * Add upstream patch to allow building on aarch64-apple-darwin Closes #72
1 parent 546ccc6 commit faeadb3

File tree

1 file changed

+43
-8
lines changed

1 file changed

+43
-8
lines changed

src/lib.rs

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
extern crate cc;
22

33
use std::env;
4-
use std::fs::{self, File};
5-
use std::io::{Read, Write};
4+
use std::fs;
65
use std::path::{Path, PathBuf};
76
use std::process::Command;
87

@@ -158,6 +157,7 @@ impl Build {
158157
}
159158

160159
let os = match target {
160+
"aarch64-apple-darwin" => "darwin64-arm64-cc",
161161
// Note that this, and all other android targets, aren't using the
162162
// `android64-aarch64` (or equivalent) builtin target. That
163163
// apparently has a crazy amount of build logic in OpenSSL 1.1.1
@@ -448,24 +448,59 @@ fn cp_r(src: &Path, dst: &Path) {
448448
}
449449

450450
fn apply_patches(target: &str, inner: &Path) {
451+
apply_patches_musl(target, inner);
452+
apply_patches_aarch64_apple_darwin(target, inner);
453+
}
454+
455+
fn apply_patches_musl(target: &str, inner: &Path) {
451456
if !target.contains("musl") {
452457
return;
453458
}
454459

455460
// Undo part of https://github.com/openssl/openssl/commit/c352bd07ed2ff872876534c950a6968d75ef121e on MUSL
456461
// since it doesn't have asm/unistd.h
457-
let mut buf = String::new();
458462
let path = inner.join("crypto/rand/rand_unix.c");
459-
File::open(&path).unwrap().read_to_string(&mut buf).unwrap();
463+
let buf = fs::read_to_string(&path).unwrap();
460464

461465
let buf = buf
462466
.replace("asm/unistd.h", "sys/syscall.h")
463467
.replace("__NR_getrandom", "SYS_getrandom");
464468

465-
File::create(&path)
466-
.unwrap()
467-
.write_all(buf.as_bytes())
468-
.unwrap();
469+
fs::write(path, buf).unwrap();
470+
}
471+
472+
fn apply_patches_aarch64_apple_darwin(target: &str, inner: &Path) {
473+
if target != "aarch64-apple-darwin" {
474+
return;
475+
}
476+
477+
// Apply build system changes to allow configuring and building
478+
// for Apple's ARM64 platform.
479+
// https://github.com/openssl/openssl/pull/12369
480+
481+
let path = inner.join("Configurations/10-main.conf");
482+
let mut buf = fs::read_to_string(&path).unwrap();
483+
484+
assert!(
485+
!buf.contains("darwin64-arm64-cc"),
486+
"{} already contains instructions for aarch64-apple-darwin",
487+
path.display(),
488+
);
489+
490+
const PATCH: &str = r#"
491+
"darwin64-arm64-cc" => {
492+
inherit_from => [ "darwin-common", asm("aarch64_asm") ],
493+
CFLAGS => add("-Wall"),
494+
cflags => add("-arch arm64"),
495+
lib_cppflags => add("-DL_ENDIAN"),
496+
bn_ops => "SIXTY_FOUR_BIT_LONG",
497+
perlasm_scheme => "ios64",
498+
},"#;
499+
500+
let x86_64_stanza = buf.find(r#" "darwin64-x86_64-cc""#).unwrap();
501+
buf.insert_str(x86_64_stanza, PATCH);
502+
503+
fs::write(path, buf).unwrap();
469504
}
470505

471506
fn sanitize_sh(path: &Path) -> String {

0 commit comments

Comments
 (0)