From 18e49f2c97f0e33a645f364ed9de8e3da6c36d41 Mon Sep 17 00:00:00 2001 From: Levente Polyak Date: Tue, 8 Nov 2022 20:32:52 +0100 Subject: [PATCH] debugflags: ensure to only append debug flags once when building During a package build we call prepare_buildenv in multiple stages of the process. For debug packages, one of the hooks is buildenv_debugflags which populates the debug flags to the according variables. The issue is that the behavior of the current implementation of buildenv_debugflags is not idempotent, so consecutive calls will append the same flags again. In certain cases this isn't an issue, however for context aware build frontends like cargo any change of the build inputs leads to a fresh build. This means that any invocation of such a build ecosystem inside the package() function will trigger a full rebuild, which is not desired. To fix this issue, this commit makes buildenv_debugflags idempotent by only appending flags once to the target variables. Signed-off-by: Levente Polyak --- scripts/libmakepkg/buildenv/debugflags.sh.in | 9 +++++---- scripts/libmakepkg/buildenv/lto.sh.in | 7 ++++--- scripts/libmakepkg/buildenv/rust.sh.in | 5 +++-- scripts/libmakepkg/util/util.sh.in | 10 ++++++++++ 4 files changed, 22 insertions(+), 9 deletions(-) diff --git a/scripts/libmakepkg/buildenv/debugflags.sh.in b/scripts/libmakepkg/buildenv/debugflags.sh.in index 71022836..22a79281 100644 --- a/scripts/libmakepkg/buildenv/debugflags.sh.in +++ b/scripts/libmakepkg/buildenv/debugflags.sh.in @@ -25,14 +25,15 @@ LIBMAKEPKG_BUILDENV_DEBUGFLAGS_SH=1 MAKEPKG_LIBRARY=${MAKEPKG_LIBRARY:-'@libmakepkgdir@'} source "$MAKEPKG_LIBRARY/util/option.sh" +source "$MAKEPKG_LIBRARY/util/util.sh" buildenv_functions+=('buildenv_debugflags') buildenv_debugflags() { if check_option "debug" "y" && ! check_option "buildflags" "n"; then - DEBUG_CFLAGS+=" -ffile-prefix-map=$srcdir=${DBGSRCDIR:-/usr/src/debug}/${pkgbase}" - DEBUG_CXXFLAGS+=" -ffile-prefix-map=$srcdir=${DBGSRCDIR:-/usr/src/debug}/${pkgbase}" - CFLAGS+=" $DEBUG_CFLAGS" - CXXFLAGS+=" $DEBUG_CXXFLAGS" + append_once DEBUG_CFLAGS "-ffile-prefix-map=$srcdir=${DBGSRCDIR:-/usr/src/debug}/${pkgbase}" + append_once DEBUG_CXXFLAGS "-ffile-prefix-map=$srcdir=${DBGSRCDIR:-/usr/src/debug}/${pkgbase}" + append_once CFLAGS "$DEBUG_CFLAGS" + append_once CXXFLAGS "$DEBUG_CXXFLAGS" fi } diff --git a/scripts/libmakepkg/buildenv/lto.sh.in b/scripts/libmakepkg/buildenv/lto.sh.in index ebaa3656..290ad925 100644 --- a/scripts/libmakepkg/buildenv/lto.sh.in +++ b/scripts/libmakepkg/buildenv/lto.sh.in @@ -25,14 +25,15 @@ LIBMAKEPKG_BUILDENV_LTO_SH=1 MAKEPKG_LIBRARY=${MAKEPKG_LIBRARY:-'@libmakepkgdir@'} source "$MAKEPKG_LIBRARY/util/option.sh" +source "$MAKEPKG_LIBRARY/util/util.sh" build_options+=('lto') buildenv_functions+=('buildenv_lto') buildenv_lto() { if check_option "lto" "y" && ! check_option "buildflags" "n"; then - CFLAGS+=" ${LTOFLAGS:--flto}" - CXXFLAGS+=" ${LTOFLAGS:--flto}" - LDFLAGS+=" ${LTOFLAGS:--flto}" + append_once CFLAGS "${LTOFLAGS:--flto}" + append_once CXXFLAGS "${LTOFLAGS:--flto}" + append_once LDFLAGS "${LTOFLAGS:--flto}" fi } diff --git a/scripts/libmakepkg/buildenv/rust.sh.in b/scripts/libmakepkg/buildenv/rust.sh.in index 6d2d378d..94d68f87 100644 --- a/scripts/libmakepkg/buildenv/rust.sh.in +++ b/scripts/libmakepkg/buildenv/rust.sh.in @@ -24,13 +24,14 @@ LIBMAKEPKG_BUILDENV_RUST_SH=1 MAKEPKG_LIBRARY=${MAKEPKG_LIBRARY:-'@libmakepkgdir@'} source "$MAKEPKG_LIBRARY/util/option.sh" +source "$MAKEPKG_LIBRARY/util/util.sh" buildenv_var+=('RUSTFLAGS' 'DEBUG_RUSTFLAGS') buildenv_functions+=('buildenv_rust') buildenv_rust() { if check_option "debug" "y" && ! check_option "buildflags" "n"; then - DEBUG_RUSTFLAGS+=" --remap-path-prefix=$srcdir=${DBGSRCDIR:-/usr/src/debug}/${pkgbase}" - RUSTFLAGS+=" $DEBUG_RUSTFLAGS" + append_once DEBUG_RUSTFLAGS "--remap-path-prefix=$srcdir=${DBGSRCDIR:-/usr/src/debug}/${pkgbase}" + append_once RUSTFLAGS "$DEBUG_RUSTFLAGS" fi } \ No newline at end of file diff --git a/scripts/libmakepkg/util/util.sh.in b/scripts/libmakepkg/util/util.sh.in index bf3a83e2..83b8e57d 100644 --- a/scripts/libmakepkg/util/util.sh.in +++ b/scripts/libmakepkg/util/util.sh.in @@ -112,3 +112,13 @@ source_safe() { eval "$shellopts" } + +# Append a string to a variable if the value is not already contained. +# usage : append_once( $ref, $value ) +append_once() { + local -n var=$1 + local value=$2 + if [[ ! $var =~ (^| )"$value"($| ) ]]; then + var+=" $value" + fi +}