csharp-flutter/build.sh

270 lines
6.5 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)"
}
# 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/android_fake_clang.sh
# 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
cp $generated_project_dir/bin/Release/net8.0/linux-bionic-$arch/native/$generated_project_name.so $output_dir/${abis[$arch]}/lib$generated_project_name.so
done
}
# 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
}
## ARGUMENTS
# Check for arguments
if [ $# -eq 0 ]; then
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
;;
*)
help
exit 1
;;
esac
shift
done
# Check for required arguments
if [ -z "$input_project_dir" ]; then
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 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 CAN_BUILD_IOS to true
if [[ "$OSTYPE" == "darwin"* ]]; then
command_exists xcodebuild || {
log "Xcode is not installed or not in PATH. Disabling iOS build."
CAN_BUILD_IOS=false
}
log "Xcode detected. Enabling iOS build."
CAN_BUILD_IOS=true
else
log "Not running on macOS. Disabling iOS build."
CAN_BUILD_IOS=false
fi
# Check for ANDROID_NDK_HOME
if [ -z "$ANDROID_NDK_HOME" ]; then
log "ANDROID_NDK_HOME is not set. Disabling Android build."
CAN_BUILD_ANDROID=false
else
log "ANDROID_NDK_HOME is set to $ANDROID_NDK_HOME. Enabling Android build."
CAN_BUILD_ANDROID=true
fi
# Check if CAN_BUILD_IOS or CAN_BUILD_ANDROID is true
if [ "$CAN_BUILD_IOS" = false ] && [ "$CAN_BUILD_ANDROID" = false ]; then
log "Neither iOS nor Android build is enabled. Aborting."
exit 1
fi
## CONFIG
# 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: \n$generated_project_dir, \n$output_dir, \n$input_project_dir/obj and \n$input_project_dir/bin. \nContinue?"
rm -r "$generated_project_dir"
rm -r "$output_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 [ "$CAN_BUILD_ANDROID" = true ]; then
build_android
fi
# Build iOS libraries
if [ "$CAN_BUILD_IOS" = true ]; then
build_ios
fi
echo "Build completed. See $log_file for details."