• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/bash
2#
3#  Copyright (c) 2019, 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
30set -euo pipefail
31
32readonly OT_TMP_DIR=/tmp/ot-size-report
33readonly OT_SHA_NEW=${GITHUB_SHA:-$(git rev-parse HEAD)}
34readonly OT_SHA_OLD="$(git cat-file -p "${OT_SHA_NEW}" | grep 'parent ' | head -n1 | cut -d' ' -f2)"
35readonly OT_REPORT_FILE=/tmp/size_report
36
37setup_arm_gcc_7()
38{
39    if arm-none-eabi-gcc --version | grep -q 'Arm Embedded Processors 7'; then
40        return 0
41    fi
42
43    (cd /tmp/ \
44        && wget --tries 4 --no-check-certificate --quiet https://developer.arm.com/-/media/Files/downloads/gnu-rm/7-2018q2/gcc-arm-none-eabi-7-2018-q2-update-linux.tar.bz2 \
45        && tar xjf gcc-arm-none-eabi-7-2018-q2-update-linux.tar.bz2)
46    export PATH=/tmp/gcc-arm-none-eabi-7-2018-q2-update/bin:$PATH
47
48    arm-none-eabi-gcc --version
49}
50
51setup_ninja_build()
52{
53    sudo apt-get --no-install-recommends install -y ninja-build
54}
55
56setup()
57{
58    setup_arm_gcc_7
59    setup_ninja_build
60}
61
62markdown_init()
63{
64    echo '|  name  |  branch  |  text  | data  | bss  | total |'
65    echo '| :----: | :------: | -----: | ----: | ---: | ----: |'
66}
67
68markdown_size()
69{
70    local name
71    name=$(basename "$1")
72
73    read -r -a size_old <<<"$(size "$1" | awk '{text+=$1} {bss+=$2} {data+=$3} {total+=$4} END {printf "%d %d %d %d", text, bss, data, total}')"
74    read -r -a size_new <<<"$(size "$2" | awk '{text+=$1} {bss+=$2} {data+=$3} {total+=$4} END {printf "%d %d %d %d", text, bss, data, total}')"
75
76    local -a size_diff
77
78    for i in 0 1 2 3; do
79        size_diff[$i]="$((size_new["$i"] - size_old["$i"]))"
80        if [[ ${size_diff["$i"]} != 0 ]]; then
81            size_diff["$i"]=$(printf '%+d' "${size_diff["$i"]}")
82        fi
83    done
84
85    echo "| ${name} | -${OT_SHA_OLD:0:7} | ${size_old[0]} | ${size_old[1]} | ${size_old[2]} | ${size_old[3]} |"
86    echo "|  | +${OT_SHA_NEW:0:7} | ${size_new[0]} | ${size_new[1]} | ${size_new[2]} | ${size_new[3]} |"
87    echo "|  | +/- | ${size_diff[0]} | ${size_diff[1]} | ${size_diff[2]} | ${size_diff[3]} |"
88}
89
90markdown()
91{
92    case "$1" in
93        init)
94            shift
95            markdown_init "$@" >"${OT_REPORT_FILE}"
96            ;;
97        size)
98            shift
99            markdown_size "$@" >>"${OT_REPORT_FILE}"
100            ;;
101        post)
102            mdv "${OT_REPORT_FILE}"
103            ;;
104    esac
105}
106
107nm_size()
108{
109    arm-none-eabi-nm --print-size --defined-only -C "$1" | cut -d' ' -f2- >nmsize_old
110    arm-none-eabi-nm --print-size --defined-only -C "$2" | cut -d' ' -f2- >nmsize_new
111    diff -Nuar nmsize_old nmsize_new || true
112}
113
114size_nrf52840_version()
115{
116    local options=(
117        "-DOT_ANYCAST_LOCATOR=ON"
118        "-DOT_BORDER_AGENT=ON"
119        "-DOT_BORDER_ROUTER=ON"
120        "-DOT_CHANNEL_MANAGER=ON"
121        "-DOT_CHANNEL_MONITOR=ON"
122        "-DOT_CHILD_SUPERVISION=ON"
123        "-DOT_COAP=ON"
124        "-DOT_COAPS=ON"
125        "-DOT_COMMISSIONER=ON"
126        "-DOT_DATASET_UPDATER=ON"
127        "-DOT_DHCP6_CLIENT=ON"
128        "-DOT_DHCP6_SERVER=ON"
129        "-DOT_DIAGNOSTIC=ON"
130        "-DOT_DNSSD_SERVER=ON"
131        "-DOT_DNS_CLIENT=ON"
132        "-DOT_ECDSA=ON"
133        "-DOT_FULL_LOGS=ON"
134        "-DOT_JAM_DETECTION=ON"
135        "-DOT_JOINER=ON"
136        "-DOT_LINK_RAW=ON"
137        "-DOT_MAC_FILTER=ON"
138        "-DOT_MESSAGE_USE_HEAP=ON"
139        "-DOT_MTD_NETDIAG=ON"
140        "-DOT_NETDATA_PUBLISHER=ON"
141        "-DOT_PING_SENDER=ON"
142        "-DOT_SERVICE=ON"
143        "-DOT_SLAAC=ON"
144        "-DOT_SNTP_CLIENT=ON"
145        "-DOT_SRP_CLIENT=ON"
146        "-DOT_SRP_SERVER=ON"
147        "-DOT_TIME_SYNC=ON"
148        "-DOT_UDP_FORWARD=ON"
149        "-DOT_UPTIME=ON"
150    )
151
152    local thread_version=$1
153
154    if [[ ${thread_version} != "1.1" ]]; then
155        options+=(
156            "-DOT_THREAD_VERSION=1.3"
157            "-DOT_BACKBONE_ROUTER=ON"
158            "-DOT_DUA=ON"
159            "-DOT_MLR=ON"
160            "-DOT_CSL_RECEIVER=ON"
161            "-DOT_LINK_METRICS_INITIATOR=ON"
162            "-DOT_LINK_METRICS_SUBJECT=ON"
163        )
164    fi
165
166    rm -rf "${OT_TMP_DIR}"
167
168    local build_dir="build"
169
170    # new commit
171    mkdir -p "${OT_TMP_DIR}/b"
172    script/git-tool clone https://github.com/openthread/ot-nrf528xx.git "${OT_TMP_DIR}/b"
173    rm -rf "${OT_TMP_DIR}/b/openthread/*" # replace openthread submodule with latest commit
174    git archive "${OT_SHA_NEW}" | tar x -C "${OT_TMP_DIR}/b/openthread"
175
176    (cd "${OT_TMP_DIR}/b" \
177        && OT_CMAKE_BUILD_DIR=${build_dir} script/build nrf52840 UART_trans "${options[@]}")
178
179    # old commit
180    if [[ "${GITHUB_ACTIONS+x}" ]]; then
181        git fetch --depth 1 --no-recurse-submodules origin "${OT_SHA_OLD}"
182    fi
183
184    mkdir -p "${OT_TMP_DIR}/a"
185    git clone https://github.com/openthread/ot-nrf528xx.git "${OT_TMP_DIR}/a"
186    rm -rf "${OT_TMP_DIR}/a/openthread/*" # replace openthread submodule with last commit
187    git archive "${OT_SHA_OLD}" | tar x -C "${OT_TMP_DIR}/a/openthread"
188
189    (cd "${OT_TMP_DIR}/a" \
190        && OT_CMAKE_BUILD_DIR=${build_dir} script/build nrf52840 UART_trans "${options[@]}")
191
192    # rename the generated files to be ready for size-report
193    # shellcheck disable=SC2011
194    (
195        cd "${OT_TMP_DIR}"/a/"${build_dir}"/bin
196        ls | xargs -I{} mv {} {}_"${thread_version}"
197        cd "${OT_TMP_DIR}"/b/"${build_dir}"/bin
198        ls | xargs -I{} mv {} {}_"${thread_version}"
199
200        cd "${OT_TMP_DIR}"/a/"${build_dir}"/lib
201        ls ./*.a | xargs -I{} mv {} {}_"${thread_version}"
202        cd "${OT_TMP_DIR}"/b/"${build_dir}"/lib
203        ls ./*.a | xargs -I{} mv {} {}_"${thread_version}"
204    )
205
206    local bins=(
207        "ot-cli-ftd"
208        "ot-cli-mtd"
209        "ot-ncp-ftd"
210        "ot-ncp-mtd"
211        "ot-rcp"
212    )
213
214    local libs=(
215        "libopenthread-cli-ftd.a"
216        "libopenthread-cli-mtd.a"
217        "libopenthread-ftd.a"
218        "libopenthread-mtd.a"
219        "libopenthread-ncp-ftd.a"
220        "libopenthread-ncp-mtd.a"
221        "libopenthread-rcp.a"
222        "libopenthread-radio.a"
223    )
224
225    for file in "${bins[@]}"; do
226        "${reporter}" size "${OT_TMP_DIR}"/a/"${build_dir}"/bin/"${file}"_"${thread_version}" "${OT_TMP_DIR}"/b/"${build_dir}"/bin/"${file}"_"${thread_version}"
227        echo nm_size "${OT_TMP_DIR}"/a/"${build_dir}"/bin/"${file}"_"${thread_version}" "${OT_TMP_DIR}"/b/"${build_dir}"/bin/"${file}"_"${thread_version}"
228        nm_size "${OT_TMP_DIR}"/a/"${build_dir}"/bin/"${file}"_"${thread_version}" "${OT_TMP_DIR}"/b/"${build_dir}"/bin/"${file}"_"${thread_version}"
229    done
230
231    for file in "${libs[@]}"; do
232        "${reporter}" size "${OT_TMP_DIR}"/a/"${build_dir}"/lib/"${file}"_"${thread_version}" "${OT_TMP_DIR}"/b/"${build_dir}"/lib/"${file}"_"${thread_version}"
233        echo nm_size "${OT_TMP_DIR}"/a/"${build_dir}"/lib/"${file}"_"${thread_version}" "${OT_TMP_DIR}"/b/"${build_dir}"/lib/"${file}"_"${thread_version}"
234        nm_size "${OT_TMP_DIR}"/a/"${build_dir}"/lib/"${file}"_"${thread_version}" "${OT_TMP_DIR}"/b/"${build_dir}"/lib/"${file}"_"${thread_version}"
235    done
236}
237
238size_nrf52840()
239{
240    export OT_SHA_NEW OT_SHA_OLD
241
242    local reporter="${OT_SIZE_REPORTER:-markdown}"
243    "${reporter}" init OpenThread
244
245    size_nrf52840_version 1.1
246    size_nrf52840_version 1.3
247
248    "${reporter}" post
249}
250
251main()
252{
253    if [[ $# == 0 ]]; then
254        setup
255        size_nrf52840
256    elif [[ $1 == setup ]]; then
257        setup
258    elif [[ $1 == nrf52840 ]]; then
259        size_nrf52840
260    else
261        echo "USAGE: $0 [setup|nrf52840]"
262        exit 128
263    fi
264}
265
266main "$@"
267