xref: /aosp_15_r20/external/ot-br-posix/tests/scripts/bootstrap.sh (revision 4a64e381480ef79f0532b2421e44e6ee336b8e0d)
1#!/bin/bash
2#
3#  Copyright (c) 2017, 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 -euxo pipefail
31
32TOOLS_HOME="$HOME"/.cache/tools
33[[ -d $TOOLS_HOME ]] || mkdir -p "$TOOLS_HOME"
34
35MDNSRESPONDER_PATCH_PATH=$(realpath "$(dirname "$0")"/../../third_party/mDNSResponder)
36
37disable_install_recommends()
38{
39    OTBR_APT_CONF_FILE=/etc/apt/apt.conf
40
41    if [[ -f ${OTBR_APT_CONF_FILE} ]] && grep Install-Recommends "${OTBR_APT_CONF_FILE}"; then
42        return 0
43    fi
44
45    sudo tee -a /etc/apt/apt.conf <<EOF
46APT::Get::Install-Recommends "false";
47APT::Get::Install-Suggests "false";
48EOF
49}
50
51install_common_dependencies()
52{
53    # Common dependencies
54    sudo apt-get install --no-install-recommends -y \
55        libdbus-1-dev \
56        ninja-build \
57        doxygen \
58        expect \
59        net-tools \
60        libboost-dev \
61        libboost-filesystem-dev \
62        libboost-system-dev \
63        libavahi-common-dev \
64        libavahi-client-dev \
65        libreadline-dev \
66        libncurses-dev \
67        libjsoncpp-dev \
68        coreutils \
69        git \
70        libprotobuf-dev \
71        protobuf-compiler
72}
73
74install_openthread_binraries()
75{
76    pip3 install -U pip
77    pip3 install -U cmake
78    cd third_party/openthread/repo
79    mkdir -p build && cd build
80
81    cmake .. -GNinja -DOT_PLATFORM=simulation -DOT_FULL_LOGS=1 -DOT_COMMISSIONER=ON -DOT_JOINER=ON
82    ninja
83    sudo ninja install
84
85    sudo apt-get install --no-install-recommends -y socat
86}
87
88configure_network()
89{
90    echo 0 | sudo tee /proc/sys/net/ipv6/conf/all/disable_ipv6
91    echo 1 | sudo tee /proc/sys/net/ipv6/conf/all/forwarding
92    echo 1 | sudo tee /proc/sys/net/ipv4/conf/all/forwarding
93}
94
95case "$(uname)" in
96    "Linux")
97        disable_install_recommends
98        sudo apt-get update
99        install_common_dependencies
100
101        if [ "$BUILD_TARGET" == script-check ] || [ "$BUILD_TARGET" == docker-check ]; then
102            install_openthread_binraries
103            configure_network
104            exit 0
105        fi
106
107        if [ "$BUILD_TARGET" == otbr-dbus-check ]; then
108            install_openthread_binraries
109            configure_network
110            install_common_dependencies
111            exit 0
112        fi
113
114        if [ "$BUILD_TARGET" == check ] || [ "$BUILD_TARGET" == meshcop ]; then
115            install_openthread_binraries
116            sudo apt-get install --no-install-recommends -y avahi-daemon avahi-utils
117            configure_network
118        fi
119
120        if [ "$BUILD_TARGET" == scan-build ]; then
121            pip3 install -U cmake
122            sudo apt-get install --no-install-recommends -y clang clang-tools
123        fi
124
125        if [ "$BUILD_TARGET" == pretty-check ]; then
126            sudo apt-get install -y clang-format-14 shellcheck
127            sudo snap install shfmt
128        fi
129
130        if [ "${OTBR_MDNS-}" == 'mDNSResponder' ]; then
131            SOURCE_NAME=mDNSResponder-1790.80.10
132            wget https://github.com/apple-oss-distributions/mDNSResponder/archive/refs/tags/$SOURCE_NAME.tar.gz \
133                && mkdir -p $SOURCE_NAME \
134                && tar xvf $SOURCE_NAME.tar.gz -C $SOURCE_NAME --strip-components=1 \
135                && cd "$SOURCE_NAME" \
136                && (
137                    for patch in "$MDNSRESPONDER_PATCH_PATH"/*.patch; do
138                        patch -p1 <"$patch"
139                    done
140                ) \
141                && cd mDNSPosix \
142                && make os=linux tls=no && sudo make install os=linux tls=no
143        fi
144
145        # Enable IPv6
146        if [ "$BUILD_TARGET" == check ]; then
147            echo 0 | sudo tee /proc/sys/net/ipv6/conf/all/disable_ipv6
148        fi
149
150        # Allow access syslog file for unit test
151        if [ "$BUILD_TARGET" == check ]; then
152            sudo chmod a+r /var/log/syslog
153        fi
154
155        # Prepare Raspbian image
156        if [ "$BUILD_TARGET" == raspbian-gcc ]; then
157            sudo apt-get install --no-install-recommends --allow-unauthenticated -y qemu qemu-user-static binfmt-support parted
158
159            (mkdir -p docker-rpi-emu \
160                && cd docker-rpi-emu \
161                && (git --git-dir=.git rev-parse --is-inside-work-tree || git --git-dir=.git init .) \
162                && git fetch --depth 1 https://github.com/ryankurte/docker-rpi-emu.git master \
163                && git checkout FETCH_HEAD)
164
165            pip3 install git-archive-all
166
167            IMAGE_NAME=$(basename "${IMAGE_URL}" .zip)
168            IMAGE_FILE="$IMAGE_NAME".img
169            [ -f "$TOOLS_HOME"/images/"$IMAGE_FILE" ] || {
170                # unit MB
171                EXPAND_SIZE=1024
172
173                [ -d "$TOOLS_HOME"/images ] || mkdir -p "$TOOLS_HOME"/images
174
175                [[ -f "$IMAGE_NAME".zip ]] || curl -LO "$IMAGE_URL"
176
177                unzip "$IMAGE_NAME".zip -d /tmp
178
179                (cd /tmp \
180                    && dd if=/dev/zero bs=1048576 count="$EXPAND_SIZE" >>"$IMAGE_FILE" \
181                    && mv "$IMAGE_FILE" "$TOOLS_HOME"/images/"$IMAGE_FILE")
182
183                (cd docker-rpi-emu/scripts \
184                    && sudo ./expand.sh "$TOOLS_HOME"/images/"$IMAGE_FILE" "$EXPAND_SIZE")
185            }
186        fi
187        ;;
188
189    "Darwin")
190        /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
191        ;;
192
193    *)
194        echo "Unknown os type"
195        exit 1
196        ;;
197esac
198