317 lines
7.7 KiB
Bash
Executable File
317 lines
7.7 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
|
|
## INIT
|
|
|
|
# Get script directory
|
|
script_dir="$(cd "$(dirname "$0")" && pwd)"
|
|
|
|
# Change working directory to script directory
|
|
cd $script_dir
|
|
|
|
# Set up log file
|
|
log_file="$script_dir/build.log"
|
|
|
|
rm -f $log_file
|
|
|
|
## FUNCTIONS
|
|
|
|
# Function to print usage instructions
|
|
help() {
|
|
echo "Usage: $0 [options]"
|
|
echo "Options:"
|
|
echo " -h, --help Print this help and exit"
|
|
echo " -i Input project name (required)"
|
|
echo " -g Generated project name (default: <input-project-name>.Generated)"
|
|
echo " -o Output folder name (default: libs)"
|
|
echo " --ndk Android NDK path (default: ANDROID_NDK_HOME environment variable)"
|
|
echo " --no-android Disable Android build (default: false)"
|
|
echo " --no-ios Disable iOS build (default: false)"
|
|
}
|
|
|
|
# Function to log a message to the console and log file
|
|
log() {
|
|
echo "$1"
|
|
echo "$1" >>$log_file
|
|
}
|
|
|
|
# Function to run a command and log the output
|
|
run() {
|
|
eval $1 2>&1 | tee -a $log_file
|
|
}
|
|
|
|
# Function to check if a command exists
|
|
command_exists() {
|
|
command -v "$1" >/dev/null 2>&1
|
|
}
|
|
|
|
# Function to replace INPUT_PROJECT_NAME and GENERATED_PROJECT_NAME
|
|
# by the actual project names in the passed file
|
|
replace_project_names() {
|
|
sed -i '' "s/INPUT_PROJECT_NAME/$input_project_name/g" $1
|
|
sed -i '' "s/GENERATED_PROJECT_NAME/$generated_project_name/g" $1
|
|
}
|
|
|
|
# Function to ask for confirmation before continuing
|
|
# $1: message
|
|
ask_confirmation() {
|
|
read -p "$1 (y/n) " -n 1 -r
|
|
echo
|
|
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
# Function to build the Android libraries
|
|
build_android() {
|
|
cd $script_dir
|
|
|
|
# Copy template beyondnetgen.android.config.json
|
|
cp template.beyondnetgen.android.config.json beyondnetgen.android.config.json
|
|
replace_project_names beyondnetgen.android.config.json
|
|
|
|
# Generate project
|
|
log "Generating Android project"
|
|
run "beyondnetgen beyondnetgen.android.config.json"
|
|
|
|
# Copy android_fake_clang.* to generated project
|
|
cp android_fake_clang.* "$generated_project_dir/"
|
|
|
|
# Copy template csproj
|
|
cp template.csproj "$generated_project_dir/$generated_project_name.csproj"
|
|
replace_project_names "$generated_project_dir/$generated_project_name.csproj"
|
|
|
|
cd $generated_project_dir
|
|
|
|
declare -A abis
|
|
abis["arm64"]="arm64-v8a"
|
|
abis["x64"]="x86_64"
|
|
|
|
# Build for each architecture
|
|
for arch in "${!abis[@]}"; do
|
|
log "Building Android library for $arch"
|
|
|
|
run "dotnet publish -r linux-bionic-$arch -v normal -c Release -p:DisableUnsupportedError=true -p:PublishAotUsingRuntimePack=true -p:RemoveSections=true"
|
|
|
|
# Copy the generated library to the output directory
|
|
mkdir -p $output_dir/${abis[$arch]}
|
|
cp $generated_project_dir/bin/Release/net8.0/linux-bionic-$arch/native/$generated_project_name.so $output_dir/${abis[$arch]}/lib$input_project_name.so
|
|
done
|
|
|
|
# Copy the generated headers to the output directory
|
|
log "Copying header to output directory"
|
|
cp "$generated_project_dir/Generated.h" "$output_dir/$input_project_name.h"
|
|
}
|
|
|
|
# Function to build the iOS libraries
|
|
build_ios() {
|
|
cd $script_dir
|
|
|
|
# Copy template beyondnetgen.ios.config.json
|
|
cp template.beyondnetgen.ios.config.json beyondnetgen.ios.config.json
|
|
replace_project_names beyondnetgen.ios.config.json
|
|
|
|
# Generate project
|
|
log "Generating iOS project"
|
|
run "beyondnetgen beyondnetgen.ios.config.json"
|
|
|
|
# Copy $input_project_dir/bin/Release/net8.0/*.xcframework to output directory
|
|
log "Copying iOS libraries to output directory"
|
|
cp $input_project_dir/bin/Release/net8.0/*.xcframework $output_dir
|
|
|
|
# Copy the generated headers to the output directory
|
|
log "Copying header to output directory"
|
|
cp "$generated_project_dir/Generated.h" "$output_dir/$input_project_name.h"
|
|
}
|
|
|
|
## ARGUMENTS
|
|
|
|
# Set default values
|
|
WANT_TO_BUILD_ANDROID=true
|
|
WANT_TO_BUILD_IOS=true
|
|
|
|
# Check for arguments
|
|
if [ $# -eq 0 ]; then
|
|
log "No arguments supplied"
|
|
help
|
|
exit 1
|
|
fi
|
|
|
|
# Parse arguments
|
|
while [ "$1" != "" ]; do
|
|
case $1 in
|
|
-h | --help)
|
|
help
|
|
exit 0
|
|
;;
|
|
-i)
|
|
shift
|
|
input_project_name=$1
|
|
;;
|
|
-g)
|
|
shift
|
|
generated_project_name=$1
|
|
;;
|
|
-o)
|
|
shift
|
|
output=$1
|
|
;;
|
|
--ndk)
|
|
shift
|
|
export ANDROID_NDK_HOME=$1
|
|
;;
|
|
--no-android)
|
|
WANT_TO_BUILD_ANDROID=false
|
|
;;
|
|
--no-ios)
|
|
WANT_TO_BUILD_IOS=false
|
|
;;
|
|
*)
|
|
log "Unknown argument: $1"
|
|
help
|
|
exit 1
|
|
;;
|
|
esac
|
|
shift
|
|
done
|
|
|
|
# Check for required arguments
|
|
if [ -z "$input_project_name" ]; then
|
|
log "Input project name is required"
|
|
help
|
|
exit 1
|
|
fi
|
|
|
|
# Set default values
|
|
if [ -z "$generated_project_name" ]; then
|
|
generated_project_name="$input_project_name.Generated"
|
|
fi
|
|
|
|
if [ -z "$output" ]; then
|
|
output="libs"
|
|
fi
|
|
|
|
input_project_dir="$script_dir/$input_project_name"
|
|
generated_project_dir="$script_dir/$generated_project_name"
|
|
output_dir="$script_dir/$output"
|
|
|
|
## PREREQUISITES CHECK
|
|
|
|
# Check for bash >= 4.0
|
|
if [ "${BASH_VERSINFO:-0}" -lt 4 ]; then
|
|
log "bash >= 4.0 is required. Aborting."
|
|
exit 1
|
|
fi
|
|
|
|
# Check for dotnet
|
|
command_exists dotnet || {
|
|
log "dotnet is required but it's not installed or not in PATH. Aborting."
|
|
exit 1
|
|
}
|
|
|
|
# Check if dotnet is in version 8.0 or higher
|
|
dotnet_version=$(dotnet --version)
|
|
|
|
if [[ $dotnet_version =~ ^8\. ]]; then
|
|
log "dotnet version $dotnet_version is supported"
|
|
else
|
|
log "dotnet version $dotnet_version is not supported. Aborting."
|
|
exit 1
|
|
fi
|
|
|
|
# Check for beyondnetgen
|
|
command_exists beyondnetgen || {
|
|
log "beyondnetgen is required but it's not installed or not in PATH. Aborting."
|
|
exit 1
|
|
}
|
|
|
|
# Check if host is macos, if so, check for xcode, and set BUILD_IOS to true
|
|
ostype=$(uname -s)
|
|
log "Host OS: $ostype"
|
|
if [ "$ostype" == "Darwin" ]; then
|
|
if [ "$WANT_TO_BUILD_IOS" = false ]; then
|
|
log "Disabling iOS build."
|
|
BUILD_IOS=false
|
|
else
|
|
command_exists xcodebuild || {
|
|
log "Xcode is not installed or not in PATH. Disabling iOS build."
|
|
BUILD_IOS=false
|
|
}
|
|
|
|
log "Xcode detected. Enabling iOS build."
|
|
BUILD_IOS=true
|
|
fi
|
|
else
|
|
BUILD_IOS=false
|
|
fi
|
|
|
|
# Check for ANDROID_NDK_HOME
|
|
if [ "$ANDROID_NDK_HOME" ] && [ "$WANT_TO_BUILD_ANDROID" = true ]; then
|
|
log "ANDROID_NDK_HOME is set to $ANDROID_NDK_HOME. Enabling Android build."
|
|
BUILD_ANDROID=true
|
|
else
|
|
BUILD_ANDROID=false
|
|
if [ -z "$ANDROID_NDK_HOME" ]; then
|
|
log "ANDROID_NDK_HOME is not set. Disabling Android build."
|
|
else
|
|
log "Disabling Android build."
|
|
fi
|
|
fi
|
|
|
|
# Check if BUILD_IOS or BUILD_ANDROID is true
|
|
if [ "$BUILD_IOS" = false ] && [ "$BUILD_ANDROID" = false ]; then
|
|
log "Neither iOS nor Android build is enabled. Aborting."
|
|
exit 1
|
|
fi
|
|
|
|
## CONFIG
|
|
|
|
cd $script_dir
|
|
|
|
# Copy template beyondnetgen.*.config.json
|
|
cp template.beyondnetgen.android.config.json beyondnetgen.android.config.json
|
|
replace_project_names beyondnetgen.android.config.json
|
|
|
|
cp template.beyondnetgen.ios.config.json beyondnetgen.ios.config.json
|
|
replace_project_names beyondnetgen.ios.config.json
|
|
|
|
# Check if output_dir exists, if not, create it
|
|
if [ ! -d "$output_dir" ]; then
|
|
log "Creating output directory $output_dir"
|
|
mkdir -p $output_dir
|
|
fi
|
|
|
|
## BUILD
|
|
|
|
# Clean previous build
|
|
log "Cleaning previous build..."
|
|
|
|
# Ask for confirmation
|
|
ask_confirmation "Will delete: $generated_project_dir, $output_dir, $input_project_dir/obj $input_project_dir/bin. Continue?"
|
|
|
|
rm -r $generated_project_dir
|
|
rm -r "$input_project_dir/obj"
|
|
rm -r "$input_project_dir/bin"
|
|
|
|
# Build input project
|
|
log "Building input project"
|
|
cd $input_project_dir
|
|
run "dotnet publish -v normal -c Release"
|
|
|
|
# Build Android libraries
|
|
if [ "$BUILD_ANDROID" = true ]; then
|
|
build_android
|
|
fi
|
|
|
|
# Build iOS libraries
|
|
if [ "$BUILD_IOS" = true ]; then
|
|
build_ios
|
|
fi
|
|
|
|
# Check last command exit code
|
|
if [ $? -ne 0 ]; then
|
|
log "Build failed. See $log_file for details."
|
|
exit 1
|
|
else
|
|
log "Finished building. See $log_file for details."
|
|
fi
|