xref: /aosp_15_r20/external/ot-br-posix/tests/scripts/bootstrap.sh (revision 4a64e381480ef79f0532b2421e44e6ee336b8e0d)
1*4a64e381SAndroid Build Coastguard Worker#!/bin/bash
2*4a64e381SAndroid Build Coastguard Worker#
3*4a64e381SAndroid Build Coastguard Worker#  Copyright (c) 2017, The OpenThread Authors.
4*4a64e381SAndroid Build Coastguard Worker#  All rights reserved.
5*4a64e381SAndroid Build Coastguard Worker#
6*4a64e381SAndroid Build Coastguard Worker#  Redistribution and use in source and binary forms, with or without
7*4a64e381SAndroid Build Coastguard Worker#  modification, are permitted provided that the following conditions are met:
8*4a64e381SAndroid Build Coastguard Worker#  1. Redistributions of source code must retain the above copyright
9*4a64e381SAndroid Build Coastguard Worker#     notice, this list of conditions and the following disclaimer.
10*4a64e381SAndroid Build Coastguard Worker#  2. Redistributions in binary form must reproduce the above copyright
11*4a64e381SAndroid Build Coastguard Worker#     notice, this list of conditions and the following disclaimer in the
12*4a64e381SAndroid Build Coastguard Worker#     documentation and/or other materials provided with the distribution.
13*4a64e381SAndroid Build Coastguard Worker#  3. Neither the name of the copyright holder nor the
14*4a64e381SAndroid Build Coastguard Worker#     names of its contributors may be used to endorse or promote products
15*4a64e381SAndroid Build Coastguard Worker#     derived from this software without specific prior written permission.
16*4a64e381SAndroid Build Coastguard Worker#
17*4a64e381SAndroid Build Coastguard Worker#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18*4a64e381SAndroid Build Coastguard Worker#  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19*4a64e381SAndroid Build Coastguard Worker#  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20*4a64e381SAndroid Build Coastguard Worker#  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21*4a64e381SAndroid Build Coastguard Worker#  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22*4a64e381SAndroid Build Coastguard Worker#  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23*4a64e381SAndroid Build Coastguard Worker#  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24*4a64e381SAndroid Build Coastguard Worker#  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25*4a64e381SAndroid Build Coastguard Worker#  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26*4a64e381SAndroid Build Coastguard Worker#  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27*4a64e381SAndroid Build Coastguard Worker#  POSSIBILITY OF SUCH DAMAGE.
28*4a64e381SAndroid Build Coastguard Worker#
29*4a64e381SAndroid Build Coastguard Worker
30*4a64e381SAndroid Build Coastguard Workerset -euxo pipefail
31*4a64e381SAndroid Build Coastguard Worker
32*4a64e381SAndroid Build Coastguard WorkerTOOLS_HOME="$HOME"/.cache/tools
33*4a64e381SAndroid Build Coastguard Worker[[ -d $TOOLS_HOME ]] || mkdir -p "$TOOLS_HOME"
34*4a64e381SAndroid Build Coastguard Worker
35*4a64e381SAndroid Build Coastguard WorkerMDNSRESPONDER_PATCH_PATH=$(realpath "$(dirname "$0")"/../../third_party/mDNSResponder)
36*4a64e381SAndroid Build Coastguard Worker
37*4a64e381SAndroid Build Coastguard Workerdisable_install_recommends()
38*4a64e381SAndroid Build Coastguard Worker{
39*4a64e381SAndroid Build Coastguard Worker    OTBR_APT_CONF_FILE=/etc/apt/apt.conf
40*4a64e381SAndroid Build Coastguard Worker
41*4a64e381SAndroid Build Coastguard Worker    if [[ -f ${OTBR_APT_CONF_FILE} ]] && grep Install-Recommends "${OTBR_APT_CONF_FILE}"; then
42*4a64e381SAndroid Build Coastguard Worker        return 0
43*4a64e381SAndroid Build Coastguard Worker    fi
44*4a64e381SAndroid Build Coastguard Worker
45*4a64e381SAndroid Build Coastguard Worker    sudo tee -a /etc/apt/apt.conf <<EOF
46*4a64e381SAndroid Build Coastguard WorkerAPT::Get::Install-Recommends "false";
47*4a64e381SAndroid Build Coastguard WorkerAPT::Get::Install-Suggests "false";
48*4a64e381SAndroid Build Coastguard WorkerEOF
49*4a64e381SAndroid Build Coastguard Worker}
50*4a64e381SAndroid Build Coastguard Worker
51*4a64e381SAndroid Build Coastguard Workerinstall_common_dependencies()
52*4a64e381SAndroid Build Coastguard Worker{
53*4a64e381SAndroid Build Coastguard Worker    # Common dependencies
54*4a64e381SAndroid Build Coastguard Worker    sudo apt-get install --no-install-recommends -y \
55*4a64e381SAndroid Build Coastguard Worker        libdbus-1-dev \
56*4a64e381SAndroid Build Coastguard Worker        ninja-build \
57*4a64e381SAndroid Build Coastguard Worker        doxygen \
58*4a64e381SAndroid Build Coastguard Worker        expect \
59*4a64e381SAndroid Build Coastguard Worker        net-tools \
60*4a64e381SAndroid Build Coastguard Worker        libboost-dev \
61*4a64e381SAndroid Build Coastguard Worker        libboost-filesystem-dev \
62*4a64e381SAndroid Build Coastguard Worker        libboost-system-dev \
63*4a64e381SAndroid Build Coastguard Worker        libavahi-common-dev \
64*4a64e381SAndroid Build Coastguard Worker        libavahi-client-dev \
65*4a64e381SAndroid Build Coastguard Worker        libreadline-dev \
66*4a64e381SAndroid Build Coastguard Worker        libncurses-dev \
67*4a64e381SAndroid Build Coastguard Worker        libjsoncpp-dev \
68*4a64e381SAndroid Build Coastguard Worker        coreutils \
69*4a64e381SAndroid Build Coastguard Worker        git \
70*4a64e381SAndroid Build Coastguard Worker        libprotobuf-dev \
71*4a64e381SAndroid Build Coastguard Worker        protobuf-compiler
72*4a64e381SAndroid Build Coastguard Worker}
73*4a64e381SAndroid Build Coastguard Worker
74*4a64e381SAndroid Build Coastguard Workerinstall_openthread_binraries()
75*4a64e381SAndroid Build Coastguard Worker{
76*4a64e381SAndroid Build Coastguard Worker    pip3 install -U pip
77*4a64e381SAndroid Build Coastguard Worker    pip3 install -U cmake
78*4a64e381SAndroid Build Coastguard Worker    cd third_party/openthread/repo
79*4a64e381SAndroid Build Coastguard Worker    mkdir -p build && cd build
80*4a64e381SAndroid Build Coastguard Worker
81*4a64e381SAndroid Build Coastguard Worker    cmake .. -GNinja -DOT_PLATFORM=simulation -DOT_FULL_LOGS=1 -DOT_COMMISSIONER=ON -DOT_JOINER=ON
82*4a64e381SAndroid Build Coastguard Worker    ninja
83*4a64e381SAndroid Build Coastguard Worker    sudo ninja install
84*4a64e381SAndroid Build Coastguard Worker
85*4a64e381SAndroid Build Coastguard Worker    sudo apt-get install --no-install-recommends -y socat
86*4a64e381SAndroid Build Coastguard Worker}
87*4a64e381SAndroid Build Coastguard Worker
88*4a64e381SAndroid Build Coastguard Workerconfigure_network()
89*4a64e381SAndroid Build Coastguard Worker{
90*4a64e381SAndroid Build Coastguard Worker    echo 0 | sudo tee /proc/sys/net/ipv6/conf/all/disable_ipv6
91*4a64e381SAndroid Build Coastguard Worker    echo 1 | sudo tee /proc/sys/net/ipv6/conf/all/forwarding
92*4a64e381SAndroid Build Coastguard Worker    echo 1 | sudo tee /proc/sys/net/ipv4/conf/all/forwarding
93*4a64e381SAndroid Build Coastguard Worker}
94*4a64e381SAndroid Build Coastguard Worker
95*4a64e381SAndroid Build Coastguard Workercase "$(uname)" in
96*4a64e381SAndroid Build Coastguard Worker    "Linux")
97*4a64e381SAndroid Build Coastguard Worker        disable_install_recommends
98*4a64e381SAndroid Build Coastguard Worker        sudo apt-get update
99*4a64e381SAndroid Build Coastguard Worker        install_common_dependencies
100*4a64e381SAndroid Build Coastguard Worker
101*4a64e381SAndroid Build Coastguard Worker        if [ "$BUILD_TARGET" == script-check ] || [ "$BUILD_TARGET" == docker-check ]; then
102*4a64e381SAndroid Build Coastguard Worker            install_openthread_binraries
103*4a64e381SAndroid Build Coastguard Worker            configure_network
104*4a64e381SAndroid Build Coastguard Worker            exit 0
105*4a64e381SAndroid Build Coastguard Worker        fi
106*4a64e381SAndroid Build Coastguard Worker
107*4a64e381SAndroid Build Coastguard Worker        if [ "$BUILD_TARGET" == otbr-dbus-check ]; then
108*4a64e381SAndroid Build Coastguard Worker            install_openthread_binraries
109*4a64e381SAndroid Build Coastguard Worker            configure_network
110*4a64e381SAndroid Build Coastguard Worker            install_common_dependencies
111*4a64e381SAndroid Build Coastguard Worker            exit 0
112*4a64e381SAndroid Build Coastguard Worker        fi
113*4a64e381SAndroid Build Coastguard Worker
114*4a64e381SAndroid Build Coastguard Worker        if [ "$BUILD_TARGET" == check ] || [ "$BUILD_TARGET" == meshcop ]; then
115*4a64e381SAndroid Build Coastguard Worker            install_openthread_binraries
116*4a64e381SAndroid Build Coastguard Worker            sudo apt-get install --no-install-recommends -y avahi-daemon avahi-utils
117*4a64e381SAndroid Build Coastguard Worker            configure_network
118*4a64e381SAndroid Build Coastguard Worker        fi
119*4a64e381SAndroid Build Coastguard Worker
120*4a64e381SAndroid Build Coastguard Worker        if [ "$BUILD_TARGET" == scan-build ]; then
121*4a64e381SAndroid Build Coastguard Worker            pip3 install -U cmake
122*4a64e381SAndroid Build Coastguard Worker            sudo apt-get install --no-install-recommends -y clang clang-tools
123*4a64e381SAndroid Build Coastguard Worker        fi
124*4a64e381SAndroid Build Coastguard Worker
125*4a64e381SAndroid Build Coastguard Worker        if [ "$BUILD_TARGET" == pretty-check ]; then
126*4a64e381SAndroid Build Coastguard Worker            sudo apt-get install -y clang-format-14 shellcheck
127*4a64e381SAndroid Build Coastguard Worker            sudo snap install shfmt
128*4a64e381SAndroid Build Coastguard Worker        fi
129*4a64e381SAndroid Build Coastguard Worker
130*4a64e381SAndroid Build Coastguard Worker        if [ "${OTBR_MDNS-}" == 'mDNSResponder' ]; then
131*4a64e381SAndroid Build Coastguard Worker            SOURCE_NAME=mDNSResponder-1790.80.10
132*4a64e381SAndroid Build Coastguard Worker            wget https://github.com/apple-oss-distributions/mDNSResponder/archive/refs/tags/$SOURCE_NAME.tar.gz \
133*4a64e381SAndroid Build Coastguard Worker                && mkdir -p $SOURCE_NAME \
134*4a64e381SAndroid Build Coastguard Worker                && tar xvf $SOURCE_NAME.tar.gz -C $SOURCE_NAME --strip-components=1 \
135*4a64e381SAndroid Build Coastguard Worker                && cd "$SOURCE_NAME" \
136*4a64e381SAndroid Build Coastguard Worker                && (
137*4a64e381SAndroid Build Coastguard Worker                    for patch in "$MDNSRESPONDER_PATCH_PATH"/*.patch; do
138*4a64e381SAndroid Build Coastguard Worker                        patch -p1 <"$patch"
139*4a64e381SAndroid Build Coastguard Worker                    done
140*4a64e381SAndroid Build Coastguard Worker                ) \
141*4a64e381SAndroid Build Coastguard Worker                && cd mDNSPosix \
142*4a64e381SAndroid Build Coastguard Worker                && make os=linux tls=no && sudo make install os=linux tls=no
143*4a64e381SAndroid Build Coastguard Worker        fi
144*4a64e381SAndroid Build Coastguard Worker
145*4a64e381SAndroid Build Coastguard Worker        # Enable IPv6
146*4a64e381SAndroid Build Coastguard Worker        if [ "$BUILD_TARGET" == check ]; then
147*4a64e381SAndroid Build Coastguard Worker            echo 0 | sudo tee /proc/sys/net/ipv6/conf/all/disable_ipv6
148*4a64e381SAndroid Build Coastguard Worker        fi
149*4a64e381SAndroid Build Coastguard Worker
150*4a64e381SAndroid Build Coastguard Worker        # Allow access syslog file for unit test
151*4a64e381SAndroid Build Coastguard Worker        if [ "$BUILD_TARGET" == check ]; then
152*4a64e381SAndroid Build Coastguard Worker            sudo chmod a+r /var/log/syslog
153*4a64e381SAndroid Build Coastguard Worker        fi
154*4a64e381SAndroid Build Coastguard Worker
155*4a64e381SAndroid Build Coastguard Worker        # Prepare Raspbian image
156*4a64e381SAndroid Build Coastguard Worker        if [ "$BUILD_TARGET" == raspbian-gcc ]; then
157*4a64e381SAndroid Build Coastguard Worker            sudo apt-get install --no-install-recommends --allow-unauthenticated -y qemu qemu-user-static binfmt-support parted
158*4a64e381SAndroid Build Coastguard Worker
159*4a64e381SAndroid Build Coastguard Worker            (mkdir -p docker-rpi-emu \
160*4a64e381SAndroid Build Coastguard Worker                && cd docker-rpi-emu \
161*4a64e381SAndroid Build Coastguard Worker                && (git --git-dir=.git rev-parse --is-inside-work-tree || git --git-dir=.git init .) \
162*4a64e381SAndroid Build Coastguard Worker                && git fetch --depth 1 https://github.com/ryankurte/docker-rpi-emu.git master \
163*4a64e381SAndroid Build Coastguard Worker                && git checkout FETCH_HEAD)
164*4a64e381SAndroid Build Coastguard Worker
165*4a64e381SAndroid Build Coastguard Worker            pip3 install git-archive-all
166*4a64e381SAndroid Build Coastguard Worker
167*4a64e381SAndroid Build Coastguard Worker            IMAGE_NAME=$(basename "${IMAGE_URL}" .zip)
168*4a64e381SAndroid Build Coastguard Worker            IMAGE_FILE="$IMAGE_NAME".img
169*4a64e381SAndroid Build Coastguard Worker            [ -f "$TOOLS_HOME"/images/"$IMAGE_FILE" ] || {
170*4a64e381SAndroid Build Coastguard Worker                # unit MB
171*4a64e381SAndroid Build Coastguard Worker                EXPAND_SIZE=1024
172*4a64e381SAndroid Build Coastguard Worker
173*4a64e381SAndroid Build Coastguard Worker                [ -d "$TOOLS_HOME"/images ] || mkdir -p "$TOOLS_HOME"/images
174*4a64e381SAndroid Build Coastguard Worker
175*4a64e381SAndroid Build Coastguard Worker                [[ -f "$IMAGE_NAME".zip ]] || curl -LO "$IMAGE_URL"
176*4a64e381SAndroid Build Coastguard Worker
177*4a64e381SAndroid Build Coastguard Worker                unzip "$IMAGE_NAME".zip -d /tmp
178*4a64e381SAndroid Build Coastguard Worker
179*4a64e381SAndroid Build Coastguard Worker                (cd /tmp \
180*4a64e381SAndroid Build Coastguard Worker                    && dd if=/dev/zero bs=1048576 count="$EXPAND_SIZE" >>"$IMAGE_FILE" \
181*4a64e381SAndroid Build Coastguard Worker                    && mv "$IMAGE_FILE" "$TOOLS_HOME"/images/"$IMAGE_FILE")
182*4a64e381SAndroid Build Coastguard Worker
183*4a64e381SAndroid Build Coastguard Worker                (cd docker-rpi-emu/scripts \
184*4a64e381SAndroid Build Coastguard Worker                    && sudo ./expand.sh "$TOOLS_HOME"/images/"$IMAGE_FILE" "$EXPAND_SIZE")
185*4a64e381SAndroid Build Coastguard Worker            }
186*4a64e381SAndroid Build Coastguard Worker        fi
187*4a64e381SAndroid Build Coastguard Worker        ;;
188*4a64e381SAndroid Build Coastguard Worker
189*4a64e381SAndroid Build Coastguard Worker    "Darwin")
190*4a64e381SAndroid Build Coastguard Worker        /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
191*4a64e381SAndroid Build Coastguard Worker        ;;
192*4a64e381SAndroid Build Coastguard Worker
193*4a64e381SAndroid Build Coastguard Worker    *)
194*4a64e381SAndroid Build Coastguard Worker        echo "Unknown os type"
195*4a64e381SAndroid Build Coastguard Worker        exit 1
196*4a64e381SAndroid Build Coastguard Worker        ;;
197*4a64e381SAndroid Build Coastguard Workeresac
198