pxl8/pxl8.sh

503 lines
13 KiB
Bash
Raw Normal View History

2025-08-13 15:04:49 -05:00
#!/bin/bash
set -e
2025-11-28 14:41:35 -06:00
CC="${CC:-clang}"
2025-09-30 21:20:30 -05:00
if command -v ccache >/dev/null 2>&1; then
CC="ccache $CC"
fi
2025-08-13 15:04:49 -05:00
CFLAGS="-std=c23 -Wall -Wextra"
LIBS="-lm"
MODE="debug"
BUILDDIR=".build"
BINDIR="bin"
if command -v mold >/dev/null 2>&1; then
LINKER_FLAGS="-fuse-ld=mold"
else
LINKER_FLAGS=""
fi
case "$(uname)" in
2025-09-30 21:28:22 -05:00
Linux)
2025-08-13 15:04:49 -05:00
LINKER_FLAGS="$LINKER_FLAGS -rdynamic"
;;
2025-09-30 21:28:22 -05:00
Darwin)
export MACOSX_DEPLOYMENT_TARGET="$(sw_vers -productVersion | cut -d '.' -f 1)"
2025-08-13 15:04:49 -05:00
;;
MINGW*|MSYS*|CYGWIN*)
LINKER_FLAGS="$LINKER_FLAGS -Wl,--export-all-symbols"
;;
*)
LINKER_FLAGS="$LINKER_FLAGS -rdynamic"
;;
esac
BOLD='\033[1m'
2025-11-11 22:42:10 -06:00
GREEN='\033[38;2;184;187;38m'
2025-08-13 15:04:49 -05:00
NC='\033[0m'
2025-11-11 22:42:10 -06:00
RED='\033[38;2;251;73;52m'
2025-08-13 15:04:49 -05:00
if [[ "$(uname)" == "Linux" ]]; then
CFLAGS="$CFLAGS -D_GNU_SOURCE"
fi
2025-11-05 13:33:47 -06:00
build_luajit() {
if [[ ! -f "lib/luajit/src/libluajit.a" ]]; then
print_info "Building LuaJIT"
cd lib/luajit
make clean >/dev/null 2>&1 || true
make -j$(nproc 2>/dev/null || echo 4) > /dev/null 2>&1
cd - > /dev/null
print_info "Built LuaJIT"
fi
}
build_sdl() {
if [[ ! -f "lib/SDL/build/libSDL3.so" ]] && [[ ! -f "lib/SDL/build/libSDL3.a" ]] && [[ ! -f "lib/SDL/build/libSDL3.dylib" ]]; then
print_info "Building SDL3"
mkdir -p lib/SDL/build
cd lib/SDL/build
cmake .. -DCMAKE_BUILD_TYPE=Release > /dev/null 2>&1
cmake --build . --parallel $(nproc 2>/dev/null || echo 4) > /dev/null 2>&1
cd - > /dev/null
print_info "Built SDL3"
fi
}
2025-09-28 13:10:29 -05:00
compile_source_file() {
local src_file="$1"
local obj_file="$2"
local compile_flags="$3"
print_info "Compiling: $src_file"
if ! $CC -c $compile_flags "$src_file" -o "$obj_file"; then
print_error "Compilation failed for $src_file"
exit 1
fi
2025-08-13 15:04:49 -05:00
}
2025-09-28 13:10:29 -05:00
make_lib_dirs() {
mkdir -p lib/linenoise lib/fennel lib/miniz
2025-09-28 13:10:29 -05:00
}
print_error() {
2025-11-05 13:33:47 -06:00
echo -e "${RED}${BOLD}[$(timestamp) ERROR]${NC} $1" >&2
2025-09-28 13:10:29 -05:00
}
print_info() {
2025-11-05 13:33:47 -06:00
echo -e "${GREEN}${BOLD}[$(timestamp) INFO]${NC} $1"
2025-09-28 13:10:29 -05:00
}
2025-08-13 15:04:49 -05:00
print_usage() {
echo -e "${BOLD}pxl8${NC} - framework build tool"
echo
echo -e "${BOLD}USAGE:${NC}"
echo " ./pxl8.sh <command> [options]"
echo
echo -e "${BOLD}COMMANDS:${NC}"
2025-11-05 13:39:26 -06:00
echo " ase Aseprite tools (use 'ase help' for subcommands)"
2025-08-13 15:04:49 -05:00
echo " build Build pxl8"
echo " clean Remove build artifacts"
2025-11-01 12:39:59 -05:00
echo " help Show this help message"
2025-11-27 21:37:23 -06:00
echo " install Install pxl8 to ~/.local/bin"
2025-09-28 13:10:29 -05:00
echo " run Build and run pxl8 (optional: cart.pxc or folder)"
2025-08-13 15:04:49 -05:00
echo " update Download/update all dependencies"
2025-09-28 13:10:29 -05:00
echo " vendor Fetch source for dependencies (ex. SDL3)"
echo
2025-08-13 15:04:49 -05:00
echo -e "${BOLD}OPTIONS:${NC}"
2025-09-28 13:10:29 -05:00
echo " --all Clean both build artifacts and dependencies"
echo " --deps Clean only dependencies"
echo " --release Build/run/clean in release mode (default: debug)"
2025-08-13 15:04:49 -05:00
}
setup_sdl3() {
if [[ -d "lib/SDL/build" ]]; then
SDL3_CFLAGS="-Ilib/SDL/include"
SDL3_LIBS="-Llib/SDL/build -lSDL3"
SDL3_RPATH="-Wl,-rpath,$(pwd)/lib/SDL/build"
print_info "Using vendored SDL3"
else
SDL3_CFLAGS=$(pkg-config --cflags sdl3 2>/dev/null || echo "")
SDL3_LIBS=$(pkg-config --libs sdl3 2>/dev/null || echo "")
SDL3_RPATH=""
2025-09-28 13:10:29 -05:00
2025-08-13 15:04:49 -05:00
if [[ -z "$SDL3_LIBS" ]]; then
case "$(uname)" in
Darwin)
2025-09-28 13:10:29 -05:00
if [[ -f "/usr/local/include/SDL3/SDL.h" ]]; then
SDL3_CFLAGS="-I/usr/local/include/SDL3"
SDL3_LIBS="-L/usr/local/lib -lSDL3"
fi
2025-08-13 15:04:49 -05:00
;;
Linux)
2025-09-28 13:10:29 -05:00
if [[ -f "/usr/include/SDL3/SDL.h" ]]; then
SDL3_CFLAGS="-I/usr/include/SDL3"
SDL3_LIBS="-lSDL3"
fi
2025-08-13 15:04:49 -05:00
;;
MINGW*|MSYS*|CYGWIN*)
2025-09-28 13:10:29 -05:00
if [[ -f "/mingw64/include/SDL3/SDL.h" ]]; then
SDL3_CFLAGS="-I/mingw64/include/SDL3"
SDL3_LIBS="-lSDL3"
fi
2025-08-13 15:04:49 -05:00
;;
esac
fi
2025-09-28 13:10:29 -05:00
if [[ -z "$SDL3_LIBS" ]]; then
print_info "System SDL3 not found, vendoring SDL3..."
update_sdl
build_sdl
SDL3_CFLAGS="-Ilib/SDL/include"
SDL3_LIBS="-Llib/SDL/build -lSDL3"
SDL3_RPATH="-Wl,-rpath,$(pwd)/lib/SDL/build"
print_info "Using vendored SDL3"
else
print_info "Using system SDL3"
fi
2025-08-13 15:04:49 -05:00
fi
CFLAGS="$CFLAGS $SDL3_CFLAGS"
LIBS="$LIBS $SDL3_LIBS $SDL3_RPATH"
}
2025-11-05 13:33:47 -06:00
timestamp() {
date +"%H:%M:%S"
}
2025-08-13 15:04:49 -05:00
update_fennel() {
print_info "Fetching Fennel"
2025-09-28 13:10:29 -05:00
2025-08-13 15:04:49 -05:00
local version="1.5.3"
2025-09-28 13:10:29 -05:00
2025-11-05 13:33:47 -06:00
if curl -sL --max-time 5 -o lib/fennel/fennel.lua "https://fennel-lang.org/downloads/fennel-${version}.lua" 2>/dev/null; then
2025-08-13 15:04:49 -05:00
if [[ -f "lib/fennel/fennel.lua" ]] && [[ -s "lib/fennel/fennel.lua" ]]; then
2025-11-05 13:33:47 -06:00
print_info "Updated Fennel (${version})"
2025-08-13 15:04:49 -05:00
else
print_error "Downloaded file is empty or missing"
return 1
fi
else
print_error "Failed to download Fennel"
return 1
fi
}
2025-09-28 13:10:29 -05:00
update_linenoise() {
print_info "Fetching linenoise"
2025-11-05 13:33:47 -06:00
if curl -sL --max-time 5 -o lib/linenoise/linenoise.c https://raw.githubusercontent.com/antirez/linenoise/master/linenoise.c 2>/dev/null && \
curl -sL --max-time 5 -o lib/linenoise/linenoise.h https://raw.githubusercontent.com/antirez/linenoise/master/linenoise.h 2>/dev/null; then
print_info "Updated linenoise"
2025-09-28 13:10:29 -05:00
else
print_error "Failed to download linenoise"
return 1
fi
}
2025-08-13 15:04:49 -05:00
update_luajit() {
print_info "Fetching LuaJIT"
2025-11-05 13:33:47 -06:00
2025-08-13 15:04:49 -05:00
local version="2.1"
local branch="v${version}"
2025-11-05 13:33:47 -06:00
2025-08-13 15:04:49 -05:00
if [[ -d "lib/luajit/.git" ]]; then
cd lib/luajit && git pull --quiet origin ${branch}
cd - > /dev/null
else
rm -rf lib/luajit
git clone --quiet --branch ${branch} https://github.com/LuaJIT/LuaJIT.git lib/luajit
fi
2025-11-05 13:33:47 -06:00
print_info "Updated LuaJIT (${version})"
2025-08-13 15:04:49 -05:00
}
update_miniz() {
print_info "Fetching miniz"
2025-11-05 13:33:47 -06:00
2025-08-13 15:04:49 -05:00
local version="3.0.2"
2025-11-05 13:33:47 -06:00
2025-09-28 13:10:29 -05:00
if curl -sL --max-time 5 -o /tmp/miniz.zip "https://github.com/richgel999/miniz/releases/download/${version}/miniz-${version}.zip" 2>/dev/null; then
2025-08-13 15:04:49 -05:00
unzip -qjo /tmp/miniz.zip miniz.c miniz.h -d lib/miniz/ 2>/dev/null
rm -f /tmp/miniz.zip
2025-11-05 13:33:47 -06:00
2025-08-13 15:04:49 -05:00
if [[ -f "lib/miniz/miniz.c" ]] && [[ -f "lib/miniz/miniz.h" ]]; then
2025-11-05 13:33:47 -06:00
print_info "Updated miniz (${version})"
2025-08-13 15:04:49 -05:00
else
print_error "Failed to extract miniz files"
return 1
fi
else
print_error "Failed to download miniz"
return 1
fi
}
2025-09-28 13:10:29 -05:00
update_sdl() {
2025-08-13 15:04:49 -05:00
print_info "Fetching SDL3"
2025-11-05 13:33:47 -06:00
2025-08-13 15:04:49 -05:00
if [[ -d "lib/SDL/.git" ]]; then
cd lib/SDL && git pull --quiet origin main
cd - > /dev/null
else
rm -rf lib/SDL
git clone --quiet https://github.com/libsdl-org/SDL.git lib/SDL
fi
2025-11-05 13:33:47 -06:00
print_info "Updated SDL3"
2025-08-13 15:04:49 -05:00
}
2025-11-05 13:33:47 -06:00
COMMAND="$1"
shift || true
for arg in "$@"; do
case $arg in
--release)
MODE="release"
;;
esac
done
if [ "$MODE" = "release" ]; then
CFLAGS="$CFLAGS -O3 -ffast-math -funroll-loops -fno-unwind-tables -fno-asynchronous-unwind-tables"
2025-11-05 13:33:47 -06:00
BUILDDIR="$BUILDDIR/release"
BINDIR="$BINDIR/release"
else
CFLAGS="$CFLAGS -g -O1 -DDEBUG"
2025-11-05 13:33:47 -06:00
BUILDDIR="$BUILDDIR/debug"
BINDIR="$BINDIR/debug"
fi
2025-11-15 14:23:53 -06:00
DEP_CFLAGS="-O3 -ffast-math -funroll-loops"
2025-08-13 15:04:49 -05:00
case "$COMMAND" in
build)
mkdir -p "$BUILDDIR"
mkdir -p "$BINDIR"
if [[ ! -d "lib/luajit" ]] || \
2025-08-13 15:04:49 -05:00
[[ ! -f "lib/linenoise/linenoise.c" ]] || \
[[ ! -f "lib/miniz/miniz.c" ]] || \
[[ ! -f "lib/fennel/fennel.lua" ]]; then
print_info "Missing dependencies, fetching..."
2025-09-28 13:10:29 -05:00
make_lib_dirs
2025-08-13 15:04:49 -05:00
update_fennel
2025-09-28 13:10:29 -05:00
update_linenoise
2025-08-13 15:04:49 -05:00
update_luajit
update_miniz
fi
if [[ ! -f "bin/.gitignore" ]]; then
echo "*" > "bin/.gitignore"
fi
if [[ ! -f ".build/.gitignore" ]]; then
echo "*" > ".build/.gitignore"
fi
if [[ ! -f "lib/.gitignore" ]]; then
echo "*" > "lib/.gitignore"
fi
if [[ -d "lib/luajit" ]]; then
build_luajit
fi
if [[ -d "lib/SDL" ]]; then
build_sdl
fi
setup_sdl3
print_info "Building pxl8 ($MODE mode)"
2025-09-30 21:20:30 -05:00
if [[ "$CC" == ccache* ]]; then
print_info "Compiler cache: ccache enabled"
fi
INCLUDES="-Isrc -Ilib -Ilib/luajit/src -Ilib/linenoise -Ilib/miniz"
2025-08-13 15:04:49 -05:00
COMPILE_FLAGS="$CFLAGS $INCLUDES"
2025-11-15 14:23:53 -06:00
DEP_COMPILE_FLAGS="$DEP_CFLAGS $INCLUDES"
2025-08-13 15:04:49 -05:00
EXECUTABLE="$BINDIR/pxl8"
2025-09-24 00:39:44 -05:00
LIB_SOURCE_FILES="lib/linenoise/linenoise.c lib/miniz/miniz.c"
2025-09-24 00:39:44 -05:00
2025-09-28 13:10:29 -05:00
PXL8_SOURCE_FILES="
2025-09-24 00:39:44 -05:00
src/pxl8.c
src/pxl8_anim.c
2025-09-24 00:39:44 -05:00
src/pxl8_ase.c
2025-10-17 17:54:33 -05:00
src/pxl8_atlas.c
2025-09-24 00:39:44 -05:00
src/pxl8_blit.c
2025-10-07 10:32:48 -05:00
src/pxl8_bsp.c
2025-09-27 11:03:36 -05:00
src/pxl8_cart.c
2025-09-24 00:39:44 -05:00
src/pxl8_font.c
2025-11-28 14:41:35 -06:00
src/pxl8_gen.c
2025-09-24 00:39:44 -05:00
src/pxl8_gfx.c
2025-11-28 14:41:35 -06:00
src/pxl8_gui.c
2025-09-24 00:39:44 -05:00
src/pxl8_io.c
2025-10-04 04:13:48 -05:00
src/pxl8_math.c
2025-11-28 14:41:35 -06:00
src/pxl8_rec.c
2025-10-04 04:13:48 -05:00
src/pxl8_script.c
2025-10-17 17:54:33 -05:00
src/pxl8_sdl3.c
2025-09-24 00:39:44 -05:00
src/pxl8_tilemap.c
src/pxl8_tilesheet.c
src/pxl8_transition.c
2025-09-24 00:39:44 -05:00
src/pxl8_vfx.c
2025-10-07 10:32:48 -05:00
src/pxl8_world.c
2025-09-24 00:39:44 -05:00
"
2025-08-13 15:04:49 -05:00
LUAJIT_LIB="lib/luajit/src/libluajit.a"
OBJECT_DIR="$BUILDDIR/obj"
mkdir -p "$OBJECT_DIR"
OBJECTS=""
NEED_LINK=false
SOURCES_COMPILED=""
for src_file in $LIB_SOURCE_FILES; do
obj_name=$(basename "$src_file" .c).o
obj_file="$OBJECT_DIR/$obj_name"
OBJECTS="$OBJECTS $obj_file"
2025-09-28 13:10:29 -05:00
2025-08-13 15:04:49 -05:00
if [[ "$src_file" -nt "$obj_file" ]]; then
NEED_LINK=true
2025-11-15 14:23:53 -06:00
compile_source_file "$src_file" "$obj_file" "$DEP_COMPILE_FLAGS"
2025-08-13 15:04:49 -05:00
SOURCES_COMPILED="yes"
fi
done
2025-09-28 13:10:29 -05:00
for src_file in $PXL8_SOURCE_FILES; do
2025-08-13 15:04:49 -05:00
obj_name=$(basename "$src_file" .c).o
obj_file="$OBJECT_DIR/$obj_name"
OBJECTS="$OBJECTS $obj_file"
2025-09-28 13:10:29 -05:00
2025-08-13 15:04:49 -05:00
if [[ "$src_file" -nt "$obj_file" ]] || \
[[ "src/pxl8_types.h" -nt "$obj_file" ]] || \
[[ "src/pxl8_macros.h" -nt "$obj_file" ]]; then
NEED_LINK=true
2025-09-28 13:10:29 -05:00
compile_source_file "$src_file" "$obj_file" "$COMPILE_FLAGS"
2025-08-13 15:04:49 -05:00
SOURCES_COMPILED="yes"
fi
done
if [[ "$LUAJIT_LIB" -nt "$EXECUTABLE" ]] || [[ "$NEED_LINK" == true ]]; then
print_info "Linking executable"
if ! $CC $LINKER_FLAGS $OBJECTS $LUAJIT_LIB $LIBS -o "$EXECUTABLE"; then
print_error "Linking failed"
exit 1
fi
elif [[ -z "$SOURCES_COMPILED" ]]; then
print_info "All files are up to date, skipping build"
fi
2025-11-05 13:33:47 -06:00
print_info "Build complete"
2025-08-13 15:04:49 -05:00
print_info "Binary: $EXECUTABLE"
;;
run)
"$0" build "$@" || exit 1
2025-09-28 13:10:29 -05:00
CART=""
2025-08-13 15:04:49 -05:00
for arg in "$@"; do
if [[ "$arg" != "--release" ]]; then
2025-09-28 13:10:29 -05:00
CART="$arg"
2025-08-13 15:04:49 -05:00
break
fi
done
2025-09-28 13:10:29 -05:00
if [[ -z "$CART" ]]; then
"$BINDIR/pxl8"
else
"$BINDIR/pxl8" "$CART"
2025-08-13 15:04:49 -05:00
fi
;;
clean)
2025-09-28 13:10:29 -05:00
CLEAN_ALL=false
CLEAN_DEPS=false
CLEAN_RELEASE=false
for arg in "$@"; do
case "$arg" in
--all) CLEAN_ALL=true ;;
--deps) CLEAN_DEPS=true ;;
--release) CLEAN_RELEASE=true ;;
esac
done
if [[ "$CLEAN_RELEASE" == true ]]; then
BUILD_PATH=".build/release"
BIN_PATH="bin/release"
MODE="release"
else
BUILD_PATH=".build/debug"
BIN_PATH="bin/debug"
MODE="debug"
fi
if [[ "$CLEAN_ALL" == true ]]; then
print_info "Removing build artifacts and dependencies"
rm -rf "$BUILD_PATH" "$BIN_PATH" lib
2025-11-05 13:33:47 -06:00
print_info "Cleaned all"
2025-09-28 13:10:29 -05:00
elif [[ "$CLEAN_DEPS" == true ]]; then
print_info "Removing dependencies"
rm -rf lib
2025-11-05 13:33:47 -06:00
print_info "Cleaned dependencies"
2025-08-13 15:04:49 -05:00
else
print_info "Removing build artifacts"
2025-09-28 13:10:29 -05:00
rm -rf "$BUILD_PATH" "$BIN_PATH"
2025-11-05 13:33:47 -06:00
print_info "Cleaned"
2025-08-13 15:04:49 -05:00
fi
;;
update)
2025-11-05 13:33:47 -06:00
make_lib_dirs
2025-08-13 15:04:49 -05:00
update_fennel
2025-11-05 13:33:47 -06:00
update_linenoise
2025-08-13 15:04:49 -05:00
update_luajit
update_miniz
2025-11-05 13:33:47 -06:00
print_info "All dependencies updated"
2025-08-13 15:04:49 -05:00
;;
vendor)
2025-09-28 13:10:29 -05:00
update_sdl
2025-08-13 15:04:49 -05:00
;;
2025-11-27 21:37:23 -06:00
install)
"$0" build --release || exit 1
INSTALL_DIR="${HOME}/.local/bin"
mkdir -p "$INSTALL_DIR"
cp "bin/release/pxl8" "$INSTALL_DIR/pxl8"
chmod +x "$INSTALL_DIR/pxl8"
print_info "Installed pxl8 to $INSTALL_DIR/pxl8"
if [[ ":$PATH:" != *":$INSTALL_DIR:"* ]]; then
print_info "Note: Add $INSTALL_DIR to your PATH if not already"
fi
;;
2025-11-05 13:39:26 -06:00
ase)
2025-11-05 13:33:47 -06:00
bash tools/aseprite/pxl8-ase.sh "$@"
2025-11-01 12:39:59 -05:00
;;
2025-08-13 15:04:49 -05:00
help|--help|-h|"")
print_usage
;;
*)
print_error "Unknown command: $COMMAND"
print_usage
exit 1
;;
esac