1b1994897Sopenharmony_ci#!/bin/bash
2b1994897Sopenharmony_ci# Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3b1994897Sopenharmony_ci# Licensed under the Apache License, Version 2.0 (the "License");
4b1994897Sopenharmony_ci# you may not use this file except in compliance with the License.
5b1994897Sopenharmony_ci# You may obtain a copy of the License at
6b1994897Sopenharmony_ci#
7b1994897Sopenharmony_ci# http://www.apache.org/licenses/LICENSE-2.0
8b1994897Sopenharmony_ci#
9b1994897Sopenharmony_ci# Unless required by applicable law or agreed to in writing, software
10b1994897Sopenharmony_ci# distributed under the License is distributed on an "AS IS" BASIS,
11b1994897Sopenharmony_ci# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12b1994897Sopenharmony_ci# See the License for the specific language governing permissions and
13b1994897Sopenharmony_ci# limitations under the License.
14b1994897Sopenharmony_ci
15b1994897Sopenharmony_ci#
16b1994897Sopenharmony_ci# Aux functions
17b1994897Sopenharmony_ci#
18b1994897Sopenharmony_ci
19b1994897Sopenharmony_ciSCRIPT_DIR="$(realpath "${0}")"
20b1994897Sopenharmony_ciSCRIPT_DIR="$(dirname "${SCRIPT_DIR}")"
21b1994897Sopenharmony_cicd "${SCRIPT_DIR}"
22b1994897Sopenharmony_ci
23b1994897Sopenharmony_ciif [[ -f "${SCRIPT_DIR}/extras/install-deps-extras.sh" ]]
24b1994897Sopenharmony_cithen
25b1994897Sopenharmony_ci    source "${SCRIPT_DIR}/extras/install-deps-extras.sh"
26b1994897Sopenharmony_cifi
27b1994897Sopenharmony_ci
28b1994897Sopenharmony_cifunction print_help
29b1994897Sopenharmony_ci{
30b1994897Sopenharmony_ci    HELP_MESSAGE="
31b1994897Sopenharmony_ci    It is the bootstrap script for Panda on Ubuntu 18.04 or 20.04.
32b1994897Sopenharmony_ci
33b1994897Sopenharmony_ci    This script installs all necessary packages for building and testing Panda
34b1994897Sopenharmony_ci    in your local environment, given that your environment is Ubuntu 18.04 or 20.04.
35b1994897Sopenharmony_ci    (detected with the contents of /etc/os-release).
36b1994897Sopenharmony_ci
37b1994897Sopenharmony_ci    The script should run with superuser privileges.
38b1994897Sopenharmony_ci
39b1994897Sopenharmony_ci    EXAMPLE
40b1994897Sopenharmony_ci
41b1994897Sopenharmony_ci    $ ./scripts/install-deps-ubuntu --help
42b1994897Sopenharmony_ci    $ ./scripts/install-deps-ubuntu --install=x86 --install=arm-all --install=dev
43b1994897Sopenharmony_ci
44b1994897Sopenharmony_ci    or
45b1994897Sopenharmony_ci
46b1994897Sopenharmony_ci    $ ./scripts/install-deps-ubuntu -h
47b1994897Sopenharmony_ci    $ ./scripts/install-deps-ubuntu -i=x86 -i=arm-all -i=dev
48b1994897Sopenharmony_ci
49b1994897Sopenharmony_ci    SYNOPSIS
50b1994897Sopenharmony_ci
51b1994897Sopenharmony_ci    $0 [OPTIONS]
52b1994897Sopenharmony_ci
53b1994897Sopenharmony_ci    OPTIONS
54b1994897Sopenharmony_ci
55b1994897Sopenharmony_ci    --help              | -h              Show this message and exit.
56b1994897Sopenharmony_ci
57b1994897Sopenharmony_ci    --install=dev       | -i=dev          Install tools needed for development.
58b1994897Sopenharmony_ci
59b1994897Sopenharmony_ci    --install=arm-dev   | -i=arm-dev      Install ARM64-hosted tools needed for development.
60b1994897Sopenharmony_ci
61b1994897Sopenharmony_ci    --install=arm-all   | -i=arm-all      Install extra packages for cross-compiling for AArch32 and AArch64.
62b1994897Sopenharmony_ci
63b1994897Sopenharmony_ci    --install=x86       | -i=x86          Install extra packages for cross-compiling for x86.
64b1994897Sopenharmony_ci
65b1994897Sopenharmony_ci    --install=windows   | -i=windows      Install extra packages for cross-compiling for Windows.
66b1994897Sopenharmony_ci    "
67b1994897Sopenharmony_ci
68b1994897Sopenharmony_ci    if [[ -n "${EXTRA_OPTIONS}" ]]
69b1994897Sopenharmony_ci    then
70b1994897Sopenharmony_ci        HELP_MESSAGE="${HELP_MESSAGE}${ADDITIONAL_OPTIONS_HELP}"
71b1994897Sopenharmony_ci    fi
72b1994897Sopenharmony_ci
73b1994897Sopenharmony_ci
74b1994897Sopenharmony_ci    HELP_MESSAGE="${HELP_MESSAGE}
75b1994897Sopenharmony_ci    CAVEAT
76b1994897Sopenharmony_ci
77b1994897Sopenharmony_ci    * Packages for cross-compiling for aarch64 and x86 cannot co-exist, so the
78b1994897Sopenharmony_ci    script (read: apt) will replace any conflicting packages on each run.
79b1994897Sopenharmony_ci    * However, packages for cross-compiling for aarch64 and 32-bit ARM can
80b1994897Sopenharmony_ci    co-exist, so they are in a single 'arm-all' dependency list.
81b1994897Sopenharmony_ci    "
82b1994897Sopenharmony_ci
83b1994897Sopenharmony_ci    echo "$HELP_MESSAGE"
84b1994897Sopenharmony_ci}
85b1994897Sopenharmony_ci
86b1994897Sopenharmony_cifunction install_dep
87b1994897Sopenharmony_ci{
88b1994897Sopenharmony_ci    local fname=$1
89b1994897Sopenharmony_ci
90b1994897Sopenharmony_ci    if [[ ! -f "$fname" ]] ; then
91b1994897Sopenharmony_ci        echo "FATAL: Dependency list $fname not found."
92b1994897Sopenharmony_ci        exit 1
93b1994897Sopenharmony_ci    fi
94b1994897Sopenharmony_ci
95b1994897Sopenharmony_ci    echo "Processing $fname"
96b1994897Sopenharmony_ci    grep --color=never -o '^[^#]*' "$fname" | xargs apt install -y --no-install-recommends -o Dpkg::Options::="--force-overwrite"
97b1994897Sopenharmony_ci}
98b1994897Sopenharmony_ci
99b1994897Sopenharmony_ci#
100b1994897Sopenharmony_ci# Main logic
101b1994897Sopenharmony_ci#
102b1994897Sopenharmony_ci
103b1994897Sopenharmony_ci#
104b1994897Sopenharmony_ci# Parse command-line arguments
105b1994897Sopenharmony_ci#
106b1994897Sopenharmony_ci
107b1994897Sopenharmony_ci# Set default flag values
108b1994897Sopenharmony_ciINSTALL_DEV=no
109b1994897Sopenharmony_ciINSTALL_CROSS_x86=no
110b1994897Sopenharmony_ciINSTALL_CROSS_WINDOWS=no
111b1994897Sopenharmony_ciINSTALL_ARM_DEV=no
112b1994897Sopenharmony_ciINSTALL_CROSS_ARM_ALL=no
113b1994897Sopenharmony_ciSRC_LIST_STR='# This file is generated automatically by Panda install-deps-ubuntu script. DO NOT EDIT!!!\n'
114b1994897Sopenharmony_ci
115b1994897Sopenharmony_cifor i in "$@"
116b1994897Sopenharmony_cido
117b1994897Sopenharmony_ci    ERROR_ARG=""
118b1994897Sopenharmony_ci    case $i in
119b1994897Sopenharmony_ci    -h|--help)
120b1994897Sopenharmony_ci        print_help
121b1994897Sopenharmony_ci        exit 0
122b1994897Sopenharmony_ci        ;;
123b1994897Sopenharmony_ci    --install-qemu-from-sources)
124b1994897Sopenharmony_ci        INSTALL_QEMU_FROM_SOURCES=yes
125b1994897Sopenharmony_ci        ;;
126b1994897Sopenharmony_ci    -i=*|--install=*)
127b1994897Sopenharmony_ci        FLAG_ARG=${i//[-a-zA-Z0-9]*=/}
128b1994897Sopenharmony_ci        if [[ $FLAG_ARG == "dev" ]] ; then
129b1994897Sopenharmony_ci            if [[ $INSTALL_ARM_DEV == "yes" ]] ; then
130b1994897Sopenharmony_ci                echo "FATAL: Parameter --install=dev excludes --install=arm-dev"
131b1994897Sopenharmony_ci                exit 1
132b1994897Sopenharmony_ci            else
133b1994897Sopenharmony_ci                INSTALL_DEV=yes
134b1994897Sopenharmony_ci            fi
135b1994897Sopenharmony_ci        fi
136b1994897Sopenharmony_ci        if [[ $FLAG_ARG == "x86" ]] ; then
137b1994897Sopenharmony_ci            INSTALL_CROSS_x86=yes
138b1994897Sopenharmony_ci        fi
139b1994897Sopenharmony_ci        if [[ $FLAG_ARG == "arm-all" ]] ; then
140b1994897Sopenharmony_ci            INSTALL_CROSS_ARM_ALL=yes
141b1994897Sopenharmony_ci        fi
142b1994897Sopenharmony_ci        if [[ $FLAG_ARG == "windows" ]] ; then
143b1994897Sopenharmony_ci            INSTALL_CROSS_WINDOWS=yes
144b1994897Sopenharmony_ci        fi
145b1994897Sopenharmony_ci        if [[ $FLAG_ARG == "arm-dev" ]] ; then
146b1994897Sopenharmony_ci            if [[ $INSTALL_DEV == "yes" ]] ; then
147b1994897Sopenharmony_ci                echo "FATAL: Parameter --install=arm-dev excludes --install=dev"
148b1994897Sopenharmony_ci                exit 1
149b1994897Sopenharmony_ci            else
150b1994897Sopenharmony_ci                INSTALL_ARM_DEV=yes
151b1994897Sopenharmony_ci            fi
152b1994897Sopenharmony_ci        fi
153b1994897Sopenharmony_ci      ;;
154b1994897Sopenharmony_ci    *)
155b1994897Sopenharmony_ci        ERROR_ARG="YES"
156b1994897Sopenharmony_ci      ;;
157b1994897Sopenharmony_ci    esac
158b1994897Sopenharmony_ci
159b1994897Sopenharmony_ci    if [[ -n "${EXTRA_OPTIONS}" ]]
160b1994897Sopenharmony_ci    then
161b1994897Sopenharmony_ci        extra_parse "${i}"
162b1994897Sopenharmony_ci    fi
163b1994897Sopenharmony_ci
164b1994897Sopenharmony_ci    if [[ -n "${ERROR_ARG}" ]]
165b1994897Sopenharmony_ci    then
166b1994897Sopenharmony_ci        echo "Error: Unsupported flag $i" >&2
167b1994897Sopenharmony_ci        exit 1
168b1994897Sopenharmony_ci    fi
169b1994897Sopenharmony_ci
170b1994897Sopenharmony_cidone
171b1994897Sopenharmony_ci
172b1994897Sopenharmony_ci#
173b1994897Sopenharmony_ci# Check 'sudo' and if script is running on Ubuntu
174b1994897Sopenharmony_ci#
175b1994897Sopenharmony_ci
176b1994897Sopenharmony_ciif [[ $(id -u) -ne 0 ]] ; then
177b1994897Sopenharmony_ci    print_help
178b1994897Sopenharmony_ci    echo "!!!"
179b1994897Sopenharmony_ci    echo "FATAL: Please run as root."
180b1994897Sopenharmony_ci    echo "!!!"
181b1994897Sopenharmony_ci    exit 1
182b1994897Sopenharmony_cifi
183b1994897Sopenharmony_ci
184b1994897Sopenharmony_ci#
185b1994897Sopenharmony_ci# Check specific Ubuntu version
186b1994897Sopenharmony_ci#
187b1994897Sopenharmony_ci
188b1994897Sopenharmony_ciUBUNTU_NAME=ubuntu-18-04
189b1994897Sopenharmony_ci
190b1994897Sopenharmony_ciif [ ! -f /etc/os-release ]; then
191b1994897Sopenharmony_ci    echo "FATAL: /etc/os-release not found. Exiting..."
192b1994897Sopenharmony_ci    exit 1
193b1994897Sopenharmony_cielse
194b1994897Sopenharmony_ci    . /etc/os-release
195b1994897Sopenharmony_ci
196b1994897Sopenharmony_ci    if [[ $NAME == "Ubuntu" ]]
197b1994897Sopenharmony_ci    then
198b1994897Sopenharmony_ci        set -x
199b1994897Sopenharmony_ci        apt-get update
200b1994897Sopenharmony_ci        dpkg -l | grep curl  || apt-get -y install curl
201b1994897Sopenharmony_ci        dpkg -l | grep gnupg || apt-get -y install gnupg
202b1994897Sopenharmony_ci
203b1994897Sopenharmony_ci        if [[ -n "${EXTRA_OPTIONS}" ]]
204b1994897Sopenharmony_ci        then
205b1994897Sopenharmony_ci            extra_add_repo
206b1994897Sopenharmony_ci        fi
207b1994897Sopenharmony_ci
208b1994897Sopenharmony_ci        set +x
209b1994897Sopenharmony_ci    else
210b1994897Sopenharmony_ci        echo "FATAL: Only Ubuntu is supported. This is not. Exiting..."
211b1994897Sopenharmony_ci        exit 1
212b1994897Sopenharmony_ci    fi
213b1994897Sopenharmony_cifi
214b1994897Sopenharmony_ci
215b1994897Sopenharmony_ciif [[ $VERSION_ID == "18.04" ]]; then
216b1994897Sopenharmony_ci    echo "Installing packages for Ubuntu 18.04 LTS."
217b1994897Sopenharmony_ci    UBUNTU_NAME=ubuntu-18-04
218b1994897Sopenharmony_cielif [[ $VERSION_ID == "20.04" ]]; then
219b1994897Sopenharmony_ci    echo "Installing packages for Ubuntu 20.04 LTS."
220b1994897Sopenharmony_ci    UBUNTU_NAME=ubuntu-20-04
221b1994897Sopenharmony_cielse
222b1994897Sopenharmony_ci    echo "Trying to install packages for Ubuntu with unpinned versions."
223b1994897Sopenharmony_cifi
224b1994897Sopenharmony_ci
225b1994897Sopenharmony_ciset -e
226b1994897Sopenharmony_ci
227b1994897Sopenharmony_ci#
228b1994897Sopenharmony_ci# Install dependencies
229b1994897Sopenharmony_ci#
230b1994897Sopenharmony_ci
231b1994897Sopenharmony_ciinstall_dep "$SCRIPT_DIR/dep-lists/$UBUNTU_NAME-build"
232b1994897Sopenharmony_ci
233b1994897Sopenharmony_ciif [[ "x$INSTALL_DEV" == "xyes" ]] ; then
234b1994897Sopenharmony_ci    install_dep "$SCRIPT_DIR/dep-lists/$UBUNTU_NAME-dev"
235b1994897Sopenharmony_cifi
236b1994897Sopenharmony_ci
237b1994897Sopenharmony_ciif [[ "x$INSTALL_ARM_DEV" == "xyes" ]] ; then
238b1994897Sopenharmony_ci    install_dep "$SCRIPT_DIR/dep-lists/$UBUNTU_NAME-arm-dev"
239b1994897Sopenharmony_cifi
240b1994897Sopenharmony_ci
241b1994897Sopenharmony_ciif [[ "x$INSTALL_CROSS_x86" == "xyes" ]] ; then
242b1994897Sopenharmony_ci    install_dep "$SCRIPT_DIR/dep-lists/$UBUNTU_NAME-cross-x86"
243b1994897Sopenharmony_cifi
244b1994897Sopenharmony_ci
245b1994897Sopenharmony_ciif [[ "x$INSTALL_CROSS_WINDOWS" == "xyes" ]] ; then
246b1994897Sopenharmony_ci    install_dep "$SCRIPT_DIR/dep-lists/$UBUNTU_NAME-cross-windows"
247b1994897Sopenharmony_cifi
248b1994897Sopenharmony_ci
249b1994897Sopenharmony_ciif [[ "x$INSTALL_CROSS_ARM_ALL" == "xyes" ]] ; then
250b1994897Sopenharmony_ci    if [[ -z "${EXTRA_OPTIONS}" ]]
251b1994897Sopenharmony_ci    then
252b1994897Sopenharmony_ci        "${SCRIPT_DIR}/install-deps-qemu"
253b1994897Sopenharmony_ci    fi
254b1994897Sopenharmony_ci    install_dep "$SCRIPT_DIR/dep-lists/$UBUNTU_NAME-cross-arm-all"
255b1994897Sopenharmony_cifi
256b1994897Sopenharmony_ci
257b1994897Sopenharmony_ciif [[ -n "${EXTRA_OPTIONS}" ]]
258b1994897Sopenharmony_cithen
259b1994897Sopenharmony_ci    extra_install
260b1994897Sopenharmony_cifi
261