1#!/usr/bin/env bash
2set -euo pipefail
3
4if [ -z "${IDF_PATH:-}" ]; then
5    echo "IDF_PATH must be set before running this script"
6    exit 1
7fi
8
9failed=""
10
11# check_lib_symbols <libraray path> <symbols to look for...>
12#
13# If the given library contains references to the listed symbols, prints
14# a message and adds the library to "failed" list.
15function check_lib_symbols {
16    lib="$1"
17    symbols="${@:2}"
18    syms_file="$(mktemp)"
19    # for symbols="foo bar" create grep search argument "foo\|bar"
20    symbols_args="${symbols// /\\|}"
21    errors=0
22    nm -A $lib 2>/dev/null | { grep -w "${symbols_args}" > ${syms_file} || true; }
23    if [ $(wc -l <${syms_file}) != 0 ]; then
24        echo "${lib}: found illegal symbol references:"
25        cat ${syms_file} | sed 's/^/\t/'
26        failed="${failed} ${lib}"
27        errors=1
28    fi
29    if [ $errors == 0 ]; then
30        echo "${lib}: OK"
31    fi
32    rm -f ${syms_file}
33}
34
35# Check Wi-Fi, PHY libraries for references to "printf"-like functions:
36illegal_symbols="printf ets_printf"
37
38pushd ${IDF_PATH}/components/esp_wifi/lib > /dev/null
39wifi_targets=$(find . -type d -name 'esp*' -exec basename {} \; | sort)
40for target in ${wifi_targets}; do
41    for library in ${target}/*.a; do
42        check_lib_symbols ${library} ${illegal_symbols}
43    done
44done
45popd > /dev/null
46
47pushd ${IDF_PATH}/components/esp_phy/lib > /dev/null
48phy_targets=$(find . -type d -name 'esp*' -exec basename {} \; | sort)
49for target in ${phy_targets}; do
50    for library in ${target}/*.a; do
51        check_lib_symbols ${library} ${illegal_symbols}
52    done
53done
54popd > /dev/null
55
56# Print summary
57if [ -n "${failed}" ]; then
58    echo "Issues found in the following libraries:"
59    for lib in $failed; do
60        echo "- $lib"
61    done
62    exit 1
63fi
64