-
Notifications
You must be signed in to change notification settings - Fork 224
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
37 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 35 additions & 47 deletions
82
automation/swift-components-docs/generate-swift-project.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,54 @@ | ||
#!/usr/bin/env bash | ||
# | ||
# This script builds the Rust crate in its directory and generates Swift bindings, headers, and a module map using UniFFI in library mode. | ||
# This script builds the Rust crate in its directory and generates Swift bindings, | ||
# headers, and a module map using UniFFI in library mode. | ||
|
||
FRAMEWORK_NAME="SwiftComponents" | ||
set -euo pipefail # Ensure script exits on errors or unset variables | ||
|
||
FRAMEWORK_NAME="SwiftComponents" | ||
THIS_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )" | ||
REPO_ROOT="$( dirname "$( dirname "$THIS_DIR" )" )" | ||
WORKING_DIR=$THIS_DIR | ||
|
||
MANIFEST_PATH="$WORKING_DIR/Cargo.toml" | ||
|
||
if [[ ! -f "$MANIFEST_PATH" ]]; then | ||
echo "Could not locate Cargo.toml in $MANIFEST_PATH" | ||
exit 1 | ||
fi | ||
|
||
CRATE_NAME=$(grep --max-count=1 '^name =' "$MANIFEST_PATH" | cut -d '"' -f 2) | ||
if [[ -z "$CRATE_NAME" ]]; then | ||
echo "Could not determine crate name from $MANIFEST_PATH" | ||
exit 1 | ||
fi | ||
|
||
# Helper to run the cargo build command in a controlled environment. | ||
# It's important that we don't let environment variables from the user's default | ||
# desktop build environment leak into the iOS build, otherwise it might e.g. | ||
# link against the desktop build of NSS. | ||
|
||
WORKING_DIR="$THIS_DIR" | ||
CARGO="$HOME/.cargo/bin/cargo" | ||
LIBS_DIR="$REPO_ROOT/libs" | ||
|
||
setup_env () { | ||
LIBS_DIR="$REPO_ROOT/libs/ios/arm64" | ||
|
||
env -i \ | ||
NSS_STATIC=1 \ | ||
NSS_DIR="$LIBS_DIR/nss" \ | ||
PATH="${PATH}" | ||
} | ||
|
||
set -euvx | ||
|
||
setup_env | ||
|
||
# Create directories for Swift files, headers, and module map | ||
INCLUDE_DIR="$WORKING_DIR/Sources/$FRAMEWORK_NAME/include" | ||
SWIFT_DIR="$WORKING_DIR/Sources/$FRAMEWORK_NAME" | ||
|
||
mkdir -p "$INCLUDE_DIR" | ||
|
||
# Generate Swift bindings, headers, and module map using uniffi-bindgen in library mode | ||
cargo build -p megazord --release | ||
# Build the Rust crate using Cargo | ||
echo "Building the Rust crate..." | ||
$CARGO build -p megazord --release | ||
|
||
# Define the path to the generated Rust library | ||
LIBRARY_FILE="$REPO_ROOT/target/release/libmegazord.dylib" | ||
$CARGO uniffi-bindgen generate --library "$LIBRARY_FILE" --language swift --out-dir "$SWIFT_DIR" | ||
if [[ ! -f "$LIBRARY_FILE" ]]; then | ||
echo "Error: Rust library not found at $LIBRARY_FILE" | ||
exit 1 | ||
fi | ||
|
||
# Generate Swift bindings, headers, and module map using uniffi-bindgen | ||
echo "Generating Swift bindings with uniffi-bindgen..." | ||
$CARGO uniffi-bindgen generate --library "$LIBRARY_FILE" --language swift --out-dir "$SWIFT_DIR" | ||
|
||
# Move header files to the include directory | ||
mv "$SWIFT_DIR"/*.h "$INCLUDE_DIR" | ||
# Move generated header files to the include directory | ||
echo "Moving header files to include directory..." | ||
mv "$SWIFT_DIR"/*.h "$INCLUDE_DIR" || { | ||
echo "Error: Failed to move header files." | ||
exit 1 | ||
} | ||
|
||
## Remove any component modulemaps | ||
rm -rf "$WORKING_DIR"/Sources/"$FRAMEWORK_NAME"/*.modulemap | ||
# Remove any old modulemaps | ||
echo "Cleaning up old module maps..." | ||
rm -f "$SWIFT_DIR"/*.modulemap | ||
|
||
echo "Generate the modulemap" | ||
./generate-modulemap.sh | ||
# Generate a new module map | ||
echo "Generating module map..." | ||
if [[ ! -f "$WORKING_DIR/generate-modulemap.sh" ]]; then | ||
echo "Error: generate-modulemap.sh script not found." | ||
exit 1 | ||
fi | ||
"$WORKING_DIR/generate-modulemap.sh" | ||
|
||
echo "Successfully generated Swift bindings, headers, and module map." | ||
# Success message | ||
echo "Successfully generated Swift bindings, headers, and module map." |