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