1#!/bin/bash
2# SPDX-License-Identifier: BSD-3-Clause
3# Copyright(c) 2018 Intel Corporation. All rights reserved.
4
5# fail immediately on any errors
6set -e
7
8SOF_TOP=$(cd "$(dirname "$0")/.." && pwd)
9
10print_usage()
11{
12        cat <<EOFUSAGE
13
14Configures and builds selected CMake projects in the tools/ directory.
15Attention: the list of selected shortcuts below is _not_ exhaustive. To
16build _everything_ don't select any particular target; this will build
17CMake's default target "ALL".
18
19usage: $0 [-c|-f|-h|-l|-p|-t|-T]
20       -h Display help
21
22       -c Rebuild ctl/
23       -l Rebuild logger/
24       -p Rebuild probes/
25       -T Rebuild topology/ (not topology/development/! Use ALL)
26       -t Rebuild test/topology/ (or tools/test/topology/tplg-build.sh directly)
27
28       -C No build, only CMake re-configuration. Shows CMake targets.
29EOFUSAGE
30
31        warn_if_incremental_build
32}
33
34# generate Makefiles
35reconfigure_build()
36{
37        rm -rf "$BUILD_TOOLS_DIR"
38        mkdir -p "$BUILD_TOOLS_DIR"
39
40        ( cd "$BUILD_TOOLS_DIR"
41          cmake -GNinja -DCMAKE_BUILD_TYPE="$CMAKE_BUILD_TYPE" "${SOF_REPO}/tools"
42        )
43}
44
45make_tool()
46{
47        # if no argument provided, all the tools will be built. Empty tool is
48        # okay.
49        tool=$1
50
51        ( set -x
52        # shellcheck disable=SC2086
53        cmake --build $BUILD_TOOLS_DIR  --  -j "$NO_PROCESSORS" $tool
54        )
55}
56
57print_build_info()
58{
59       cat <<EOFUSAGE
60
61Build commands for respective tools:
62        ctl:        ninja -C "$BUILD_TOOLS_DIR" sof-ctl
63        logger:     ninja -C "$BUILD_TOOLS_DIR" sof-logger
64        probes:     ninja -C "$BUILD_TOOLS_DIR" sof-probes
65        topologies: ninja -C "$BUILD_TOOLS_DIR" topologies
66        test tplgs: ninja -C "$BUILD_TOOLS_DIR" tests
67               (or ./tools/test/topology/tplg-build.sh directly)
68
69        list of targets:
70                    ninja -C "$BUILD_TOOLS_DIR/" help
71EOFUSAGE
72
73       warn_if_incremental_build
74}
75
76warn_if_incremental_build()
77{
78        $warn_incremental_build || return 0
79        cat <<EOF
80
81WARNING: building tools/ is now incremental by default!
82         To build from scratch delete: $BUILD_TOOLS_DIR
83         or use the -C option.
84
85EOF
86}
87
88main()
89{
90        local DO_BUILD_ctl DO_BUILD_logger DO_BUILD_probes \
91                DO_BUILD_tests DO_BUILD_topologies SCRIPT_DIR SOF_REPO CMAKE_ONLY \
92                BUILD_ALL
93        SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd)
94        SOF_REPO=$(dirname "$SCRIPT_DIR")
95        : "${BUILD_TOOLS_DIR:=$SOF_REPO/tools/build_tools}"
96        : "${NO_PROCESSORS:=$(nproc)}"
97        BUILD_ALL=false
98
99        if [ $# -eq 0 ]; then
100                BUILD_ALL=true
101        fi
102
103        DO_BUILD_ctl=false
104        DO_BUILD_logger=false
105        DO_BUILD_probes=false
106        DO_BUILD_tests=false
107        DO_BUILD_topologies=false
108        CMAKE_ONLY=false
109
110        # better safe than sorry
111        local warn_incremental_build=true
112
113        # eval is a sometimes necessary evil
114        # shellcheck disable=SC2034
115        while getopts "cfhlptTC" OPTION; do
116                case "$OPTION" in
117                c) DO_BUILD_ctl=true ;;
118                l) DO_BUILD_logger=true ;;
119                p) DO_BUILD_probes=true ;;
120                t) DO_BUILD_tests=true ;;
121                T) DO_BUILD_topologies=true ;;
122                C) CMAKE_ONLY=true ;;
123                h) print_usage; exit 1;;
124                *) print_usage; exit 1;;
125                esac
126        done
127        shift "$((OPTIND - 1))"
128
129        if "$CMAKE_ONLY"; then
130                reconfigure_build
131                print_build_info
132                exit
133        fi
134
135        test -e "$BUILD_TOOLS_DIR"/build.ninja ||
136        test -e "$BUILD_TOOLS_DIR"/Makefile    || {
137            warn_incremental_build=false
138            reconfigure_build
139        }
140
141        if "$BUILD_ALL"; then
142                # default CMake targets
143                make_tool # trust set -e
144
145                warn_if_incremental_build
146                exit 0
147        fi
148
149        # Keep 'topologies' first because it's the noisiest.
150        for util in topologies tests; do
151                if eval '$DO_BUILD_'$util; then
152                        make_tool $util
153                fi
154        done
155
156        for tool in ctl logger probes; do
157                if eval '$DO_BUILD_'$tool; then
158                        make_tool sof-$tool
159                fi
160        done
161
162        warn_if_incremental_build
163}
164
165main "$@"
166