From e77153f5e8420ac9dbc60eed7723189d63d5e956 Mon Sep 17 00:00:00 2001 From: Sean Bryan Date: Fri, 19 Jan 2024 18:35:24 +1100 Subject: [PATCH] Add build script wrapper around CMake --- .gitignore | 4 ++- build.bash | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+), 1 deletion(-) create mode 100755 build.bash diff --git a/.gitignore b/.gitignore index 50b0866e6..492d9980f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ # Fortran *.mod -*.o \ No newline at end of file +*.o +build +bin diff --git a/build.bash b/build.bash new file mode 100755 index 000000000..1a657ae81 --- /dev/null +++ b/build.bash @@ -0,0 +1,81 @@ +#!/bin/bash + +show_help() { + cat << EOF +Usage: ./$(basename "${0}") [OPTIONS] + +Build script wrapper around CMake. + +Options: + --clean Delete build directory before invoking CMake. + --mpi Compile MPI executable. + -d, --debug Compile in debug mode. + -v, --verbose Enable verbose output when building the project. + -h, --help Show this screen. + +EOF +} + +# Argument parsing adapted and stolen from http://mywiki.wooledge.org/BashFAQ/035#Complex_nonstandard_add-on_utilities +while [ $# -gt 0 ]; do + case $1 in + --clean) + clean=1 + ;; + --mpi) + mpi=1 + ;; + -d|--debug) + debug=1 + ;; + -v|--verbose) + verbose=1 + ;; + -h|--help) + show_help + exit + ;; + ?*) + printf 'WARN: Unknown option (ignored): %s\n' "$1" >&2 + ;; + esac + shift +done + +if hostname -f | grep gadi.nci.org.au > /dev/null; then + . /etc/bashrc + module purge + module add cmake/3.24.2 + module add intel-compiler/2019.5.281 + module add netcdf/4.6.3 + # This is required so that the netcdf-fortran library is discoverable by + # pkg-config: + prepend_path PKG_CONFIG_PATH "${NETCDF_BASE}/lib/Intel/pkgconfig" + if [[ -n $mpi ]]; then + module add intel-mpi/2019.5.281 + fi +fi + +if [[ -n $clean ]]; then + rm -r build +fi + +cmake_args=() +if [[ -n $debug ]]; then + cmake_args+=(-DCMAKE_BUILD_TYPE=Debug) +else + cmake_args+=(-DCMAKE_BUILD_TYPE=Release) +fi +if [[ -n $mpi ]]; then + cmake_args+=(-DCABLE_MPI="ON") + cmake_args+=(-DCMAKE_Fortran_COMPILER="mpif90") +fi + +cmake_build_args=() +if [[ -n $verbose ]]; then + cmake_build_args+=(-v) +fi + +cmake -S . -B build "${cmake_args[@]}" &&\ +cmake --build build -j 4 "${cmake_build_args[@]}" &&\ +cmake --install build --prefix .