suicmez/compile_and_run.sh
2025-12-19 21:07:07 +05:30

108 lines
4.3 KiB
Bash
Executable file

#!/bin/bash
# Compile Sui source to C, then compile and run the executable
DEBUG_FLAG=""
if [ "$1" = "--debug" ]; then
DEBUG_FLAG="--debug"
shift
fi
OPTIMIZE_FLAG=""
if [ "$1" = "--optimize" ]; then
OPTIMIZE_FLAG="-O3"
shift
fi
if [ $# -eq 0 ]; then
echo "Usage: ./compile_and_run.sh [--debug] [--optimize] <sui_source.sui> [program_args...]"
echo ""
echo "Options:"
echo " --debug Generate debug printf statements in the C output"
echo " --optimize Compile with -O3 optimization"
echo ""
echo "Examples:"
echo " ./compile_and_run.sh tests/structs.sui"
echo " ./compile_and_run.sh --debug tests/structs.sui"
echo " ./compile_and_run.sh --optimize tests/structs.sui"
echo " ./compile_and_run.sh tests/myprogram.sui arg1 arg2"
exit 1
fi
INPUT_SUI="$1"
shift # Remove first argument, keep the rest as program args
if [ ! -f "$INPUT_SUI" ]; then
echo "Error: File not found: $INPUT_SUI"
exit 1
fi
# Determine output names
C_FILE="${INPUT_SUI%.sui}.c"
# Extract just the filename without extension for the temp binary
BINARY_NAME=$(basename "${INPUT_SUI%.sui}")
OUTPUT_BINARY="/tmp/suic_$BINARY_NAME"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "Step 1: Compiling Sui to C..."
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
if [ -n "$DEBUG_FLAG" ]; then
CARGO_CMD="cargo run -- \"$DEBUG_FLAG\" \"$INPUT_SUI\""
else
CARGO_CMD="cargo run -- \"$INPUT_SUI\""
fi
if ! eval "$CARGO_CMD" > /dev/null 2>&1; then
echo ""
echo "✗ Sui compilation failed"
exit 1
fi
if [ ! -f "$C_FILE" ]; then
echo "✗ Error: Generated C file not found: $C_FILE"
exit 1
fi
echo "✓ C file generated: $C_FILE"
echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "Step 2: Compiling C to executable..."
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
# Get raylib compile and link flags
RAYLIB_CFLAGS=$(pkg-config --cflags raylib 2>/dev/null || echo "-I/usr/include")
RAYLIB_LIBS=$(pkg-config --libs raylib 2>/dev/null || echo "-lraylib -lm")
# Get ODE compile and link flags
ODE_CFLAGS=$(pkg-config --cflags ode 2>/dev/null || echo "-I/usr/include")
ODE_LIBS=$(pkg-config --libs ode 2>/dev/null || echo "-lode -lm")
if ! gcc $OPTIMIZE_FLAG -I. -Ilibfishsoup/include "$C_FILE" libsuicmez/libsuicmez.c libsuicmez/suicmez_gc.c libfishsoup/src/*.c $RAYLIB_CFLAGS $ODE_CFLAGS -o "$OUTPUT_BINARY" $RAYLIB_LIBS $ODE_LIBS -lz -lm 2>&1; then
echo ""
echo "✗ C compilation failed"
exit 1
fi
echo "✓ Executable created: $OUTPUT_BINARY"
echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "Step 3: Running program..."
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""
# Run with any additional arguments passed to this script
"$OUTPUT_BINARY" "$@"
EXIT_CODE=$?
echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
if [ $EXIT_CODE -eq 0 ]; then
echo "✓ Program completed successfully"
else
echo "✗ Program exited with code: $EXIT_CODE"
fi
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
exit $EXIT_CODE