#!/bin/bash

# Copyright (c) 2017-2026 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.

TEST_NAME=compile
. $srcdir/common.sh

PLUGIN_OPTS="-fplugin-arg-annobin-no-attach -fplugin-arg-annobin-note-format=note"

start_test

COMMAND="$GCC -fplugin=$PLUGIN $PLUGIN_OPTS -c $srcdir/hello.c $GCC_OPTS"
$COMMAND > $G_OUT 2>&1
if [ $? != 0 ];
then
    echo "$TEST_NAME: SKIP: could not compile test file"
    echo "$TEST_NAME: gcc command line: $COMMAND"
    echo "$TEST_NAME: gcc output:"
    cat $G_OUT
    end_test
    exit $EXIT_TEST_SKIPPED
fi

COMMAND="$GCC -fplugin=$PLUGIN $PLUGIN_OPTS \
     -c \
     -fPIC \
     -Wall \
     -g \
     --save-temps -fverbose-asm \
     -fno-stack-protector \
     -fplugin-arg-annobin-stack-threshold=0x10 \
     $srcdir/hello.c"
$COMMAND >$G_OUT 2>&1
if [ $? != 0 ];
then
    echo " $TEST_NAME: FAIL: unable to compile test file hello.c"
    echo " $TEST_NAME: command: $COMMAND"
    cat $G_OUT
    end_test
    exit $EXIT_TEST_FAILED
fi

COMMAND="$GCC -fplugin=$PLUGIN $PLUGIN_OPTS \
     -O3 \
     -c \
     -fPIC \
     -fno-stack-protector \
     -fplugin-arg-annobin-global-file-syms \
     $srcdir/hello2.c"
$COMMAND >$G_OUT 2>&1
if [ $? != 0 ];
then
    echo " $TEST_NAME: FAIL: unable to compile test file hello2.c"
    echo " $TEST_NAME: command: $COMMAND"
    cat $G_OUT
    end_test
    exit $EXIT_TEST_FAILED
fi

COMMAND="$GCC -fplugin=$PLUGIN $PLUGIN_OPTS \
     -O2 \
     -c \
     -fPIE \
     -g3 \
     -fstack-protector-strong \
     -D_FORTIFY_SOURCE=2 \
     -fplugin-arg-annobin-no-stack-size-notes \
     -grecord-gcc-switches \
     $srcdir/hello3.c"
$COMMAND >$G_OUT 2>&1
if [ $? != 0 ];
then
    echo " $TEST_NAME: FAIL: unable to compile test file hello3.c"
    echo " $TEST_NAME: command: $COMMAND"
    cat $G_OUT
    end_test
    exit $EXIT_TEST_FAILED
fi

COMMAND="$GCC -fplugin=$PLUGIN $PLUGIN_OPTS \
    -O2 \
    -fpic \
    -fstack-protector \
    -fplugin-arg-annobin-version \
    -shared \
    $srcdir/hello_lib.c \
    -o libhello.so"
$COMMAND >$G_OUT 2>&1
if [ $? != 0 ];
then
    echo " $TEST_NAME: FAIL: unable to compile the shared library"
    echo " $TEST_NAME: command: $COMMAND"
    cat $G_OUT
    end_test
    exit $EXIT_TEST_FAILED
fi

COMMAND="$GCC -L . -pie -Wl,-z,now,-z,relro \
           hello.o hello2.o hello3.o -lhello -o compile-test.exe"
$COMMAND >$G_OUT 2>&1
if [ $? != 0 ];
then
    echo " $TEST_NAME: FAIL: unable to link objects"
    echo " $TEST_NAME: command: $COMMAND"
    cat $G_OUT
    end_test
    exit $EXIT_TEST_FAILED
fi

# FIXME - we should check that the notes were parsed correctly...
$READELF --notes --wide compile-test.exe > /dev/null

end_test
