3 """Compiles a source file and compares generated .mod files against expected.
6 sys.argv[1]: a source file with contains the input and expected output
7 sys.argv[2]: the Flang frontend driver
8 sys.argv[3:]: Optional arguments to the Flang frontend driver"""
18 from pathlib
import Path
19 from difflib
import unified_diff
21 cm
.check_args_long(sys
.argv
)
22 srcdir
= Path(sys
.argv
[1])
23 sources
= list(glob
.iglob(str(srcdir
)))
24 sources
= sorted(sources
)
27 flang_fc1
= cm
.set_executable(sys
.argv
[2])
28 flang_fc_args
= sys
.argv
[3:]
29 flang_fc1_options
= "-fsyntax-only"
31 with tempfile
.TemporaryDirectory() as tmpdir
:
33 src
= Path(src
).resolve()
36 expected_files
= set()
42 prev_files
= set(os
.listdir(tmpdir
))
43 cmd
= [flang_fc1
, *flang_fc_args
, flang_fc1_options
, str(src
)]
44 proc
= subprocess
.check_output(cmd
, stderr
=subprocess
.PIPE
,
45 universal_newlines
=True, cwd
=tmpdir
)
46 actual_files
= set(os
.listdir(tmpdir
)).difference(prev_files
)
48 # The first 3 bytes of the files are an UTF-8 BOM
49 with
open(src
, 'r', encoding
="utf-8", errors
="strict") as f
:
51 m
= re
.search(r
"^!Expect: (.*)", line
)
53 expected_files
.add(m
.group(1))
55 extra_files
= actual_files
.difference(expected_files
)
57 print(f
"Unexpected .mod files produced: {extra_files}")
60 for mod
in expected_files
:
61 mod
= Path(tmpdir
).joinpath(mod
)
63 print(f
"Compilation did not produce expected mod file: {mod}")
65 with
open(mod
, 'r', encoding
="utf-8", errors
="strict") as f
:
71 with
open(src
, 'r', encoding
="utf-8", errors
="strict") as f
:
73 if f
"!Expect: {mod.name}" in line
:
75 if re
.match(r
"^$", line
):
77 m
= re
.sub(r
"^!", "", line
.lstrip())
80 diffs
= "\n".join(unified_diff(actual
.replace(" ", "").split("\n"),
81 expect
.replace(" ", "").split("\n"),
82 fromfile
=mod
.name
, tofile
="Expect", n
=999999))