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