# shellcheck shell=bash

# QA checks for ignored *FLAGS.

ignored_flag_check() {
	if ! type -P scanelf >/dev/null || contains_word binchecks "${PORTAGE_RESTRICT}"; then
		return
	fi

	local qa_var="QA_FLAGS_IGNORED_${ARCH/-/_}"
	eval "[[ -n \${!qa_var} ]] && QA_FLAGS_IGNORED=(\"\${${qa_var}[@]}\")"
	if [[ ${#QA_FLAGS_IGNORED[@]} -eq 1 ]] ; then
		local shopts=$-
		set -o noglob
		QA_FLAGS_IGNORED=(${QA_FLAGS_IGNORED})
		set +o noglob
		set -${shopts}
	fi

	local f x

	# Check for files built without respecting *FLAGS. Note that
	# -frecord-gcc-switches must be in all *FLAGS variables, in
	# order to avoid false positive results here.
	# NOTE: This check must execute before estrip --prepallstrip,
	# since estrip strips the .GCC.command.line sections.
	if [[ "${CFLAGS}" == *-frecord-gcc-switches* ]] && \
	[[ "${CXXFLAGS}" == *-frecord-gcc-switches* ]] && \
	[[ "${FFLAGS}" == *-frecord-gcc-switches* ]] && \
	[[ "${FCFLAGS}" == *-frecord-gcc-switches* ]] ; then
		rm -f "${T}"/scanelf-ignored-CFLAGS.log
		for x in $(scanelf -qyRF '#k%p' -k '!.GCC.command.line' "${ED%/}/") ; do
			# Separate out file types that are known to support
			# .GCC.command.line sections, using the `file` command
			# similar to how estrip uses it.
			f=$(file -S "${x}") || continue
			[[ -z ${f} ]] && continue
			if [[ ${f} == *"SB executable"* || ${f} == *"SB pie executable"* ||
				${f} == *"SB shared object"* ]] ; then
				echo "${x}" >> "${T}"/scanelf-ignored-CFLAGS.log
			fi
		done

		if [[ -f "${T}"/scanelf-ignored-CFLAGS.log ]] ; then

			if [[ ! -v QA_STRICT_FLAGS_IGNORED ]] ; then
				for x in "${QA_FLAGS_IGNORED[@]}" ; do
					sed -e "s#^${x#/}\$##" -i "${T}"/scanelf-ignored-CFLAGS.log
				done
			fi
			# Filter anything under /usr/lib/debug/ in order to avoid
			# duplicate warnings for splitdebug files.
			sed -e "s#^usr/lib/debug/.*##" -e "/^\$/d" -e "s#^#/#" \
				-i "${T}"/scanelf-ignored-CFLAGS.log
			f=$(<"${T}"/scanelf-ignored-CFLAGS.log)
			if [[ -n ${f} ]] ; then
				__vecho -ne '\n'
				eqawarn "${PORTAGE_COLOR_BAD}QA Notice: Files built without respecting CFLAGS have been detected${PORTAGE_COLOR_NORMAL}"
				eqawarn " Please include the following list of files in your report:"
				eqawarn "${f}"
				__vecho -ne '\n'
				sleep 1
			else
				rm -f "${T}"/scanelf-ignored-CFLAGS.log
			fi
		fi
	fi

	# Check for files built without respecting LDFLAGS
	if [[ "${LDFLAGS}" == *,--defsym=__gentoo_check_ldflags__* ]] \
		&& ! contains_word binchecks "${PORTAGE_RESTRICT}"
	then
		f=$(LC_ALL=C comm -2 -3 <(scanelf -qyRF '#k%p' -k .dynsym "${ED%/}/" | LC_ALL=C sort) \
			<(scanelf -qyRF '#s%p' -s __gentoo_check_ldflags__ "${ED%/}/" | LC_ALL=C sort))
		if [[ -n ${f} ]] ; then
			echo "${f}" > "${T}"/scanelf-ignored-LDFLAGS.log
			if [[ ! -v QA_STRICT_FLAGS_IGNORED ]] ; then
				for x in "${QA_FLAGS_IGNORED[@]}" ; do
					sed -e "s#^${x#/}\$##" -i "${T}"/scanelf-ignored-LDFLAGS.log
				done
			fi
			# Filter anything under /usr/lib/debug/ in order to avoid
			# duplicate warnings for splitdebug files.
			sed -e "s#^usr/lib/debug/.*##" -e "/^\$/d" -e "s#^#/#" \
				-i "${T}"/scanelf-ignored-LDFLAGS.log
			f=$(<"${T}"/scanelf-ignored-LDFLAGS.log)
			if [[ -n ${f} ]] ; then
				__vecho -ne '\n'
				eqawarn "${PORTAGE_COLOR_BAD}QA Notice: Files built without respecting LDFLAGS have been detected${PORTAGE_COLOR_NORMAL}"
				eqawarn " Please include the following list of files in your report:"
				eqawarn "${f}"
				__vecho -ne '\n'
				sleep 1
			else
				rm -f "${T}"/scanelf-ignored-LDFLAGS.log
			fi
		fi
	fi
}

ignored_flag_check
: # guarantee successful exit

# vim:ft=bash
