Skip to content

Commit b6f7970

Browse files
committed
allow zigbuild
1 parent d7197e3 commit b6f7970

File tree

1 file changed

+28
-17
lines changed

1 file changed

+28
-17
lines changed

scripts/compile_to_native.sh

+28-17
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,26 @@
22

33
# Function to display usage information
44
usage() {
5-
echo "Usage: $0 [OS] [ARCH] [LIB_NAME] [LIB_TYPE] [OUT_DIR]"
6-
echo "Compile the project for the specified OS, architecture, library name, library type, and output directory."
5+
echo "Usage: $0 [OS] [ARCH] [LIB_NAME] [LIB_TYPE] [OUT_DIR] [BUILD_TOOL]"
6+
echo "Compile the project for the specified OS, architecture, library name, library type, output directory, and build tool."
77
echo "If no OS and ARCH are provided, it defaults to the current system's OS and architecture."
88
echo "If no LIB_NAME is provided, it defaults to 'c_eth_kzg'."
99
echo "If no LIB_TYPE is provided, it defaults to 'both'."
1010
echo "If no OUT_DIR is provided, it defaults to './bindings/c/build'."
11+
echo "If no BUILD_TOOL is provided, it defaults to 'cargo'."
1112
echo
1213
echo "Arguments:"
13-
echo " OS Operating system (e.g., Linux, Darwin, MINGW64_NT)"
14-
echo " ARCH Architecture (e.g., x86_64, arm64, universal)"
15-
echo " LIB_NAME Library name (e.g., c_eth_kzg)"
16-
echo " LIB_TYPE Library type to copy (static, dynamic, or both)"
17-
echo " OUT_DIR Output directory for the compiled libraries"
14+
echo " OS Operating system (e.g., Linux, Darwin, MINGW64_NT)"
15+
echo " ARCH Architecture (e.g., x86_64, arm64, universal)"
16+
echo " LIB_NAME Library name (e.g., c_eth_kzg)"
17+
echo " LIB_TYPE Library type to copy (static, dynamic, or both)"
18+
echo " OUT_DIR Output directory for the compiled libraries"
19+
echo " BUILD_TOOL Build tool to use (cargo or zigbuild)"
1820
echo
1921
echo "Examples:"
20-
echo " $0 # Uses the system's OS and architecture, copies both libraries to the default directory with the default library name"
21-
echo " $0 Linux x86_64 my_lib static # Compiles for Linux on x86_64 and copies only static libraries to the default directory with the library name 'my_lib'"
22-
echo " $0 Darwin arm64 my_lib dynamic ./out/dir # Compiles for macOS on ARM (Apple Silicon) and copies only dynamic libraries to './out/dir' with the library name 'my_lib'"
23-
echo " $0 Darwin universal my_lib both ./out/dir # Compiles a universal binary for macOS and copies both static and dynamic libraries to './out/dir' with the library name 'my_lib'"
22+
echo " $0 # Uses the system's OS and architecture, copies both libraries to the default directory with the default library name, using cargo"
23+
echo " $0 Linux x86_64 my_lib static . cargo # Compiles for Linux on x86_64 and copies only static libraries to the current directory with the library name 'my_lib', using cargo"
24+
echo " $0 Darwin arm64 my_lib dynamic ./out/dir zigbuild # Compiles for macOS on ARM (Apple Silicon) and copies only dynamic libraries to './out/dir' with the library name 'my_lib', using zigbuild"
2425
exit 1
2526
}
2627

@@ -33,17 +34,19 @@ fi
3334
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
3435
PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
3536

36-
# Determine the operating system, architecture, library name, library type, and output directory if not provided
37+
# Determine the operating system, architecture, library name, library type, output directory, and build tool if not provided
3738
OS="${1:-$(uname)}"
3839
ARCH="${2:-$(uname -m)}"
3940
LIB_NAME="${3:-c_eth_kzg}"
4041
LIB_TYPE="${4:-both}"
4142
OUT_DIR="${5:-$PROJECT_ROOT/bindings/c/build}"
43+
BUILD_TOOL="${6:-cargo}"
4244
echo "Detected/Provided OS: $OS"
4345
echo "Detected/Provided architecture: $ARCH"
4446
echo "Library name: $LIB_NAME"
4547
echo "Library type to copy: $LIB_TYPE"
4648
echo "Output directory: $OUT_DIR"
49+
echo "Build tool: $BUILD_TOOL"
4750

4851
STATIC_LIB_NAME=""
4952
DYNAMIC_LIB_NAME=""
@@ -132,14 +135,23 @@ case "$OS" in
132135
;;
133136
esac
134137

138+
# Function to perform the build
139+
do_build() {
140+
local target=$1
141+
if [ "$BUILD_TOOL" == "zigbuild" ]; then
142+
cargo zigbuild --release --target=$target
143+
else
144+
cargo build --release --target=$target
145+
fi
146+
}
147+
135148
# Build for universal mac target if selected
136149
if [[ "$ARCH" == "universal" ]]; then
137-
138150
check_rust_target_installed "x86_64-apple-darwin"
139151
check_rust_target_installed "aarch64-apple-darwin"
140152

141-
cargo build --release --target=x86_64-apple-darwin
142-
cargo build --release --target=aarch64-apple-darwin
153+
do_build "x86_64-apple-darwin"
154+
do_build "aarch64-apple-darwin"
143155

144156
# Create the universal binary
145157
mkdir -p "$OUT_DIR/$TARGET_NAME"
@@ -151,9 +163,8 @@ if [[ "$ARCH" == "universal" ]]; then
151163
"$PROJECT_ROOT/target/x86_64-apple-darwin/release/$DYNAMIC_LIB_NAME" \
152164
"$PROJECT_ROOT/target/aarch64-apple-darwin/release/$DYNAMIC_LIB_NAME"
153165
else
154-
155166
check_rust_target_installed "$TARGET_NAME"
156-
cargo build --release --target=$TARGET_NAME
167+
do_build "$TARGET_NAME"
157168

158169
# Create the output directory if it doesn't exist
159170
mkdir -p "$OUT_DIR/$TARGET_NAME"

0 commit comments

Comments
 (0)