1#!/bin/bash
2#
3# Copyright (c) 2022, 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
32POSIX_DIR="$(cd "$(dirname "$0")" && pwd)"
33OT_DIR="${POSIX_DIR}/../../.."
34ETC_DIR="${POSIX_DIR}/etc"
35SNIFFER_DIR="${POSIX_DIR}/sniffer_sim"
36
37PACKAGES=(
38    "docker.io"
39    "git"
40    "jq"
41    "socat"
42    "tshark"
43)
44
45sudo apt install -y "${PACKAGES[@]}"
46
47pip3 install -r "${POSIX_DIR}/requirements.txt"
48python3 -m grpc_tools.protoc -I"${SNIFFER_DIR}" --python_out="${SNIFFER_DIR}" --grpc_python_out="${SNIFFER_DIR}" proto/sniffer.proto
49
50CONFIG_NAME=${1:-"${POSIX_DIR}/config.yml"}
51# convert YAML to JSON
52CONFIG=$(python3 -c 'import json, sys, yaml; print(json.dumps(yaml.safe_load(open(sys.argv[1]))))' "$CONFIG_NAME")
53
54MAX_NETWORK_SIZE=$(jq -r '.ot_build.max_number' <<<"$CONFIG")
55
56build_ot()
57{
58    # SC2155: Declare and assign separately to avoid masking return values
59    local target build_dir cflags version options
60    target="ot-cli-ftd"
61    build_dir=$(jq -r '.subpath' <<<"$1")
62    cflags=$(jq -r '.cflags | join(" ")' <<<"$1")
63    version=$(jq -r '.version' <<<"$1")
64    options=$(jq -r '.options | join(" ")' <<<"$1")
65    # Intended splitting of options
66    read -ra options <<<"$options"
67
68    (
69        cd "$OT_DIR"
70
71        OT_CMAKE_NINJA_TARGET="$target" \
72            OT_CMAKE_BUILD_DIR="$build_dir" \
73            CFLAGS="$cflags" \
74            CXXFLAGS="$cflags" \
75            script/cmake-build \
76            simulation \
77            "${options[@]}" \
78            -DOT_THREAD_VERSION="$version" \
79            -DOT_SIMULATION_MAX_NETWORK_SIZE="$MAX_NETWORK_SIZE"
80    )
81}
82
83build_otbr()
84{
85    # SC2155: Declare and assign separately to avoid masking return values
86    local target build_dir version rcp_options
87    target="ot-rcp"
88    build_dir=$(jq -r '.rcp_subpath' <<<"$1")
89    version=$(jq -r '.version' <<<"$1")
90    rcp_options=$(jq -r '.rcp_options | join(" ")' <<<"$1")
91    # Intended splitting of rcp_options
92    read -ra rcp_options <<<"$rcp_options"
93
94    (
95        cd "$OT_DIR"
96
97        OT_CMAKE_NINJA_TARGET="$target" \
98            OT_CMAKE_BUILD_DIR="$build_dir" \
99            script/cmake-build \
100            simulation \
101            "${rcp_options[@]}" \
102            -DOT_THREAD_VERSION="$version" \
103            -DOT_SIMULATION_MAX_NETWORK_SIZE="$MAX_NETWORK_SIZE"
104    )
105
106    # SC2155: Declare and assign separately to avoid masking return values
107    local otbr_docker_image build_args options
108    otbr_docker_image=$(jq -r '.docker_image' <<<"$1")
109    build_args=$(jq -r '.build_args | map("--build-arg " + .) | join(" ")' <<<"$1")
110    # Intended splitting of build_args
111    read -ra build_args <<<"$build_args"
112    options=$(jq -r '.options | join(" ")' <<<"$1")
113
114    local otbr_options=(
115        "$options"
116        "-DOT_THREAD_VERSION=$version"
117        "-DOT_SIMULATION_MAX_NETWORK_SIZE=$MAX_NETWORK_SIZE"
118    )
119
120    docker build . \
121        -t "${otbr_docker_image}" \
122        -f "${ETC_DIR}/Dockerfile" \
123        "${build_args[@]}" \
124        --build-arg OTBR_OPTIONS="${otbr_options[*]}"
125}
126
127for item in $(jq -c '.ot_build.ot | .[]' <<<"$CONFIG"); do
128    build_ot "$item"
129done
130
131git clone https://github.com/openthread/ot-br-posix.git --recurse-submodules --shallow-submodules --depth=1
132(
133    cd ot-br-posix
134    # Use system V `service` command instead
135    mkdir -p root/etc/init.d
136    cp "${ETC_DIR}/commissionerd" root/etc/init.d/commissionerd
137    sudo chown root:root root/etc/init.d/commissionerd
138    sudo chmod +x root/etc/init.d/commissionerd
139
140    cp "${ETC_DIR}/server.patch" script/server.patch
141    patch script/server script/server.patch
142    mkdir -p root/tmp
143    cp "${ETC_DIR}/requirements.txt" root/tmp/requirements.txt
144
145    for item in $(jq -c '.ot_build.otbr | .[]' <<<"$CONFIG"); do
146        build_otbr "$item"
147    done
148)
149rm -rf ot-br-posix
150