multitarget

This commit is contained in:
davidon-top 2024-02-04 12:23:19 +01:00
parent 65bc945f66
commit f4f215a4cb
Signed by: DavidOnTop
GPG key ID: FAB914DDC2F180EB

196
ch
View file

@ -1,117 +1,109 @@
#!/bin/zsh #!/usr/bin/env zsh
######################################################## ###########################################################
## edit thease variables to match your project structure ## edit thease variables to match your project structure ##
######################################################## ###########################################################
BUILD_DEBUG_FOLDER="build/debug" BUILD_FOLDER="build"
BUILD_RELEASE_FOLDER="build/release" DEFAULT_BUILD_TYPE="debug" # "debug" or "release"
DEFAULT_BUILD_TYPE="d" # "d" for debug and "r" for release TARGETS=["{{project-name}}"]
DEFAULT_TARGET="{{project-name}}" # should be the name of the executable beeing built, this is used for the run action
####################################################################################### ##########################################################################################
## this is where the script starts you should not need to edit anything below this line ## this is where the script starts you should not need to edit anything below this line ##
####################################################################################### ##########################################################################################
#
if [ -n "$TARGET" ]; then NC="\033[0m"
DEFAULT_TARGET=$TARGET
fi
NC="\033[0m" # No Color
COLOR="\033[1;33m" COLOR="\033[1;33m"
if [ -z "$1" ] zparseopts -D -E -F -- d:=debug r:=release t+=target h:=help || exit 1
then
echo "No action specified" if (( $#help > 0 )); then
echo "run cmakehelper.sh help for more info" echo "Usage:"
exit 1 echo " ./ch actions [options]"
echo ""
echo "Actions:"
echo " c - clean project"
echo " g - generate cmake files"
echo " b - build project"
echo " r - run project"
echo " s - strip executable"
echo " d - debug with gdb"
echo ""
echo "actions can be chained so gbr will first run generate then build and then run"
echo ""
echo "Options:"
echo " -d, --debug Builds cmake in debug mode"
echo " -r, --release Builds cmake in release mode"
echo " -t CMAKE_TARGET, --target CMAKE_TARGET"
echo " Builds specific target. Builds all targets specified in order that they are specified in. Clean action is not recomended when using multiple targets."
exit 0
fi fi
case $1 in if [[ -z "$1" ]]; then
"init" | "-i" | "--init") echo "no actions provided"
SCRIPT_PATH=$(realpath $0) exit 1
cp -f $SCRIPT_PATH ./cmakehelper.sh fi
echo "Initialized cmakehelper.sh edit the variables at the top of the script to match your project structure"
exit 0
;;
"help" | "-h" | "--help")
echo "Possible actions are:"
echo "c - clean project"
echo "g - generate cmake files"
echo "b - build project"
echo "r - run project"
echo "s - strip executable"
echo "d - debug with gdb"
echo ""
echo "actions can be chained so gbr will first run generate then build and then run"
echo ""
echo "second argument is the build type, possible values are d or debug and r or release"
echo "if no build type is specified then DEFAULT_BUILD_TYPE is used"
echo "build type is required for all actions (accept if you have the same folder for bouth build and release then its only needed for build action)"
exit 0
;;
esac
if [ -z "$2" ] if (( $#debug > 0 )); then
then BUILD_TYPE="debug"
BUILD_TYPE=$DEFAULT_BUILD_TYPE elif (( $#release > 0 )); then
BUILD_TYPE="release"
else else
BT=$(echo $2 | tr '[:upper:]' '[:lower:]') BUILD_TYPE=$DEFAULT_BUILD_TYPE
if [[ $BT == "d" || $BT == "debug" ]]; then
BUILD_TYPE="d"
elif [[ $BT == "r" || $BT == "release" ]]; then
BUILD_TYPE="r"
else
echo "unknown build type $2"
exit 1
fi
fi fi
if (( $#target != 0 )); then
TARGETS=$#target
fi
echo() { echo() {
command echo -e "${COLOR}$@${NC}" command echo -e "${COLOR}$@${NC}"
} }
ARG=$1 ARG=$1
if [[ $BUILD_TYPE == "d" ]]; then
BUILD_FOLDER=$BUILD_DEBUG_FOLDER BF="$BUILD_FOLDER/$BUILD_TYPE"
else
BUILD_FOLDER=$BUILD_RELEASE_FOLDER for TARGET in $TARGETS; do
fi for (( i=0; i<${#ARG}; i++ )); do
for (( i=0; i<${#ARG}; i++ )); do case ${ARG:$i:1} in
case ${ARG:$i:1} in "c")
"c") echo "cleaning project"
echo "cleaning project" rm -rf $BUILD_FOLDER
rm -rf $BUILD_FOLDER ;;
;; "g" | "b")
"g") mkdir -p $BF
echo "generating cmake files" ;;
mkdir -p $BUILD_FOLDER "g")
if [[ $BUILD_TYPE == "d" ]]; then echo "generating cmake files"
cmake -S . -B $BUILD_DEBUG_FOLDER -DCMAKE_BUILD_TYPE=Debug CMAKE_BUILD_TYPE=${BUILD_TYPE^}
else cmake -S . -B $BF -DCMAKE_BUILD_TYPE=$CMAKE_BUILD_TYPE
cmake -S . -B $BUILD_RELEASE_FOLDER -DCMAKE_BUILD_TYPE=Release ;;
fi "b")
;; echo "building project"
"b") cmake --build $BF
echo "building project" ;;
cmake --build $BUILD_FOLDER "r")
;; echo "running project"
"r") if [[ -f "$BF/$TARGET" ]]; then
echo "running project" ./$BF/$TARGET
./$BUILD_FOLDER/$DEFAULT_TARGET fi
;; ;;
"s") "s")
echo "stripping executable" echo "stripping executable"
strip -s $BUILD_FOLDER/$DEFAULT_TARGET -o "$BUILD_FOLDER/$DEFAULT_TARGET-stripped" strip -s $BF/$TARGET -o "$BF/$TARGET-stripped"
;; ;;
"d") "d")
echo "debug project" echo "debug project"
if [[ $BUILD_TYPE == "r" ]]; then if [[ $BUILD_TYPE == "release" ]]; then
echo "WARNING: debugging release build" echo "WARNING: debugging release build"
fi fi
gdb ./$BUILD_FOLDER/$DEFAULT_TARGET gdb $BF/$TARGET
*) ;;
echo "unknown action ${1:$i:1}" *)
exit 1 echo "unknown action ${1:$i:1}"
;; exit 1
esac ;;
esac
done
done done