]> git.bitcoin.ninja Git - ldk-c-bindings/commitdiff
Support cross-language LTO on OSX (with upstream clang/lld)
authorMatt Corallo <git@bluematt.me>
Tue, 30 Nov 2021 16:19:08 +0000 (16:19 +0000)
committerMatt Corallo <git@bluematt.me>
Tue, 30 Nov 2021 20:47:13 +0000 (20:47 +0000)
deterministic-build-wrappers/clang-lto-link-osx [new file with mode: 0755]
genbindings.sh

diff --git a/deterministic-build-wrappers/clang-lto-link-osx b/deterministic-build-wrappers/clang-lto-link-osx
new file mode 100755 (executable)
index 0000000..ae566d1
--- /dev/null
@@ -0,0 +1,15 @@
+#!/bin/sh
+# We want to use rustc's -C linker-plugin-lto, but it passes several arguments
+# that are not understood by OSX clang/ld64.lld. Specifically, it passes
+# -plugin-opt* arguments to set optimization levels, which are not supported.
+# Thus, we intercept the call here, strip the unsupported arguments, and pass
+# -flto and -O3.
+args=("$@")
+for ((i=0; i<"${#args[@]}"; ++i)); do
+    case ${args[i]} in
+        -Wl,-plugin-opt*)
+                       args[i]=""
+                       ;;
+    esac
+done
+$LDK_CLANG_PATH -flto -O3 -Wl,-O3 "${args[@]}"
index f572ccc58729fa341df495a3fc65410f8f508b21..8d4d72699955ee19d12b948b26643dbd31537857 100755 (executable)
@@ -351,7 +351,7 @@ if [ "$HOST_PLATFORM" = "host: x86_64-apple-darwin" ]; then
                echo "Apple clang isn't compatible with upstream clang, install upstream clang"
                CLANG_LLVM_V="0"
        else
-               CLANG_LLVM_V=$(clang --version | head -n1 | awk '{ print substr($4, 0, 2); }')
+               CLANG_LLVM_V=$(clang --version | head -n1 | awk '{ print substr($3, 0, 2); }')
                if [ -x "$(which ld64.lld)" ]; then
                        LLD_LLVM_V="$(ld64.lld --version | awk '{ print substr($2, 0, 2); }')"
                fi
@@ -438,13 +438,13 @@ echo "C++ Bin size and runtime with only RL (LTO) optimized:"
 ls -lha a.out
 time ./a.out > /dev/null
 
-if [ "$HOST_PLATFORM" != "host: x86_64-apple-darwin" -a "$CLANGPP" != "" ]; then
+if [ "$CLANGPP" != "" ]; then
        # If we can use cross-language LTO, use it for building C dependencies (i.e. libsecp256k1) as well
        export CC="$CLANG"
        # The cc-rs crate tries to force -fdata-sections and -ffunction-sections on, which
        # breaks -fembed-bitcode, so we turn off cc-rs' default flags and specify exactly
        # what we want here.
-       export CFLAGS_$ENV_TARGET="$BASE_CFLAGS -fPIC -fembed-bitcode -march=sandybridge"
+       export CFLAGS_$ENV_TARGET="$BASE_CFLAGS -fPIC -fembed-bitcode -march=sandybridge -mcpu=sandybridge -mtune=sandybridge"
        export CRATE_CC_NO_DEFAULTS=true
 fi
 
@@ -478,25 +478,35 @@ for IDX in ${!EXTRA_TARGETS[@]}; do
        RUSTFLAGS="$BASE_RUSTFLAGS -C linker=${EXTRA_CCS[$IDX]}" CARGO_PROFILE_RELEASE_LTO=true cargo rustc -v --release --target "${EXTRA_TARGETS[$IDX]}" -- -C lto
 done
 
-if [ "$CFLAGS_aarch64_apple_darwin" != "" ]; then
-       RUSTFLAGS="$BASE_RUSTFLAGS -C target-cpu=apple-a14" CARGO_PROFILE_RELEASE_LTO=true cargo rustc -v --release --target aarch64-apple-darwin -- -C lto
-fi
-
-if [ "$HOST_PLATFORM" != "host: x86_64-apple-darwin" -a "$CLANGPP" != "" -a "$LLD" != "" ]; then
+if [ "$CLANGPP" != "" -a "$LLD" != "" ]; then
        # Finally, test cross-language LTO. Note that this will fail if rustc and clang++
        # build against different versions of LLVM (eg when rustc is installed via rustup
        # or Ubuntu packages). This should work fine on Distros which do more involved
        # packaging than simply shipping the rustup binaries (eg Debian should Just Work
        # here).
-       export CFLAGS_$ENV_TARGET="$BASE_CFLAGS -O3 -fPIC -fembed-bitcode -march=sandybridge"
+       LINK_ARG_FLAGS="-C link-arg=-fuse-ld=$LLD"
+       if [ "$HOST_PLATFORM" = "host: x86_64-apple-darwin" ]; then
+               export LDK_CLANG_PATH=$(which $CLANG)
+               export CLANG="$(pwd)/../deterministic-build-wrappers/clang-lto-link-osx"
+               for ARG in "CFLAGS_aarch64_apple_darwin"; do
+                       MANUAL_LINK_CFLAGS="$MANUAL_LINK_CFLAGS -C link-arg=$ARG"
+               done
+               export CFLAGS_aarch64_apple_darwin="$CFLAGS_aarch64_apple_darwin -O3 -fPIC -fembed-bitcode"
+               LINK_ARG_FLAGS="$LINK_ARG_FLAGS -C link-arg="-isysroot$(xcrun --show-sdk-path)" -C link-arg=-mmacosx-version-min=10.9"
+               RUSTFLAGS="$BASE_RUSTFLAGS -C target-cpu=apple-a14" CARGO_PROFILE_RELEASE_LTO=true cargo rustc -v --release --target aarch64-apple-darwin -- -C linker-plugin-lto -C lto -C linker=$CLANG $LINK_ARG_FLAGS -C link-arg=-mcpu=apple-a14
+       fi
+       export CFLAGS_$ENV_TARGET="$BASE_CFLAGS -O3 -fPIC -fembed-bitcode -march=sandybridge -mcpu=sandybridge -mtune=sandybridge"
        # Rust doesn't recognize CFLAGS changes, so we need to clean build artifacts
        cargo clean --release
-       CARGO_PROFILE_RELEASE_LTO=true cargo rustc -v --release -- -C linker-plugin-lto -C lto -C linker=$CLANG -C link-arg=-fuse-ld=$LLD
+       CARGO_PROFILE_RELEASE_LTO=true cargo rustc -v --release -- -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
        $CLANGPP $LOCAL_CFLAGS -flto -fuse-ld=lld -O2 demo.cpp target/release/libldk.a -ldl
        strip ./a.out
        echo "C++ Bin size and runtime with cross-language LTO:"
        ls -lha a.out
        time ./a.out > /dev/null
 else
-       echo "WARNING: Building with cross-language LTO is not avilable on OSX or without clang-$RUSTC_LLVM_V"
+       if [ "$CFLAGS_aarch64_apple_darwin" != "" ]; then
+               RUSTFLAGS="$BASE_RUSTFLAGS -C target-cpu=apple-a14" CARGO_PROFILE_RELEASE_LTO=true cargo rustc -v --release --target aarch64-apple-darwin -- -C lto
+       fi
+       echo "WARNING: Building with cross-language LTO is not avilable without clang-$RUSTC_LLVM_V"
 fi