mergerfs/buildtools/run-rpmbuild
Antonio SJ Musumeci d83e01b40a build: select a working C++20 compiler for RPM builds
A compiler activated while installing dependencies does not survive in
the caller's shell, so make rpm could fall back to one that can't
compile mergerfs. Route the RPM build through buildtools/run-rpmbuild,
which keeps an explicit compiler and otherwise activates an installed
GCC Toolset when the default can't handle C++20. RPMBUILD and
RPMBUILD_FLAGS override the rpmbuild command.

Rocky 8's toolsets lack the annobin plugin the distribution's RPM
flags load. Test the effective compiler with the RPM flags and pass
--undefine _annotated_build only when that fails, instead of guessing
from the compiler's path in build-mergerfs. Native and container
builds use the same check.
2026-11-29 23:11:08 -05:00

85 lines
2.3 KiB
Bash
Executable file

#!/bin/sh
#
# usage: run-rpmbuild <rpmbuild> [args...]
#
# Run rpmbuild with a C++20 capable compiler.
#
# If CXX is unset and the default g++ can't build C++20 (ex: EL8's g++
# 8) the newest installed GCC Toolset / devtoolset is activated.
#
# redhat-rpm-config's %{optflags} load the annobin GCC plugin which
# toolset compilers often don't have ("inaccessible plugin file
# plugin/annobin.so"). If the selected compiler fails with %{optflags}
# but works with annotation disabled `--undefine _annotated_build` is
# passed to rpmbuild.
set -e
if [ $# -lt 1 ]; then
echo "usage: $0 <rpmbuild> [args...]" >&2
exit 1
fi
cxx20_ok()
{
printf 'int main(){return 0;}\n' | \
"${CXX:-g++}" -std=c++20 -x c++ -fsyntax-only - >/dev/null 2>&1
}
# Print "<version> <path>" for each toolset activation script.
toolset_scripts()
{
for f in \
/usr/lib/gcc-toolset/*-env.source \
/opt/rh/gcc-toolset-*/enable \
/opt/rh/devtoolset-*/enable
do
[ -f "$f" ] || continue
v="$(printf '%s\n' "$f" | sed -n \
-e 's,^/usr/lib/gcc-toolset/\([0-9][0-9]*\)-env\.source$,\1,p' \
-e 's,^/opt/rh/gcc-toolset-\([0-9][0-9]*\)/enable$,\1,p' \
-e 's,^/opt/rh/devtoolset-\([0-9][0-9]*\)/enable$,\1,p')"
[ -n "$v" ] && printf '%s %s\n' "$v" "$f"
done
}
activate_toolset()
{
for f in $(toolset_scripts | sort -k1,1nr | cut -d' ' -f2); do
if (. "$f" || :; cxx20_ok); then
echo "run-rpmbuild: activating $f" >&2
. "$f" || :
export CC=gcc
export CXX=g++
return 0
fi
done
return 1
}
optflags_ok()
{
flags="$(rpm "$@" --eval '%{optflags}')" || return 1
printf 'int main(){return 0;}\n' | \
"${CXX:-g++}" $flags -x c++ -c -o /dev/null - >/dev/null 2>&1
}
if [ -z "${CXX}" ] && ! cxx20_ok; then
if ! activate_toolset; then
echo "run-rpmbuild: warning: g++ lacks C++20 support and no" \
"usable GCC Toolset was found (see buildtools/install-build-pkgs)" >&2
fi
fi
extra_flags=
if command -v rpm >/dev/null 2>&1 && \
! optflags_ok && \
optflags_ok --undefine _annotated_build
then
echo "run-rpmbuild: ${CXX:-g++} can't use annobin; disabling _annotated_build" >&2
extra_flags="--undefine _annotated_build"
fi
rpmbuild="$1"
shift
exec "$rpmbuild" $extra_flags "$@"