From 378553619d95df11aecf67958c2017328f598816 Mon Sep 17 00:00:00 2001 From: Jordan Santell Date: Mon, 10 Jul 2023 11:24:22 -0700 Subject: [PATCH] chore: Provide a script to generate headers that uses codegen_units=1 when building on macos to circumvent a header generation issue. Update CI and C makefile to use this script. Fixes #473 --- .github/workflows/noosphere_apple_build.yaml | 4 +- c/example/Makefile | 14 +++--- scripts/generate_headers | 27 ++++++++++ scripts/xcode_local_build | 53 ++++++++++++++++++++ 4 files changed, 89 insertions(+), 9 deletions(-) create mode 100755 scripts/generate_headers create mode 100755 scripts/xcode_local_build diff --git a/.github/workflows/noosphere_apple_build.yaml b/.github/workflows/noosphere_apple_build.yaml index fe463bc48..cca561d0e 100644 --- a/.github/workflows/noosphere_apple_build.yaml +++ b/.github/workflows/noosphere_apple_build.yaml @@ -96,9 +96,7 @@ jobs: brew: protobuf cmake - name: 'Generate the header' run: | - cd rust/noosphere/include/noosphere - cargo run --example generate_header --features headers --locked - cd - + scripts/generate_header - uses: actions/upload-artifact@v3 with: name: include diff --git a/c/example/Makefile b/c/example/Makefile index 5bc3529f6..6c3509efa 100644 --- a/c/example/Makefile +++ b/c/example/Makefile @@ -1,13 +1,15 @@ LIBNOOSPHERE := ../../target/debug/deps/libnoosphere.a +HEADER := ../../target/headers/include/noosphere/noosphere.h +INCLUDE_PATH := ../../target/headers/include/noosphere -noosphere.h: - cargo run -p noosphere --example generate_header --features headers +$(HEADER): + ../../scripts/generate_headers $(LIBNOOSPHERE): cargo build -p noosphere -main.o: noosphere.h - $(CC) -c main.c +main.o: $(HEADER) + $(CC) -I$(INCLUDE_PATH) -c main.c main.out: main.o $(LIBNOOSPHERE) - $(CC) main.o $(LIBNOOSPHERE) -lm -o main.out + $(CC) $(LIBNOOSPHERE) -I$(INCLUDE_PATH) main.o -lm -o main.out .PHONY: build run clean @@ -16,4 +18,4 @@ build: main.out run: main.out ./main.out clean: - rm -rf noosphere.h main.o main.out + rm -rf $(HEADER) main.o main.out diff --git a/scripts/generate_headers b/scripts/generate_headers new file mode 100755 index 000000000..0e8d81c13 --- /dev/null +++ b/scripts/generate_headers @@ -0,0 +1,27 @@ +#!/usr/bin/env bash +set -e +set -x + +SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) +PROJECT_DIR="$SCRIPT_DIR/../" + +pushd $PROJECT_DIR + +rm -rf ./target/headers +mkdir -p ./target/headers +cp -r ./rust/noosphere/include ./target/headers/ + +if [[ "$OSTYPE" == "darwin"* ]]; then + # macos linker fails to generate all exports without this flag + # https://github.com/subconsciousnetwork/noosphere/issues/473 + CARGO_PROFILE_DEV_CODEGEN_UNITS=1 cargo run --verbose --package noosphere --example generate_header --features headers --locked +else + cargo run --verbose --package noosphere --example generate_header --features headers --locked +fi + +mv ./noosphere.h ./target/headers/include/noosphere/noosphere.h + +popd + +set +x +set +e diff --git a/scripts/xcode_local_build b/scripts/xcode_local_build new file mode 100755 index 000000000..756513b93 --- /dev/null +++ b/scripts/xcode_local_build @@ -0,0 +1,53 @@ +#!/usr/bin/env bash +set -e +set -x + +SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) +PROJECT_DIR="$SCRIPT_DIR/../" + +pushd $PROJECT_DIR + +rm -rf ./LibNoosphere.xcframework + +$SCRIPT_DIR/generate_headers + +TARGETS=( + "aarch64-apple-ios" + "x86_64-apple-ios" + "aarch64-apple-ios-sim" + "x86_64-apple-darwin" + "aarch64-apple-darwin" +) + +for TARGET in "${TARGETS[@]}"; do + rustup target install $TARGET + cargo build --package noosphere --release --target $TARGET --locked + # cargo build --package noosphert buie --target $TARGET --locked +done + +mkdir -p ./target/macos +mkdir -p ./target/simulator + +lipo -create \ + ./target/x86_64-apple-darwin/release/libnoosphere.a \ + ./target/aarch64-apple-darwin/release/libnoosphere.a \ + -output ./target/macos/libnoosphere.a + +lipo -create \ + ./target/x86_64-apple-ios/release/libnoosphere.a \ + ./target/aarch64-apple-ios-sim/release/libnoosphere.a \ + -output ./target/simulator/libnoosphere.a + +xcodebuild -create-xcframework \ + -library ./target/macos/libnoosphere.a \ + -headers ./target/headers/include/ \ + -library ./target/simulator/libnoosphere.a \ + -headers ./target/headers/include/ \ + -library ./target/aarch64-apple-ios/release/libnoosphere.a \ + -headers ./target/headers/include/ \ + -output ./LibNoosphere.xcframework + +popd + +set +x +set +e