Skip to content

Commit eeefb40

Browse files
committed
feat: optimize binary size and iOS-only build
- Separate CLI feature for uniffi-bindgen (reduces static lib from 28MB to 17MB) - Add release profile: disable LTO, enable strip, optimize for size - Remove macOS support from Swift build (iOS only) - Fix path casing issues in build scripts using realpath - Add bindings/ to gitignore
1 parent ef26438 commit eeefb40

File tree

5 files changed

+30
-41
lines changed

5 files changed

+30
-41
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
/target
2-
.DS_Store
2+
.DS_Store/bindings

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ name = "cooklang_find"
1212
[[bin]]
1313
name = "uniffi-bindgen"
1414
path = "src/bin/uniffi_bindgen.rs"
15+
required-features = ["cli"]
16+
17+
[features]
18+
default = []
19+
cli = ["uniffi/cli"]
1520

1621
[dependencies]
1722
camino = { version = "1.1", features = ["serde1"] }
@@ -20,7 +25,7 @@ serde = { version = "1.0", features = ["derive"] }
2025
serde_yaml = "0.9"
2126
serde_json = "1.0"
2227
thiserror = "2"
23-
uniffi = { version = "0.28", features = ["cli"] }
28+
uniffi = "0.28"
2429

2530
[build-dependencies]
2631
uniffi = { version = "0.28", features = ["build"] }
@@ -29,3 +34,9 @@ uniffi = { version = "0.28", features = ["build"] }
2934
tempfile = "3"
3035
indoc = "2"
3136
uniffi = { version = "0.28", features = ["bindgen-tests"] }
37+
38+
[profile.release]
39+
lto = false
40+
strip = true
41+
opt-level = "s"
42+
panic = "abort"

scripts/build-kotlin.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ generate_bindings() {
163163
fi
164164
fi
165165

166-
cargo run --release --bin uniffi-bindgen -- generate \
166+
cargo run --release --features cli --bin uniffi-bindgen -- generate \
167167
--library "$lib_path" \
168168
--language kotlin \
169169
--config "${PROJECT_ROOT}/uniffi.toml" \

scripts/build-swift.sh

Lines changed: 15 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,13 @@
44

55
set -e
66

7-
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
8-
PROJECT_ROOT="$(dirname "$SCRIPT_DIR")"
7+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)"
8+
# Use realpath to get canonical path with correct case on macOS
9+
PROJECT_ROOT="$(realpath "$(dirname "$SCRIPT_DIR")")"
10+
11+
# Ensure we're working from the canonical path
12+
cd "$PROJECT_ROOT"
13+
914
OUTPUT_DIR="${PROJECT_ROOT}/bindings/swift"
1015
XCFRAMEWORK_DIR="${OUTPUT_DIR}/CooklangFind.xcframework"
1116

@@ -51,13 +56,9 @@ check_requirements() {
5156

5257
# Install required Rust targets
5358
install_targets() {
54-
log_info "Installing Rust targets for Apple platforms..."
55-
56-
# macOS
57-
rustup target add x86_64-apple-darwin 2>/dev/null || true
58-
rustup target add aarch64-apple-darwin 2>/dev/null || true
59+
log_info "Installing Rust targets for iOS..."
5960

60-
# iOS
61+
# iOS only
6162
rustup target add aarch64-apple-ios 2>/dev/null || true
6263
rustup target add aarch64-apple-ios-sim 2>/dev/null || true
6364
rustup target add x86_64-apple-ios 2>/dev/null || true
@@ -82,7 +83,7 @@ generate_bindings() {
8283

8384
# Find a built library to generate bindings from
8485
local lib_path=""
85-
for target in aarch64-apple-darwin x86_64-apple-darwin aarch64-apple-ios; do
86+
for target in aarch64-apple-ios aarch64-apple-ios-sim x86_64-apple-ios; do
8687
local candidate="${PROJECT_ROOT}/target/${target}/release/libcooklang_find.dylib"
8788
if [[ -f "$candidate" ]]; then
8889
lib_path="$candidate"
@@ -105,7 +106,7 @@ generate_bindings() {
105106
fi
106107
fi
107108

108-
cargo run --release --bin uniffi-bindgen -- generate \
109+
cargo run --release --features cli --bin uniffi-bindgen -- generate \
109110
--library "$lib_path" \
110111
--language swift \
111112
--config "${PROJECT_ROOT}/uniffi.toml" \
@@ -114,7 +115,7 @@ generate_bindings() {
114115
log_info "Swift bindings generated at: $OUTPUT_DIR/Sources/CooklangFind"
115116
}
116117

117-
# Create XCFramework (macOS only)
118+
# Create XCFramework (iOS only)
118119
create_xcframework() {
119120
if [[ "$OSTYPE" != "darwin"* ]]; then
120121
log_warn "XCFramework creation is only supported on macOS"
@@ -128,24 +129,6 @@ create_xcframework() {
128129

129130
local framework_args=()
130131

131-
# macOS universal binary
132-
if [[ -f "${PROJECT_ROOT}/target/aarch64-apple-darwin/release/libcooklang_find.a" ]] && \
133-
[[ -f "${PROJECT_ROOT}/target/x86_64-apple-darwin/release/libcooklang_find.a" ]]; then
134-
log_info "Creating macOS universal binary..."
135-
mkdir -p "$OUTPUT_DIR/tmp/macos"
136-
lipo -create \
137-
"${PROJECT_ROOT}/target/aarch64-apple-darwin/release/libcooklang_find.a" \
138-
"${PROJECT_ROOT}/target/x86_64-apple-darwin/release/libcooklang_find.a" \
139-
-output "$OUTPUT_DIR/tmp/macos/libcooklang_find.a"
140-
141-
# Create module for macOS
142-
mkdir -p "$OUTPUT_DIR/tmp/macos/Headers"
143-
cp "$OUTPUT_DIR/Sources/CooklangFind/CooklangFindFFI.h" "$OUTPUT_DIR/tmp/macos/Headers/"
144-
cp "$OUTPUT_DIR/Sources/CooklangFind/CooklangFindFFI.modulemap" "$OUTPUT_DIR/tmp/macos/Headers/module.modulemap"
145-
146-
framework_args+=(-library "$OUTPUT_DIR/tmp/macos/libcooklang_find.a" -headers "$OUTPUT_DIR/tmp/macos/Headers")
147-
fi
148-
149132
# iOS device
150133
if [[ -f "${PROJECT_ROOT}/target/aarch64-apple-ios/release/libcooklang_find.a" ]]; then
151134
log_info "Adding iOS device library..."
@@ -202,10 +185,7 @@ import PackageDescription
202185
let package = Package(
203186
name: "CooklangFind",
204187
platforms: [
205-
.iOS(.v13),
206-
.macOS(.v10_15),
207-
.tvOS(.v13),
208-
.watchOS(.v6)
188+
.iOS(.v13)
209189
],
210190
products: [
211191
.library(
@@ -250,7 +230,7 @@ main() {
250230
echo "Usage: $0 [OPTIONS]"
251231
echo ""
252232
echo "Options:"
253-
echo " --all Build for all Apple platforms"
233+
echo " --all Build for all iOS platforms"
254234
echo " --generate-only Only generate Swift bindings (no compilation)"
255235
echo " --help, -h Show this help message"
256236
exit 0
@@ -273,13 +253,11 @@ main() {
273253
install_targets
274254

275255
if [[ "$OSTYPE" == "darwin"* ]]; then
276-
build_target "aarch64-apple-darwin"
277-
build_target "x86_64-apple-darwin"
278256
build_target "aarch64-apple-ios"
279257
build_target "aarch64-apple-ios-sim"
280258
build_target "x86_64-apple-ios"
281259
else
282-
log_warn "Cross-compiling for Apple platforms requires macOS"
260+
log_warn "Cross-compiling for iOS requires macOS"
283261
log_info "Building for host platform only..."
284262
cargo build --release
285263
fi

0 commit comments

Comments
 (0)