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