#!/bin/sh

# When SOURCE_DATE_EPOCH is defined in the environment,
# and the date it contains is older than an Ada source,
# gcc (>= 7.1.0-9) writes SOURCE_DATE_EPOCH in the .ali file
# instead of the actual source file timestamp.
# gprbuild/2017 detects the mismatch and insists on recompiling.

set -C -e -f -u

cd "$AUTOPKGTEST_TMP"

cat > a.adb <<EOF
procedure A is
begin
   null;
end A;
EOF
cat > p.gpr <<EOF
project P is
   for Main use ("a.adb");
end P;
EOF
export SOURCE_DATE_EPOCH=100
gprbuild -v p.gpr
echo -n 'SOURCE_DATE_EPOCH : '
date -d@$SOURCE_DATE_EPOCH +%Y%m%d%H%M%S
echo -n 'timestamp in .ali : '
sed '/^D a\.adb\t*\([0-9]\{14\}\) .*/ ! d; s//\1/; q' a.ali
echo -n 'file mtime        : '
ls -o --time-style=+%Y%m%d%H%M%S a.adb | cut -d' ' -f5
! (gprbuild -vh p.gpr | grep "different time stamp for a\.adb")
