Skip to content

Commit 31641dd

Browse files
authored
Merge pull request #75 from TheBlueMatt/main
Fix M1 build
2 parents de4d808 + 81edb94 commit 31641dd

File tree

1 file changed

+27
-19
lines changed

1 file changed

+27
-19
lines changed

genbindings.sh

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ cd "$ORIG_PWD"
2121
# Generate (and reasonably test) C bindings
2222

2323
# First we set various compiler flags...
24-
HOST_PLATFORM="$(rustc --version --verbose | grep "host:")"
24+
HOST_PLATFORM="$(rustc --version --verbose | grep "host:" | awk '{ print $2 }')"
25+
ENV_TARGET=$(echo $HOST_PLATFORM | sed 's/-/_/g')
2526

2627
# Set path to include our rustc wrapper as well as cbindgen
2728
export LDK_RUSTC_PATH="$(which rustc)"
@@ -44,18 +45,18 @@ BASE_CFLAGS="$BASE_CFLAGS -frandom-seed=42"
4445
LOCAL_CFLAGS="-Wall -Wno-nullability-completeness -pthread -Iinclude/"
4546

4647
HOST_OSX=false
47-
if [ "$HOST_PLATFORM" = "host: x86_64-apple-darwin" ]; then
48+
if [ "$HOST_PLATFORM" = "x86_64-apple-darwin" ]; then
4849
HOST_OSX=true
49-
elif [ "$HOST_PLATFORM" = "host: aarch64-apple-darwin" ]; then
50+
elif [ "$HOST_PLATFORM" = "aarch64-apple-darwin" ]; then
5051
HOST_OSX=true
5152
fi
5253

5354
BASE_HOST_CFLAGS="$BASE_CFLAGS"
5455

5556
if [ "$HOST_OSX" = "true" ]; then
5657
export MACOSX_DEPLOYMENT_TARGET=10.9
57-
LOCAL_CFLAGS="$LOCAL_CFLAGS -isysroot$(xcrun --show-sdk-path) -mmacosx-version-min=10.9"
58-
BASE_HOST_CFLAGS="$BASE_HOST_CFLAGS -isysroot$(xcrun --show-sdk-path) -mmacosx-version-min=10.9"
58+
LOCAL_CFLAGS="$LOCAL_CFLAGS --target=$HOST_PLATFORM -isysroot$(xcrun --show-sdk-path) -mmacosx-version-min=10.9"
59+
BASE_HOST_CFLAGS="$BASE_HOST_CFLAGS --target=$HOST_PLATFORM -isysroot$(xcrun --show-sdk-path) -mmacosx-version-min=10.9"
5960
# Targeting aarch64 appears to be supported only starting with Big Sur, so check it before use
6061
clang -o /dev/null $BASE_HOST_CFLAGS --target=aarch64-apple-darwin -mcpu=apple-a14 genbindings_path_map_test_file.c &&
6162
export CFLAGS_aarch64_apple_darwin="$BASE_HOST_CFLAGS --target=aarch64-apple-darwin -mcpu=apple-a14" ||
@@ -64,12 +65,15 @@ fi
6465

6566
rm genbindings_path_map_test_file.c
6667

67-
ENV_TARGET=$(rustc --version --verbose | grep host | awk '{ print $2 }' | sed 's/-/_/g')
6868
case "$ENV_TARGET" in
6969
"x86_64"*)
7070
export RUSTFLAGS="$BASE_RUSTFLAGS -C target-cpu=sandybridge"
7171
export CFLAGS_$ENV_TARGET="$BASE_HOST_CFLAGS -march=sandybridge -mcpu=sandybridge -mtune=sandybridge"
7272
;;
73+
"aarch64_apple_darwin")
74+
export RUSTFLAGS="$BASE_RUSTFLAGS -C target-cpu=apple-a14"
75+
export CFLAGS_$ENV_TARGET="$BASE_HOST_CFLAGS -mcpu=apple-a14"
76+
;;
7377
*)
7478
# Assume this isn't targeted at another host and build for the host's CPU.
7579
export RUSTFLAGS="$BASE_RUSTFLAGS -C target-cpu=native"
@@ -225,6 +229,7 @@ else
225229
fi
226230

227231
# Build C++ class methods which call trait methods
232+
echo "Updating C++ header, this may take some time, especially on macOS"
228233
set +x # Echoing every command is very verbose here
229234
OLD_IFS="$IFS"
230235
export IFS=''
@@ -328,7 +333,7 @@ if [ "$2" = "true" ]; then
328333
fi
329334

330335
# Then, check with memory sanitizer, if we're on Linux and have rustc nightly
331-
if [ "$HOST_PLATFORM" = "host: x86_64-unknown-linux-gnu" ]; then
336+
if [ "$HOST_PLATFORM" = "x86_64-unknown-linux-gnu" ]; then
332337
if cargo +nightly --version >/dev/null 2>&1; then
333338
LLVM_V=$(rustc +nightly --version --verbose | grep "LLVM version" | awk '{ print substr($3, 0, 2); }')
334339
if [ -x "$(which clang-$LLVM_V)" ]; then
@@ -431,7 +436,7 @@ if [ "$CLANG" != "" -a "$CLANGPP" = "" ]; then
431436
fi
432437

433438
# Finally, if we're on OSX or on Linux, build the final debug binary with address sanitizer (and leave it there)
434-
if [ "$HOST_PLATFORM" = "host: x86_64-unknown-linux-gnu" -o "$HOST_PLATFORM" = "host: x86_64-apple-darwin" ]; then
439+
if [ "$HOST_PLATFORM" = "x86_64-unknown-linux-gnu" -o "$HOST_PLATFORM" = "x86_64-apple-darwin" ]; then
435440
if [ "$CLANGPP" != "" ]; then
436441
if [ "$HOST_OSX" = "true" ]; then
437442
# OSX sed is for some reason not compatible with GNU sed
@@ -536,17 +541,20 @@ if [ "$CLANGPP" != "" -a "$LLD" != "" ]; then
536541
LINK_ARG_FLAGS="$LINK_ARG_FLAGS -C link-arg="-isysroot$(xcrun --show-sdk-path)" -C link-arg=-mmacosx-version-min=10.9"
537542
RUSTFLAGS="$BASE_RUSTFLAGS -C target-cpu=apple-a14 -C embed-bitcode=yes -C linker-plugin-lto -C lto -C linker=$CLANG $LINK_ARG_FLAGS -C link-arg=-mcpu=apple-a14" CARGO_PROFILE_RELEASE_LTO=true cargo build $CARGO_BUILD_ARGS -v --release --target aarch64-apple-darwin
538543
fi
539-
export CFLAGS_$ENV_TARGET="$BASE_HOST_CFLAGS -O3 -fPIC -fembed-bitcode -march=sandybridge -mcpu=sandybridge -mtune=sandybridge"
540-
# Rust doesn't recognize CFLAGS changes, so we need to clean build artifacts
541-
cargo clean --release
542-
CARGO_PROFILE_RELEASE_LTO=true RUSTFLAGS="$RUSTFLAGS -C embed-bitcode=yes -C linker-plugin-lto -C lto -C linker=$CLANG $LINK_ARG_FLAGS -C link-arg=-march=sandybridge -C link-arg=-mcpu=sandybridge -C link-arg=-mtune=sandybridge" cargo build $CARGO_BUILD_ARGS -v --release
543-
544-
if [ "$2" = "true" ]; then
545-
$CLANGPP $LOCAL_CFLAGS -flto -fuse-ld=$LLD -O2 demo.cpp target/release/libldk.a -ldl
546-
strip ./a.out
547-
echo "C++ Bin size and runtime with cross-language LTO:"
548-
ls -lha a.out
549-
time ./a.out > /dev/null
544+
# If we're on an M1 don't bother building X86 binaries
545+
if [ "$HOST_PLATFORM" != "aarch64-apple-darwin" ]; then
546+
export CFLAGS_$ENV_TARGET="$BASE_HOST_CFLAGS -O3 -fPIC -fembed-bitcode -march=sandybridge -mcpu=sandybridge -mtune=sandybridge"
547+
# Rust doesn't recognize CFLAGS changes, so we need to clean build artifacts
548+
cargo clean --release
549+
CARGO_PROFILE_RELEASE_LTO=true RUSTFLAGS="$RUSTFLAGS -C embed-bitcode=yes -C linker-plugin-lto -C lto -C linker=$CLANG $LINK_ARG_FLAGS -C link-arg=-march=sandybridge -C link-arg=-mcpu=sandybridge -C link-arg=-mtune=sandybridge" cargo build $CARGO_BUILD_ARGS -v --release
550+
551+
if [ "$2" = "true" ]; then
552+
$CLANGPP $LOCAL_CFLAGS -flto -fuse-ld=$LLD -O2 demo.cpp target/release/libldk.a -ldl
553+
strip ./a.out
554+
echo "C++ Bin size and runtime with cross-language LTO:"
555+
ls -lha a.out
556+
time ./a.out > /dev/null
557+
fi
550558
fi
551559
else
552560
if [ "$CFLAGS_aarch64_apple_darwin" != "" ]; then

0 commit comments

Comments
 (0)