Skip to content

Instantly share code, notes, and snippets.

@kybr
Created February 1, 2018 02:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kybr/97c359f88b00cf2c3d5feeeeeecebbf2 to your computer and use it in GitHub Desktop.
Save kybr/97c359f88b00cf2c3d5feeeeeecebbf2 to your computer and use it in GitHub Desktop.
#!/bin/bash
if [ $# == 0 ]; then
echo "pass file to run"
echo "ex) ./run.sh src/main.cpp"
exit 1
fi
INITIALDIR=${PWD} # gives absolute path
# echo "Script executed from: ${INITIALDIR}"
# BASH_SOURCE has the script's path
# could be absolute, could be relative
SCRIPT_PATH=$(dirname ${BASH_SOURCE[0]})
FIRSTCHAR=${SCRIPT_PATH:0:1}
if [ ${FIRSTCHAR} == "/" ]; then
# it's asolute path
AL_LIB_PATH=${SCRIPT_PATH}
else
# SCRIPT_PATH was relative
AL_LIB_PATH=${INITIALDIR}/${SCRIPT_PATH}
fi
# resolve flags ###############################################################
# check if we want debug build
BUILD_TYPE=Release
DO_CLEAN=0
while getopts ":dnc" opt; do
case $opt in
d)
BUILD_TYPE=Debug
POSTFIX=_debug
shift # consume option
;;
n)
EXIT_AFTER_BUILD=1
shift
;;
c)
DO_CLEAN=1
shift
;;
esac
done
echo "BUILD TYPE: ${BUILD_TYPE}"
# first build allolib ###########################################################
echo " "
echo "___ building allolib __________"
echo " "
cd ${AL_LIB_PATH}
git submodule init
git submodule update
if [ ${DO_CLEAN} == 1 ]; then
echo "cleaning build"
rm -r build
fi
mkdir -p build
cd build
mkdir -p "${BUILD_TYPE}"
cd "${BUILD_TYPE}"
cmake ../.. -GNinja -DCMAKE_BUILD_TYPE=${BUILD_TYPE}
ninja
LIB_BUILD_RESULT=$?
# if lib failed to build, exit
if [ ${LIB_BUILD_RESULT} != 0 ]; then
exit 1
fi
# then build the app ###########################################################
APP_FILE_INPUT="$1" # first argument (assumming we consumed all the options above)
APP_PATH=$(dirname ${APP_FILE_INPUT})
APP_FILE=$(basename ${APP_FILE_INPUT})
APP_NAME=${APP_FILE%.*} # remove extension (once, assuming .cpp)
echo " "
echo "___ building ${APP_NAME} __________"
echo " "
# echo "app path: ${APP_PATH}"
# echo "app file: ${APP_FILE}"
# echo "app name: ${APP_NAME}"
cd ${INITIALDIR}
cd ${APP_PATH}
if [ ${DO_CLEAN} == 1 ]; then
echo "cleaning build"
rm -r build
fi
mkdir -p build
cd build
mkdir -p ${APP_NAME}
cd ${APP_NAME}
cmake ${AL_LIB_PATH}/cmake/single_file -GNinja -DCMAKE_BUILD_TYPE=${BUILD_TYPE} -Dal_path=${AL_LIB_PATH} -DAL_APP_FILE=../../${APP_FILE}
ninja
APP_BUILD_RESULT=$?
# if app failed to build, exit
if [ ${APP_BUILD_RESULT} != 0 ]; then
exit 1
fi
if [ ${EXIT_AFTER_BUILD} ]; then
exit 0
fi
# run app ######################################################################
# go to where the binary is so we have cwd there
# (app's cmake is set to put binary in 'bin')
cd ${INITIALDIR}
cd ${APP_PATH}/bin
echo " "
echo "___ running ${APP_NAME} __________"
echo " "
./"${APP_NAME}${POSTFIX}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment