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#    Description:
30#      This file installs all needed dependencies and toolchains needed for
31#      example compilation and programming.
32#
33
34set -euxo pipefail
35
36install_packages_pretty_format()
37{
38    echo 'Installing pretty tools useful for code contributions...'
39
40    # add clang-format and clang-tidy for pretty
41    sudo apt-get --no-install-recommends install -y clang-format-9 clang-tidy-9 || echo 'WARNING: could not install clang-format-9 and clang-tidy-9, which is useful if you plan to contribute C/C++ code to the OpenThread project.'
42
43    # add yapf for pretty
44    python3 -m pip install yapf==0.31.0 || echo 'WARNING: could not install yapf, which is useful if you plan to contribute python code to the OpenThread project.'
45
46    # add mdv for local size report
47    python3 -m pip install mdv || echo 'WARNING: could not install mdv, which is required to post markdown size report for OpenThread.'
48
49    # add shfmt for shell pretty, try brew only because snap does not support home directory not being /home and doesn't work in docker.
50    command -v shfmt || brew install shfmt || echo 'WARNING: could not install shfmt, which is useful if you plan to contribute shell scripts to the OpenThread project.'
51}
52
53install_packages_apt()
54{
55    echo 'Installing toolchain dependencies...'
56
57    # apt-get update and install dependencies
58    sudo apt-get update
59    sudo apt-get --no-install-recommends install -y automake g++ libtool lsb-release make cmake ninja-build shellcheck
60
61    echo 'Installing GNU Arm Embedded Toolchain...'
62
63    PLATFORM=$(lsb_release -is)
64    ARCH=$(arch)
65
66    if [ "$PLATFORM" = "Raspbian" ]; then
67        sudo apt-get --no-install-recommends install -y binutils-arm-none-eabi gcc-arm-none-eabi gdb-arm-none-eabi libnewlib-arm-none-eabi libstdc++-arm-none-eabi-newlib
68    elif [ "$PLATFORM" = "Ubuntu" ]; then
69        sudo apt-get --no-install-recommends install -y ca-certificates wget
70        (cd /tmp \
71            && wget -c https://developer.arm.com/-/media/Files/downloads/gnu-rm/9-2020q2/gcc-arm-none-eabi-9-2020-q2-update-"$ARCH"-linux.tar.bz2 \
72            && sudo tar xjf gcc-arm-none-eabi-9-2020-q2-update-"$ARCH"-linux.tar.bz2 -C /opt \
73            && rm gcc-arm-none-eabi-9-2020-q2-update-"$ARCH"-linux.tar.bz2 \
74            && sudo ln -s -f /opt/gcc-arm-none-eabi-9-2020-q2-update/bin/* /usr/local/bin/.)
75    fi
76
77    if [ "$PLATFORM" != "Raspbian" ]; then
78        install_packages_pretty_format
79    fi
80}
81
82install_packages_opkg()
83{
84    echo 'opkg not supported currently' && false
85}
86
87install_packages_rpm()
88{
89    echo 'rpm not supported currently' && false
90}
91
92install_packages_brew()
93{
94    echo 'Installing toolchain dependencies...'
95
96    # add build tools
97    brew install automake libtool cmake ninja shfmt shellcheck
98
99    echo 'Installing GNU Arm Embedded Toolchain...'
100
101    # add ARM toolchain
102    brew tap ArmMbed/homebrew-formulae
103    brew install arm-none-eabi-gcc
104
105    # check for gcc for simulation
106    if ! command -v gcc; then
107        echo 'warning: clang/gcc needed for simulation'
108        echo 'warning: please install Command Line Tools from https://developer.apple.com/download/more/'
109    fi
110
111    echo 'Installing pretty tools useful for code contributions...'
112
113    # add clang-format for pretty
114    CLANG_FORMAT_VERSION="clang-format version 9"
115    command -v clang-format-9 || (command -v clang-format && (clang-format --version | grep -q "${CLANG_FORMAT_VERSION}")) || {
116        brew install llvm@9
117        sudo ln -s "$(brew --prefix llvm@9)/bin/clang-format" /usr/local/bin/clang-format-9
118    } || echo 'WARNING: could not install llvm@9, which is useful if you plan to contribute C/C++ code to the OpenThread project.'
119
120    # add yapf for pretty
121    python3 -m pip install yapf || echo 'Failed to install python code formatter yapf. Install it manually if you need.'
122}
123
124install_packages_source()
125{
126    echo 'source not supported currently' && false
127}
128
129install_packages()
130{
131    PM=source
132    if command -v apt-get; then
133        PM=apt
134    elif command -v rpm; then
135        PM=rpm
136    elif command -v opkg; then
137        PM=opkg
138    elif command -v brew; then
139        PM=brew
140    fi
141    install_packages_$PM
142}
143
144main()
145{
146    install_packages
147    echo 'bootstrap completed successfully.'
148}
149
150main
151