aboutsummaryrefslogtreecommitdiff
path: root/build.sh
blob: 6b2713c057399b68d4c75c28124a7039fa6af6c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/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