• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_SRP_SERVER_FAST_START_MDOE=ON"
114    "-DOT_UPTIME=ON"
115)
116readonly OT_POSIX_SIM_COMMON_OPTIONS
117
118die()
119{
120    echo " ** ERROR: Openthread CMake doesn't support platform \"$1\""
121    exit 1
122}
123
124build()
125{
126    local platform=$1
127    local builddir="${OT_CMAKE_BUILD_DIR:-build/${platform}}"
128    shift
129
130    mkdir -p "${builddir}"
131    cd "${builddir}"
132
133    cmake -GNinja -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DOT_COMPILE_WARNING_AS_ERROR=ON "$@" "${OT_SRCDIR}"
134    if [[ -z ${OT_CMAKE_NINJA_TARGET// /} ]]; then
135        ninja
136    else
137        IFS=' ' read -r -a OT_CMAKE_NINJA_TARGET <<<"${OT_CMAKE_NINJA_TARGET}"
138        ninja "${OT_CMAKE_NINJA_TARGET[@]}"
139    fi
140
141    cd "${OT_SRCDIR}"
142}
143
144main()
145{
146    if [[ $# == 0 ]]; then
147        echo "Please specify a platform: ${OT_PLATFORMS[*]}"
148        exit 1
149    fi
150
151    local platform="$1"
152    # Check if the platform supports cmake.
153    echo "${OT_PLATFORMS[@]}" | grep -wq "${platform}" || die "${platform}"
154
155    shift
156    local local_options=()
157    local options=(
158        "-DOT_SLAAC=ON"
159    )
160
161    case "${platform}" in
162        android-ndk)
163            if [ -z "${NDK-}" ]; then
164                echo "
165The 'NDK' environment variable needs to point to the Android NDK toolchain.
166Please ensure the NDK is downloaded and extracted then try to run this script again
167
168For example:
169    NDK=/opt/android-ndk-r25c ./script/cmake-build-android
170
171You can download the NDK at https://developer.android.com/ndk/downloads
172
173            "
174                exit 1
175            fi
176
177            NDK_CMAKE_TOOLCHAIN_FILE="${NDK?}/build/cmake/android.toolchain.cmake"
178            if [ ! -f "${NDK_CMAKE_TOOLCHAIN_FILE}" ]; then
179                echo "
180Could not fild the Android NDK CMake toolchain file
181- NDK=${NDK}
182- NDK_CMAKE_TOOLCHAIN_FILE=${NDK_CMAKE_TOOLCHAIN_FILE}
183
184            "
185                exit 2
186            fi
187            local_options+=(
188                "-DOT_LOG_OUTPUT=PLATFORM_DEFINED"
189
190                # Add Android NDK flags
191                "-DOT_ANDROID_NDK=1"
192                "-DCMAKE_TOOLCHAIN_FILE=${NDK?}/build/cmake/android.toolchain.cmake"
193
194                # Android API needs to be >= android-24 for `getifsaddrs()`
195                "-DANDROID_PLATFORM=android-24"
196
197                # Store thread settings in the CWD when executing ot-cli or ot-daemon
198                '-DOT_POSIX_SETTINGS_PATH="./thread"'
199            )
200
201            # Rewrite platform to posix
202            platform="posix"
203
204            # Check if OT_DAEMON or OT_APP_CLI flags are needed
205            if [[ ${OT_CMAKE_NINJA_TARGET[*]} =~ "ot-daemon" ]] || [[ ${OT_CMAKE_NINJA_TARGET[*]} =~ "ot-ctl" ]]; then
206                local_options+=("-DOT_DAEMON=ON")
207            elif [[ ${OT_CMAKE_NINJA_TARGET[*]} =~ "ot-cli" ]]; then
208                local_options+=("-DOT_APP_CLI=ON")
209            fi
210
211            options+=("${local_options[@]}")
212            ;;
213
214        posix)
215            local_options+=(
216                "-DOT_TCP=OFF"
217                "-DOT_LOG_OUTPUT=PLATFORM_DEFINED"
218                "-DOT_POSIX_MAX_POWER_TABLE=ON"
219            )
220            options+=("${OT_POSIX_SIM_COMMON_OPTIONS[@]}" "${local_options[@]}")
221            ;;
222        simulation)
223            local_options+=(
224                "-DOT_LINK_RAW=ON"
225                "-DOT_DNS_DSO=ON"
226                "-DOT_DNS_CLIENT_OVER_TCP=ON"
227                "-DOT_UDP_FORWARD=ON"
228            )
229            options+=("${OT_POSIX_SIM_COMMON_OPTIONS[@]}" "${local_options[@]}")
230            ;;
231        *)
232            options+=("-DCMAKE_TOOLCHAIN_FILE=examples/platforms/${platform}/arm-none-eabi.cmake")
233            ;;
234    esac
235
236    options+=(
237        "-DOT_PLATFORM=${platform}"
238    )
239    options+=("$@")
240    build "${platform}" "${options[@]}"
241}
242
243main "$@"
244