#! /bin/bash # Leavitt - Source base todo manager for C family of languages # # Copyright (c) 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 . # ----------------------------------------------------------------------------- # Commentary # ----------------------------------------------------------------------------- # This is the builder script for the Leavitt project. It makes it easier to # interact with the CMake build scripts. # # In order to define a subcommand all you need to do is to define a function # with the following syntax: # # function subcommand-name() { ## DESCRIPTION # .. subcommand body .. # } # # Make sure to provid one line of DESCRIPTION for the subcommand and use two "#" # characters to start the description following by a space. Otherwise, your # subcommand won't be registered set -e # ----------------------------------------------------------------------------- # Vars & Config # ----------------------------------------------------------------------------- command=$1 VERSION="0.5.0" CC=$(which clang) CXX=$(which clang++) export CC export CXX CLANG_DIR="$(dirname "$CXX")/../" # 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 ME=$(cd "$(dirname "$0")/." >/dev/null 2>&1 ; pwd -P) ROOT_DIR=$ME/ BUILD_DIR=$ROOT_DIR/build CMAKEARGS_DEBUG=("-DCMAKE_BUILD_TYPE=Debug") # Verbose -DCMAKE_VERBOSE_MAKEFILE:BOOL=ON CMAKEARGS=("-DCMAKE_EXPORT_COMPILE_COMMANDS=ON" "-DLEAVITT_CCACHE_DIR=$HOME/.ccache" "-DCT_Clang_INSTALL_DIR=$CLANG_DIR") # ----------------------------------------------------------------------------- # Helper functions # ----------------------------------------------------------------------------- function fn-names() { grep -E '^function [0-9a-zA-Z_-]+\(\) \{ ## .*$$' "$0" | sed 's/^function \([a-zA-Z0-9_-]*\)() { ## \(.*\)/\1/' } function pushed_build() { mkdir -p "$BUILD_DIR" pushd "$BUILD_DIR" > /dev/null || return } function popd_build() { popd > /dev/null || return } function build-gen() { pushed_build info "Running: " info "cmake -G Ninja ${CMAKEARGS[*]} ${CMAKEARGS_DEBUG=[*]}" "\"$*\" \"$ROOT_DIR\"" cmake -G Ninja "${CMAKEARGS[@]}" "${CMAKEARGS_DEBUG[@]}" "$*" "$ROOT_DIR" popd_build } function info() { if [ "$1" ] then echo -e "[\033[01;32mINFO\033[00m]: $*" fi } function error() { if [ "$1" ] then echo -e "[\033[01;31mERR\033[00m]: $*" fi } function warn() { if [ "$1" ] then echo -e "[\033[01;33mWARN\033[00m]: $*" fi } # ----------------------------------------------------------------------------- # Subcomaands # ----------------------------------------------------------------------------- function compile() { ## Compiles the project using the generated build scripts pushed_build cmake --build . popd_build } function build() { ## Builds the project by regenerating the build scripts local cpus clean build-gen "$@" pushed_build cpus=$(grep ^cpu\\scores /proc/cpuinfo | uniq | awk '{print $4}') cmake --build . -j "$cpus" popd_build } function build-tidy() { ## Builds the project using clang-tidy (It takes longer than usual) build "-DLEAVITT_ENABLE_TIDY=ON" "${@:2}" } function build-release() { ## Builds the project in "Release" mode clean pushed_build cmake -G Ninja -DCMAKE_BUILD_TYPE=Release "$ROOT_DIR" cmake --build . popd_build } function build-docs() { ## Builds the documentation of Leavitt clean pushed_build cmake -G Ninja -DLEAVITT_ENABLE_DOCS=ON "$ROOT_DIR" cmake --build . popd_build } function clean() { ## Cleans up the source dir and removes the build rm -rf "$BUILD_DIR" rm -rf "$(find . -iname '*~')" } function run() { ## Runs `leavittc` and passes all the given aruguments to it "$BUILD_DIR"/bin/leavitt "$@" } function memcheck-leavitt() { ## Runs `valgrind` to check `leavittc` birany export ASAN_FLAG="" build pushed_build valgrind --tool=memcheck --leak-check=yes --trace-children=yes "$BUILD_DIR"/bin/leavitt "$@" popd_build } function build-tests() { ## Generates and build the project including the test cases clean pushed_build cmake -G Ninja -DCMAKE_BUILD_TYPE=Debug -DLEAVITT_BUILD_TESTING=ON "$ROOT_DIR" cmake --build . popd_build } function setup() { ## Setup the working directory and make it ready for development if command -v python3 >/dev/null 2>&1; then pip install pre-commit pre-commit install else error "Python is required to setup pre-commit" fi } function scan-build() { ## Runs the `scan-build` utility to analyze the build process clean build-gen pushed_build # The scan-build utility scans the build for bugs checkout the man page scan-build --force-analyze-debug-code --use-analyzer="$CC" cmake --build . popd_build } function help() { ## Print out this help message echo "Commands:" grep -E '^function [a-zA-Z0-9_-]+\(\) \{ ## .*$$' "$0" | \ sort | \ sed 's/^function \([a-zA-Z0-9_-]*\)() { ## \(.*\)/\1:\2/' | \ awk 'BEGIN {FS=":"}; {printf "\033[36m%-30s\033[0m %s\n", $1, $2}' } # ----------------------------------------------------------------------------- # Main logic # ----------------------------------------------------------------------------- echo -e "\nLeavitt Builder Version $VERSION" echo -e "\nCopyright (C) 2022" echo -e "Sameer Rahmani " echo -e "Leavitt comes with ABSOLUTELY NO WARRANTY;" echo -e "This is free software, and you are welcome" echo -e "to redistribute it under certain conditions;" echo -e "for details take a look at the LICENSE file.\n" # Find the subcommand in the functions and run we find it. for fn in $(fn-names); do if [[ $fn == "$command" ]]; then eval "$fn ${*:2}" exit $? fi done # If we couldn't find the command print out the help message help