-
Notifications
You must be signed in to change notification settings - Fork 377
/
crosstool-ng.sh
executable file
·112 lines (95 loc) · 2.78 KB
/
crosstool-ng.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/bin/bash
set -x
set -eo pipefail
# shellcheck disable=SC1091
. lib.sh
silence_stdout() {
if [[ "${VERBOSE}" == "1" ]]; then
"${@}"
else
"${@}" >/dev/null
fi
}
main() {
local config="${1}"
local nproc="${2}"
local ctng_version=${3:-crosstool-ng-1.26.0}
local ctng_url="https://github.com/crosstool-ng/crosstool-ng"
local username=crosstool
local crosstooldir=/opt/crosstool
local buildir
local srcdir="/home/${username}/src"
local dstdir="/x-tools"
local sleep=15s
local timeout=5m
install_packages autoconf \
bison \
curl \
flex \
gawk \
help2man \
libncurses-dev \
libtool-bin \
patch \
python3 \
python3-dev \
python3-pip \
rsync \
texinfo \
wget \
unzip \
xz-utils
# configure and install crosstool-ng
local td
td="$(mktemp -d)"
pushd "${td}"
mkdir "crosstool-ng-${ctng_version}"
pushd "crosstool-ng-${ctng_version}"
git init
git fetch --depth=1 "${ctng_url}" "${ctng_version}"
git reset --hard FETCH_HEAD
./bootstrap
./configure --prefix="${crosstooldir}"
make -j"${nproc}"
make install
popd
popd
# configure and install our toolchain
buildir="$(mktemp -d)"
# copy our config files, and make sure the l
# crosstool-ng can't be run as root, so we do this instead.
adduser --disabled-password --gecos "" "${username}"
chown -R "${username}":"${username}" "${buildir}"
pushd "${buildir}"
cp /"${config}" .config
chown "${username}":"${username}" .config
su "${username}" -c "${crosstooldir}/bin/ct-ng olddefconfig"
# the download steps can stall indefinitely, so we want to set a timeout to
# ensure it always completes. we therefore attempt to download until
# this step completes or fails. the built toolchain installs to `/x-tools`.
mkdir -p "${dstdir}"
chown -R "${username}":"${username}" "${dstdir}"
local step=companion_tools_for_build
su "${username}" -c "mkdir -p ${srcdir}"
download() {
# timeout is a command, not a built-in, so it won't
# work with any bash functions: must call a command.
timeout "${timeout}" \
su "${username}" -c \
"STOP=${step} CT_DEBUG_CT_SAVE_STEPS=1 ${crosstooldir}/bin/ct-ng build.${nproc}"
}
while silence_stdout download; [ $? -eq 124 ]; do
# Indicates a timeout, repeat the command.
sleep "${sleep}"
done
silence_stdout su "${username}" \
-c "CT_DEBUG_CT_SAVE_STEPS=1 ${crosstooldir}/bin/ct-ng build.${nproc}"
popd
purge_packages
rm -rf "${srcdir}"
rm -rf "${buildir}"
rm -rf "${crosstooldir}"
rm -rf "${td}"
rm "${0}"
}
main "${@}"