aboutsummaryrefslogtreecommitdiff
path: root/build.sh
blob: 8470eb6268ba16ca83ce6d70d5847682e4d4972c (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/bin/sh

set -e

this_script_path=$(readlink -f "$0")
this_script_dir=$(dirname "$this_script_path")
source_files=$(readlink -f $(find "$this_script_dir/src" -name "*.c"))

if [ -z "$CC" ]; then
    CC=cc
fi

CFLAGS="-Wall -Wextra -Werror -Wno-format-security -Wnull-dereference -Wjump-misses-init -std=c89 -D_GNU_SOURCE "
LIBS="-pthread "

if [ ! -z "$SANITIZE_ADDRESS" ]; then
    CFLAGS+="-fsanitize=address "
elif [ ! -z "$SANITIZE_THREAD" ]; then
    CFLAGS+="-fsanitize=thread "
fi

if [ ! -z "$PEDANTIC" ]; then
    CFLAGS+="-DAMAL_PEDANTIC -pedantic "
fi

build_test() {
    CFLAGS+="-g -O0 -DDEBUG"

    BUILD_ARGS="$source_files $CFLAGS $LIBS -shared -fpic -o "$this_script_dir/libamalgam.so""
    set -x
    time $CC $BUILD_ARGS
    if [ ! -z "$SCAN_BUILD" ]; then
        scan-build $CC $BUILD_ARGS
    fi
    set +x

    if [ -z "$NO_TEST" ]; then
        source_files_tests=$(readlink -f $(find "$this_script_dir/tests" -name "*.c"))
        set -x
        time $CC $source_files_tests $CFLAGS $LIBS -o test "$this_script_dir/libamalgam.so"
    fi

    set +x
    compile_commands=$(
    first=0
    echo "["
    for source_file in $source_files $source_files_tests; do
        if [ $first == 1 ]; then
            echo "  ,"
        fi
        first=1
        o_file="${source_file}.o"
        echo "  {"
        echo "    \"file\": \"$source_file\","
        echo "    \"directory\": \"$this_script_dir\","
        echo "    \"command\": \"$CC -o $o_file $CFLAGS $LIBS -c $source_file\""
        echo "  }"
    done
    echo "]")
    echo "$compile_commands" > "$this_script_dir/compile_commands.json"
}

build_release() {
    CFLAGS+="-O2 -DNDEBUG -s"

    BUILD_ARGS="$source_files $CFLAGS $LIBS -shared -fpic -o "$this_script_dir/libamalgam.so""
    set -x
    time $CC $BUILD_ARGS
    if [ ! -z "$SCAN_BUILD" ]; then
        scan-build $CC $BUILD_ARGS
    fi
    set +x

    set +x
    compile_commands=$(
    first=0
    echo "["
    for source_file in $source_files; do
        if [ $first == 1 ]; then
            echo "  ,"
        fi
        first=1
        o_file="${source_file}.o"
        echo "  {"
        echo "    \"file\": \"$source_file\","
        echo "    \"directory\": \"$this_script_dir\","
        echo "    \"command\": \"$CC -o $o_file $CFLAGS $LIBS -c $source_file\""
        echo "  }"
    done
    echo "]")
    echo "$compile_commands" > "$this_script_dir/compile_commands.json"
}

case "$1" in
    "test")
        build_test
        ;;
    "release")
        build_release
        ;;
    *)
        echo "usage: build.sh COMMAND"
        echo "COMMANDS:"
        echo "  test       Build tests"
        echo "  release    Build as a release package, enabling all optimizations and stripping of binary"
        exit 1
        ;;
esac