aboutsummaryrefslogtreecommitdiff
path: root/build.sh
blob: c3093808aac3364f2b18e41ac680dc787fdffa1b (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
109
110
111
#!/bin/sh

set -e

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

cpu_arch="$ARCH"
if [ -z "$ARCH" ]; then
    cpu_arch=$(uname -m)
    echo "Cpu architecture detected: $cpu_arch." 'You can override the architecture with the environment variable $ARCH'
fi

if [ "$cpu_arch" = "x86_64" ]; then
    source_files="$source_files $(find "executor/x86_64" -name "*.c")"
else
    echo "WARNING: There is no machine code implementation for your cpu architecture: $cpu_arch. An interpreter will be used instead"
    echo "TODO: Implement interpreter"
    exit 2
    #source_files="$source_files $(find "executor/interpreter" -name "*.c")"
fi

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

CFLAGS="-Wall -Wextra -Werror -Wno-format-security -Wno-error=attributes -Wno-attributes -Wnull-dereference -std=c89 -D_GNU_SOURCE"
LIBS="-pthread "

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

if [ -n "$PEDANTIC" ]; then
    CFLAGS="$CFLAGS -DAMAL_PEDANTIC -pedantic "
fi

build_compile_commands() {
    set +x
    compile_commands=$(
    first=0
    echo "["
    for source_file in $@; 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" > "compile_commands.json"
}

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

    BUILD_ARGS="$source_files $CFLAGS $LIBS -shared -fPIC -o libamalgam.so"
    set -x
    time $CC $BUILD_ARGS
    if [ -n "$SCAN_BUILD" ]; then
        scan-build $CC $BUILD_ARGS
    fi
    set +x

    source_files_test=$(find "tests" -name "*.c")
    set -x
    time $CC $source_files_test $CFLAGS $LIBS -o test "./libamalgam.so" $(pkg-config --cflags --libs glfw3 glew)
    set +x

    build_compile_commands $source_files $source_files_test
}

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

    BUILD_ARGS="$source_files $CFLAGS $LIBS -shared -fPIC -o libamalgam.so"
    set -x
    time $CC $BUILD_ARGS
    if [ -n "$SCAN_BUILD" ]; then
        scan-build $CC $BUILD_ARGS
    fi
    set +x

    build_compile_commands $source_files
}

case "$1" in
    "test")
        build_test
        ./tools/highlevel_c.py
        ;;
    "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