#!/bin/sh # # usage: run-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 [args...]" >&2 exit 1 fi # Accepting -std=c++20 is not enough: src/rwlock.hpp needs C++20 atomic # waiting, which GCC 10 lacks. cxx20_ok() { printf '%s\n' \ '#include ' \ 'int main(){std::atomic v{0}; v.notify_all(); v.wait(1); return 0;}' | \ "${CXX:-g++}" -std=c++20 -x c++ -fsyntax-only - >/dev/null 2>&1 } # Print " " 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 "$@"