xref: /aosp_15_r20/external/openthread/script/cmake-build (revision cfb92d1480a9e65faed56933e9c12405f45898b4)
1*cfb92d14SAndroid Build Coastguard Worker#!/bin/bash
2*cfb92d14SAndroid Build Coastguard Worker#
3*cfb92d14SAndroid Build Coastguard Worker#  Copyright (c) 2020, The OpenThread Authors.
4*cfb92d14SAndroid Build Coastguard Worker#  All rights reserved.
5*cfb92d14SAndroid Build Coastguard Worker#
6*cfb92d14SAndroid Build Coastguard Worker#  Redistribution and use in source and binary forms, with or without
7*cfb92d14SAndroid Build Coastguard Worker#  modification, are permitted provided that the following conditions are met:
8*cfb92d14SAndroid Build Coastguard Worker#  1. Redistributions of source code must retain the above copyright
9*cfb92d14SAndroid Build Coastguard Worker#     notice, this list of conditions and the following disclaimer.
10*cfb92d14SAndroid Build Coastguard Worker#  2. Redistributions in binary form must reproduce the above copyright
11*cfb92d14SAndroid Build Coastguard Worker#     notice, this list of conditions and the following disclaimer in the
12*cfb92d14SAndroid Build Coastguard Worker#     documentation and/or other materials provided with the distribution.
13*cfb92d14SAndroid Build Coastguard Worker#  3. Neither the name of the copyright holder nor the
14*cfb92d14SAndroid Build Coastguard Worker#     names of its contributors may be used to endorse or promote products
15*cfb92d14SAndroid Build Coastguard Worker#     derived from this software without specific prior written permission.
16*cfb92d14SAndroid Build Coastguard Worker#
17*cfb92d14SAndroid Build Coastguard Worker#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18*cfb92d14SAndroid Build Coastguard Worker#  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19*cfb92d14SAndroid Build Coastguard Worker#  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20*cfb92d14SAndroid Build Coastguard Worker#  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21*cfb92d14SAndroid Build Coastguard Worker#  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22*cfb92d14SAndroid Build Coastguard Worker#  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23*cfb92d14SAndroid Build Coastguard Worker#  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24*cfb92d14SAndroid Build Coastguard Worker#  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25*cfb92d14SAndroid Build Coastguard Worker#  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26*cfb92d14SAndroid Build Coastguard Worker#  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27*cfb92d14SAndroid Build Coastguard Worker#  POSSIBILITY OF SUCH DAMAGE.
28*cfb92d14SAndroid Build Coastguard Worker#
29*cfb92d14SAndroid Build Coastguard Worker
30*cfb92d14SAndroid Build Coastguard Worker#
31*cfb92d14SAndroid Build Coastguard Worker#  This script calls cmake and ninja to compile OpenThread for the given platform.
32*cfb92d14SAndroid Build Coastguard Worker#
33*cfb92d14SAndroid Build Coastguard Worker#  Compile with default build options:
34*cfb92d14SAndroid Build Coastguard Worker#
35*cfb92d14SAndroid Build Coastguard Worker#      script/cmake-build ${platform}
36*cfb92d14SAndroid Build Coastguard Worker#
37*cfb92d14SAndroid Build Coastguard Worker#  Compile with the specified build option enabled:
38*cfb92d14SAndroid Build Coastguard Worker#
39*cfb92d14SAndroid Build Coastguard Worker#      script/cmake-build ${platform} -D${option}=ON
40*cfb92d14SAndroid Build Coastguard Worker#
41*cfb92d14SAndroid Build Coastguard Worker#  Compile with the specified build option disabled that already enabled by default:
42*cfb92d14SAndroid Build Coastguard Worker#
43*cfb92d14SAndroid Build Coastguard Worker#      script/cmake-build ${platform} -D${option}=OFF
44*cfb92d14SAndroid Build Coastguard Worker#
45*cfb92d14SAndroid Build Coastguard Worker#  Compile with the specified ninja build target:
46*cfb92d14SAndroid Build Coastguard Worker#
47*cfb92d14SAndroid Build Coastguard Worker#      OT_CMAKE_NINJA_TARGET="ot-cli-ftd" script/cmake-build ${platform}
48*cfb92d14SAndroid Build Coastguard Worker#      OT_CMAKE_NINJA_TARGET="ot-cli-ftd ot-cli-mtd" script/cmake-build ${platform}
49*cfb92d14SAndroid Build Coastguard Worker#
50*cfb92d14SAndroid Build Coastguard Worker#  Compile with the specified build directory:
51*cfb92d14SAndroid Build Coastguard Worker#
52*cfb92d14SAndroid Build Coastguard Worker#      OT_CMAKE_BUILD_DIR="./build/temp"  script/cmake-build ${platform}
53*cfb92d14SAndroid Build Coastguard Worker#
54*cfb92d14SAndroid Build Coastguard Worker#  Examples:
55*cfb92d14SAndroid Build Coastguard Worker#
56*cfb92d14SAndroid Build Coastguard Worker#      script/cmake-build simulation
57*cfb92d14SAndroid Build Coastguard Worker#
58*cfb92d14SAndroid Build Coastguard Worker#      script/cmake-build simulation -DOT_FULL_LOGS=ON -DOT_CHANNEL_MANAGER=OFF
59*cfb92d14SAndroid Build Coastguard Worker#
60*cfb92d14SAndroid Build Coastguard Worker#      OT_CMAKE_NINJA_TARGET="ot-cli-mtd" OT_CMAKE_BUILD_DIR="./build/temp" script/cmake-build simulation -DOT_FULL_LOGS=ON -DOT_CHANNEL_MANAGER=OFF
61*cfb92d14SAndroid Build Coastguard Worker#
62*cfb92d14SAndroid Build Coastguard Worker
63*cfb92d14SAndroid Build Coastguard Workerset -euxo pipefail
64*cfb92d14SAndroid Build Coastguard Worker
65*cfb92d14SAndroid Build Coastguard WorkerOT_CMAKE_NINJA_TARGET=${OT_CMAKE_NINJA_TARGET-}
66*cfb92d14SAndroid Build Coastguard Worker
67*cfb92d14SAndroid Build Coastguard WorkerOT_SRCDIR="$(cd "$(dirname "$0")"/.. && pwd)"
68*cfb92d14SAndroid Build Coastguard Workerreadonly OT_SRCDIR
69*cfb92d14SAndroid Build Coastguard Worker
70*cfb92d14SAndroid Build Coastguard WorkerOT_PLATFORMS=(simulation posix android-ndk)
71*cfb92d14SAndroid Build Coastguard Workerreadonly OT_PLATFORMS
72*cfb92d14SAndroid Build Coastguard Worker
73*cfb92d14SAndroid Build Coastguard WorkerOT_POSIX_SIM_COMMON_OPTIONS=(
74*cfb92d14SAndroid Build Coastguard Worker    "-DOT_ANYCAST_LOCATOR=ON"
75*cfb92d14SAndroid Build Coastguard Worker    "-DOT_BORDER_AGENT=ON"
76*cfb92d14SAndroid Build Coastguard Worker    "-DOT_BORDER_AGENT_ID=ON"
77*cfb92d14SAndroid Build Coastguard Worker    "-DOT_BORDER_ROUTER=ON"
78*cfb92d14SAndroid Build Coastguard Worker    "-DOT_CHANNEL_MANAGER=ON"
79*cfb92d14SAndroid Build Coastguard Worker    "-DOT_CHANNEL_MONITOR=ON"
80*cfb92d14SAndroid Build Coastguard Worker    "-DOT_COAP=ON"
81*cfb92d14SAndroid Build Coastguard Worker    "-DOT_COAPS=ON"
82*cfb92d14SAndroid Build Coastguard Worker    "-DOT_COAP_BLOCK=ON"
83*cfb92d14SAndroid Build Coastguard Worker    "-DOT_COAP_OBSERVE=ON"
84*cfb92d14SAndroid Build Coastguard Worker    "-DOT_COMMISSIONER=ON"
85*cfb92d14SAndroid Build Coastguard Worker    "-DOT_COMPILE_WARNING_AS_ERROR=ON"
86*cfb92d14SAndroid Build Coastguard Worker    "-DOT_COVERAGE=ON"
87*cfb92d14SAndroid Build Coastguard Worker    "-DOT_DATASET_UPDATER=ON"
88*cfb92d14SAndroid Build Coastguard Worker    "-DOT_DHCP6_CLIENT=ON"
89*cfb92d14SAndroid Build Coastguard Worker    "-DOT_DHCP6_SERVER=ON"
90*cfb92d14SAndroid Build Coastguard Worker    "-DOT_DIAGNOSTIC=ON"
91*cfb92d14SAndroid Build Coastguard Worker    "-DOT_DNSSD_SERVER=ON"
92*cfb92d14SAndroid Build Coastguard Worker    "-DOT_DNS_CLIENT=ON"
93*cfb92d14SAndroid Build Coastguard Worker    "-DOT_ECDSA=ON"
94*cfb92d14SAndroid Build Coastguard Worker    "-DOT_HISTORY_TRACKER=ON"
95*cfb92d14SAndroid Build Coastguard Worker    "-DOT_IP6_FRAGM=ON"
96*cfb92d14SAndroid Build Coastguard Worker    "-DOT_JAM_DETECTION=ON"
97*cfb92d14SAndroid Build Coastguard Worker    "-DOT_JOINER=ON"
98*cfb92d14SAndroid Build Coastguard Worker    "-DOT_LOG_LEVEL_DYNAMIC=ON"
99*cfb92d14SAndroid Build Coastguard Worker    "-DOT_MAC_FILTER=ON"
100*cfb92d14SAndroid Build Coastguard Worker    "-DOT_NEIGHBOR_DISCOVERY_AGENT=ON"
101*cfb92d14SAndroid Build Coastguard Worker    "-DOT_NETDATA_PUBLISHER=ON"
102*cfb92d14SAndroid Build Coastguard Worker    "-DOT_NETDIAG_CLIENT=ON"
103*cfb92d14SAndroid Build Coastguard Worker    "-DOT_PING_SENDER=ON"
104*cfb92d14SAndroid Build Coastguard Worker    "-DOT_RCP_RESTORATION_MAX_COUNT=2"
105*cfb92d14SAndroid Build Coastguard Worker    "-DOT_RCP_TX_WAIT_TIME_SECS=5"
106*cfb92d14SAndroid Build Coastguard Worker    "-DOT_REFERENCE_DEVICE=ON"
107*cfb92d14SAndroid Build Coastguard Worker    "-DOT_SERVICE=ON"
108*cfb92d14SAndroid Build Coastguard Worker    "-DOT_SNTP_CLIENT=ON"
109*cfb92d14SAndroid Build Coastguard Worker    "-DOT_SRP_CLIENT=ON"
110*cfb92d14SAndroid Build Coastguard Worker    "-DOT_SRP_SERVER=ON"
111*cfb92d14SAndroid Build Coastguard Worker    "-DOT_UPTIME=ON"
112*cfb92d14SAndroid Build Coastguard Worker    "-DOT_BLE_TCAT=ON"
113*cfb92d14SAndroid Build Coastguard Worker)
114*cfb92d14SAndroid Build Coastguard Workerreadonly OT_POSIX_SIM_COMMON_OPTIONS
115*cfb92d14SAndroid Build Coastguard Worker
116*cfb92d14SAndroid Build Coastguard Workerdie()
117*cfb92d14SAndroid Build Coastguard Worker{
118*cfb92d14SAndroid Build Coastguard Worker    echo " ** ERROR: Openthread CMake doesn't support platform \"$1\""
119*cfb92d14SAndroid Build Coastguard Worker    exit 1
120*cfb92d14SAndroid Build Coastguard Worker}
121*cfb92d14SAndroid Build Coastguard Worker
122*cfb92d14SAndroid Build Coastguard Workerbuild()
123*cfb92d14SAndroid Build Coastguard Worker{
124*cfb92d14SAndroid Build Coastguard Worker    local platform=$1
125*cfb92d14SAndroid Build Coastguard Worker    local builddir="${OT_CMAKE_BUILD_DIR:-build/${platform}}"
126*cfb92d14SAndroid Build Coastguard Worker    shift
127*cfb92d14SAndroid Build Coastguard Worker
128*cfb92d14SAndroid Build Coastguard Worker    mkdir -p "${builddir}"
129*cfb92d14SAndroid Build Coastguard Worker    cd "${builddir}"
130*cfb92d14SAndroid Build Coastguard Worker
131*cfb92d14SAndroid Build Coastguard Worker    cmake -GNinja -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DOT_COMPILE_WARNING_AS_ERROR=ON "$@" "${OT_SRCDIR}"
132*cfb92d14SAndroid Build Coastguard Worker    if [[ -z ${OT_CMAKE_NINJA_TARGET// /} ]]; then
133*cfb92d14SAndroid Build Coastguard Worker        ninja
134*cfb92d14SAndroid Build Coastguard Worker    else
135*cfb92d14SAndroid Build Coastguard Worker        IFS=' ' read -r -a OT_CMAKE_NINJA_TARGET <<<"${OT_CMAKE_NINJA_TARGET}"
136*cfb92d14SAndroid Build Coastguard Worker        ninja "${OT_CMAKE_NINJA_TARGET[@]}"
137*cfb92d14SAndroid Build Coastguard Worker    fi
138*cfb92d14SAndroid Build Coastguard Worker
139*cfb92d14SAndroid Build Coastguard Worker    cd "${OT_SRCDIR}"
140*cfb92d14SAndroid Build Coastguard Worker}
141*cfb92d14SAndroid Build Coastguard Worker
142*cfb92d14SAndroid Build Coastguard Workermain()
143*cfb92d14SAndroid Build Coastguard Worker{
144*cfb92d14SAndroid Build Coastguard Worker    if [[ $# == 0 ]]; then
145*cfb92d14SAndroid Build Coastguard Worker        echo "Please specify a platform: ${OT_PLATFORMS[*]}"
146*cfb92d14SAndroid Build Coastguard Worker        exit 1
147*cfb92d14SAndroid Build Coastguard Worker    fi
148*cfb92d14SAndroid Build Coastguard Worker
149*cfb92d14SAndroid Build Coastguard Worker    local platform="$1"
150*cfb92d14SAndroid Build Coastguard Worker    # Check if the platform supports cmake.
151*cfb92d14SAndroid Build Coastguard Worker    echo "${OT_PLATFORMS[@]}" | grep -wq "${platform}" || die "${platform}"
152*cfb92d14SAndroid Build Coastguard Worker
153*cfb92d14SAndroid Build Coastguard Worker    shift
154*cfb92d14SAndroid Build Coastguard Worker    local local_options=()
155*cfb92d14SAndroid Build Coastguard Worker    local options=(
156*cfb92d14SAndroid Build Coastguard Worker        "-DOT_SLAAC=ON"
157*cfb92d14SAndroid Build Coastguard Worker    )
158*cfb92d14SAndroid Build Coastguard Worker
159*cfb92d14SAndroid Build Coastguard Worker    case "${platform}" in
160*cfb92d14SAndroid Build Coastguard Worker        android-ndk)
161*cfb92d14SAndroid Build Coastguard Worker            if [ -z "${NDK-}" ]; then
162*cfb92d14SAndroid Build Coastguard Worker                echo "
163*cfb92d14SAndroid Build Coastguard WorkerThe 'NDK' environment variable needs to point to the Android NDK toolchain.
164*cfb92d14SAndroid Build Coastguard WorkerPlease ensure the NDK is downloaded and extracted then try to run this script again
165*cfb92d14SAndroid Build Coastguard Worker
166*cfb92d14SAndroid Build Coastguard WorkerFor example:
167*cfb92d14SAndroid Build Coastguard Worker    NDK=/opt/android-ndk-r25c ./script/cmake-build-android
168*cfb92d14SAndroid Build Coastguard Worker
169*cfb92d14SAndroid Build Coastguard WorkerYou can download the NDK at https://developer.android.com/ndk/downloads
170*cfb92d14SAndroid Build Coastguard Worker
171*cfb92d14SAndroid Build Coastguard Worker            "
172*cfb92d14SAndroid Build Coastguard Worker                exit 1
173*cfb92d14SAndroid Build Coastguard Worker            fi
174*cfb92d14SAndroid Build Coastguard Worker
175*cfb92d14SAndroid Build Coastguard Worker            NDK_CMAKE_TOOLCHAIN_FILE="${NDK?}/build/cmake/android.toolchain.cmake"
176*cfb92d14SAndroid Build Coastguard Worker            if [ ! -f "${NDK_CMAKE_TOOLCHAIN_FILE}" ]; then
177*cfb92d14SAndroid Build Coastguard Worker                echo "
178*cfb92d14SAndroid Build Coastguard WorkerCould not fild the Android NDK CMake toolchain file
179*cfb92d14SAndroid Build Coastguard Worker- NDK=${NDK}
180*cfb92d14SAndroid Build Coastguard Worker- NDK_CMAKE_TOOLCHAIN_FILE=${NDK_CMAKE_TOOLCHAIN_FILE}
181*cfb92d14SAndroid Build Coastguard Worker
182*cfb92d14SAndroid Build Coastguard Worker            "
183*cfb92d14SAndroid Build Coastguard Worker                exit 2
184*cfb92d14SAndroid Build Coastguard Worker            fi
185*cfb92d14SAndroid Build Coastguard Worker            local_options+=(
186*cfb92d14SAndroid Build Coastguard Worker                "-DOT_LOG_OUTPUT=PLATFORM_DEFINED"
187*cfb92d14SAndroid Build Coastguard Worker
188*cfb92d14SAndroid Build Coastguard Worker                # Add Android NDK flags
189*cfb92d14SAndroid Build Coastguard Worker                "-DOT_ANDROID_NDK=1"
190*cfb92d14SAndroid Build Coastguard Worker                "-DCMAKE_TOOLCHAIN_FILE=${NDK?}/build/cmake/android.toolchain.cmake"
191*cfb92d14SAndroid Build Coastguard Worker
192*cfb92d14SAndroid Build Coastguard Worker                # Android API needs to be >= android-24 for `getifsaddrs()`
193*cfb92d14SAndroid Build Coastguard Worker                "-DANDROID_PLATFORM=android-24"
194*cfb92d14SAndroid Build Coastguard Worker
195*cfb92d14SAndroid Build Coastguard Worker                # Store thread settings in the CWD when executing ot-cli or ot-daemon
196*cfb92d14SAndroid Build Coastguard Worker                '-DOT_POSIX_SETTINGS_PATH="./thread"'
197*cfb92d14SAndroid Build Coastguard Worker            )
198*cfb92d14SAndroid Build Coastguard Worker
199*cfb92d14SAndroid Build Coastguard Worker            # Rewrite platform to posix
200*cfb92d14SAndroid Build Coastguard Worker            platform="posix"
201*cfb92d14SAndroid Build Coastguard Worker
202*cfb92d14SAndroid Build Coastguard Worker            # Check if OT_DAEMON or OT_APP_CLI flags are needed
203*cfb92d14SAndroid Build Coastguard Worker            if [[ ${OT_CMAKE_NINJA_TARGET[*]} =~ "ot-daemon" ]] || [[ ${OT_CMAKE_NINJA_TARGET[*]} =~ "ot-ctl" ]]; then
204*cfb92d14SAndroid Build Coastguard Worker                local_options+=("-DOT_DAEMON=ON")
205*cfb92d14SAndroid Build Coastguard Worker            elif [[ ${OT_CMAKE_NINJA_TARGET[*]} =~ "ot-cli" ]]; then
206*cfb92d14SAndroid Build Coastguard Worker                local_options+=("-DOT_APP_CLI=ON")
207*cfb92d14SAndroid Build Coastguard Worker            fi
208*cfb92d14SAndroid Build Coastguard Worker
209*cfb92d14SAndroid Build Coastguard Worker            options+=("${local_options[@]}")
210*cfb92d14SAndroid Build Coastguard Worker            ;;
211*cfb92d14SAndroid Build Coastguard Worker
212*cfb92d14SAndroid Build Coastguard Worker        posix)
213*cfb92d14SAndroid Build Coastguard Worker            local_options+=(
214*cfb92d14SAndroid Build Coastguard Worker                "-DOT_TCP=OFF"
215*cfb92d14SAndroid Build Coastguard Worker                "-DOT_LOG_OUTPUT=PLATFORM_DEFINED"
216*cfb92d14SAndroid Build Coastguard Worker                "-DOT_POSIX_MAX_POWER_TABLE=ON"
217*cfb92d14SAndroid Build Coastguard Worker            )
218*cfb92d14SAndroid Build Coastguard Worker            options+=("${OT_POSIX_SIM_COMMON_OPTIONS[@]}" "${local_options[@]}")
219*cfb92d14SAndroid Build Coastguard Worker            ;;
220*cfb92d14SAndroid Build Coastguard Worker        simulation)
221*cfb92d14SAndroid Build Coastguard Worker            local_options+=(
222*cfb92d14SAndroid Build Coastguard Worker                "-DOT_LINK_RAW=ON"
223*cfb92d14SAndroid Build Coastguard Worker                "-DOT_DNS_DSO=ON"
224*cfb92d14SAndroid Build Coastguard Worker                "-DOT_DNS_CLIENT_OVER_TCP=ON"
225*cfb92d14SAndroid Build Coastguard Worker                "-DOT_UDP_FORWARD=ON"
226*cfb92d14SAndroid Build Coastguard Worker            )
227*cfb92d14SAndroid Build Coastguard Worker            options+=("${OT_POSIX_SIM_COMMON_OPTIONS[@]}" "${local_options[@]}")
228*cfb92d14SAndroid Build Coastguard Worker            ;;
229*cfb92d14SAndroid Build Coastguard Worker        *)
230*cfb92d14SAndroid Build Coastguard Worker            options+=("-DCMAKE_TOOLCHAIN_FILE=examples/platforms/${platform}/arm-none-eabi.cmake")
231*cfb92d14SAndroid Build Coastguard Worker            ;;
232*cfb92d14SAndroid Build Coastguard Worker    esac
233*cfb92d14SAndroid Build Coastguard Worker
234*cfb92d14SAndroid Build Coastguard Worker    options+=(
235*cfb92d14SAndroid Build Coastguard Worker        "-DOT_PLATFORM=${platform}"
236*cfb92d14SAndroid Build Coastguard Worker    )
237*cfb92d14SAndroid Build Coastguard Worker    options+=("$@")
238*cfb92d14SAndroid Build Coastguard Worker    build "${platform}" "${options[@]}"
239*cfb92d14SAndroid Build Coastguard Worker}
240*cfb92d14SAndroid Build Coastguard Worker
241*cfb92d14SAndroid Build Coastguard Workermain "$@"
242