Skip to content

Instantly share code, notes, and snippets.

@0xd61
Last active October 2, 2021 23:34
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 0xd61/7d2d5913cfa6000e04e5e88db149d569 to your computer and use it in GitHub Desktop.
Save 0xd61/7d2d5913cfa6000e04e5e88db149d569 to your computer and use it in GitHub Desktop.
Linux C++ default buildfile
#!/usr/bin/env sh
set -e
APP_NAME="myapp"
DEFINE_PREFIX="MYAPP"
BINARY="${APP_NAME}_linux_x86_64"
BASE_DIR="$PWD"
PLATFORM_DIR="$(dirname "$0")"
PLATFORM_NAME="$(basename "${PLATFORM_DIR}")"
BUILD_DIR="${BASE_DIR}/build/${PLATFORM_NAME}"
DATA_DIR="${BASE_DIR}/data"
INCLUDE_DIR="${BASE_DIR}/include"
# NOTE(dgl): Load env variables if file is available
[ -f "${BASE_DIR}/ENV" ] && . "${BASE_DIR}/ENV"
# NOTE(dgl): check if tools are available
[ -d "${BUILD_DIR}" ] || mkdir -p "${BUILD_DIR}"
[ -d "${DATA_DIR}" ] || mkdir -p "${DATA_DIR}"
CommonCompilerFlags="-O0 -g -ggdb -fdiagnostics-color=always -std=c++11 -fno-rtti -fno-exceptions -ffast-math -msse4.1 -msse2
-Wall -Werror -Wconversion
-Wno-writable-strings -Wno-gnu-anonymous-struct
-Wno-padded -Wno-string-conversion
-Wno-error=sign-conversion -Wno-incompatible-function-pointer-types
-Wno-error=unused-variable -Wno-unused-function
-Wno-error=unused-command-line-argument"
CommonDefines="-D${DEFINE_PREFIX}_DEBUG=1 -D${DEFINE_PREFIX}_INTERNAL=1"
CommonIncludeFlags=""
CommonLinkerFlags="-lxcb"
fetch() {
echo "Fetching dependencies"
}
build() {
echo "Build application"
clang++ -shared -Wl,-soname,"${APP_NAME}.so" -fPIC $CommonIncludeFlags $CommonCompilerFlags $CommonDefines -o "${BUILD_DIR}/${APP_NAME}.so" "${BASE_DIR}/lib/${APP_NAME}.cpp"
clang++ $CommonIncludeFlags $CommonCompilerFlags $CommonDefines $CommonLinkerFlags -o "${BUILD_DIR}/${BINARY}" "${PLATFORM_DIR}/main.cpp"
}
launch() {
echo "Launch application"
"${BUILD_DIR}/${BINARY}" $@
}
debug() {
echo "Debug application"
GDB_FRONTEND="$(which gf2)"
if [ -z "${GDB_FRONTEND}" ]; then
gdb "${BUILD_DIR}/${BINARY}" $@
else
gf2 "${BUILD_DIR}/${BINARY}" $@
fi
}
COMMAND="$1"
shift
ARGS="$@"
case "${COMMAND}" in
fetch)
fetch
;;
run)
fetch
build
launch "${ARGS}"
;;
build)
fetch
build
;;
launch)
launch "${ARGS}"
;;
debug)
fetch
build
debug "${ARGS}"
;;
all)
build
launch "${ARGS}"
;;
help)
echo "Usage: $(basename "$0") [command]"
echo "By default build, install and run ${BINARY}."
echo
echo "Optional [command] can be:"
echo " fetch - download dependent libraries"
echo " run - build and launch application"
echo " build - build application"
echo " launch - only start application"
echo " debug - run in gdb"
;;
*)
fetch
build
launch "${ARGS}"
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment