File size: 3,474 Bytes
23d354c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/usr/bin/env bash

# SPDX-License-Identifier: BSD-3-Clause
# Copyright (c) 2025, The OpenROAD Authors

set -euo pipefail

DIR="$(dirname $(readlink -f $0))"
cd "$DIR/../"

# default values, can be overwritten by cmdline args
buildDir="build"
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
  numThreads=$(nproc --all)
elif [[ "$OSTYPE" == "darwin"* ]]; then
  numThreads=$(sysctl -n hw.ncpu)
else
  cat << EOF
WARNING: Unsupported OSTYPE: cannot determine number of host CPUs
  Defaulting to 2 threads. Use -threads=N to use N threads
EOF
  numThreads=2
fi
cmakeOptions=""
cleanBefore=no
compiler=gcc

_help() {
    cat <<EOF
usage: $0 [OPTIONS]

Build OpenSTA from source.

OPTIONS:
  -cmake='-<key>=<value> ...'   User defined cmake options
                                  Note: use single quote after
                                  -cmake= and double quotes if
                                  <key> has multiple <values>
                                  e.g.: -cmake='-DFLAGS="-a -b"'
  -compiler=COMPILER_NAME       Compiler name: gcc or clang
                                  Default: gcc
  -dir=PATH                     Path to store build files.
                                  Default: ./build
  -coverage                     Enable cmake coverage options
  -clean                        Remove build dir before compile
  -threads=NUM_THREADS          Number of threads to use during
                                  compile. Default: \`nproc\` on linux
                                  or \`sysctl -n hw.ncpu\` on macOS
  -O0                           Disable optimizations for accurate
                                  coverage measurement
  -help                         Shows this message

EOF
    exit "${1:-1}"
}

while [ "$#" -gt 0 ]; do
    case "${1}" in
        -h|-help)
            _help 0
            ;;
        -coverage )
            cmakeOptions+=" -DCMAKE_BUILD_TYPE=Debug"
            cmakeOptions+=" -DCMAKE_CXX_FLAGS='--coverage'"
            cmakeOptions+=" -DCMAKE_EXE_LINKER_FLAGS='--coverage'"
            ;;
        -O0 )
            cmakeOptions+=" -DCMAKE_CXX_FLAGS_DEBUG='-g -O0'"
            ;;
        -cmake=*)
            cmakeOptions+=" ${1#*=}"
            ;;
        -clean )
            cleanBefore=yes
            ;;
        -compiler=*)
            compiler="${1#*=}"
            ;;
        -dir=* )
            buildDir="${1#*=}"
            ;;
        -threads=* )
            numThreads="${1#*=}"
            ;;
        -compiler | -cmake | -dir | -threads )
            echo "${1} requires an argument" >&2
            _help
            ;;
        *)
            echo "unknown option: ${1}" >&2
            _help
            ;;
    esac
    shift 1
done

case "${compiler}" in
    "gcc" )
        export CC="$(command -v gcc)"
        export CXX="$(command -v g++)"
        ;;
    "clang" )
        export CC="$(command -v clang)"
        export CXX="$(command -v clang++)"
        ;;
    *)
        echo "Compiler $compiler not supported. Use gcc or clang." >&2
        _help 1
esac

if [[ -z "${CC}" || -z "${CXX}" ]]; then
    echo "Compiler $compiler not installed." >&2
    _help 1
fi

if [[ "${cleanBefore}" == "yes" ]]; then
    rm -rf "${buildDir}"
fi

mkdir -p "${buildDir}"

echo "[INFO] Compiler: ${compiler} (${CC})"
echo "[INFO] Build dir: ${buildDir}"
echo "[INFO] Using ${numThreads} threads."

eval cmake "${cmakeOptions}" -B "${buildDir}" .
eval time cmake --build "${buildDir}" -j "${numThreads}"