1#!/bin/bash
2
3set -euo pipefail
4
5cd "$(dirname $(readlink -f $0))/../"
6
7# default values, can be overwritten by cmdline args
8buildDir="build"
9numThreads="$(nproc)"
10cmakeOptions=("")
11cleanBefore=no
12keepLog=no
13compiler=gcc
14
15_help() {
16    cat <<EOF
17usage: $0 [OPTIONS]
18
19OPTIONS:
20  -cmake='-<key>=<value> [-<key>=<value> ...]'  User defined cmake options
21                                                  Note: use single quote after
22                                                  -cmake= and double quotes if
23                                                  <key> has muliple <values>
24                                                  e.g.: -cmake='-DFLAGS="-a -b"'
25  -compiler=COMPILER_NAME                       Compiler name: gcc or clang
26                                                  Default: gcc
27  -dir=PATH                                     Path to store build files.
28                                                  Default: ./build
29  -coverage                                     Enable cmake coverage options
30  -clean                                        Remove build dir before compile
31  -no-gui                                       Disable GUI support
32  -threads=NUM_THREADS                          Number of threads to use during
33                                                  compile. Default: \`nproc\`
34  -keep-log                                     Keep a compile log in build dir
35  -help                                         Shows this message
36
37EOF
38    exit "${1:-1}"
39}
40
41while [ "$#" -gt 0 ]; do
42    case "${1}" in
43        -h|-help)
44            _help 0
45            ;;
46        -no-gui)
47            cmakeOptions+=( -DBUILD_GUI=OFF )
48            ;;
49        -compiler=*)
50            compiler="${1#*=}"
51            ;;
52        -coverage )
53            cmakeOptions+=( -DCMAKE_BUILD_TYPE=Debug )
54            cmakeOptions+=( -DCMAKE_CXX_FLAGS="-fprofile-arcs -ftest-coverage" )
55            cmakeOptions+=( -DCMAKE_EXE_LINKER_FLAGS=-lgcov )
56            ;;
57        -cmake=*)
58            cmakeOptions+=( "${1#*=}" )
59            ;;
60        -clean )
61            cleanBefore=yes
62            ;;
63        -dir=* )
64            buildDir="${1#*=}"
65            ;;
66        -keep-log )
67            keepLog=yes
68            ;;
69        -threads=* )
70            numThreads="${1#*=}"
71            ;;
72        -compiler | -cmake | -dir | -threads )
73            echo "${1} requires an argument" >&2
74            _help
75            ;;
76        *)
77            echo "unknown option: ${1}" >&2
78            _help
79            ;;
80    esac
81    shift 1
82done
83
84case "${compiler}" in
85    "gcc" )
86        if [[ -f "/opt/rh/devtoolset-8/enable" ]]; then
87            # the scl script has unbound variables
88            set +u
89            source /opt/rh/devtoolset-8/enable
90            set -u
91        fi
92        export CC="$(command -v gcc)"
93        export CXX="$(command -v g++)"
94        ;;
95    "clang" )
96        if [[ -f "/opt/rh/llvm-toolset-7.0/enable" ]]; then
97            # the scl script has unbound variables
98            set +u
99            source /opt/rh/llvm-toolset-7.0/enable
100            set -u
101        fi
102        export CC="$(command -v clang)"
103        export CXX="$(command -v clang++)"
104        ;;
105    *)
106        echo "Compiler $compiler is not supported" >&2
107        _help 1
108esac
109
110if [[ "${cleanBefore}" == "yes" ]]; then
111    rm -rf "${buildDir}"
112fi
113
114mkdir -p "${buildDir}"
115if [[ "${keepLog}" == "yes"  ]]; then
116    logName="${buildDir}/openroad-build-$(date +%s).log"
117else
118    logName=/dev/null
119fi
120
121cmake "${cmakeOptions[@]}" -B "${buildDir}" . 2>&1 | tee "${logName}"
122time cmake --build "${buildDir}" -j "${numThreads}" 2>&1 | tee -a "${logName}"
123