1#!/usr/bin/env bash
2# Compile a source file and compare generated .mod files against expected.
3
4set -e
5F18_OPTIONS="-fdebug-resolve-names -fparse-only"
6srcdir=$(dirname $0)
7source $srcdir/common.sh
8
9actual=$temp/actual.mod
10expect=$temp/expect.mod
11actual_files=$temp/actual_files
12prev_files=$temp/prev_files
13diffs=$temp/diffs
14
15set $src
16
17touch $actual
18for src in "$@"; do
19  [[ ! -f $src ]] && echo "File not found: $src" && exit 1
20  path=$(git ls-files --full-name $src 2>/dev/null || echo $src)
21  (
22    cd $temp
23    ls -1 *.mod > prev_files
24    $F18 $F18_OPTIONS $src
25    ls -1 *.mod | comm -13 prev_files -
26  ) > $actual_files
27  expected_files=$(sed -n 's/^!Expect: \(.*\)/\1/p' $src | sort)
28  extra_files=$(echo "$expected_files" | comm -23 $actual_files -)
29  if [[ ! -z "$extra_files" ]]; then
30    echo "Unexpected .mod files produced:" $extra_files
31    die FAIL $path
32  fi
33  for mod in $expected_files; do
34    if [[ ! -f $temp/$mod ]]; then
35      echo "Compilation did not produce expected mod file: $mod"
36      die FAIL $path
37    fi
38    # The first three bytes of the file are a UTF-8 BOM
39    sed '/^[^!]*!mod\$/d' $temp/$mod > $actual
40    sed '1,/^!Expect: '"$mod"'/d' $src | sed -e '/^$/,$d' -e 's/^!//' > $expect
41    if ! diff -w -U999999 $expect $actual > $diffs; then
42      echo "Module file $mod differs from expected:"
43      sed '1,2d' $diffs
44      die FAIL $path
45    fi
46  done
47  rm -f $actual $expect
48done
49echo PASS
50