#!/bin/sh cd ${0%/*} # go to project root NAME="web-server" FLAGS="-std=c99 -Wall -Wextra -g -pedantic" SRCD="src" ODIR="obj" BIN="bin" FILES="files" RUN=0 LIBS=("magic") INC_LIBS= INC_MACROS="-D_POSIX_C_SOURCE=200112L -DFILES=\"$FILES\" " function add_libs { for lib in ${LIBS[@]}; do INC_LIBS+="-l$lib " INC_MACROS+="-DUSE_LIB${lib^^} " done } function __help__ { echo "Usage:" echo " ./build.sh [command | option] ..." echo "" echo "Options:" echo " -(library) add the libarary into the list of libraries" echo "" echo "Commands:" echo " help print this text" echo " run will run the program after all commands are interpreted" echo " valgrind use valgrind when running" echo " -, nolib clear the list of libraries that are going to be loaded" echo " clean will clean the temporary folders like 'obj' or 'bin'" echo "" echo "Note: Each command and option is interpreted in order" exit 0 } function __nolib__ { LIBS=() } function __run__ { RUN=1 } function __valgrind__ { VALGRND="valgrind --leak-check=full --show-leak-kinds=all -s" } function __clean__ { rm -rf $BIN rm -rf $ODIR exit 0 } if ! [[ $# -eq 0 ]] then for cmd in "$@"; do if [ $(cut -c1-1 <<< $cmd) == "-" ]; then if [ $(expr length $cmd) == 1 ]; then __nolib__ else LIBS+=("$(cut -c2- <<< $cmd)") fi else __"$cmd"__ fi done fi set -xe mkdir -p $BIN mkdir -p $ODIR mkdir -p $FILES { add_libs; } 2> /dev/null gcc -c $SRCD/server.c -o $ODIR/server.o $FLAGS $INC_MACROS gcc -c $SRCD/main.c -o $ODIR/main.o $FLAGS $INC_MACROS gcc -o $BIN/$NAME $ODIR/main.o $ODIR/server.o $FLAGS $INC_MACROS $INC_LIBS if ! { [[ $RUN -eq 0 ]]; } 2> /dev/null then $VALGRND $BIN/$NAME fi exit 0