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