#!/bin/sh # if an error occurs exit set -e # set defaults for these if they don't exist if [ -z $ARCH ]; then ARCH='linux' fi if [ -z $MODE ]; then MODE='debug' fi if [ -z $UI ]; then UI='tui' fi case $ARCH in "linux") if [ -z $CC ]; then CC='gcc' fi ;; "web") CC=emcc ;; esac # setup dirs SRC_DIR=./arch/$ARCH/$UI BUILD_DIR=./out/$ARCH/$UI # clean cmd case $1 in "clean") echo "Deleting $BUILD_DIR" rm -rf $BUILD_DIR exit 0 esac # run cmd case $1 in "run") case $ARCH in "linux") $BUILD_DIR/undar $2 ;; "web") emrun $BUILD_DIR/undar.html ;; esac exit 0 esac # create the build dir if it doesnt exist if [ -d $BUILD_DIR ]; then echo "Building to $BUILD_DIR" else echo "$BUILD_DIR not found, creating" mkdir -p $BUILD_DIR fi # setup the build flags based on the build mode case $MODE in "debug") BUILD_FLAGS="-g -Wall -Wextra -Werror -pedantic" ;; "release") BUILD_FLAGS="-O2 -Wall -Wextra -Werror -pedantic" ;; esac # set up the flags based on the UI case $UI in "tui") case $CC in "gcc") LINK_FLAGS="" ;; "emcc") LINK_FLAGS="-s ASYNCIFY --shell-file $SRC_DIR/../shell_minimal.html" ;; esac ;; "gui") case $CC in "gcc") LINK_FLAGS="$(sdl2-config --libs --cflags)" ;; "emcc") LINK_FLAGS="-s USE_SDL=2 -s ASYNCIFY --shell-file $SRC_DIR/../shell_minimal.html" ;; esac ;; esac # build the core VM VM_BUILD_FLAGS="$BUILD_FLAGS -std=c89 -ffreestanding -nostdlib -fno-builtin" ${CC} -c vm/libc.c -o $BUILD_DIR/libc.o $VM_BUILD_FLAGS ${CC} -c vm/vm.c -o $BUILD_DIR/vm.o $VM_BUILD_FLAGS # Set up the final build command case $ARCH in "linux") BUILD_CMD="$CC -o $BUILD_DIR/undar $SRC_DIR/main.c $LINK_FLAGS $BUILD_DIR/libc.o $BUILD_DIR/vm.o $BUILD_FLAGS $LINK_FLAGS" ;; "web") BUILD_CMD="$CC $SRC_DIR/main.c $BUILD_DIR/libc.o $BUILD_DIR/vm.o -o $BUILD_DIR/undar.html $BUILD_FLAGS $LINK_FLAGS" ;; esac echo "$BUILD_CMD" ${BUILD_CMD} echo "Finished building to $BUILD_DIR/undar"