#! /bin/bash # Serene Programming Language # # Copyright (c) 2019-2022 Sameer Rahmani # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, version 2. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . command=$1 export CC=$(which clang) export CXX=$(which clang++) # TODO: Add sloppiness to the cmake list file as well export CCACHE_SLOPPINESS="pch_defines,time_macros" export ASAN_OPTIONS=check_initialization_order=1 LSAN_OPTIONS=suppressions=$(pwd)/.ignore_sanitize export LSAN_OPTIONS export LDFLAGS="-fuse-ld=lld" # The `builder` script is supposed to be run from the # root of the source tree ROOT_DIR=$(pwd) BUILD_DIR=$ROOT_DIR/build ME=$(cd "$(dirname "$0")/." >/dev/null 2>&1 ; pwd -P) CMAKEARGS_DEBUG=" -DCMAKE_BUILD_TYPE=Debug -DSERENE_WITH_MLIR_CL_OPTION=ON" # Verbose -DCMAKE_VERBOSE_MAKEFILE:BOOL=ON CMAKEARGS=" -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DSERENE_CCACHE_DIR=$HOME/.ccache" # The scan-build utility scans the build for bugs checkout the man page scanbuild="scan-build --force-analyze-debug-code --use-analyzer=$(which clang)" function gen_precompile_header_index() { echo "// DO NOT EDIT THIS FILE: It is aute generated by './builder gen_precompile_index'" > ./include/serene_precompiles.h echo "#ifndef SERENE_PRECOMPIL_H" >> ./include/serene_precompiles.h echo "#define SERENE_PRECOMPIL_H" >> ./include/serene_precompiles.h grep -oP "#include .llvm/.*" . -R|cut -d':' -f2|tail +2 >> ./include/serene_precompiles.h grep -oP "#include .mlir/.*" . -R|cut -d':' -f2|tail +2 >> ./include/serene_precompiles.h echo "#endif" >> ./include/serene_precompiles.h } function pushed_build() { pushd "$BUILD_DIR" > /dev/null || return } function popd_build() { popd > /dev/null || return } function build-gen() { pushed_build echo "Running: " echo "cmake -G Ninja $CMAKEARGS $CMAKEARGS_DEBUG \"$@\" \"$ROOT_DIR\"" cmake -G Ninja $CMAKEARGS $CMAKEARGS_DEBUG "$@" "$ROOT_DIR" popd_build } function compile() { pushed_build cmake --build . popd_build } function build() { build-gen "$@" pushed_build cmake --build . popd_build } function build-20() { pushed_build cmake -G Ninja -DCMAKE_BUILD_TYPE=Debug -DCPP_20_SUPPORT=ON "$@" "$ROOT_DIR" cmake --build . popd_build } function build-release() { pushed_build cmake -G Ninja -DCMAKE_BUILD_TYPE=Release "$ROOT_DIR" cmake --build . popd_build } function build-docs() { pushed_build cmake -G Ninja -DCMAKE_BUILD_TYPE=Docs "$ROOT_DIR" cmake --build . popd_build } function clean() { rm -rf "$BUILD_DIR" } function run() { LD_PRELOAD=$(clang -print-file-name=libclang_rt.asan-x86_64.so) "$BUILD_DIR"/src/serenec/serenec "$@" } function repl() { LD_PRELOAD=$(clang -print-file-name=libclang_rt.asan-x86_64.so) "$BUILD_DIR"/src/serene-repl/serene-repl "$@" } function memcheck() { export ASAN_FLAG="" build pushed_build valgrind --tool=memcheck --leak-check=yes --trace-children=yes "$BUILD_DIR"/bin/serenec "$@" popd_build } function run-tests() { LD_PRELOAD=$(clang -print-file-name=libclang_rt.asan-x86_64.so) "$BUILD_DIR"/src/tests/tests } function tests() { pushed_build cmake -G Ninja -DCMAKE_BUILD_TYPE=Debug -DBUILD_TESTING=ON "$ROOT_DIR" cmake --build . popd_build } case "$command" in "docker-llvm") docker build -f resources/docker/Dockerfile.llvm -t serene/llvm:15 . ;; "docker-serene") docker build -f resources/docker/Dockerfile.serene -t serene/build:$(git rev-parse HEAD) . ;; "setup") rm -rfv $ME/.git/hooks/pre-commit ln -s $ME/scripts/pre-commit $ME/.git/hooks/pre-commit ;; "build") clean mkdir -p "$BUILD_DIR" build "${@:2}" ;; "build-tidy") clean mkdir -p "$BUILD_DIR" build "-DSERENE_ENABLE_TIDY=ON" "${@:2}" ;; "build-20") clean mkdir -p "$BUILD_DIR" build-20 "${@:2}" ;; "build-docs") clean mkdir -p "$BUILD_DIR" build-docs "${@:2}" ;; "build-release") clean mkdir -p "$BUILD_DIR" build-release "${@:2}" ;; "compile") compile ;; "compile-and-tests") compile run-tests ;; "run") run "${@:2}" ;; "repl") repl "${@:2}" ;; "run-tests") run-tests "${@:2}" ;; "scan-build") clean mkdir -p "$BUILD_DIR" build-gen pushed_build exec $scanbuild cmake --build . popd_build ;; "memcheck") memcheck "${@:2}" ;; "tests") clean mkdir -p "$BUILD_DIR" tests run-tests ;; "clean") rm -rf "$BUILD_DIR" ;; "gen_precompile_index") gen_precompile_header_index ;; "full-build") clean mkdir -p "$BUILD_DIR" build tests run-tests memcheck ;; *) echo "Commands: " echo "setup - Setup the githooks for devs" echo "full-build - Build and test Serene." echo "build - Build Serene from scratch in DEBUG mode." echo "build-release - Build Serene from scratch in RELEASE mode." echo "compile - reCompiles the project using the already exist cmake configuration" echo "compile-and-tests - reCompiles the project using the already exist cmake configuration and runs the tests" echo "run - Runs the serene executable" echo "scan-build - Compiles serene with static analyzer" echo "tests - Runs the test cases" echo "memcheck - Runs the memcheck(valgrind) tool." echo "clean - :D" ;; esac