aboutsummaryrefslogtreecommitdiff
path: root/build.sh
blob: dcbbc2906cee1bdc4fecefeaa4fd3fae30e04faf (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
#!/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 $ARCH environment variable.'
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=gcc
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_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_release() {
    CFLAGS="$CFLAGS -O2 -DNDEBUG -s"
    # TODO: -static -flto
    # LIBS="-static $LIBS" also remove below -shared -fPIC
    # CFLAGS="$CFLAGS -flto"

    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
}

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