Skip to content

Commit

Permalink
feat: add support for building universal binaries on M1
Browse files Browse the repository at this point in the history
It's now possible to build universal binaries on x86_64 as well as aarch64.

Closes #420.
  • Loading branch information
vmx authored and rvagg committed May 29, 2024
1 parent 81140a5 commit 201c990
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
9 changes: 4 additions & 5 deletions install-filcrypto
Original file line number Diff line number Diff line change
Expand Up @@ -158,17 +158,15 @@ build_from_source() {

cargo --version

additional_flags=""
# For building on Darwin, we try to use cargo-lipo instead of cargo build.
# Note that the cross compile works on x86_64 for m1, but doesn't work on m1.
# For m1, we will build natively if building from source.
# In the past we were only able to build universal binaries on x86_64,
# for now we just keep that behaviour. This means that on aarch64 (e.g.
# Apple M1) it's a native, non-universal binary.
if [ "$(uname -s)" = "Darwin" ] && [ "$(uname -m)" = "x86_64" ]; then
# Rustup only installs the correct versions for the current
# architecture you're on. As we cross-compile to aarch64, we need to
# make sure that toolchain is installes as well.
rustup target add aarch64-apple-darwin
build="lipo"
additional_flags="--targets x86_64-apple-darwin,aarch64-apple-darwin "
else
build="build"
fi
Expand Down Expand Up @@ -226,6 +224,7 @@ build_from_source() {
use_fixed_rows_to_discard=",fixed-rows-to-discard"
fi

additional_flags=""
# Add feature specific rust flags as needed here.
if [ "${FFI_USE_BLST_PORTABLE}" == "1" ]; then
additional_flags="${additional_flags} --no-default-features --features ${use_multicore_sdr},blst-portable${gpu_flags}${use_fixed_rows_to_discard}"
Expand Down
25 changes: 23 additions & 2 deletions rust/scripts/build-release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ main() {

# shellcheck disable=SC2068 # the rest of the parameters should be split
RUSTFLAGS="${__rust_flags}" \
cargo "${__action}" \
cargo build \
--release --locked ${@:2} 2>&1 | tee ${__build_output_log_tmp}

# parse build output for linker flags
Expand All @@ -37,16 +37,37 @@ main() {
| cut -d ':' -f 3)

echo "Linker Flags: ${__linker_flags}"
if [ "$(uname -s)" = "Darwin" ] && [ "$(uname -m)" = "x86_64" ]; then
# Build a universal binary when `lipo` is enabled, independent of which
# architecture we are on.
if [ "${__action}" = "lipo" ]; then
# With lipo enabled, this replacement may not be necessary,
# but leaving it in doesn't hurt as it does nothing if not
# needed
__linker_flags=$(echo ${__linker_flags} | sed 's/-lOpenCL/-framework OpenCL/g')
echo "Using Linker Flags: ${__linker_flags}"

# Build again for the other architecture.
if [ "$(uname -m)" = "x86_64" ]; then
__target="aarch64-apple-darwin"
else
__target="x86_64-apple-darwin"
fi

# shellcheck disable=SC2068 # the rest of the parameters should be split
RUSTFLAGS="${__rust_flags}" \
cargo build \
--release --locked --target ${__target} ${@:2} 2>&1 \
| tee ${__build_output_log_tmp}

# Create the universal binary/
lipo -create -output libfilcrypto.a \
target/release/libfilcrypto.a \
target/${__target}/release/libfilcrypto.a

find . -type f -name "libfilcrypto.a"
rm -f ./target/aarch64-apple-darwin/release/libfilcrypto.a
rm -f ./target/x86_64-apple-darwin/release/libfilcrypto.a
rm -f ./target/release/libfilcrypto.a
echo "Eliminated non-universal binary libraries"
find . -type f -name "libfilcrypto.a"
fi
Expand Down

0 comments on commit 201c990

Please sign in to comment.