Gentoo YUCKFLAGS QA script

The following QA script for Portage checks built objects for undesirable CFLAGS - YUCKFLAGS. The example list is a bunch of flags that are infamous for breaking common programmer assumptions, which is whence my interest in something like this came. When I asked #gentoo whether Portage has a mechanism for this, Sam James pointed me to the ignored-CFLAGS QA script and was interested in the results, so I'm publishing the thing here.

Install the script into /usr/lib/portage/python3.x/install-qa-check.d/ (after reading it of course, don't just add random shit to your package manager) and remember to add -frecord-gcc-switches to your CFLAGS so the flags are actually recorded. It may make sense to replace the local definition of YUCKFLAGS with one in make.conf.

# QA checks for flags that may be a bad upstream decision.
# Based on the QA script for ignored CFLAGS.
# Without -frecord-gcc-switches, this reports nothing at all.

yuckflag_check() {
	local YUCKFLAGS="-O3 -ffast-math -fstrict-aliasing"
	local output_file="${T}/YUCKFLAGS.log"
	local temp_file="${T}/YUCKFLAGS.tmp"

	type -P scanelf > /dev/null || return
	type -P objdump > /dev/null || return
	has binchecks ${PORTAGE_RESTRICT} && return

	local x
	rm -f "${output_file}"

	for x in $(scanelf -qyRF '#k%p' -k '.GCC.command.line' "${ED%/}/") ; do
		local file="${ED%/}/${x}"
		local offset length flag
		offset=$(objdump -h "${file}" | grep '\.GCC\.command\.line' | cut -d ' ' -f 10)
		offset=$((16#${offset} + 1)) # tail is 1-indexed
		length=$(objdump -h "${file}" | grep '\.GCC\.command\.line' | cut -d ' ' -f 4)
		length=$((16#${length}))

		rm -f "${temp_file}"
		for flag in ${YUCKFLAGS} ; do
			if tail -c "+${offset}" <"${file}" | head -c "${length}" | grep -a -F -- "${flag}" >/dev/null ; then
				echo -n " ${flag}" >> "${temp_file}"
			fi
		done

		if [[ -f "${temp_file}" ]] ; then
			echo -n "/${x}" >> "${output_file}"
			cat "${temp_file}" >> "${output_file}"
			echo >> "${output_file}"
		fi
	done
	rm -f "${temp_file}"

	if [[ -f "${output_file}" ]] ; then
		local summary=$(<"${output_file}")
		__vecho -ne '\n'
		eqawarn "QA Notice: The following files were built with questionable flags:"
		eqawarn "${summary}"
		__vecho -ne '\n'
		sleep 1
	fi
}

yuckflag_check
: # guarantee successful exit

# vim:ft=sh

Date: 2022-02-01 11:18:24