Skip to content

Commit 4a2d8a2

Browse files
authored
Merge pull request #107 from TheBlueMatt/main
Support Building OSX binaries on Linux
2 parents 3b25dea + c5c4841 commit 4a2d8a2

File tree

3 files changed

+81
-17
lines changed

3 files changed

+81
-17
lines changed

.github/workflows/build.yml

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515
run: |
1616
apt-get update
1717
apt-get -y dist-upgrade
18-
apt-get -y install cargo libstd-rust-dev-wasm32 valgrind lld git g++ clang
18+
apt-get -y install cargo libstd-rust-dev-wasm32 valgrind lld git g++ clang wget
1919
- name: Checkout source code
2020
uses: actions/checkout@v2
2121
with:
@@ -58,12 +58,61 @@ jobs:
5858
git diff --exit-code
5959
fi
6060
61+
check_macos:
62+
runs-on: ubuntu-latest
63+
# Ubuntu's version of rustc uses its own LLVM instead of being a real native package.
64+
# This leaves us with an incompatible LLVM version when linking. Instead, use a real OS.
65+
container: debian:bookworm
66+
env:
67+
TOOLCHAIN: stable
68+
steps:
69+
- name: Install native Rust toolchain, Valgrind, and build utilitis
70+
run: |
71+
apt-get update
72+
apt-get -y dist-upgrade
73+
apt-get -y install cargo libstd-rust-dev-wasm32 valgrind lld git g++ clang wget rust-src
74+
- name: Checkout source code
75+
uses: actions/checkout@v2
76+
with:
77+
fetch-depth: 0
78+
- name: Sanity test bindings against Cargo.toml RL
79+
working-directory: lightning-c-bindings
80+
run: |
81+
# Note that the version tags aren't checked into git
82+
touch src/version.rs
83+
RUSTFLAGS="--cfg=c_bindings" cargo check --features std
84+
- name: Install cbindgen
85+
run: |
86+
git clone https://github.com/eqrion/cbindgen
87+
cd cbindgen/
88+
git checkout v0.20.0
89+
cargo update -p indexmap --precise "1.6.2" --verbose
90+
cargo install --locked --path .
91+
- name: Checkout Rust-Lightning git
92+
run: |
93+
git clone https://github.com/rust-bitcoin/rust-lightning
94+
cd rust-lightning
95+
git checkout 0.0.115-bindings
96+
- name: Fix Github Actions to not be broken
97+
run: git config --global --add safe.directory /__w/ldk-c-bindings/ldk-c-bindings
98+
- name: Fetch MacOS SDK
99+
run: |
100+
wget https://bitcoincore.org/depends-sources/sdks/Xcode-12.2-12B45b-extracted-SDK-with-libcxx-headers.tar.gz
101+
tar xvvf Xcode-12.2-12B45b-extracted-SDK-with-libcxx-headers.tar.gz
102+
- name: Rebuild bindings, and check the sample app builds + links
103+
run: |
104+
# rust-src doesn't distribute the rustlib Cargo.lock, but an empty
105+
# file seems to suffice to make `-Zbuild-std` happy.
106+
touch /usr/lib/rustlib/src/rust/Cargo.lock
107+
MACOS_SDK="$PWD/Xcode-12.2-12B45b-extracted-SDK-with-libcxx-headers" ./genbindings.sh ./rust-lightning true
108+
61109
osx:
62110
strategy:
63111
matrix:
64112
include:
65-
- platform: macos-10.15
66113
- platform: macos-11
114+
- platform: macos-12
115+
- platform: macos-13
67116
runs-on: ${{ matrix.platform }}
68117
env:
69118
TOOLCHAIN: stable

deterministic-build-wrappers/clang-lto-link-osx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/bin/sh
1+
#!/bin/bash
22
# We want to use rustc's -C linker-plugin-lto, but it passes several arguments
33
# that are not understood by OSX clang/ld64.lld. Specifically, it passes
44
# -plugin-opt* arguments to set optimization levels, which are not supported.

genbindings.sh

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,20 @@ fi
5353

5454
BASE_HOST_CFLAGS="$BASE_CFLAGS"
5555

56-
if [ "$HOST_OSX" = "true" ]; then
56+
if [ "$MACOS_SDK" = "" -a "$HOST_OSX" = "true" ]; then
57+
MACOS_SDK="$(xcrun --show-sdk-path)"
58+
[ "$MACOS_SDK" = "" ] && exit 1
59+
fi
60+
61+
if [ "$MACOS_SDK" != "" ]; then
5762
export MACOSX_DEPLOYMENT_TARGET=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"
60-
# Targeting aarch64 appears to be supported only starting with Big Sur, so check it before use
61-
clang -o /dev/null $BASE_HOST_CFLAGS --target=aarch64-apple-darwin -mcpu=apple-a14 genbindings_path_map_test_file.c &&
62-
export CFLAGS_aarch64_apple_darwin="$BASE_HOST_CFLAGS --target=aarch64-apple-darwin -mcpu=apple-a14" ||
63-
echo "WARNING: Can not build targeting aarch64-apple-darin. Upgrade to Big Sur or try upstream clang"
63+
BASE_HOST_OSX_CFLAGS="$BASE_HOST_CFLAGS -isysroot$MACOS_SDK -mmacosx-version-min=10.9"
64+
export CFLAGS_aarch64_apple_darwin="$BASE_HOST_OSX_CFLAGS --target=aarch64-apple-darwin -mcpu=apple-a14"
65+
export CFLAGS_x86_64_apple_darwin="$BASE_HOST_OSX_CFLAGS --target=x86_64-apple-darwin -march=sandybridge -mtune=sandybridge"
66+
if [ "$HOST_OSX" = "true" ]; then
67+
LOCAL_CFLAGS="$LOCAL_CFLAGS --target=$HOST_PLATFORM -isysroot$MACOS_SDK -mmacosx-version-min=10.9"
68+
BASE_HOST_CFLAGS="$BASE_HOST_OSX_CFLAGS --target=$HOST_PLATFORM"
69+
fi
6470
fi
6571

6672
rm genbindings_path_map_test_file.c
@@ -214,7 +220,7 @@ EOF
214220
cd lightning-c-bindings
215221

216222
RUSTFLAGS="$RUSTFLAGS --cfg=test_mod_pointers" cargo build $CARGO_BUILD_ARGS
217-
if [ "$CFLAGS_aarch64_apple_darwin" != "" ]; then
223+
if [ "$CFLAGS_aarch64_apple_darwin" != "" -a "$HOST_OSX" = "true" ]; then
218224
RUSTFLAGS="$BASE_RUSTFLAGS -C target-cpu=apple-a14" cargo build $CARGO_BUILD_ARGS --target aarch64-apple-darwin
219225
fi
220226
cbindgen -v --config cbindgen.toml -o include/lightning.h >/dev/null 2>&1
@@ -454,7 +460,7 @@ if [ "$HOST_PLATFORM" = "x86_64-unknown-linux-gnu" -o "$HOST_PLATFORM" = "x86_64
454460
sed -i .bk 's/,"cdylib"]/]/g' Cargo.toml
455461
fi
456462

457-
if [ "$CFLAGS_aarch64_apple_darwin" != "" ]; then
463+
if [ "$CFLAGS_aarch64_apple_darwin" != "" -a "$HOST_OSX" = "true" ]; then
458464
RUSTFLAGS="$BASE_RUSTFLAGS -C target-cpu=apple-a14" RUSTC_BOOTSTRAP=1 cargo rustc $CARGO_BUILD_ARGS --target aarch64-apple-darwin -v -- -Zsanitizer=address -Cforce-frame-pointers=yes || ( mv Cargo.toml.bk Cargo.toml; exit 1)
459465
fi
460466
RUSTFLAGS="$RUSTFLAGS --cfg=test_mod_pointers" RUSTC_BOOTSTRAP=1 cargo rustc $CARGO_BUILD_ARGS -v -- -Zsanitizer=address -Cforce-frame-pointers=yes || ( mv Cargo.toml.bk Cargo.toml; exit 1)
@@ -541,18 +547,27 @@ if [ "$CLANGPP" != "" -a "$LLD" != "" ]; then
541547
# packaging than simply shipping the rustup binaries (eg Debian should Just Work
542548
# here).
543549
LINK_ARG_FLAGS="-C link-arg=-fuse-ld=$LLD"
544-
if [ "$HOST_OSX" = "true" ]; then
545-
export LDK_CLANG_PATH=$(which $CLANG)
550+
export LDK_CLANG_PATH=$(which $CLANG)
551+
if [ "$MACOS_SDK" != "" ]; then
546552
export CLANG="$(pwd)/../deterministic-build-wrappers/clang-lto-link-osx"
547-
for ARG in "CFLAGS_aarch64_apple_darwin"; do
553+
for ARG in $CFLAGS_aarch64_apple_darwin; do
548554
MANUAL_LINK_CFLAGS="$MANUAL_LINK_CFLAGS -C link-arg=$ARG"
549555
done
550556
export CFLAGS_aarch64_apple_darwin="$CFLAGS_aarch64_apple_darwin -O3 -fPIC -fembed-bitcode"
551-
LINK_ARG_FLAGS="$LINK_ARG_FLAGS -C link-arg="-isysroot$(xcrun --show-sdk-path)" -C link-arg=-mmacosx-version-min=10.9"
552-
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
557+
RUSTC_BOOTSTRAP=1 RUSTFLAGS="$BASE_RUSTFLAGS -C target-cpu=apple-a14 -C embed-bitcode=yes -C linker-plugin-lto -C lto -C linker=$CLANG $MANUAL_LINK_CFLAGS $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 -Zbuild-std=std,panic_abort
558+
if [ "$HOST_OSX" != "true" ]; then
559+
# If we're not on OSX but can build OSX binaries, build the x86_64 OSX release now
560+
MANUAL_LINK_CFLAGS=""
561+
for ARG in $CFLAGS_x86_64_apple_darwin; do
562+
MANUAL_LINK_CFLAGS="$MANUAL_LINK_CFLAGS -C link-arg=$ARG"
563+
done
564+
export CFLAGS_x86_64_apple_darwin="$CFLAGS_x86_64_apple_darwin -O3 -fPIC -fembed-bitcode"
565+
RUSTC_BOOTSTRAP=1 RUSTFLAGS="$BASE_RUSTFLAGS -C target-cpu=sandybridge -C embed-bitcode=yes -C linker-plugin-lto -C lto -C linker=$CLANG $MANUAL_LINK_CFLAGS $LINK_ARG_FLAGS -C link-arg=-mcpu=sandybridge -C link-arg=-mtune=sandybridge" CARGO_PROFILE_RELEASE_LTO=true cargo build $CARGO_BUILD_ARGS -v --release --target x86_64-apple-darwin -Zbuild-std=std,panic_abort
566+
fi
553567
fi
554568
# If we're on an M1 don't bother building X86 binaries
555569
if [ "$HOST_PLATFORM" != "aarch64-apple-darwin" ]; then
570+
[ "$HOST_OSX" != "true" ] && export CLANG="$LDK_CLANG_PATH"
556571
export CFLAGS_$ENV_TARGET="$BASE_HOST_CFLAGS -O3 -fPIC -fembed-bitcode"
557572
# Rust doesn't recognize CFLAGS changes, so we need to clean build artifacts
558573
cargo clean --release

0 commit comments

Comments
 (0)