#!/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