• 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#
49#  Compile with the specified build directory:
50#
51#      OT_CMAKE_BUILD_DIR="./build/temp"  script/cmake-build ${platform}
52#
53#  Examples:
54#
55#      script/cmake-build simulation
56#
57#      script/cmake-build simulation -DOT_FULL_LOGS=ON -DOT_CHANNEL_MANAGER=OFF
58#
59#      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
60#
61
62set -euxo pipefail
63
64OT_CMAKE_NINJA_TARGET=${OT_CMAKE_NINJA_TARGET:-}
65
66OT_SRCDIR="$(cd "$(dirname "$0")"/.. && pwd)"
67
68readonly OT_SRCDIR
69readonly OT_PLATFORMS=(cc2538 simulation posix)
70readonly OT_POSIX_SIM_COMMON_OPTIONS=(
71    "-DOT_ANYCAST_LOCATOR=ON"
72    "-DOT_BORDER_AGENT=ON"
73    "-DOT_BORDER_ROUTER=ON"
74    "-DOT_COAP=ON"
75    "-DOT_COAP_BLOCK=ON"
76    "-DOT_COAP_OBSERVE=ON"
77    "-DOT_COAPS=ON"
78    "-DOT_COMMISSIONER=ON"
79    "-DOT_CHANNEL_MANAGER=ON"
80    "-DOT_CHANNEL_MONITOR=ON"
81    "-DOT_CHILD_SUPERVISION=ON"
82    "-DOT_DATASET_UPDATER=ON"
83    "-DOT_DHCP6_CLIENT=ON"
84    "-DOT_DHCP6_SERVER=ON"
85    "-DOT_DIAGNOSTIC=ON"
86    "-DOT_DNS_CLIENT=ON"
87    "-DOT_ECDSA=ON"
88    "-DOT_HISTORY_TRACKER=ON"
89    "-DOT_IP6_FRAGM=ON"
90    "-DOT_JAM_DETECTION=ON"
91    "-DOT_JOINER=ON"
92    "-DOT_LEGACY=ON"
93    "-DOT_MAC_FILTER=ON"
94    "-DOT_MTD_NETDIAG=ON"
95    "-DOT_NEIGHBOR_DISCOVERY_AGENT=ON"
96    "-DOT_NETDATA_PUBLISHER=ON"
97    "-DOT_PING_SENDER=ON"
98    "-DOT_REFERENCE_DEVICE=ON"
99    "-DOT_SERVICE=ON"
100    "-DOT_SNTP_CLIENT=ON"
101    "-DOT_SRP_CLIENT=ON"
102    "-DOT_COVERAGE=ON"
103    "-DOT_LOG_LEVEL_DYNAMIC=ON"
104    "-DOT_COMPILE_WARNING_AS_ERROR=ON"
105    "-DOT_RCP_RESTORATION_MAX_COUNT=2"
106    "-DOT_UPTIME=ON"
107)
108
109die()
110{
111    echo " ** ERROR: Openthread CMake doesn't support platform \"$1\""
112    exit 1
113}
114
115build()
116{
117    local platform=$1
118    local builddir="${OT_CMAKE_BUILD_DIR:-build/${platform}}"
119    shift
120
121    mkdir -p "${builddir}"
122    cd "${builddir}"
123
124    cmake -GNinja -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DOT_COMPILE_WARNING_AS_ERROR=ON "$@" "${OT_SRCDIR}"
125
126    if [[ -n ${OT_CMAKE_NINJA_TARGET[*]} ]]; then
127        ninja "${OT_CMAKE_NINJA_TARGET[@]}"
128    else
129        ninja
130    fi
131
132    cd "${OT_SRCDIR}"
133}
134
135main()
136{
137    if [[ $# == 0 ]]; then
138        echo "Please specify a platform: ${OT_PLATFORMS[*]}"
139        exit 1
140    fi
141
142    local platform="$1"
143    # Check if the platform supports cmake.
144    echo "${OT_PLATFORMS[@]}" | grep -wq "${platform}" || die "${platform}"
145
146    shift
147    local local_options=()
148    local options=(
149        "-DOT_PLATFORM=${platform}"
150        "-DOT_SLAAC=ON"
151    )
152
153    case "${platform}" in
154        posix)
155            local_options+=(
156                "-DOT_LOG_OUTPUT=PLATFORM_DEFINED"
157                "-DOT_POSIX_MAX_POWER_TABLE=ON"
158            )
159            options+=("${OT_POSIX_SIM_COMMON_OPTIONS[@]}" "${local_options[@]}")
160            ;;
161        simulation)
162            local_options=("-DOT_LINK_RAW=ON")
163            options+=("${OT_POSIX_SIM_COMMON_OPTIONS[@]}" "${local_options[@]}")
164            ;;
165        cc2538)
166            options+=("-DCMAKE_TOOLCHAIN_FILE=examples/platforms/${platform}/arm-none-eabi.cmake" "-DCMAKE_BUILD_TYPE=MinSizeRel")
167            ;;
168        *)
169            options+=("-DCMAKE_TOOLCHAIN_FILE=examples/platforms/${platform}/arm-none-eabi.cmake")
170            ;;
171    esac
172
173    options+=("$@")
174    build "${platform}" "${options[@]}"
175}
176
177main "$@"
178