-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.sh
More file actions
executable file
·301 lines (250 loc) · 8.38 KB
/
Copy pathutils.sh
File metadata and controls
executable file
·301 lines (250 loc) · 8.38 KB
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
#!/bin/bash
# Spacemit K1 Bootloader Build Utilities
# Adapted from TI build system for RISC-V
BUILD=$(dirname "$(readlink -e "$0")")
ROOT=$(readlink -e "${BUILD}/../")
OUT="${ROOT}/out"
TOOLCHAINS="${SYSTEM_WIDE_TOOLCHAINS:-${ROOT}/toolchains}"
MODES=("release" "debug" "factory")
INIT_PATH=$PATH
# Source directories
OPENSBI_DIR="${ROOT}/pi-opensbi"
UBOOT_DIR="${ROOT}/pi-u-boot"
function pushd {
command pushd "$@" > /dev/null
}
function popd {
command popd > /dev/null
}
function find_path {
local path="$1"
local real_path=""
if [ -e "${path}" ]; then
real_path=$(readlink -e "${path}")
fi
echo "${real_path}"
}
function check_local_changes {
local repo_path="$1" && shift
local projects=("$@")
for project in "${projects[@]}"; do
if [ -d "${repo_path}/${project}" ]; then
pushd "${repo_path}/${project}"
git status > /dev/null 2>&1 || { popd; continue; }
if ! git diff --quiet HEAD 2>/dev/null; then
warning "Local changes detected in: ${project}"
fi
popd
fi
done
}
# RISC-V toolchain - use bootlin toolchain (glibc, stable)
# Using 2023.11-1 for better glibc compatibility (requires glibc 2.31+)
RISCV_TOOLCHAIN_VERSION="2023.11-1"
RISCV_TOOLCHAIN_NAME="riscv64-lp64d--glibc--stable-${RISCV_TOOLCHAIN_VERSION}"
RISCV_TOOLCHAIN_URL="https://toolchains.bootlin.com/downloads/releases/toolchains/riscv64-lp64d/tarballs/${RISCV_TOOLCHAIN_NAME}.tar.bz2"
# Buildroot toolchain path (if available)
BUILDROOT_TOOLCHAIN="/srv/spacemit/buildroot/output/k1_v2/host/bin"
# Download and extract RISC-V toolchain
function download_riscv64_toolchain {
local toolchain_dir="${TOOLCHAINS}/riscv64-lp64d--glibc--stable-${RISCV_TOOLCHAIN_VERSION}"
local tarball="${TOOLCHAINS}/${RISCV_TOOLCHAIN_NAME}.tar.bz2"
if [ -d "${toolchain_dir}" ]; then
echo "RISC-V toolchain already exists at ${toolchain_dir}"
return 0
fi
echo "Downloading RISC-V toolchain from Bootlin (${RISCV_TOOLCHAIN_VERSION})..."
mkdir -p "${TOOLCHAINS}"
if ! command -v wget &> /dev/null && ! command -v curl &> /dev/null; then
error_exit "wget or curl is required to download toolchain"
fi
if command -v wget &> /dev/null; then
wget -q --show-progress -O "${tarball}" "${RISCV_TOOLCHAIN_URL}"
else
curl -L -# -o "${tarball}" "${RISCV_TOOLCHAIN_URL}"
fi
echo "Extracting toolchain..."
tar -xjf "${tarball}" -C "${TOOLCHAINS}"
rm -f "${tarball}"
echo "RISC-V toolchain installed to ${toolchain_dir}"
}
# Check if a toolchain works (glibc compatibility)
function check_toolchain_works {
local gcc_path="$1"
if [ -x "${gcc_path}" ]; then
# Try to run gcc --version to check glibc compatibility
"${gcc_path}" --version &> /dev/null
return $?
fi
return 1
}
# RISC-V 64-bit cross-compiler
function riscv64_env {
local toolchain_dir="${TOOLCHAINS}/riscv64-lp64d--glibc--stable-${RISCV_TOOLCHAIN_VERSION}"
# Try system toolchain first
if command -v riscv64-linux-gnu-gcc &> /dev/null; then
if check_toolchain_works "$(command -v riscv64-linux-gnu-gcc)"; then
export CROSS_COMPILE=riscv64-linux-gnu-
export ARCH=riscv
return
fi
fi
if command -v riscv64-unknown-linux-gnu-gcc &> /dev/null; then
if check_toolchain_works "$(command -v riscv64-unknown-linux-gnu-gcc)"; then
export CROSS_COMPILE=riscv64-unknown-linux-gnu-
export ARCH=riscv
return
fi
fi
# Try buildroot toolchain (check glibc compatibility)
if [ -x "${BUILDROOT_TOOLCHAIN}/riscv64-unknown-linux-gnu-gcc" ]; then
if check_toolchain_works "${BUILDROOT_TOOLCHAIN}/riscv64-unknown-linux-gnu-gcc"; then
export PATH="${BUILDROOT_TOOLCHAIN}:$PATH"
export CROSS_COMPILE=riscv64-unknown-linux-gnu-
export ARCH=riscv
return
else
warning "Buildroot toolchain found but incompatible with system glibc"
fi
fi
# Try downloaded bootlin toolchain
if [ -d "${toolchain_dir}/bin" ]; then
if check_toolchain_works "${toolchain_dir}/bin/riscv64-buildroot-linux-gnu-gcc"; then
export PATH="${toolchain_dir}/bin:$PATH"
export CROSS_COMPILE=riscv64-buildroot-linux-gnu-
export ARCH=riscv
return
fi
fi
# Download toolchain
echo "RISC-V toolchain not found or incompatible, downloading Bootlin toolchain..."
download_riscv64_toolchain
if [ -d "${toolchain_dir}/bin" ]; then
export PATH="${toolchain_dir}/bin:$PATH"
export CROSS_COMPILE=riscv64-buildroot-linux-gnu-
export ARCH=riscv
else
error_exit "Failed to setup RISC-V toolchain"
fi
}
function check_riscv64 {
local toolchain_dir="${TOOLCHAINS}/riscv64-lp64d--glibc--stable-${RISCV_TOOLCHAIN_VERSION}"
# Check if RISC-V toolchain exists
if command -v riscv64-linux-gnu-gcc &> /dev/null; then
return 0
elif command -v riscv64-unknown-linux-gnu-gcc &> /dev/null; then
return 0
elif [ -x "${BUILDROOT_TOOLCHAIN}/riscv64-unknown-linux-gnu-gcc" ]; then
return 0
elif [ -d "${toolchain_dir}/bin" ]; then
return 0
else
warning "RISC-V toolchain not found in PATH or ${TOOLCHAINS}"
warning "It will be downloaded automatically when building"
return 1
fi
}
# Legacy ARM functions (kept for compatibility, but not used for Spacemit)
function gnueabihf_env {
export PATH="${TOOLCHAINS}/arm-gnu-toolchain-13.3.rel1-x86_64-arm-none-linux-gnueabihf/bin:$PATH"
export CROSS_COMPILE=arm-none-linux-gnueabihf-
}
function aarch64_env {
export PATH="${TOOLCHAINS}/arm-gnu-toolchain-13.3.rel1-x86_64-aarch64-none-linux-gnu/bin:$PATH"
export CROSS_COMPILE=aarch64-none-linux-gnu-
export CROSS_COMPILE64=aarch64-none-linux-gnu-
}
function avbtool_env {
if [ -d "${ROOT}/prebuilts/build-tools/linux-x86/bin/" ]; then
export PATH="${ROOT}/prebuilts/build-tools/linux-x86/bin/:$PATH"
fi
}
function clear_vars {
export PATH=$INIT_PATH
unset ARCH
unset CROSS_COMPILE
}
function check_env {
# out directory
! [ -d "${OUT}" ] && mkdir -p "${OUT}"
# toolchains directory
! [ -d "${TOOLCHAINS}" ] && mkdir -p "${TOOLCHAINS}"
# Check RISC-V toolchain
check_riscv64 || true
}
function config_value {
local value=$(cat "$1" | shyaml --quiet get-value "$2" 2>/dev/null)
echo "${value}"
}
function board_name {
local yaml_config=$(basename "$1")
echo "${yaml_config%.yaml}"
}
function out_dir {
local board=$(board_name "$1")
local mode="${2:-release}"
echo "${OUT}/${board}/${mode}"
}
function display_current_build {
local board=$(board_name "$1")
local build="$2"
local mode="$3"
printf "\n"
printf "%0.s-" {1..20}
printf "> Build %s: %s - %s <" "${build}" "${board}" "${mode}"
printf "%0.s-" {1..20}
printf "\n"
}
function usage {
cat <<DELIM__
usage: $(basename "$0") [options]
$ $(basename "$0") --config=config/boards/spacemit-k1.yaml
Options:
--config board config file
--clean (OPTIONAL) clean before build
--mode (OPTIONAL) [release|debug|factory] mode (default: release)
--help (OPTIONAL) display usage
DELIM__
}
function warning {
local warning="$1"
printf "\033[0;33mWARNING:\033[0m ${warning}\n"
}
function error {
local error="$1"
printf "\033[0;31mERROR:\033[0m ${error}\n\n"
}
function error_exit {
error "$1"
exit 1
}
function error_usage_exit {
error "$1"
usage
exit 1
}
function main {
local script=$(basename "$0")
local build="${script%.*}"
local clean=false
local config=""
local mode="release"
local opts_args="clean,config:,help,mode:"
local opts=$(getopt -o '' -l "${opts_args}" -- "$@")
eval set -- "${opts}"
while true; do
case "$1" in
--config) config=$(find_path "$2"); shift 2 ;;
--clean) clean=true; shift ;;
--mode) mode="$2"; shift 2 ;;
--help) usage; exit 0 ;;
--) shift; break ;;
esac
done
# check arguments
[ -z "${config}" ] && error_usage_exit "Cannot find board config file"
! [[ " ${MODES[*]} " =~ " ${mode} " ]] && error_usage_exit "${mode} mode not supported"
# build
check_env
${build} "${config}" "${clean}" "${mode}"
}