xref: /aosp_15_r20/external/openthread/tools/harness-simulation/posix/install.sh (revision cfb92d1480a9e65faed56933e9c12405f45898b4)
1*cfb92d14SAndroid Build Coastguard Worker#!/bin/bash
2*cfb92d14SAndroid Build Coastguard Worker#
3*cfb92d14SAndroid Build Coastguard Worker# Copyright (c) 2022, The OpenThread Authors.
4*cfb92d14SAndroid Build Coastguard Worker# All rights reserved.
5*cfb92d14SAndroid Build Coastguard Worker#
6*cfb92d14SAndroid Build Coastguard Worker# Redistribution and use in source and binary forms, with or without
7*cfb92d14SAndroid Build Coastguard Worker# modification, are permitted provided that the following conditions are met:
8*cfb92d14SAndroid Build Coastguard Worker# 1. Redistributions of source code must retain the above copyright
9*cfb92d14SAndroid Build Coastguard Worker#    notice, this list of conditions and the following disclaimer.
10*cfb92d14SAndroid Build Coastguard Worker# 2. Redistributions in binary form must reproduce the above copyright
11*cfb92d14SAndroid Build Coastguard Worker#    notice, this list of conditions and the following disclaimer in the
12*cfb92d14SAndroid Build Coastguard Worker#    documentation and/or other materials provided with the distribution.
13*cfb92d14SAndroid Build Coastguard Worker# 3. Neither the name of the copyright holder nor the
14*cfb92d14SAndroid Build Coastguard Worker#    names of its contributors may be used to endorse or promote products
15*cfb92d14SAndroid Build Coastguard Worker#    derived from this software without specific prior written permission.
16*cfb92d14SAndroid Build Coastguard Worker#
17*cfb92d14SAndroid Build Coastguard Worker# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18*cfb92d14SAndroid Build Coastguard Worker# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19*cfb92d14SAndroid Build Coastguard Worker# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20*cfb92d14SAndroid Build Coastguard Worker# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21*cfb92d14SAndroid Build Coastguard Worker# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22*cfb92d14SAndroid Build Coastguard Worker# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23*cfb92d14SAndroid Build Coastguard Worker# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24*cfb92d14SAndroid Build Coastguard Worker# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25*cfb92d14SAndroid Build Coastguard Worker# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26*cfb92d14SAndroid Build Coastguard Worker# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27*cfb92d14SAndroid Build Coastguard Worker# POSSIBILITY OF SUCH DAMAGE.
28*cfb92d14SAndroid Build Coastguard Worker#
29*cfb92d14SAndroid Build Coastguard Worker
30*cfb92d14SAndroid Build Coastguard Workerset -euxo pipefail
31*cfb92d14SAndroid Build Coastguard Worker
32*cfb92d14SAndroid Build Coastguard WorkerPOSIX_DIR="$(cd "$(dirname "$0")" && pwd)"
33*cfb92d14SAndroid Build Coastguard WorkerOT_DIR="${POSIX_DIR}/../../.."
34*cfb92d14SAndroid Build Coastguard WorkerETC_DIR="${POSIX_DIR}/etc"
35*cfb92d14SAndroid Build Coastguard WorkerSNIFFER_DIR="${POSIX_DIR}/sniffer_sim"
36*cfb92d14SAndroid Build Coastguard Worker
37*cfb92d14SAndroid Build Coastguard WorkerPACKAGES=(
38*cfb92d14SAndroid Build Coastguard Worker    "docker.io"
39*cfb92d14SAndroid Build Coastguard Worker    "git"
40*cfb92d14SAndroid Build Coastguard Worker    "jq"
41*cfb92d14SAndroid Build Coastguard Worker    "socat"
42*cfb92d14SAndroid Build Coastguard Worker    "tshark"
43*cfb92d14SAndroid Build Coastguard Worker)
44*cfb92d14SAndroid Build Coastguard Worker
45*cfb92d14SAndroid Build Coastguard Workersudo apt install -y "${PACKAGES[@]}"
46*cfb92d14SAndroid Build Coastguard Worker
47*cfb92d14SAndroid Build Coastguard Workerpip3 install -r "${POSIX_DIR}/requirements.txt"
48*cfb92d14SAndroid Build Coastguard Workerpython3 -m grpc_tools.protoc -I"${SNIFFER_DIR}" --python_out="${SNIFFER_DIR}" --grpc_python_out="${SNIFFER_DIR}" proto/sniffer.proto
49*cfb92d14SAndroid Build Coastguard Worker
50*cfb92d14SAndroid Build Coastguard WorkerCONFIG_NAME=${1:-"${POSIX_DIR}/config.yml"}
51*cfb92d14SAndroid Build Coastguard Worker# convert YAML to JSON
52*cfb92d14SAndroid Build Coastguard WorkerCONFIG=$(python3 -c 'import json, sys, yaml; print(json.dumps(yaml.safe_load(open(sys.argv[1]))))' "$CONFIG_NAME")
53*cfb92d14SAndroid Build Coastguard Worker
54*cfb92d14SAndroid Build Coastguard WorkerMAX_NETWORK_SIZE=$(jq -r '.ot_build.max_number' <<<"$CONFIG")
55*cfb92d14SAndroid Build Coastguard Worker
56*cfb92d14SAndroid Build Coastguard Workerbuild_ot()
57*cfb92d14SAndroid Build Coastguard Worker{
58*cfb92d14SAndroid Build Coastguard Worker    # SC2155: Declare and assign separately to avoid masking return values
59*cfb92d14SAndroid Build Coastguard Worker    local target build_dir cflags version options
60*cfb92d14SAndroid Build Coastguard Worker    target="ot-cli-ftd"
61*cfb92d14SAndroid Build Coastguard Worker    build_dir=$(jq -r '.subpath' <<<"$1")
62*cfb92d14SAndroid Build Coastguard Worker    cflags=$(jq -r '.cflags | join(" ")' <<<"$1")
63*cfb92d14SAndroid Build Coastguard Worker    version=$(jq -r '.version' <<<"$1")
64*cfb92d14SAndroid Build Coastguard Worker    options=$(jq -r '.options | join(" ")' <<<"$1")
65*cfb92d14SAndroid Build Coastguard Worker    # Intended splitting of options
66*cfb92d14SAndroid Build Coastguard Worker    read -ra options <<<"$options"
67*cfb92d14SAndroid Build Coastguard Worker
68*cfb92d14SAndroid Build Coastguard Worker    (
69*cfb92d14SAndroid Build Coastguard Worker        cd "$OT_DIR"
70*cfb92d14SAndroid Build Coastguard Worker
71*cfb92d14SAndroid Build Coastguard Worker        OT_CMAKE_NINJA_TARGET="$target" \
72*cfb92d14SAndroid Build Coastguard Worker            OT_CMAKE_BUILD_DIR="$build_dir" \
73*cfb92d14SAndroid Build Coastguard Worker            CFLAGS="$cflags" \
74*cfb92d14SAndroid Build Coastguard Worker            CXXFLAGS="$cflags" \
75*cfb92d14SAndroid Build Coastguard Worker            script/cmake-build \
76*cfb92d14SAndroid Build Coastguard Worker            simulation \
77*cfb92d14SAndroid Build Coastguard Worker            "${options[@]}" \
78*cfb92d14SAndroid Build Coastguard Worker            -DOT_THREAD_VERSION="$version" \
79*cfb92d14SAndroid Build Coastguard Worker            -DOT_SIMULATION_MAX_NETWORK_SIZE="$MAX_NETWORK_SIZE"
80*cfb92d14SAndroid Build Coastguard Worker    )
81*cfb92d14SAndroid Build Coastguard Worker}
82*cfb92d14SAndroid Build Coastguard Worker
83*cfb92d14SAndroid Build Coastguard Workerbuild_otbr()
84*cfb92d14SAndroid Build Coastguard Worker{
85*cfb92d14SAndroid Build Coastguard Worker    # SC2155: Declare and assign separately to avoid masking return values
86*cfb92d14SAndroid Build Coastguard Worker    local target build_dir version rcp_options
87*cfb92d14SAndroid Build Coastguard Worker    target="ot-rcp"
88*cfb92d14SAndroid Build Coastguard Worker    build_dir=$(jq -r '.rcp_subpath' <<<"$1")
89*cfb92d14SAndroid Build Coastguard Worker    version=$(jq -r '.version' <<<"$1")
90*cfb92d14SAndroid Build Coastguard Worker    rcp_options=$(jq -r '.rcp_options | join(" ")' <<<"$1")
91*cfb92d14SAndroid Build Coastguard Worker    # Intended splitting of rcp_options
92*cfb92d14SAndroid Build Coastguard Worker    read -ra rcp_options <<<"$rcp_options"
93*cfb92d14SAndroid Build Coastguard Worker
94*cfb92d14SAndroid Build Coastguard Worker    (
95*cfb92d14SAndroid Build Coastguard Worker        cd "$OT_DIR"
96*cfb92d14SAndroid Build Coastguard Worker
97*cfb92d14SAndroid Build Coastguard Worker        OT_CMAKE_NINJA_TARGET="$target" \
98*cfb92d14SAndroid Build Coastguard Worker            OT_CMAKE_BUILD_DIR="$build_dir" \
99*cfb92d14SAndroid Build Coastguard Worker            script/cmake-build \
100*cfb92d14SAndroid Build Coastguard Worker            simulation \
101*cfb92d14SAndroid Build Coastguard Worker            "${rcp_options[@]}" \
102*cfb92d14SAndroid Build Coastguard Worker            -DOT_THREAD_VERSION="$version" \
103*cfb92d14SAndroid Build Coastguard Worker            -DOT_SIMULATION_MAX_NETWORK_SIZE="$MAX_NETWORK_SIZE"
104*cfb92d14SAndroid Build Coastguard Worker    )
105*cfb92d14SAndroid Build Coastguard Worker
106*cfb92d14SAndroid Build Coastguard Worker    # SC2155: Declare and assign separately to avoid masking return values
107*cfb92d14SAndroid Build Coastguard Worker    local otbr_docker_image build_args options
108*cfb92d14SAndroid Build Coastguard Worker    otbr_docker_image=$(jq -r '.docker_image' <<<"$1")
109*cfb92d14SAndroid Build Coastguard Worker    build_args=$(jq -r '.build_args | map("--build-arg " + .) | join(" ")' <<<"$1")
110*cfb92d14SAndroid Build Coastguard Worker    # Intended splitting of build_args
111*cfb92d14SAndroid Build Coastguard Worker    read -ra build_args <<<"$build_args"
112*cfb92d14SAndroid Build Coastguard Worker    options=$(jq -r '.options | join(" ")' <<<"$1")
113*cfb92d14SAndroid Build Coastguard Worker
114*cfb92d14SAndroid Build Coastguard Worker    local otbr_options=(
115*cfb92d14SAndroid Build Coastguard Worker        "$options"
116*cfb92d14SAndroid Build Coastguard Worker        "-DOT_THREAD_VERSION=$version"
117*cfb92d14SAndroid Build Coastguard Worker        "-DOT_SIMULATION_MAX_NETWORK_SIZE=$MAX_NETWORK_SIZE"
118*cfb92d14SAndroid Build Coastguard Worker    )
119*cfb92d14SAndroid Build Coastguard Worker
120*cfb92d14SAndroid Build Coastguard Worker    docker build . \
121*cfb92d14SAndroid Build Coastguard Worker        -t "${otbr_docker_image}" \
122*cfb92d14SAndroid Build Coastguard Worker        -f "${ETC_DIR}/Dockerfile" \
123*cfb92d14SAndroid Build Coastguard Worker        "${build_args[@]}" \
124*cfb92d14SAndroid Build Coastguard Worker        --build-arg OTBR_OPTIONS="${otbr_options[*]}"
125*cfb92d14SAndroid Build Coastguard Worker}
126*cfb92d14SAndroid Build Coastguard Worker
127*cfb92d14SAndroid Build Coastguard Workerfor item in $(jq -c '.ot_build.ot | .[]' <<<"$CONFIG"); do
128*cfb92d14SAndroid Build Coastguard Worker    build_ot "$item"
129*cfb92d14SAndroid Build Coastguard Workerdone
130*cfb92d14SAndroid Build Coastguard Worker
131*cfb92d14SAndroid Build Coastguard Workergit clone https://github.com/openthread/ot-br-posix.git --recurse-submodules --shallow-submodules --depth=1
132*cfb92d14SAndroid Build Coastguard Worker(
133*cfb92d14SAndroid Build Coastguard Worker    cd ot-br-posix
134*cfb92d14SAndroid Build Coastguard Worker    # Use system V `service` command instead
135*cfb92d14SAndroid Build Coastguard Worker    mkdir -p root/etc/init.d
136*cfb92d14SAndroid Build Coastguard Worker    cp "${ETC_DIR}/commissionerd" root/etc/init.d/commissionerd
137*cfb92d14SAndroid Build Coastguard Worker    sudo chown root:root root/etc/init.d/commissionerd
138*cfb92d14SAndroid Build Coastguard Worker    sudo chmod +x root/etc/init.d/commissionerd
139*cfb92d14SAndroid Build Coastguard Worker
140*cfb92d14SAndroid Build Coastguard Worker    cp "${ETC_DIR}/server.patch" script/server.patch
141*cfb92d14SAndroid Build Coastguard Worker    patch script/server script/server.patch
142*cfb92d14SAndroid Build Coastguard Worker    mkdir -p root/tmp
143*cfb92d14SAndroid Build Coastguard Worker    cp "${ETC_DIR}/requirements.txt" root/tmp/requirements.txt
144*cfb92d14SAndroid Build Coastguard Worker
145*cfb92d14SAndroid Build Coastguard Worker    for item in $(jq -c '.ot_build.otbr | .[]' <<<"$CONFIG"); do
146*cfb92d14SAndroid Build Coastguard Worker        build_otbr "$item"
147*cfb92d14SAndroid Build Coastguard Worker    done
148*cfb92d14SAndroid Build Coastguard Worker)
149*cfb92d14SAndroid Build Coastguard Workerrm -rf ot-br-posix
150