#!/bin/bash

# Copyright (c) 2021 Red Hat.
#
# This is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published
# by the Free Software Foundation; either version 3, or (at your
# option) any later version.
#
# It is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# See BZ 2010675

TEST_NAME=gaps
. $srcdir/common.sh

OPTS="$(rpm --eval '%build_cflags %build_ldflags')"

start_test

if ! [[ $OPTS == *"redhat-annobin-cc1"* ]];
then
    echo "gaps-test: skip: redhat-annobin-cc1 not found in rpm build flags"
    echo "rpm build flags: $OPTS"
    end_test
    exit 0
fi

$GCC $OPTS $srcdir/main.c -o main.exe
if [ $? != 0 ];
then
    echo "gaps-test: FAIL: unable to compile test file"
    end_test
    exit 1
fi

# Run annocheck

SKIPS="--skip-property-note --skip-dynamic-tags --skip-not-dynamic-tags --skip-branch-protection --skip-not-branch-protection --skip-lto"
$ANNOCHECK main.exe $SKIPS > main.out
grep -e "PASS" main.out
if [ $? != 0 ];
then
    echo "gaps-test: FAIL: using rpm build macros did not produce an executable that passes annocheck"
    $ANNOCHECK main.exe $SKIPS --verbose --enable-notes
    echo $GCC $OPTS $srcdir/main.c -o main.exe
    end_test
    exit 1
fi

end_test
