-
Notifications
You must be signed in to change notification settings - Fork 1
/
build-sargon.sh
executable file
·122 lines (100 loc) · 4.08 KB
/
build-sargon.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/env zsh
set -e
set -u
# Credits: https://github.com/ianthetechie/uniffi-starter/blob/main/rust/build-ios.sh
# NOTE: You MUST run this every time you make changes to the core. Unfortunately, calling this from Xcode directly
# does not work so well.
# In release mode, we create a ZIP archive of the xcframework and update Package.swift with the computed checksum.
# This is only needed when cutting a new release, not for local development.
release=false
# When we test we only need to build for macos.
maconly=false
TAG_OF_RELEASE=""
for arg in "$@"
do
case $arg in
--release-tag)
release=true
TAG_OF_RELEASE="$2"
shift # Remove --release-tag from processing
;;
--maconly)
maconly=true
shift # Remove --maconly from processing
;;
*)
shift # Ignore other argument from processing
;;
esac
done
# Potential optimizations for the future:
# regularly check: https://github.com/ianthetechie/uniffi-starter/blob/main/rust/build-ios.sh
# for improvements!
generate_ffi() {
echo "📦 Generating framework module mapping and FFI bindings $1"
if $maconly; then
local TARGET_FOR_DYLIB_PATH="aarch64-apple-darwin"
else
local TARGET_FOR_DYLIB_PATH="aarch64-apple-ios"
fi
cargo run -p sargon-uniffi --features build-binary --bin sargon-bindgen generate --library target/$TARGET_FOR_DYLIB_PATH/release/lib$1_uniffi.dylib --language swift --out-dir target/uniffi-xcframework-staging
mkdir -p apple/Sources/UniFFI/
mv target/uniffi-xcframework-staging/*.swift apple/Sources/UniFFI/
mv target/uniffi-xcframework-staging/$1FFI.modulemap target/uniffi-xcframework-staging/module.modulemap # Convention requires this have a specific name
}
build_xcframework() {
# Builds an XCFramework
echo "📦 Generating XCFramework $1"
rm -rf target/swift # Delete the output folder so we can regenerate it
local XCFRAME_PATH="target/swift/lib$1-rs.xcframework"
local XCFRAME_ZIP_PATH="$XCFRAME_PATH.zip"
if $maconly; then
xcodebuild -create-xcframework \
-library target/aarch64-apple-darwin/release/lib$1_uniffi.a -headers target/uniffi-xcframework-staging \
-output $XCFRAME_PATH
else
xcodebuild -create-xcframework \
-library target/aarch64-apple-darwin/release/lib$1_uniffi.a -headers target/uniffi-xcframework-staging \
-library target/aarch64-apple-ios/release/lib$1_uniffi.a -headers target/uniffi-xcframework-staging \
-library target/aarch64-apple-ios-sim/release/lib$1_uniffi.a -headers target/uniffi-xcframework-staging \
-output $XCFRAME_PATH
fi
if $release; then
local CHKSUM="RELEASE_WAS_FALSE_THUS_NO_CHECKSUM"
echo "📦 ('release' is true) Building xcframework archive"
zip -r $XCFRAME_ZIP_PATH $XCFRAME_PATH
CHKSUM=$(swift package compute-checksum $XCFRAME_ZIP_PATH)
sed -i "" -E "s/(let releaseTag = \")[^\"]+(\")/\1$TAG_OF_RELEASE\2/g" Package.swift
sed -i "" -E "s/(let releaseChecksum = \")[^\"]+(\")/\1$CHKSUM\2/g" Package.swift
echo "$CHKSUM;$XCFRAME_ZIP_PATH"
else
echo "BUILT_WITHOUT_RELEASE"
fi
}
me=$(basename "$0")
REL_DIR=$0:P
DIR="$( cd "$( dirname "$REL_DIR" )" && pwd )";
PARENT_DIRECTORY="${DIR%/../../*}"
if $release; then
echo "📦 Start of '$me' (see: '$DIR/$me'), TAG_OF_RELEASE = '$TAG_OF_RELEASE'"
else
echo "📦 Start of '$me' (see: '$DIR/$me')"
fi
cd "$DIR"
cd "../../" # go to parent of parent, which is project root.
cargo build -p sargon-uniffi --lib --release --target aarch64-apple-darwin
if $maconly; then
echo "📦 Build for macOS only (skipping iOS)"
else
echo "📦 Building iOS and macOS targets"
cargo build -p sargon-uniffi --lib --release --target aarch64-apple-ios-sim
cargo build -p sargon-uniffi --lib --release --target aarch64-apple-ios
fi
basename=sargon
generate_ffi $basename
OUTPUT_OF_BUILD=$(build_xcframework $basename)
echo "📦 ✅ End of '$me', output"
# This print MUST be the last print.
# The path is read by `release.sh` script.
# This is probably terrible... but I'm a Rust/Swift dev, not a bash script dev...
echo "$OUTPUT_OF_BUILD"