Skip to content

Commit

Permalink
Restore old build process for v2 (#263)
Browse files Browse the repository at this point in the history
The v2-specific automation updates from #203 didn't seem to work correctly when @nplasterer and I tested it, it seemed like the v3 and v2 releases (which publish to different repos) get mixed together, and we're not sure if this is because of user error or a genuine issue.

I was going to go back and figure out what is wrong later, but as @neekolas needs this now, I'm reverting the build scripts to what they were in 7f78ade, which has been working for me. Later, we can revert this PR and debug the automation.
  • Loading branch information
richardhuaaa authored Oct 5, 2023
1 parent 82a007b commit 8efd3b8
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 103 deletions.
132 changes: 66 additions & 66 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ on:
- main
- v2
jobs:

build:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
target: [
target:
[
x86_64-linux-android,
i686-linux-android,
armv7-linux-androideabi,
Expand Down Expand Up @@ -64,7 +64,8 @@ jobs:
strategy:
fail-fast: false
matrix:
target: [
target:
[
aarch64-apple-darwin,
x86_64-apple-darwin,
aarch64-apple-ios,
Expand All @@ -74,7 +75,7 @@ jobs:
steps:
- name: Checkout
uses: actions/checkout@v1

- name: "Cache"
uses: actions/cache@v3
with:
Expand All @@ -91,7 +92,7 @@ jobs:
profile: minimal
override: true
target: ${{ matrix.target }}

- name: Build target
uses: actions-rs/cargo@v1
with:
Expand All @@ -103,7 +104,7 @@ jobs:
with:
name: ${{ matrix.target }}
path: bindings_swift/target/${{ matrix.target }}/release/libxmtp_rust_swift.a
retention-days: 1
retention-days: 1

kotlin:
runs-on: macos-latest
Expand All @@ -125,8 +126,8 @@ jobs:
profile: minimal
override: true
target: x86_64-apple-darwin
- name : Install kotlin
run: brew install ktlint
- name: Install kotlin
run: brew install ktlint
- name: Generate Bindings
working-directory: xmtp_dh
run: ./gen_kotlin.sh
Expand All @@ -137,67 +138,66 @@ jobs:
path: xmtp_dh/src/uniffi/xmtp_dh/xmtp_dh.kt
retention-days: 1

swift:
runs-on: macos-latest
steps:
- name: Checkout
uses: actions/checkout@v1
- name: Cache
uses: actions/cache@v3
with:
path: |
~/.cargo/registry
~/.cargo/git
./target/release
key: ${{ hashFiles('**/Cargo.toml','**/Cargo.lock') }}
- name: Install rust
uses: actions-rs/toolchain@v1
with:
toolchain: stable
profile: minimal
override: true
target: x86_64-apple-darwin
- name : Install swift
run: brew install swiftformat
- name: Generate and package bindings
working-directory: bindings_swift
run: make bridge
- name: Upload binding archive
uses: actions/upload-artifact@v3
with:
name: bridge
path: |
bindings_swift/include
bindings_swift/Sources
retention-days: 1
# swift:
# runs-on: macos-latest
# steps:
# - name: Checkout
# uses: actions/checkout@v1
# - name: Cache
# uses: actions/cache@v3
# with:
# path: |
# ~/.cargo/registry
# ~/.cargo/git
# ./target/release
# key: ${{ hashFiles('**/Cargo.toml','**/Cargo.lock') }}
# - name: Install rust
# uses: actions-rs/toolchain@v1
# with:
# toolchain: stable
# profile: minimal
# override: true
# target: x86_64-apple-darwin
# - name : Install swift
# run: brew install swiftformat
# - name: Generate and package bindings
# working-directory: bindings_swift
# run: make bridge
# - name: Upload binding archive
# uses: actions/upload-artifact@v3
# with:
# name: bridge
# path: |
# bindings_swift/include
# bindings_swift/Sources
# retention-days: 1

package-swift:
needs: [ build-macos, swift ]
runs-on: macos-latest
steps:
- name: Checkout
uses: actions/checkout@v1
- name: Download libs
uses: actions/download-artifact@v3
with:
path: bindings_swift/build
- name: Build xmtp-rust-swift.zip
working-directory: bindings_swift
run: |
rm -r ./include
mv build/bridge/include .
mv build/bridge/Sources .
make swift
- name: Upload xmtp-rust-swift.zip
uses: actions/upload-artifact@v3
with:
name: xmtp-rust-swift.zip
path: bindings_swift/xmtp-rust-swift.zip
retention-days: 1

# package-swift:
# needs: [ build-macos, swift ]
# runs-on: macos-latest
# steps:
# - name: Checkout
# uses: actions/checkout@v1
# - name: Download libs
# uses: actions/download-artifact@v3
# with:
# path: bindings_swift/build
# - name: Build xmtp-rust-swift.zip
# working-directory: bindings_swift
# run: |
# rm -r ./include
# mv build/bridge/include .
# mv build/bridge/Sources .
# make swift
# - name: Upload xmtp-rust-swift.zip
# uses: actions/upload-artifact@v3
# with:
# name: xmtp-rust-swift.zip
# path: bindings_swift/xmtp-rust-swift.zip
# retention-days: 1

package-android:
needs: [ build, kotlin ]
needs: [build, kotlin]
runs-on: ubuntu-latest
steps:
- name: Download libs & bindings
Expand Down
38 changes: 17 additions & 21 deletions bindings_swift/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,13 @@ $(ARCHS_MAC): %:
aarch64-apple-ios:
cargo build --target $@ --target-dir ./target --release

# lib: $(ARCHS_IOS) $(ARCHS_MAC) aarch64-apple-ios
lib:
for arch in $(ARCHS_IOS) $(ARCHS_MAC) aarch64-apple-ios; do \
mkdir -p build/$$arch/; \
cp target/$$arch/release/$(LIB) build/$$arch/; \
done
$(LIB): $(ARCHS_IOS) $(ARCHS_MAC) aarch64-apple-ios
rm -rf lipo_macos lipo_ios_simulator
mkdir -p lipo_macos lipo_ios_simulator
lipo -create -output lipo_ios_simulator/libxmtp_rust_swift.a $(foreach arch,$(ARCHS_IOS),$(wildcard target/$(arch)/release/$(LIB)))
lipo -create -output lipo_macos/libxmtp_rust_swift.a $(foreach arch,$(ARCHS_MAC),$(wildcard target/$(arch)/release/$(LIB)))

lipo:
rm -rf build/lipo_macos build/lipo_ios_simulator
mkdir -p build/lipo_macos build/lipo_ios_simulator
lipo -create -output build/lipo_ios_simulator/$(LIB) $(foreach arch,$(ARCHS_IOS),$(wildcard build/$(arch)/$(LIB)))
lipo -create -output build/lipo_macos/$(LIB) $(foreach arch,$(ARCHS_MAC),$(wildcard build/$(arch)/$(LIB)))

bridge:
move_gen:
rm -rf Generated
# Clean and build to regenerate swift-bridge "Generated" directory
cargo clean
Expand All @@ -46,19 +39,22 @@ bridge:
# Keep top-level Generated for push-to-swift step where .swift files are
# copied over to another repo (xmtp-rust-swift)
find include/Generated -type f -name "*.swift" -delete
./make_xmtp_rust_swift_sources.sh

framework: lipo
framework: move_gen $(LIB)
rm -rf XMTPRustSwift.xcframework
xcodebuild -create-xcframework \
-library build/lipo_macos/libxmtp_rust_swift.a -headers include/ \
-library build/lipo_ios_simulator/libxmtp_rust_swift.a -headers include/ \
-library build/aarch64-apple-ios/libxmtp_rust_swift.a -headers include/ \
-library ./lipo_macos/libxmtp_rust_swift.a \
-headers ./include/ \
-library ./lipo_ios_simulator/libxmtp_rust_swift.a \
-headers ./include/ \
-library ./target/aarch64-apple-ios/release/libxmtp_rust_swift.a \
-headers ./include/ \
-output XMTPRustSwift.xcframework
rm -f bundle.zip
zip -r bundle.zip XMTPRustSwift.xcframework
openssl dgst -sha256 bundle.zip

swift: framework
rm -f xmtp-rust-swift.zip
zip -r xmtp-rust-swift.zip XMTPRustSwift.xcframework Sources
openssl dgst -sha256 xmtp-rust-swift.zip
./copy_xcframework_to_swift.sh

.PHONY: $(ARCHS_IOS) $(ARCHS_MAC) framework all aarch64-apple-ios download-toolchains swift
20 changes: 4 additions & 16 deletions bindings_swift/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,19 @@ It pairs with [xmtp-rust-swift](https://github.com/xmtp/xmtp-rust-swift) which i

- Rust
- Run `make download-toolchains` to get all the iOS and MacOS toolchains
- Clone `xmtp-rust-swift` from above and put it at the same directory level as this repository (so `../xmtp-rust-swift`)

## Workflow

- Write code in `./src` to expose functionality to Swift
- Run `cargo test` to make sure your code works
- Run `make swift` to build local crate, generate Swift bindings, package the xcframework, and push all files to `../xmtp-rust-swift`

### Building xcframework
### Just xcframework

- Run `make framework`

### Building the binaries and bindings

The Build Action rebuilds all binaries and bindings on every commit to this branch. It also packages everything into an artifact `xmtp-rust-swift.zip` that can be unzipped over the target repo (`xmtp/xmtp-rust-swift`).

### Releasing new version

Tag the commit you want to release with the appropriate version (e.g. 0.3.0-beta0).
The Release github workflow will do the following:

- check out `xmtp-rust-repo` and update it with the contents of the `xmtp-rust-swift.zip` file that was generated by the Build action.
- push new commit to the `xmtp-rust-swift` repo and tag it with the same tag

NOTES: To allow the workflow to push to another repo the setup follows [this guide](https://docs.github.com/en/apps/creating-github-apps/authenticating-with-a-github-app/making-authenticated-api-requests-with-a-github-app-in-a-github-actions-workflow#authenticating-with-a-github-app). It uses [this app installed on the org](https://github.com/organizations/xmtp/settings/apps/libxmtp-release). The relevant secrets are stored only [in this repo](https://github.com/xmtp/libxmtp/settings/secrets/actions). If additional repos are added to this workflow they MUST be added to [this installation](https://github.com/organizations/xmtp/settings/installations/39118494) of the app.

### Steps for xmtp-ios SDK integration
## Optional Steps for xmtp-ios integration

- Get set up with Xcode
- Clone [xmtp-ios](https://github.com/xmtp/xmtp-ios)
Expand Down
58 changes: 58 additions & 0 deletions bindings_swift/copy_xcframework_to_swift.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#!/bin/bash

set -ex

# This script copies the built XCFramework to a the xmtp_rust_swift repo, which is a Swift package
# that encapsulates all of the good stuff here.

# Look for an xmtp_rust_swift repo at the sibling layer of the top level of this repo so ../../

REPONAME="xmtp-rust-swift"
REPOPATH="../../$REPONAME"
# Now move the XMTPRustSwift.xcframework to the Swift package
rm -rf "$REPOPATH/XMTPRustSwift.xcframework"
cp -R "XMTPRustSwift.xcframework" "$REPOPATH"

# Need to copy any Swift file in ./include/Generated to $REPOPATH/Sources/XMTPRust/*
FILES=$(find ./Generated -name "*.swift")

# HACK HACK HACK
# Here's the ultra-hack, we need to inject "import XMTPRustSwift" into the top of the Swift files
# before copying them over so they'll get the headers we moved to "include/Generated".
#
# Moreover, we inject the following lines to allow RustStrings to be interpreted as NSErrors and automatically
# displayed in the debugger:
# https://github.com/chinedufn/swift-bridge/issues/150
#
# extension RustString: @unchecked Sendable {}
#
# extension RustString: LocalizedError {
# public var errorDescription: String? {
# return NSLocalizedString("XMTP Rust Error: \(self.as_str().toString())", comment: self.as_str().toString())
# }
# }
add_xmtprustswift_import() {
echo "Injecting 'import XMTPRustSwift' text as first line into $1"
sed -i '' '1s/^/import XMTPRustSwift\n\n/' "$1"
}
add_foundation_import() {
echo "Injecting 'import Foundation' text as first line into $1"
sed -i '' '1s/^/import Foundation\n\n/' "$1"
}
add_nserror_helpers() {
sed -i '' '1s/^/extension RustString: @unchecked Sendable {}\nextension RustString: LocalizedError {\n public var errorDescription: String? {\n return NSLocalizedString("XMTP Rust Error: \\(self.as_str().toString())", comment: self.as_str().toString())\n }\n}\n\n/' "$1"
}
for f in $FILES
do
# If it's a xmtp_rust_swift.swift file, then do the injection
if [[ $f == *"xmtp_rust_swift.swift" ]]; then
add_nserror_helpers "$f"
# Only the xmtp_rust_swift.swift file needs Foundation
add_foundation_import "$f"
fi
# All files need "import XMTPRustSwift"
add_xmtprustswift_import "$f"
echo "Copying $f"
mv "$f" "$REPOPATH/Sources/XMTPRust/"
done

0 comments on commit 8efd3b8

Please sign in to comment.