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-14 clang-tidy-14 || echo 'WARNING: could not install clang-format-14 and clang-tidy-14, 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
50    command -v shfmt || sudo apt-get 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 g++ lsb-release 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 bzip2 ca-certificates wget
70        (cd /tmp \
71            && wget --tries 4 --no-check-certificate --quiet -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 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 armmbed/formulae/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 14"
115    command -v clang-format-14 || (command -v clang-format && (clang-format --version | grep -q "${CLANG_FORMAT_VERSION}")) || {
116        brew install llvm@14
117        sudo ln -s "$(brew --prefix llvm@14)/bin/clang-format" /usr/local/bin/clang-format-14
118        sudo ln -s "$(brew --prefix llvm@14)/bin/clang-tidy" /usr/local/bin/clang-tidy-14
119        sudo ln -s "$(brew --prefix llvm@14)/bin/clang-apply-replacements" /usr/local/bin/clang-apply-replacements-14
120        sudo ln -s "$(brew --prefix llvm@14)/bin/run-clang-tidy" /usr/local/bin/run-clang-tidy-14
121    } || echo 'WARNING: could not install llvm@14, which is useful if you plan to contribute C/C++ code to the OpenThread project.'
122
123    # add yapf for pretty
124    python3 -m pip install yapf || echo 'Failed to install python code formatter yapf. Install it manually if you need.'
125}
126
127install_packages_source()
128{
129    echo 'source not supported currently' && false
130}
131
132install_packages()
133{
134    PM=source
135    if command -v apt-get; then
136        PM=apt
137    elif command -v rpm; then
138        PM=rpm
139    elif command -v opkg; then
140        PM=opkg
141    elif command -v brew; then
142        PM=brew
143    fi
144    install_packages_$PM
145}
146
147main()
148{
149    install_packages
150    echo 'bootstrap completed successfully.'
151}
152
153main
154