f69f9a
#!/usr/bin/sh
f69f9a
# This is a script to select which GCC spec file fragment
f69f9a
# should be the destination of the redhat-annobin-cc1 symlink.
f69f9a
f69f9a
# Author: Nick Clifton  <nickc@redhat.com>
f69f9a
# Copyright (c) 2021 Red Hat.
f69f9a
#
f69f9a
# This is free software; you can redistribute it and/or modify it
f69f9a
# under the terms of the GNU General Public License as published
f69f9a
# by the Free Software Foundation; either version 2, or (at your
f69f9a
# option) any later version.
f69f9a
f69f9a
# It is distributed in the hope that it will be useful, but
f69f9a
# WITHOUT ANY WARRANTY; without even the implied warranty of
f69f9a
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
f69f9a
# GNU General Public License for more details.
f69f9a
#
f69f9a
# Usage:
f69f9a
#   redhat-annobin-plugin-select [script-dir]
f69f9a
#
f69f9a
# If script-dir is not provided then /usr/lib/rpm/redhat is used
f69f9a
# as the location where all of the annobin plugin selection files
f69f9a
# can be found.
f69f9a
f69f9a
if test "x$1" = "x" ;
f69f9a
then
f69f9a
    rrcdir=/usr/lib/rpm/redhat
f69f9a
else
f69f9a
    rrcdir=$1
f69f9a
fi
f69f9a
f69f9a
# Set this variable to non-zero to enable the generation of debugging
f69f9a
# messages.
f69f9a
debug=0
f69f9a
f69f9a
# Decide which version of the annobin plugin for gcc should be used.
f69f9a
# There are two possible versions, one created by the annobin package and one
f69f9a
# created by the gcc package.  The logic selects the gcc version unless both
f69f9a
# have been built by the same version of the compiler.  In that case the
f69f9a
# annobin version is selected instead.
f69f9a
#
f69f9a
# The point of all this is that the annobin plugin is very sensitive to
f69f9a
# mismatches with the version of gcc that built it.  If the plugin is built
f69f9a
# by version A of gcc, but then run on version B of gcc, it is possible for
f69f9a
# the plugin to misbehave, which then causes problems if gating tests examine
f69f9a
# the plugin's output.  (This has happened more than once in RHEL...).
f69f9a
#
f69f9a
# So the plugin is built both by gcc and by the annobin package.  This means
f69f9a
# that whenever gcc is updated a fresh plugin is built, and the logic below
f69f9a
# will select that version.  But in order to allow annobin development to
f69f9a
# proceed independtently of gcc, the annobin package can also update its
f69f9a
# version of the plugin, and the logic will select this new version.
f69f9a
f69f9a
# This is where the annobin package stores the information on the version
f69f9a
# of gcc that built the annobin plugin.
f69f9a
aver=`gcc --print-file-name=plugin`/annobin-plugin-version-info
f69f9a
f69f9a
# This is where the gcc package stores its version information.
f69f9a
gver=`gcc --print-file-name=rpmver`
f69f9a
f69f9a
aplugin=`gcc --print-file-name=plugin`/annobin.so.0.0.0
f69f9a
gplugin=`gcc --print-file-name=plugin`/gcc-annobin.so.0.0.0
f69f9a
f69f9a
# This is the file that needs to be updated when either of those version
f69f9a
# files changes.
f69f9a
rac1=redhat-annobin-cc1
f69f9a
f69f9a
# This is the GCC spec file fragment that selects the gcc-built version of
f69f9a
# the annobin plugin
f69f9a
select_gcc=redhat-annobin-select-gcc-built-plugin
f69f9a
f69f9a
# This is the GCC spec file fragment that selects the annobin-built version
f69f9a
# of the annobin plugin
f69f9a
select_annobin=redhat-annobin-select-annobin-built-plugin
f69f9a
f69f9a
install_annobin_version=0
f69f9a
install_gcc_version=0
f69f9a
f69f9a
if [ -f $aplugin ]
f69f9a
then
f69f9a
    if [ -f $gplugin ]
f69f9a
    then
f69f9a
	if [ $debug -eq 1 ]
f69f9a
	then
f69f9a
	    echo "  redhat-rpm-config: Both plugins exist, checking version information"
f69f9a
	fi
f69f9a
f69f9a
	if [ -f $gver ]
f69f9a
	then
f69f9a
	    if [ -f $aver ]
f69f9a
	    then
f69f9a
		if [ $debug -eq 1 ]
f69f9a
		then
f69f9a
		    echo "  redhat-rpm-config: Both plugin version files exist - comparing..."
f69f9a
		fi
f69f9a
f69f9a
		# Get the first line from the version info files.  This is just in
f69f9a
		# vase there are extra lines in the files.
f69f9a
		avers=`head --lines=1 $aver`
f69f9a
		gvers=`head --lines=1 $gver`
f69f9a
f69f9a
		if [ $debug -eq 1 ]
f69f9a
		then
f69f9a
		    echo "  redhat-rpm-config: Annobin plugin built by gcc $avers"
f69f9a
		    echo "  redhat-rpm-config: GCC     plugin built by gcc $gvers"
f69f9a
		fi
f69f9a
f69f9a
		# If both plugins were built by the same version of gcc then select
f69f9a
		# the one from the annobin package (in case it is built from newer
f69f9a
		# sources).  If the plugin builder versions differ, select the gcc
f69f9a
		# built version instead.  This assumes that the gcc built version
f69f9a
		# always matches the installed gcc, which should be true.
f69f9a
		if [ $avers = $gvers ]
f69f9a
		then
f69f9a
		    if [ $debug -eq 1 ]
f69f9a
		    then
f69f9a
			echo "  redhat-rpm-config: Both plugins built by the same compiler - using annobin-built plugin"
f69f9a
		    fi
f69f9a
		    install_annobin_version=1
f69f9a
		else
f69f9a
		    if [ $debug -eq 1 ]
f69f9a
		    then
f69f9a
			echo "  redhat-rpm-config: Versions differ - using gcc-built plugin"
f69f9a
		    fi
f69f9a
		    install_gcc_version=1
f69f9a
		fi
f69f9a
	    else
f69f9a
		if [ $debug -eq 1 ]
f69f9a
		then
f69f9a
		    echo "  redhat-rpm-config: Annobin version file does not exist, using gcc-built plugin"
f69f9a
		fi
f69f9a
		install_gcc_version=1
f69f9a
	    fi
f69f9a
	else
f69f9a
	    if [ -f $aver ]
f69f9a
	    then
f69f9a
		# FIXME: This is suspicious.  If the installed GCC does not supports plugins
f69f9a
		# then enabling the annobin plugin will not work.
f69f9a
		if [ $debug -eq 1 ]
f69f9a
		then
f69f9a
		    echo "  redhat-rpm-config: GCC plugin version file does not exist, using annobin-built plugin"
f69f9a
		fi
f69f9a
		install_annobin_version=1
f69f9a
	    else
f69f9a
		if [ $debug -eq 1 ]
f69f9a
		then
f69f9a
		    echo "  redhat-rpm-config: Neither version file exists - playing safe and using gcc-built plugin"
f69f9a
		    echo "  redhat-rpm-config: Note: expected to find $aver and/or $gver"
f69f9a
		fi
f69f9a
		install_gcc_version=1
f69f9a
	    fi
f69f9a
	fi
f69f9a
    else
f69f9a
	if [ $debug -eq 1 ]
f69f9a
	then
f69f9a
	    echo "  redhat-rpm-config: Only the annobin plugin exists - using that"
f69f9a
	fi
f69f9a
	install_annobin_version=1
f69f9a
    fi
f69f9a
else
f69f9a
    if [ -f $gplugin ]
f69f9a
    then
f69f9a
	if [ $debug -eq 1 ]
f69f9a
	then
f69f9a
	    echo "  redhat-rpm-config: Only the gcc plugin exists - using that"
f69f9a
	fi
f69f9a
    else
f69f9a
	if [ $debug -eq 1 ]
f69f9a
	then
f69f9a
	    echo "  redhat-rpm-config: Neither plugin exists - playing safe and using gcc-built plugin"
f69f9a
	    echo "  redhat-rpm-config: Note: expected to find $aplugin and/or $gplugin"
f69f9a
	fi
f69f9a
    fi
f69f9a
    install_gcc_version=1
f69f9a
fi
f69f9a
f69f9a
if [ $install_annobin_version -eq 1 ]
f69f9a
then
f69f9a
    if [ $debug -eq 1 ]
f69f9a
    then
f69f9a
	echo "  redhat-rpm-config: Installing annobin version of $rac1"
f69f9a
    fi
f69f9a
    pushd $rrcdir > /dev/null
f69f9a
    rm -f $rac1
f69f9a
    ln -s $select_annobin "$rac1"
f69f9a
    popd > /dev/null
f69f9a
    
f69f9a
else if [ $install_gcc_version -eq 1 ]
f69f9a
     then
f69f9a
	if [ $debug -eq 1 ]
f69f9a
	then
f69f9a
	    echo "  redhat-rpm-config: Installing gcc version of $rac1"
f69f9a
	fi
f69f9a
	pushd $rrcdir > /dev/null
f69f9a
	rm -f $rac1
f69f9a
	ln -s $select_gcc $rac1
f69f9a
	popd > /dev/null
f69f9a
     fi
f69f9a
fi