-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommon.bashinc
220 lines (192 loc) · 5.88 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
#!/usr/bin/env bash
# Copyright 2024-2025 Nick Brassel (@tzarc)
# SPDX-License-Identifier: GPL-2.0-or-later
set -eEuo pipefail
# macOS SDK Version
export SDK_VERSION=15.0
# macOS Target Version (Monterey)
export MACOSX_DEPLOYMENT_TARGET=12.0
# If necessary, re-exec with a clean environment inside docker
function respawn_docker_if_needed() {
if [[ "$(uname -s)" == "Linux" ]] && [[ -n "${EXECUTE_UNDER_DOCKER:-}" ]]; then
eval "$(parse_args "$@")"
[ -n "${container_image:-}" ] || container_image='ghcr.io/tzarc/qmk_toolchains:builder'
cd "$script_dir"
docker run --rm \
-v "$script_dir:$TC_WORKDIR" \
-w "$TC_WORKDIR" \
-e TC_WORKDIR="$TC_WORKDIR" \
$container_image \
./$(basename $this_script) "$@"
exit $?
fi
############################################################################
# If we're in the container, we need to fix up some environmental issues
# macOS include path issues...
find /gcc /usr/local -path '*/include-fixed/dispatch/object.h' -print -exec sudo rm -rf '{}' \; 2>/dev/null || true
# mingw64 include path issues...
sudo cp /qmk/x86_64-w64-mingw32/sysroot/usr/x86_64-w64-mingw32/include/hidusage.h /qmk/x86_64-w64-mingw32/sysroot/usr/x86_64-w64-mingw32/include/ddk
sudo cp /qmk/x86_64-w64-mingw32/sysroot/usr/x86_64-w64-mingw32/include/hidpi.h /qmk/x86_64-w64-mingw32/sysroot/usr/x86_64-w64-mingw32/include/ddk
}
echo
echo '========================================================================' >&2
echo '========================================================================' >&2
echo "== ${this_script:-${BASH_SOURCE[0]}} $@" >&2
echo '========================================================================' >&2
echo '========================================================================' >&2
echo >&2
function notice_text() {
echo '========================================================================' >&2
echo "${@}" >&2
echo '========================================================================' >&2
}
function rcmd() {
notice_text "Running: '${@}':"
"$@"
}
triples=(
x86_64-qmk-linux-gnu
aarch64-unknown-linux-gnu
riscv64-unknown-linux-gnu
x86_64-w64-mingw32
aarch64-apple-darwin24
x86_64-apple-darwin24
)
function cleanup() {
if [ -n "${source_dir:-}" ] && [ -e "${source_dir}/.git" ]; then
git -C "${source_dir}" clean -xfd
git -C "${source_dir}" reset --hard
fi
}
trap cleanup EXIT
function havecmd() {
type "${1}" >/dev/null 2>&1 || return 1
}
havecmd gsed && SED=gsed || SED=sed
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* | *apple*)
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
;;
*riscv64*)
echo RV64
;;
*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
# Remove the qmk vendor from the bootstrapped compiler
input_var=$(echo $input_var | sed -e 's@qmk-@@g')
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
--container-image -- Which container image to use.
__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 parse_args() {
set -eEuo pipefail
umask 022
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##*=}
echo export $N="$V"
;;
--*)
N=${1##--}
N=$(echo $N | tr '-' '_')
echo export $N=true
;;
*)
positional_args+=($1)
;;
esac
shift
unset N
unset V
done
echo "positional_args=("${positional_args[@]}")"
}