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