-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
no-std.sh
executable file
·178 lines (166 loc) · 4.8 KB
/
no-std.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
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
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
cd "$(dirname "$0")"/..
# shellcheck disable=SC2154
trap 's=$?; echo >&2 "$0: Error on line "${LINENO}": ${BASH_COMMAND}"; exit ${s}' ERR
trap -- 'exit 1' SIGINT
# USAGE:
# ./tools/no-std.sh [+toolchain] [target]...
default_targets=(
# armv4t
armv4t-none-eabi
thumbv4t-none-eabi
# armv5te
armv5te-none-eabi
thumbv5te-none-eabi
# armv6-m
thumbv6m-none-eabi
# armv7-m
thumbv7m-none-eabi
thumbv7em-none-eabi
thumbv7em-none-eabihf
# armv8-m
thumbv8m.base-none-eabi
thumbv8m.main-none-eabi
thumbv8m.main-none-eabihf
# riscv32
riscv32i-unknown-none-elf
riscv32im-unknown-none-elf
riscv32imc-unknown-none-elf
riscv32imac-unknown-none-elf
riscv32gc-unknown-none-elf
# riscv64
riscv64i-unknown-none-elf
riscv64imac-unknown-none-elf
riscv64gc-unknown-none-elf
# avr
avr-unknown-gnu-atmega2560
)
x() {
local cmd="$1"
shift
(
set -x
"${cmd}" "$@"
)
}
x_cargo() {
if [[ -n "${RUSTFLAGS:-}" ]]; then
echo "+ RUSTFLAGS='${RUSTFLAGS}' \\"
fi
RUSTFLAGS="${RUSTFLAGS:-}" \
x cargo "$@"
echo
}
bail() {
echo >&2 "error: $*"
exit 1
}
pre_args=()
if [[ "${1:-}" == "+"* ]]; then
pre_args+=("$1")
shift
fi
if [[ $# -gt 0 ]]; then
targets=("$@")
else
targets=("${default_targets[@]}")
fi
rustup_target_list=$(rustup ${pre_args[@]+"${pre_args[@]}"} target list)
rustc_target_list=$(rustc ${pre_args[@]+"${pre_args[@]}"} --print target-list)
rustc_version=$(rustc ${pre_args[@]+"${pre_args[@]}"} -Vv | grep 'release: ' | sed 's/release: //')
nightly=''
if [[ "${rustc_version}" == *"nightly"* ]] || [[ "${rustc_version}" == *"dev"* ]]; then
nightly=1
rustup ${pre_args[@]+"${pre_args[@]}"} component add rust-src &>/dev/null
fi
run() {
local target="$1"
shift
local args=(${pre_args[@]+"${pre_args[@]}"})
local target_rustflags="${RUSTFLAGS:-}"
if ! grep <<<"${rustc_target_list}" -Eq "^${target}$" || [[ -f "target-specs/${target}.json" ]]; then
if [[ ! -f "target-specs/${target}.json" ]]; then
echo "target '${target}' not available on ${rustc_version} (skipped)"
return 0
fi
target_flags=(--target "$(pwd)/target-specs/${target}.json")
else
target_flags=(--target "${target}")
fi
local subcmd=run
case "${target}" in
armv4t* | thumbv4t*)
# TODO: run tests on CI (investigate mgba-test-runner in https://github.com/agbrs/agb)
if ! type -P mgba &>/dev/null; then
subcmd=build
fi
;;
esac
args+=("${subcmd}" "${target_flags[@]}")
if grep <<<"${rustup_target_list}" -Eq "^${target}( |$)"; then
x rustup ${pre_args[@]+"${pre_args[@]}"} target add "${target}" &>/dev/null
elif [[ -n "${nightly}" ]]; then
args+=(-Z build-std="core")
else
echo "target '${target}' requires nightly compiler (skipped)"
return 0
fi
# NB: sync with tools/build.sh
case "${target}" in
thumbv[4-5]t* | armv[4-5]t* | thumbv6m*)
target_rustflags+=" --cfg portable_atomic_unsafe_assume_single_core"
;;
riscv??i-* | riscv??im-* | riscv??imc-*)
target_rustflags+=" --cfg portable_atomic_unsafe_assume_single_core --cfg portable_atomic_s_mode"
;;
esac
local test_dir
# NB: sync with tools/build.sh
case "${target}" in
armv4t* | thumbv4t*)
test_dir=tests/gba
linker=link.ld
target_rustflags+=" -C link-arg=-T${linker}"
;;
armv5te* | thumbv5te*)
test_dir=tests/no-std-qemu
;;
thumb*)
test_dir=tests/no-std-qemu
linker=link.x
target_rustflags+=" -C link-arg=-T${linker}"
;;
riscv*)
test_dir=tests/no-std-qemu
case "${target}" in
riscv32*) linker=riscv32.ld ;;
riscv64*) linker=riscv64.ld ;;
*) bail "unrecognized target '${target}'" ;;
esac
target_rustflags+=" -C link-arg=-T${linker}"
;;
avr*)
test_dir=tests/avr
;;
*) bail "unrecognized target '${target}'" ;;
esac
args+=(--all-features)
(
cd "${test_dir}"
case "${target}" in
# TODO: compiler_builtins overflow bug: https://github.com/rust-lang/compiler-builtins/pull/521
thumbv5te*) ;;
*)
RUSTFLAGS="${target_rustflags}" \
x_cargo "${args[@]}" "$@"
;;
esac
RUSTFLAGS="${target_rustflags}" \
x_cargo "${args[@]}" --release "$@"
)
}
for target in "${targets[@]}"; do
run "${target}"
done