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_BLE_TCAT=ON"
76    "-DOT_BORDER_AGENT=ON"
77    "-DOT_BORDER_AGENT_EPSKC=ON"
78    "-DOT_BORDER_AGENT_ID=ON"
79    "-DOT_BORDER_ROUTER=ON"
80    "-DOT_CHANNEL_MANAGER=ON"
81    "-DOT_CHANNEL_MONITOR=ON"
82    "-DOT_COAP=ON"
83    "-DOT_COAPS=ON"
84    "-DOT_COAP_BLOCK=ON"
85    "-DOT_COAP_OBSERVE=ON"
86    "-DOT_COMMISSIONER=ON"
87    "-DOT_COMPILE_WARNING_AS_ERROR=ON"
88    "-DOT_COVERAGE=ON"
89    "-DOT_DATASET_UPDATER=ON"
90    "-DOT_DHCP6_CLIENT=ON"
91    "-DOT_DHCP6_SERVER=ON"
92    "-DOT_DIAGNOSTIC=ON"
93    "-DOT_DNSSD_SERVER=ON"
94    "-DOT_DNS_CLIENT=ON"
95    "-DOT_ECDSA=ON"
96    "-DOT_HISTORY_TRACKER=ON"
97    "-DOT_IP6_FRAGM=ON"
98    "-DOT_JAM_DETECTION=ON"
99    "-DOT_JOINER=ON"
100    "-DOT_LOG_LEVEL_DYNAMIC=ON"
101    "-DOT_MAC_FILTER=ON"
102    "-DOT_NEIGHBOR_DISCOVERY_AGENT=ON"
103    "-DOT_NETDATA_PUBLISHER=ON"
104    "-DOT_NETDIAG_CLIENT=ON"
105    "-DOT_PING_SENDER=ON"
106    "-DOT_RCP_RESTORATION_MAX_COUNT=2"
107    "-DOT_RCP_TX_WAIT_TIME_SECS=5"
108    "-DOT_REFERENCE_DEVICE=ON"
109    "-DOT_SERVICE=ON"
110    "-DOT_SNTP_CLIENT=ON"
111    "-DOT_SRP_CLIENT=ON"
112    "-DOT_SRP_SERVER=ON"
113    "-DOT_UPTIME=ON"
114)
115readonly OT_POSIX_SIM_COMMON_OPTIONS
116
117die()
118{
119    echo " ** ERROR: Openthread CMake doesn't support platform \"$1\""
120    exit 1
121}
122
123build()
124{
125    local platform=$1
126    local builddir="${OT_CMAKE_BUILD_DIR:-build/${platform}}"
127    shift
128
129    mkdir -p "${builddir}"
130    cd "${builddir}"
131
132    cmake -GNinja -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DOT_COMPILE_WARNING_AS_ERROR=ON "$@" "${OT_SRCDIR}"
133    if [[ -z ${OT_CMAKE_NINJA_TARGET// /} ]]; then
134        ninja
135    else
136        IFS=' ' read -r -a OT_CMAKE_NINJA_TARGET <<<"${OT_CMAKE_NINJA_TARGET}"
137        ninja "${OT_CMAKE_NINJA_TARGET[@]}"
138    fi
139
140    cd "${OT_SRCDIR}"
141}
142
143main()
144{
145    if [[ $# == 0 ]]; then
146        echo "Please specify a platform: ${OT_PLATFORMS[*]}"
147        exit 1
148    fi
149
150    local platform="$1"
151    # Check if the platform supports cmake.
152    echo "${OT_PLATFORMS[@]}" | grep -wq "${platform}" || die "${platform}"
153
154    shift
155    local local_options=()
156    local options=(
157        "-DOT_SLAAC=ON"
158    )
159
160    case "${platform}" in
161        android-ndk)
162            if [ -z "${NDK-}" ]; then
163                echo "
164The 'NDK' environment variable needs to point to the Android NDK toolchain.
165Please ensure the NDK is downloaded and extracted then try to run this script again
166
167For example:
168    NDK=/opt/android-ndk-r25c ./script/cmake-build-android
169
170You can download the NDK at https://developer.android.com/ndk/downloads
171
172            "
173                exit 1
174            fi
175
176            NDK_CMAKE_TOOLCHAIN_FILE="${NDK?}/build/cmake/android.toolchain.cmake"
177            if [ ! -f "${NDK_CMAKE_TOOLCHAIN_FILE}" ]; then
178                echo "
179Could not fild the Android NDK CMake toolchain file
180- NDK=${NDK}
181- NDK_CMAKE_TOOLCHAIN_FILE=${NDK_CMAKE_TOOLCHAIN_FILE}
182
183            "
184                exit 2
185            fi
186            local_options+=(
187                "-DOT_LOG_OUTPUT=PLATFORM_DEFINED"
188
189                # Add Android NDK flags
190                "-DOT_ANDROID_NDK=1"
191                "-DCMAKE_TOOLCHAIN_FILE=${NDK?}/build/cmake/android.toolchain.cmake"
192
193                # Android API needs to be >= android-24 for `getifsaddrs()`
194                "-DANDROID_PLATFORM=android-24"
195
196                # Store thread settings in the CWD when executing ot-cli or ot-daemon
197                '-DOT_POSIX_SETTINGS_PATH="./thread"'
198            )
199
200            # Rewrite platform to posix
201            platform="posix"
202
203            # Check if OT_DAEMON or OT_APP_CLI flags are needed
204            if [[ ${OT_CMAKE_NINJA_TARGET[*]} =~ "ot-daemon" ]] || [[ ${OT_CMAKE_NINJA_TARGET[*]} =~ "ot-ctl" ]]; then
205                local_options+=("-DOT_DAEMON=ON")
206            elif [[ ${OT_CMAKE_NINJA_TARGET[*]} =~ "ot-cli" ]]; then
207                local_options+=("-DOT_APP_CLI=ON")
208            fi
209
210            options+=("${local_options[@]}")
211            ;;
212
213        posix)
214            local_options+=(
215                "-DOT_TCP=OFF"
216                "-DOT_LOG_OUTPUT=PLATFORM_DEFINED"
217                "-DOT_POSIX_MAX_POWER_TABLE=ON"
218            )
219            options+=("${OT_POSIX_SIM_COMMON_OPTIONS[@]}" "${local_options[@]}")
220            ;;
221        simulation)
222            local_options+=(
223                "-DOT_LINK_RAW=ON"
224                "-DOT_DNS_DSO=ON"
225                "-DOT_DNS_CLIENT_OVER_TCP=ON"
226                "-DOT_UDP_FORWARD=ON"
227            )
228            options+=("${OT_POSIX_SIM_COMMON_OPTIONS[@]}" "${local_options[@]}")
229            ;;
230        *)
231            options+=("-DCMAKE_TOOLCHAIN_FILE=examples/platforms/${platform}/arm-none-eabi.cmake")
232            ;;
233    esac
234
235    options+=(
236        "-DOT_PLATFORM=${platform}"
237    )
238    options+=("$@")
239    build "${platform}" "${options[@]}"
240}
241
242main "$@"
243