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