-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcommon.bashinc
424 lines (372 loc) · 14.6 KB
/
common.bashinc
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
#!/usr/bin/env bash
# Copyright 2024 Nick Brassel (@tzarc)
# SPDX-License-Identifier: GPL-2.0-or-later
# If necessary, re-exec with a clean environment inside docker
function respawn_docker_if_needed() {
if [[ "$(uname -s)" == "Linux" ]] && [[ -n "${EXECUTE_UNDER_DOCKER:-}" ]]; then
docker run --rm \
-v "$HOME/.local/crosstool-ng:$HOME/.local/crosstool-ng" \
-v "$script_dir:$TC_WORKDIR" \
-w "$TC_WORKDIR" \
ubuntu:jammy \
bash -xc "./ubuntu-prereqs.sh && groupadd -g $(id -g) $USER && useradd -g $(id -g) -u $(id -u) -M -N $USER && su -g $USER -c \"cd $TC_WORKDIR && ./$(basename $this_script) $*\" - $USER"
exit $?
fi
}
export PATH="$HOME/.local/crosstool-ng/bin:$PATH"
function havecmd() {
type "${1}" >/dev/null 2>&1 || return 1
}
havecmd gsed && SED=gsed || SED=sed
function verify_crosstool() {
if ! havecmd ct-ng; then
echo "ct-ng not found in PATH. Please install crosstool-ng first." >&2
exit 1
fi
}
function fn_os() {
local os_name=$(echo ${1:-} | tr 'A-Z' 'a-z')
if [[ -z "$os_name" ]]; then
os_name=$(uname -s | tr 'A-Z' 'a-z')
fi
case "$os_name" in
*darwin* | *macos*)
echo macos
;;
*windows* | *mingw* | *w64*)
echo windows
;;
*linux*)
echo linux
;;
*none* | *unknown* | *picolibc* | *nano*)
echo baremetal
;;
*)
echo unknown
;;
esac
}
function fn_arch() {
local arch_name=$(echo ${1:-} | tr 'A-Z' 'a-z')
if [[ -z "$arch_name" ]]; then
arch_name=$(uname -m | tr 'A-Z' 'a-z')
fi
case "$arch_name" in
*arm64* | *aarch64*)
echo ARM64
;;
*arm*)
echo ARM
;;
*riscv32*)
echo RV32
;;
*avr*)
echo AVR
;;
*x86_64* | *x64*)
echo X64
;;
*)
echo unknown
;;
esac
}
function fn_os_arch() {
echo $(fn_os ${1:-})$(fn_arch ${2:-})
}
function fn_os_arch_fromtriplet() {
local input_var=${1:-}
# AVR is a special snowflake.
if [[ "$input_var" == "avr" ]]; then
fn_os_arch_fromtriplet avr-none-none
return
fi
local input_arch=$(echo $input_var | cut -d'-' -f1)
local input_os=$(echo $input_var | cut -d'-' -f2)
# Try to skip vendor
if [[ "$input_os" == "unknown" ]] || [[ "$input_os" == "none" ]] || [[ "$input_os" == "pc" ]] || [[ "$input_os" == "rpi3" ]] || [[ "$input_os" == "w64" ]]; then
input_os=$(echo $input_var | cut -d'-' -f3)
fi
# If we're into the ABI stuff, then backtrack
if [[ "$input_os" == "eabi" ]] || [[ "$input_os" == "elf" ]] || [[ "$input_os" == "mingw32" ]]; then
input_os=$(echo $input_var | cut -d'-' -f2)
fi
fn_os_arch $input_os $input_arch
}
function build_one_args() {
cat <<__EOT__
--help -- Shows this help text
--build-target=<target> -- Which ct-ng build target to execute, default is 'build' (optional)
--sample-name=<sample> -- Seeds the crosstool-ng build with the sample configuration (required)
--vendor-name=<vendor> -- Overrides the vendor previously supplied by the sample (optional)
--canadian-host=<host> -- The intended final host machine that the toolchain will be executed (optional)
--no-cross-gdb-python -- Disabled python when building cross-gdb for canadian builds (optional)
--libc=<flavour> -- Overrides the C library to build. Usually 'picolibc', 'newlib', or 'none'. (optional)
--extra-newlib-nano -- Builds newlib-nano in addition to the usual C library (optional)
--extra-picolibc -- Builds picolibc in addition to the usual C library (optional)
--static-toolchain -- Builds the toolchain such that it's statically linked (optional)
--multilib-list=<value> -- Overrides the list of multilib targets generated. Sets \$CT_CC_GCC_MULTILIB_LIST. (optional)
--binutils-plugins -- Builds the toolchain with binutils plugins enabled (optional) -- cannot be used with \`--static-toolchain\`
--tools-prefix=<value> -- Override the build toolchain. For example \`x86_64-unknown-linux-gnu-\`, with the trailing slash. \`gcc\` et.al. will be appended. (optional)
--dir-suffix=<value> -- Appends a suffix to the toolchain output directory. (optional)
--build-host-compile -- Runs a build producing an earlier version of gcc, for subsequent gcc generated binary compatibility reasons
__EOT__
}
function build_one_help() {
for arg in "$@"; do
case "$arg" in
--help)
echo
echo "$(basename $this_script) $(build_one_args | grep '(required)' | ${SED} -e 's@\s\+--.*@@g') $(build_one_args | sort | grep '(optional)' | ${SED} -e 's@\s\+--.*@@g' -e 's@^@[@' -e 's@$@]@' | tr '\n' ' ')"
echo
build_one_args
exit 0
;;
esac
done
}
function build_one() {
set -eEuo pipefail
umask 022
verify_crosstool
while read arg; do
unset $arg
done < <(build_one_args | ${SED} -e 's@\s\+--.*@@g' -e 's@^--@@g' -e 's@-@_@g' -e 's@=.*@@g')
unset arg
local -a positional_args=()
while [[ ! -z "${1:-}" ]]; do
case "$1" in
--*=*)
N=${1%%=*}
N=${N##--}
N=$(echo $N | tr '-' '_')
V=${1##*=}
eval local $N="$V"
;;
--*)
N=${1##--}
N=$(echo $N | tr '-' '_')
eval local $N=true
;;
*)
positional_args+=($1)
;;
esac
shift
unset N
unset V
done
if [[ ! -z "${help:-}" ]]; then
build_one_args
exit 0
fi
[[ -d "$script_dir/tarballs" ]] || mkdir -p $script_dir/tarballs
if [[ ! -z "${canadian_host:-}" ]]; then
name_host=$(fn_os_arch_fromtriplet $canadian_host)
name_target=$(fn_os_arch_fromtriplet $sample_name)
else
name_host=$(fn_os_arch)
name_target=$(fn_os_arch_fromtriplet $sample_name)
fi
target_dir="host_${name_host}-target_${name_target}${dir_suffix:-}"
echo ============
echo ' BUILD TARGET:' $target_dir
echo ' BUILD DIR:' "$script_dir/build/$target_dir"
echo 'TOOLCHAIN DIR:' "$script_dir/toolchains/$target_dir"
echo ============
echo
local build_dir="$script_dir/build/$target_dir"
local toolchain_dir="$script_dir/toolchains/$target_dir"
mkdir -p "$build_dir"
cd "$build_dir"
[[ ! -e .config ]] || rm .config
ct-ng $sample_name
echo "CT_EXPERIMENTAL=y" >>.config
echo "CT_FORCE_EXTRACT=y" >>.config
echo "CT_STRIP_HOST_TOOLCHAIN_EXECUTABLES=y" >>.config
echo "CT_STRIP_TARGET_TOOLCHAIN_EXECUTABLES=y" >>.config
if [[ ! -z "${build_host_compile:-}" ]]; then
${SED} -i \
-e 's/^.*CT_GCC_V_.*$/# CT_GCC_V_???? is not set/' \
-e 's/^.*CT_GCC_VERSION.*$/# CT_GCC_VERSION is not set/' \
-e 's/^.*CT_GCC_.*_or_later$/# CT_GCC_????_or_later is not set/' \
-e 's/^.*CT_GCC_REQUIRE_.*_or_later$/# CT_GCC_REQUIRE_????_or_later is not set/' \
-e 's/^.*CT_GCC_.*_or_older.*$/# CT_GCC_V_????_or_older is not set/' \
-e 's/^.*CT_GCC_.*_or_later.*$/# CT_GCC_V_????_or_later is not set/' \
-e 's/^.*CT_GCC_later_than_.*$/# CT_GCC_later_than_???? is not set/' \
-e 's/^.*CT_GCC_older_than_.*$/# CT_GCC_older_than_???? is not set/' \
.config
echo "CT_GCC_V_12=y" >>.config
echo "CT_GCC_VERSION=\"12.4.0\"" >>.config
${SED} -i \
-e 's/^.*CT_MINGW_W64_V_.*$/# CT_MINGW_W64_V_???? is not set/' \
-e 's/^.*CT_MINGW_W64_VERSION.*$/# CT_MINGW_W64_VERSION is not set/' \
.config
echo "CT_MINGW_W64_V_V10_0=y" >>.config
echo "CT_MINGW_W64_VERSION=\"v10.0.0\"" >>.config
fi
ct-ng olddefconfig
${SED} -i \
-e '/CT_LOG_PROGRESS_BAR/s/y$/n/' \
-e 's/^.*CT_PREFIX_DIR_RO.*$/CT_PREFIX_DIR_RO=n/' \
-e 's/^.*CT_CC_LANG_CXX.*$/CT_CC_LANG_CXX=y/' \
-e 's/^.*CT_CC_GCC_LTO_ZSTD.*$/CT_CC_GCC_LTO_ZSTD=n/' \
-e 's/^.*CT_COMP_LIBS_ZSTD.*$/CT_COMP_LIBS_ZSTD=n/' \
-e 's/^.*CT_ZSTD_NEEDED.*$/CT_ZSTD_NEEDED=n/' \
-e 's/^.*CT_ZSTD.*$/CT_ZSTD=n/' \
-e 's/^.*CT_DEBUG_GDB.*$/CT_DEBUG_GDB=y/' \
-e 's@^.*CT_LOCAL_TARBALLS_DIR.*$@CT_LOCAL_TARBALLS_DIR="'"$script_dir/tarballs"'"@' \
.config
# Swap to an older version of Linux to avoid issues with older distros, especially things like older Debian with Raspberry Pi targets.
${SED} -i \
-e 's/^.*CT_LINUX_V_.*$/# CT_LINUX_V_???? is not set/' \
-e 's/^.*CT_LINUX_VERSION.*$/CT_LINUX_VERSION="4.19"/' \
-e 's/^.*CT_GLIBC_MIN_KERNEL.*$/CT_GLIBC_MIN_KERNEL="4.19"/' \
.config
echo CT_LINUX_V_4_19=y >>.config
echo unset CT_GLIBC_KERNEL_VERSION_NONE >>.config
echo CT_GLIBC_KERNEL_VERSION_AS_HEADERS=y >>.config
echo CT_GLIBC_MIN_KERNEL="\${CT_LINUX_VERSION}" >>.config
ct-ng olddefconfig
# Disable unnecessary languages
echo "CT_CC_LANG_FORTRAN=n" >>.config
echo "# CT_CC_LANG_FORTRAN is not set" >>.config
echo "CT_CC_LANG_JIT=n" >>.config
echo "# CT_CC_LANG_JIT is not set" >>.config
echo "CT_CC_LANG_ADA=n" >>.config
echo "# CT_CC_LANG_ADA is not set" >>.config
echo "CT_CC_LANG_D=n" >>.config
echo "# CT_CC_LANG_D is not set" >>.config
echo "CT_CC_LANG_OBJC=n" >>.config
echo "# CT_CC_LANG_OBJC is not set" >>.config
echo "CT_CC_LANG_OBJCXX=n" >>.config
echo "# CT_CC_LANG_OBJCXX is not set" >>.config
echo "CT_CC_LANG_GOLANG=n" >>.config
echo "# CT_CC_LANG_GOLANG is not set" >>.config
ct-ng olddefconfig
if [[ ! -z "${binutils_plugins:-}" ]] && [[ ! -z "${static_toolchain:-}" ]]; then
echo "Cannot use \`--binutils-plugins\` and \`--static-toolchain\` together." >&2
exit 1
fi
if [[ ! -z "${binutils_plugins:-}" ]]; then
echo "CT_BINUTILS_PLUGINS=y" >>.config
ct-ng olddefconfig
fi
if [[ ! -z "${static_toolchain:-}" ]]; then
${SED} -i \
-e 's/^.*CT_STATIC_TOOLCHAIN.*$/CT_STATIC_TOOLCHAIN=y/' \
-e 's/^.*CT_WANTS_STATIC_LINK.*$/CT_WANTS_STATIC_LINK=y/' \
-e 's/^.*CT_WANTS_STATIC_LINK_CXX.*$/CT_WANTS_STATIC_LINK_CXX=y/' \
.config
ct-ng olddefconfig
fi
if [[ ! -z "${libc:-}" ]]; then
echo "CT_LIBC_$(echo $libc | tr 'a-z' 'A-Z')=y" >>.config
echo "CT_LIBC=\"$(echo $libc | tr 'A-Z' 'a-z')\"" >>.config
echo 'CT_COMP_LIBS_NEWLIB_NANO=n' >>.config
echo 'CT_COMP_LIBS_PICOLIBC=n' >>.config
ct-ng olddefconfig
if [[ "${libc}" == "newlib" ]]; then
${SED} -i \
-e 's/^.*CT_LIBC_NEWLIB_DISABLE_SUPPLIED_SYSCALLS.*$/CT_LIBC_NEWLIB_DISABLE_SUPPLIED_SYSCALLS=n/' \
.config
fi
ct-ng olddefconfig
fi
if [[ ! -z "${extra_newlib_nano:-}" ]]; then
echo 'CT_COMP_LIBS_NEWLIB_NANO=y' >>.config
ct-ng olddefconfig
${SED} -i \
-e 's/^.*CT_LIBC_NEWLIB_EXTRA_SECTIONS.*$/CT_LIBC_NEWLIB_EXTRA_SECTIONS=y/' \
-e 's/^.*CT_LIBC_NEWLIB_NANO_MALLOC.*$/CT_LIBC_NEWLIB_NANO_MALLOC=y/' \
-e 's/^.*CT_COMP_LIBS_NEWLIB_NANO.*$/CT_COMP_LIBS_NEWLIB_NANO=y/' \
-e 's/^.*CT_NEWLIB_NANO_INSTALL_IN_TARGET.*$/CT_NEWLIB_NANO_INSTALL_IN_TARGET=y/' \
-e 's/^.*CT_LIBC_NEWLIB_NANO_EXTRA_SECTIONS.*$/CT_LIBC_NEWLIB_NANO_EXTRA_SECTIONS=y/' \
.config
ct-ng olddefconfig
fi
if [[ ! -z "${extra_picolibc:-}" ]]; then
echo 'CT_COMP_LIBS_PICOLIBC=y' >>.config
ct-ng olddefconfig
${SED} -i \
-e 's/^.*CT_PICOLIBC_INSTALL_IN_TARGET.*$/CT_PICOLIBC_INSTALL_IN_TARGET=y/' \
-e 's/^.*CT_LIBC_PICOLIBC_GCC_LIBSTDCXX.*$/CT_LIBC_PICOLIBC_GCC_LIBSTDCXX=y/' \
.config
ct-ng olddefconfig
fi
if [[ ! -z "${multilib_list:-}" ]]; then
${SED} -i \
-e 's/^.*CT_CC_GCC_MULTILIB_LIST.*$/CT_CC_GCC_MULTILIB_LIST="'$multilib_list'"/' \
.config
ct-ng olddefconfig
fi
if [[ ! -z "${vendor_name:-}" ]]; then
${SED} -i \
-e 's/^.*CT_TARGET_VENDOR.*$/CT_TARGET_VENDOR="'$vendor_name'"/' \
.config
ct-ng olddefconfig
fi
if [[ ! -z "${no_cross_gdb_python:-}" ]]; then
${SED} -i \
-e 's/^.*CT_GDB_CROSS_PYTHON.*$/# CT_GDB_CROSS_PYTHON is not set/' \
.config
ct-ng olddefconfig
fi
if [[ ! -z "${canadian_host:-}" ]]; then
${SED} -i \
-e 's/^.*CT_CROSS.*$/# CT_CROSS is not set/' \
-e 's/^.*CT_CANADIAN.*$/CT_CANADIAN=y/' \
-e 's/^.*CT_TOOLCHAIN_TYPE.*$/CT_TOOLCHAIN_TYPE="canadian"/' \
.config
echo "CT_HOST=\"${canadian_host}\"" >>.config
ct-ng olddefconfig
fi
if [[ ! -z "$(echo $sample_name | grep linux)" ]]; then
# Swap to an older version of glibc to avoid issues with older distros, especially things like older Debian with Raspberry Pi targets.
# See the glibc version compat list here: https://abi-laboratory.pro/?view=timeline&l=glibc -- 2.28 seems safe enough?
${SED} -i \
-e 's/^.*CT_GLIBC_V_.*$/# CT_GLIBC_V_???? is not set/' \
.config
echo CT_GLIBC_V_2_28=y >>.config
ct-ng olddefconfig
fi
${SED} -i \
-e 's@^.*CT_PREFIX_DIR=.*$@CT_PREFIX_DIR="'${toolchain_dir}'"@' \
.config
echo "CT_DEBUG_CT=y" >>.config
ct-ng olddefconfig
echo "CT_DEBUG_CT_SAVE_STEPS=y" >>.config
echo "CT_DEBUG_CT_SAVE_STEPS_GZIP=y" >>.config
ct-ng olddefconfig
echo "CT_TOOLCHAIN_PKGVERSION=\"qmk/qmk_toolchains @ $(git rev-parse --short HEAD)\"" >>.config
ct-ng olddefconfig
if [[ ! -z "${tools_prefix:-}" ]]; then
echo "CT_BUILD_PREFIX=\"${tools_prefix}\"" >>.config
ct-ng olddefconfig
fi
# Add the other compilers to the PATH for use in canadian builds
while read bindir; do
export PATH="$bindir:$PATH"
done < <(find "$script_dir/toolchains/host_$(fn_os_arch)"* -maxdepth 1 -type d -name bin)
unset bindir
echo '$PATH:'
echo "$PATH" | tr ':' '\n'
hash -r
while read arg; do
unset $arg
done < <(build_one_args | ${SED} -e 's@\s\+--.*@@g' -e 's@^--@@g' -e 's@-@_@g' -e 's@=.*@@g')
unset arg
if [[ ${#positional_args[@]} -eq 0 ]]; then
positional_args+=(build)
fi
if [[ ${#positional_args[@]} -eq 1 ]] && [[ "${positional_args[0]}" == "source" ]]; then
ct-ng source || true
else
$script_dir/build-stats.sh &
STATS_PID=$!
ct-ng ${positional_args[@]}
kill $STATS_PID
fi
}