-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathgen-pack
More file actions
451 lines (393 loc) · 13.7 KB
/
Copy pathgen-pack
File metadata and controls
451 lines (393 loc) · 13.7 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
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
#
# Open-CMSIS-Pack gen-pack Bash library
#
# Copyright (c) 2022-2026 Arm Limited. All rights reserved.
#
# Provided as-is without warranty under Apache 2.0 License
# SPDX-License-Identifier: Apache-2.0
#
GEN_PACK_LIB_VERSION="0.14.0"
GEN_PACK_LIB_SOURCE="$(realpath "$(dirname "${BASH_SOURCE[0]}")")"
GEN_PACK_SCRIPT_SOURCE="$(dirname "$(readlink -f "$0")")"
SCRIPT="${SCRIPT:-$(basename "$0")}"
LOG_PREFIX="${LOG_PREFIX:-"$(tput setaf 4 2>/dev/null)${SCRIPT}>$(tput sgr0 2>/dev/null) "}"
shopt -s expand_aliases globstar
if [ "${BASH_VERSION%%.*}" -lt 5 ]; then
echo "${LOG_PREFIX}gen-pack requires Bash version 5 or later." >&2
echo "${LOG_PREFIX}Running Bash version is ${BASH_VERSION}!" >&2
echo "${LOG_PREFIX}" >&2
ENV_BASH="$(which bash)"
ENV_BASH_VERSION="$("${ENV_BASH}" --version | head -n1 | grep -Eo "version [1-9]+\\.[0-9]+\\.[0-9]+" | grep -Eo "[1-9]+\\.[0-9]+\\.[0-9]+" )"
if [ "${ENV_BASH_VERSION%%.*}" -lt 5 ]; then
case $(uname -s) in
'WindowsNT'|MINGW*|CYGWIN*)
echo "${LOG_PREFIX}Update MSYS2/MinGW, or install latest Git for Windows." >&2
;;
'Darwin')
echo "${LOG_PREFIX}Get Homebrew and run: brew install bash." >&2
;;
*)
echo "${LOG_PREFIX}Check your OS package manager for Bash v5." >&2
;;
esac
else
echo "${LOG_PREFIX}Bash version ${ENV_BASH_VERSION} found at ${ENV_BASH}." >&2
echo "${LOG_PREFIX}" >&2
echo "${LOG_PREFIX}Check the shebang line of the enclosing ${SCRIPT} script." >&2
echo "${LOG_PREFIX}To be OS agnostic the shebang should be" >&2
echo "${LOG_PREFIX} #!/usr/bin/env bash" >&2
fi
echo "${LOG_PREFIX}" >&2
exit 1
fi
echo "${LOG_PREFIX}Loading gen-pack library ${GEN_PACK_LIB_VERSION} from ${GEN_PACK_LIB_SOURCE}"
. "${GEN_PACK_LIB_SOURCE}/lib/patches"
. "${GEN_PACK_LIB_SOURCE}/lib/usage"
. "${GEN_PACK_LIB_SOURCE}/lib/getopts"
. "${GEN_PACK_LIB_SOURCE}/lib/utilities"
. "${GEN_PACK_LIB_SOURCE}/lib/pdsc"
. "${GEN_PACK_LIB_SOURCE}/lib/gittools"
. "${GEN_PACK_LIB_SOURCE}/lib/logging"
. "${GEN_PACK_LIB_SOURCE}/lib/helper"
#
# Add directories listed in PACK_DIRS to build folder.
# An empty PACK_DIRS setting picks up all folders.
#
# Usage: add_dirs <build>
# <build> The temporary build folder, typically PACK_BUILD
#
function add_dirs {
# Add directories
if [ -z "${PACK_DIRS}" ]; then
echo_log "PACK_DIRS is empty, defaulting to all folders."
PACK_DIRS=$(OIFS="$IFS"; IFS=$'\n'; find . -maxdepth 1 -type d -not -name ".*" -not -path "./$(realpath --relative-to="$(pwd)" "${PACK_BUILD}")" -not -path "./$(realpath --relative-to="$(pwd)" "${PACK_OUTPUT}")"; IFS="$OIFS")
fi
echo_log Adding directories to pack:
echo_log "${PACK_DIRS}"
echo_log " "
for d in ${PACK_DIRS}; do
d=$(realpath --relative-to="$(pwd)" "$d")
local tar_opts=(--exclude-vcs --exclude-ignore=.gpignore --exclude=.gpignore)
if [[ $d = ../* ]]; then
tar "${tar_opts[@]}" -C "$(dirname "$d")" -cf - "$(basename "$d")" | tar -C "$1" -xf -
else
tar "${tar_opts[@]}" -cf - "$d" | tar -C "$1" -xf -
fi
done
}
#
# Add single files listed in PACK_BASE_FILES to build folder.
#
# Usage: add_files <build>
# <build> The temporary build folder, typically PACK_BUILD
#
#
function add_files {
# Add files
if [ -n "${PACK_BASE_FILES}" ]; then
echo_log Adding files to pack:
echo_log "${PACK_BASE_FILES}"
echo_log " "
for pattern in ${PACK_BASE_FILES}; do
d=$(realpath --relative-to="$(pwd)" "$pattern")
if [[ $pattern = ../* ]]; then
cp -r "$pattern" "$1"
else
cp -f --parents "$pattern" "$1/"
fi
done
else
echo_log "PACK_BASE_FILES is empty, no additional files to be copied."
fi
}
#
# Remove specific files listed in PACK_DELETE_FILES from build folder.
#
# Usage: delete_files <build>
# <build> The temporary build folder, typically PACK_BUILD
#
function delete_files {
local build="$1"
# Delete files
if [ -n "${PACK_DELETE_FILES}" ]; then
echo_log Deleting files from pack:
echo_log "${PACK_DELETE_FILES}"
echo_log " "
local patterns
split_and_trim patterns "${PACK_DELETE_FILES}"
for pattern in "${patterns[@]}"; do
if ! is_subpath_of "${build}" "${build}/${pattern}"; then
echo_err "Skipping '${pattern}' as it resolves to outside the build folder."
continue
fi
# shellcheck disable=SC2206
local files=("${build}/"${pattern})
for file in "${files[@]}"; do
if is_subpath_of "${build}" "${file}"; then
echo_v "Removing '${file}' ..."
rm -rf "${file}"
else
echo_err "Skipping '${file}' as it resolves to outside the build folder."
fi
done
done
else
echo_log "PACK_DELETE_FILES is empty, no files to be removed."
fi
}
#
# Apply patches listed in PACK_PATCH_FILES to files in build folder.
#
# Usage: apply_patches <build>
# <build> The temporary build folder, typically PACK_BUILD
#
function apply_patches {
if [ -z "${PACK_PATCH_FILES}" ]; then
echo_log "PACK_PATCH_FILES is empty, no patches to be applied."
return
fi
echo_log Applying patches to pack:
echo_log "${PACK_PATCH_FILES}"
echo_log " "
local src dest
src="$(cwd)"
dest="${1:-${PACK_BUILD}}"
pushd "${dest}" > /dev/null || exit
for patchfile in ${PACK_PATCH_FILES}; do
local patchfile="${src}/${patchfile}"
local targets
declare -A style
style["${patchfile}"]="$(detect_eol_style "${patchfile}")"
convert_eol "LF" "${patchfile}"
mapfile -t targets < <( \
patch --dry-run -p0 -t -i "${patchfile}" | \
grep "ing file" | \
sed -E "s/.*ing file '?([^']*)'?/\1/" \
)
for target in "${targets[@]}"; do
style["${target}"]="$(detect_eol_style "${target}")"
convert_eol "LF" "${target}"
done
patch -p0 -t -i "${patchfile}"
# Revert EOL style
for target in "${targets[@]}"; do
convert_eol "${style["${target}"]}" "${target}"
done
convert_eol "${style["${patchfile}"]}" "${patchfile}"
done
popd > /dev/null || exit
}
#
# Run pack schema check
#
# Usage: check_schema <pdsc>
# <pdsc> The pack description to check
#
function check_schema {
if [ -z "${UTILITY_XMLLINT}" ]; then
echo_err "Cannot run schema check when missing xmllint utility!"
return 1
fi
echo_log "Running schema check for $1"
local CMSISSCHEMA
CMSISSCHEMA=$(realpath -m "${CMSIS_TOOLSDIR:-.}/../PACK.xsd")
local SCHEMA="${CMSISSCHEMA}"
if [ ! -f "$SCHEMA" ]; then
echo_log "Fetching schema file..."
local SCHEMAURL
SCHEMAURL=$(grep -Pio "noNamespaceSchemaLocation=\"\K[^\"]+" "$1")
if [ -z "${SCHEMAURL}" ]; then
echo_err "PDSC file is missing schema url. Consider adding attribute 'noNamespaceSchemaLocation' to '<package>' tag."
fi
local SCHEMAVERSION
SCHEMAVERSION=$(grep -Pio "schemaVersion=\"\K[^\"]+" "$1")
if [ -z "${SCHEMAVERSION}" ]; then
echo_err "PDSC file is missing schema version. Consider adding attribute 'schemaVersion' to '<package>' tag."
fi
local SCHEMA="${TEMP:-/tmp}/PACK.xsd"
local errorlevel=1
if [ -n "${SCHEMAURL}" ]; then
curl_download "${SCHEMAURL}" "${SCHEMA}"
errorlevel=$?
fi
if [ -n "${SCHEMAVERSION}" ] && [ $errorlevel -ne 0 ]; then
echo_err "Schema referenced in PDSC file not found. Looking for schema with version '${SCHEMAVERSION}' ..."
curl_download "https://raw.githubusercontent.com/Open-CMSIS-Pack/Open-CMSIS-Pack-Spec/v${SCHEMAVERSION}/schema/PACK.xsd" "${SCHEMA}"
errorlevel=$?
fi
if [ $errorlevel -ne 0 ]; then
echo_err "Schema referenced by PDSC file's schema version not found. Looking for latest schema ..."
curl_download "https://raw.githubusercontent.com/Open-CMSIS-Pack/Open-CMSIS-Pack-Spec/main/schema/PACK.xsd" "${SCHEMA}"
errorlevel=$?
fi
if [ $errorlevel -ne 0 ]; then
echo_err "build aborted: No schema file could be found!"
echo_log " "
echo_log " Hint: Assure one of the following to get the schema check working."
echo_log " - Provide PACK.XSD into '${CMSISSCHEMA}'."
echo_log " - Provide schema URL in PDSC file's xs:noNamespaceSchemaLocation attribute."
echo_log " - Provide schema release tag version in PDSC file's schemaVersion attribute."
echo_log " Available schema releases are listed on https://github.com/Open-CMSIS-Pack/Open-CMSIS-Pack-Spec/tags."
echo_log " "
exit $errorlevel
fi
fi
echo_v "\"${UTILITY_XMLLINT}\" --noout --schema \"${SCHEMA}\" \"$1\""
"${UTILITY_XMLLINT}" --noout --schema "${SCHEMA}" "$1"
local errorlevel=$?
if [ $errorlevel -ne 0 ]; then
echo_err "build aborted: Schema check of $1 against ${SCHEMA} failed!"
echo_err " "
exit 1
fi
}
#
# Run packchk
#
# Usage: check_pack <pdsc>
# <pdsc> The pack description to check
#
function check_pack {
PACKCHK_DEPS="${PACKCHK_DEPS:-ARM.CMSIS.pdsc}"
local dependencies include=()
mapfile -t dependencies < <( \
grep -Pio "<package\s+\Kvendor=\"[^\"]+\"\s+name=\"[^\"]+\"" "$1" | \
sed -E 's/vendor="([^"]*)"[ ]*name="([^"]*)"/\1.\2.pdsc/' \
)
# shellcheck disable=SC2206
dependencies+=(${PACKCHK_DEPS})
fetch_pdsc_files include "${dependencies[@]}"
if [ "${UTILITY_PACKCHK_HAS_SCHEMACHECK:-1}" -eq 0 ]; then
PACKCHK_ARGS+=(--disable-validation)
fi
# shellcheck disable=SC2086
echo_v "\"${UTILITY_PACKCHK}\" ${include[*]/#/-i } -n \"${PACK_OUTPUT}/PackName.txt\"" "${PACKCHK_ARGS[@]}" \"$1\"
# shellcheck disable=SC2048,SC2086
"${UTILITY_PACKCHK}" ${include[*]/#/-i } -n "${PACK_OUTPUT}/PackName.txt" "${PACKCHK_ARGS[@]}" "$1"
local errorlevel=$?
if [ $errorlevel -ne 0 ]; then
echo_err "build aborted: pack check failed"
echo_err " "
exit 1
fi
}
#
# Create checksum file for the pack content
# The checksum file is placed to <build>/<vendor>.<pack>.sha1
#
# By default all packaged files are considered.
# Files matching a pattern specified in PACK_CHECKSUM_EXCLUDE are excluded.
#
# Usage: create_sha1 <build> <vendor> <pack>
# <build> Pack build folder
# <vendor> Pack vendor name
# <pack> Pack name
#
function create_sha1 {
pushd "$1" > /dev/null || exit
local ignore
local find_args=()
mapfile -t ignore <<< "${PACK_CHECKSUM_EXCLUDE:-}"
for p in "${ignore[@]}"; do
p="${p#"${p%%[![:space:]]*}"}"
p="${p%"${p##*[![:space:]]}"}"
if [ -n "$p" ]; then
find_args+=("-not" "-path" "*$p")
fi
done
find . -type f "${find_args[@]}" -exec "${UTILITY_SHA1SUM}" -b {} + > "${TEMP:-/tmp}/$2.$3.sha1"
if [ -s "${TEMP:-/tmp}/$2.$3.sha1" ]; then
mv "${TEMP:-/tmp}/$2.$3.sha1" "$2.$3.sha1"
fi
popd > /dev/null || exit
}
#
# Top level function to generate a pack.
#
function gen_pack {
getopts "$@"
set -- "${POSITIONAL[@]}" # restore positional parameters
echo_log "Starting CMSIS-Pack Generation: $(date)"
echo_log " in $(pwd)"
echo_log ""
check_shebang
check_locale
check_placeholder "PACK_DIRS"
check_placeholder "PACK_BASE_FILES"
check_placeholder "PACK_DELETE_FILES"
check_placeholder "PACK_PATCH_FILES"
check_placeholder "PACKCHK_DEPS"
PACK_CHANGELOG_MODE="${PACK_CHANGELOG_MODE:-full}"
check_placeholder "PACK_CHANGELOG_MODE"
check_placeholder "PACK_CHECKSUM_EXCLUDE"
find_pack_root
find_zip
find_packchk
find_xmllint
find_sha1sum
find_curl
if [ -n "${CHANGELOG+x}" ]; then
find_git
case ${PACK_CHANGELOG_MODE} in
'full'|'release')
find_ghcli
;;
'tag')
echo_v "Skipping ghcli"
;;
*)
echo_err "PACK_CHANGELOG_MODE has invalid value '${PACK_CHANGELOG_MODE}'!"
echo_log " Valid options are: full, release, or tag."
echo_log " Defaulting to: full"
PACK_CHANGELOG_MODE="full"
find_ghcli
;;
esac
fi
find_eol_converter
echo_log " "
PACK_DESCRIPTION_FILE=$(locate_pdsc "$1")
fail_and_bail $?
echo_log "PDSC is located in ${PACK_DESCRIPTION_FILE}"
echo_log " "
PACK_BASEDIR=$(dirname "${PACK_DESCRIPTION_FILE}")
PACK_VENDOR=$(pdsc_vendor "${PACK_DESCRIPTION_FILE}")
PACK_NAME=$(pdsc_name "${PACK_DESCRIPTION_FILE}")
echo_log "Generating Pack for ${PACK_VENDOR}.${PACK_NAME}"
PACK_BUILD=$(realpath "${PACK_BASEDIR}/${PACK_BUILD:-build}")
PACK_OUTPUT=$(realpath "${PACK_BASEDIR}/${PACK_OUTPUT:-output}")
PACK_BUILD_DESCRIPTION_FILE="${PACK_BUILD}/$(basename "${PACK_DESCRIPTION_FILE}")"
echo_log " via ${PACK_BUILD}"
echo_log " to ${PACK_OUTPUT}"
echo_log ""
if [ -d "${PACK_BUILD}" ]; then
rm -r "${PACK_BUILD}"
fi
mkdir -p "${PACK_BUILD}"
mkdir -p "${PACK_OUTPUT}"
pushd "${PACK_BASEDIR}" >/dev/null || exit
[[ ${PREPROCESS} == 1 && "$(type -t preprocess)" == "function" ]] && preprocess "${PACK_BUILD}"
if [ -n "${CHANGELOG+x}" ]; then
echo_log "Generating changelog with mode '${PACK_CHANGELOG_MODE}' ..."
pdsc_update_releases "${PACK_DESCRIPTION_FILE}" "${PACK_BUILD_DESCRIPTION_FILE}" "${CHANGELOG}"
else
cp "${PACK_DESCRIPTION_FILE}" "${PACK_BUILD_DESCRIPTION_FILE}"
fi
add_dirs "${PACK_BUILD}"
add_files "${PACK_BUILD}"
delete_files "${PACK_BUILD}"
apply_patches "${PACK_BUILD}"
[[ ${POSTPROCESS} == 1 && "$(type -t postprocess)" == "function" ]] && postprocess "${PACK_BUILD}"
check_schema "${PACK_BUILD_DESCRIPTION_FILE}"
check_pack "${PACK_BUILD_DESCRIPTION_FILE}"
create_sha1 "${PACK_BUILD}" "${PACK_VENDOR}" "${PACK_NAME}"
PACK_ARCHIVE="${PACK_OUTPUT}/$(cat "${PACK_OUTPUT}/PackName.txt")"
archive "${PACK_BUILD}" "${PACK_ARCHIVE}"
if [[ $KEEP == 0 ]]; then
echo_log "Cleaning up ${PACK_BUILD}..."
rm -rf "${PACK_BUILD}"
rm -f "${PACK_OUTPUT}/PackName.txt"
fi
echo_log ""
echo_log "${LOG_COLOR_GREEN}Pack $(basename "${PACK_ARCHIVE}") generated successfully!"
}