Refactor the builder script

This commit is contained in:
Sameer Rahmani 2022-02-09 19:08:31 +00:00
parent f663ba0c25
commit bc3908a958
1 changed files with 105 additions and 118 deletions

223
builder
View File

@ -15,8 +15,31 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# -----------------------------------------------------------------------------
# Commentary
# -----------------------------------------------------------------------------
# This is the builder script for the Serene 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
# -----------------------------------------------------------------------------
# Vars & Config
# -----------------------------------------------------------------------------
command=$1
VERSION="0.4.0"
export CC=$(which clang)
export CXX=$(which clang++)
@ -38,9 +61,12 @@ 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)"
# -----------------------------------------------------------------------------
# Helper functions
# -----------------------------------------------------------------------------
function fn-names() {
grep -E '^function [0-9a-zA-Z_-]+\(\) \{ ## .*$$' $0 | sed 's/^function \([a-zA-Z0-9_-]*\)() { ## \(.*\)/\1/'
}
function gen_precompile_header_index() {
echo "// DO NOT EDIT THIS FILE: It is aute generated by './builder gen_precompile_index'" > ./include/serene_precompiles.h
@ -51,7 +77,9 @@ function gen_precompile_header_index() {
echo "#endif" >> ./include/serene_precompiles.h
}
function pushed_build() {
mkdir -p "$BUILD_DIR"
pushd "$BUILD_DIR" > /dev/null || return
}
@ -66,53 +94,68 @@ function build-gen() {
cmake -G Ninja $CMAKEARGS $CMAKEARGS_DEBUG "$@" "$ROOT_DIR"
popd_build
}
function compile() {
# -----------------------------------------------------------------------------
# Subcomaands
# -----------------------------------------------------------------------------
function compile() { ## Compiles the project using the generated build scripts
pushed_build
cmake --build .
popd_build
}
function build() {
function build() { ## Builds the project by regenerating the build scripts
clean
build-gen "$@"
pushed_build
cmake --build .
popd_build
}
function build-20() {
function build-20() { ## Builds the project using C++20 (will regenerate the build)
clean
pushed_build
cmake -G Ninja -DCMAKE_BUILD_TYPE=Debug -DCPP_20_SUPPORT=ON "$@" "$ROOT_DIR"
cmake --build .
popd_build
}
function build-release() {
function build-tidy { ## Builds the project using clang-tidy (It takes longer than usual)
build "-DSERENE_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() {
function build-docs() { ## Builds the documentation of Serene
clean
pushed_build
cmake -G Ninja -DCMAKE_BUILD_TYPE=Docs "$ROOT_DIR"
cmake --build .
popd_build
}
function clean() {
function clean() { ## Cleans up the source dir and removes the build
rm -rf "$BUILD_DIR"
rm -rf $(find . -iname "*~")
}
function run() {
LD_PRELOAD=$(clang -print-file-name=libclang_rt.asan-x86_64.so) "$BUILD_DIR"/src/serenec/serenec "$@"
function run() { ## Runs `serenec` and passes all the given aruguments to it
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 repl() { ## Runs `serene-repl` and passes all the given aruguments to it
LD_PRELOAD=$(clang -print-file-name=libclang_rt.asan-x86_64.so) \
"$BUILD_DIR"/src/serene-repl/serene-repl "$@"
}
function memcheck() {
function memcheck-serene() { ## Runs `valgrind` to check `serenec` birany
export ASAN_FLAG=""
build
pushed_build
@ -120,122 +163,66 @@ function memcheck() {
popd_build
}
function run-tests() {
LD_PRELOAD=$(clang -print-file-name=libclang_rt.asan-x86_64.so) "$BUILD_DIR"/src/tests/tests
function run-tests() { ## Runs all the test cases
LD_PRELOAD=$(clang -print-file-name=libclang_rt.asan-x86_64.so) \
"$BUILD_DIR"/src/tests/tests
}
function tests() {
function tests() { ## Generates and build the project including the test cases
pushed_build
cmake -G Ninja -DCMAKE_BUILD_TYPE=Debug -DBUILD_TESTING=ON "$ROOT_DIR"
cmake --build .
popd_build
}
# function docker-llvm() { #
# docker build -f resources/docker/Dockerfile.llvm -t serene/llvm:15-1 .
# }
case "$command" in
"docker-llvm")
docker build -f resources/docker/Dockerfile.llvm -t serene/llvm:15 .
;;
function docker-serene() { ## Build the Serene docker image for the current HEAD
docker build -f resources/docker/Dockerfile.serene -t serene/build:$(git rev-parse HEAD) .
}
"docker-serene")
docker build -f resources/docker/Dockerfile.serene -t serene/build:$(git rev-parse HEAD) .
;;
function setup() { ## Setup the working directory and make it ready for development
rm -rfv $ME/.git/hooks/pre-commit
ln -s $ME/scripts/pre-commit $ME/.git/hooks/pre-commit
}
"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}"
;;
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=$(which clang) cmake --build .
popd_build
}
"build-20")
clean
mkdir -p "$BUILD_DIR"
build-20 "${@:2}"
;;
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%-20s\033[0m %s\n", $1, $2}'
}
"build-docs")
clean
mkdir -p "$BUILD_DIR"
build-docs "${@:2}"
;;
# -----------------------------------------------------------------------------
# Main logic
# -----------------------------------------------------------------------------
echo -e "\nSerene Builder Version $VERSION"
echo -e "\nCopyright (C) 2019-2022"
echo -e "Sameer Rahmani <lxsameer@gnu.org>"
echo -e "Serene 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"
"build-release")
clean
mkdir -p "$BUILD_DIR"
build-release "${@:2}"
;;
"compile")
compile
;;
"compile-and-tests")
compile
run-tests
;;
# 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
"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
# If we couldn't find the command print out the help message
help