Add array variable support to ossbuild-project-var-set/get

Use the new functionality for new project specific array variables:
- CONFIGURE_VARS: elements of the form NAME=VALUE will be set as
  environment variables when running autoconfig configure.
- CONFIGURE_OPTS: elements will be added to the options passed to
  autoconfig configure.
This commit is contained in:
Kai Tetzlaff (kai+qnas) 2024-05-18 01:49:23 +02:00
parent 99a79e2218
commit 9c9e91848a

View File

@ -22,6 +22,11 @@ is-dry-run() {
! [ ${DRY_RUN:-0} -eq 0 ]
}
#D# Check if variable is an array.
ossbuild-var-array-p() {
declare -p "${1}" 2> /dev/null | grep -Eq -- '^declare -a'
}
#D# Wrapper around pushd.
#
# The wrapper provides the following features:
@ -116,8 +121,8 @@ ossbuild-project-var-get() {
}
name="${positional[0]}"
unset default
[ "${#positional[*]}" -le 1 ] || local default="${positional[1]}"
local default
[ "${#positional[*]}" -le 1 ] || default="${positional[1]}"
vname="$(ossbuild-project-var "${name}")"
#echo "2/ossbuild-project-var-get: ${vname}${default:+, default=${default}}" >&2
@ -131,12 +136,22 @@ ossbuild-project-var-get() {
fi
}
#echo "R/ossbuild-project-var-get: ${vname}='${!vname}'" >&2
local -n pvar_ref="${vname}"
if [ -v outvar ]; then
local -n ovar_ref="${outvar}"
# shellcheck disable=SC2034
printf -v ovar_ref '%s' "${!vname}"
if ossbuild-var-array-p "${vname}"; then
ovar_ref=("${pvar_ref[@]}")
else
ovar_ref="${pvar_ref}"
fi
else
printf '%s' "${!vname}"
if ossbuild-var-array-p "${vname}"; then
for v in "${pvar_ref[@]}"; do
printf '%s\n' "${v}"
done
else
printf '%s' "${pvar_ref}"
fi
fi
}
@ -151,10 +166,51 @@ ossbuild-project-var-get() {
#
# stdout (str): na
ossbuild-project-var-set() {
local name="${1}" value="${2}" vname
#echo "1/ossbuild-project-var-set: ${@}" >&2
local quiet=0 array=0 name vname
local -a positional
while [ ${#} -gt 0 ]; do
case "${1}" in
-q | --quiet)
((++quiet))
;;
-a)
array=1
;;
-*)
# ignore unknown option
;;
*)
positional+=("${1}")
;;
esac
shift
done
[ "${#positional[*]}" -gt 0 ] || {
bsl_logfe "missing project variable name"
return 10
}
name="${positional[0]}"
vname="$(ossbuild-project-var "${name}")"
bsl_logi "set project variable: ${vname}='${value}'"
declare -g "${vname}=${value}"
unset positional[0]
set "${positional[@]}"
[ "${#positional[@]}" -le 1 ] || array=1
unset "${vname}" # make sure that we start with a clean state
local msg='set project variable: '
if [ "${array}" -eq 0 ]; then
local value="${@}"
msg="${msg}${vname}=${value@Q}"
declare -g "${vname}=${value}"
else
local value=("${@}")
msg="${msg}${vname}=(${value[*]@Q})"
declare -ga "${vname}"
declare -n pvar_ref="${vname}"
pvar_ref=("${value[@]}")
fi
[ "${quiet}" -gt 0 ] || bsl_logi "${msg}"
}
#D# Unset (delete) project specific variable.
@ -507,12 +563,16 @@ ossbuild-autotools-build() {
|| configure_ok_p_fn='ossbuild-autotools-configure-ok-p'
if ! "${configure_ok_p_fn}" "${build_tree}"; then
bsl_logi "run configure ..."
local configure=(
local -a configure=(
"${src_tree}/configure"
--prefix="${PREFIX}"
"${configure_extra_opts[@]}"
)
ossbuild-cmd "${configure[@]}"
local -a vars opts
ossbuild-project-var-get -q -v vars CONFIGURE_VARS || true
ossbuild-project-var-get -q -v opts CONFIGURE_OPTS || true
vars+=("PKG_CONFIG_PATH=${PREFIX}/lib/pkgconfig")
[ -n "${opts[*]}" ] || configure+=("${opts[@]}")
ossbuild-cmd env "${vars[@]}" "${configure[@]}"
fi
ossbuild-cmd make