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