1 # Copyright (C) 2017-2021 Free Software Foundation, Inc.
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 3 of the License, or
6 # (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with GCC; see the file COPYING3. If not see
15 # <http://www.gnu.org/licenses/>.
17 # Contributed by Nathan Sidwell <nathan@acm.org> while at Facebook
20 # Test C++ modules, which requires multiple TUs
22 # A test case might consist of multiple source files, each is compiled
23 # separately, in a well-defined order. The resulting object files might
24 # be optionally linked and optionally executed. Grouping is indicated by
25 # naming files '*_[a-z].[CH]'
27 # { dg-module-cmi "[!]module-name" } # an interface file is (not) expected
28 # { dg-module-do [link|run] [xfail] [options] } # link [and run]
32 # If a testcase doesn't have special options, use these.
33 global DEFAULT_CXXFLAGS
34 if ![info exists DEFAULT_CXXFLAGS] then {
35 set DEFAULT_CXXFLAGS " -pedantic-errors -Wno-long-long"
37 set DEFAULT_MODFLAGS $DEFAULT_CXXFLAGS
38 set MOD_STD_LIST { 17 2a 2b }
42 if {[is_remote host]} {
43 # remote testing not functional here :(
50 set DEFAULT_REPO "gcm.cache"
52 # Register the module name this produces.
53 # dg-module-cmi !?=?NAME WHEN?
54 # dg-module-cmi !?{} - header unit
55 proc dg-module-cmi { args } {
56 if { [llength $args] > 3 } {
57 error "[lindex $args 0]: too many arguments"
60 set spec [lindex $args 1]
61 if { [llength $args] > 2 } {
62 set when [lindex $args 2]
67 if { [string index $spec 0] == "!" } {
68 set name [string range $spec 1 end]
75 if { [string index $name 0] == "=" } {
76 set cmi [string range $name 1 end]
79 # get the source file name. ick!
81 set cmi "$srcname.gcm"
82 if { [string index $cmi 0] == "/" } {
83 set cmi [string range $cmi 1 end]
87 set path [file split $cmi]
89 # sadly tcl 8.5 does not have lmap
91 foreach elt $path {lappend rplac [expr {$elt == ".." ? ",," : $elt}]}
92 set cmi [file join {*}$rplac]
94 set cmi "[regsub : $name -].gcm"
97 set cmi "$DEFAULT_REPO/$cmi"
100 # delete file, so we don't get confused by a stale one.
101 file_on_host delete "$cmi"
104 lappend module_cmis [list $spec $when $not $cmi]
107 # check the expected module files exist (or not)
108 # return list to delete
109 proc module_cmi_p { src ifs } {
111 foreach if_arg $ifs {
112 set spec [lindex $if_arg 0]
113 set when [lindex $if_arg 1]
115 switch [dg-process-target $when] {
118 "F" { setup_xfail "*-*-*" }
122 set not [lindex $if_arg 2]
123 set cmi [lindex $if_arg 3]
125 set relcmi [string map [list $srcdir "/\$srcdir"] $cmi]
126 if { $not != [file_on_host exists $cmi] } {
127 pass "$src module-cmi $spec ($relcmi)"
129 fail "$src module-cmi $spec ($relcmi)"
130 set not [expr ! $not ]
139 # link and maybe run a set of object files
140 # dg-module-do WHAT WHEN
141 proc dg-module-do { args } {
142 if { [llength $args] > 3 } {
143 error "[lindex $args 0]: too many arguments"
147 set do_what [lindex $args 1]
149 if { [llength $args] > 2 } {
150 set expected [dg-process-target [lindex $args 2]]
154 set module_do [list $do_what $expected]
157 proc module_do_it { do_what testcase std asm_list } {
161 switch [lindex $do_what 0] {
162 "compile" { return 1 }
165 default { error "unknown module-do action [lindex $do_what 0]" }
169 switch [lindex $do_what 1] {
172 "F" { set xfail {setup_xfail "*-*-*"} }
177 # make sure all asms are around
178 foreach asm $asm_list {
179 if { ! [file_on_host exists $asm] } {
187 lappend options "additional_flags=$std"
188 set ident "$ident $std"
190 if { [llength $do_what] > 3 } {
191 lappend options "additional_flags=[lindex $do_what 3]"
194 set execname "./[file tail $testcase].exe"
197 verbose "Linking $asm_list" 1
199 unresolved "$ident link"
201 set out [${tool}_target_compile $asm_list \
202 $execname executable $options]
215 unresolved "$ident execute"
217 set out [${tool}_load $execname "" ""]
218 set status [lindex $out 0]
220 $status "$ident execute"
221 if { $status != "pass" } {
227 file_on_host delete $execname
233 # delete the specified set of module files
234 proc cleanup_module_files { files } {
235 foreach file $files {
236 file_on_host delete $file
241 set testdir $srcdir/$subdir
247 # Return set of std options to iterate over, taken from g++-dg.exp & compat.exp
248 proc module-init { src } {
249 set tmp [dg-get-options $src]
252 set std_prefix "-std=c++"
255 switch [lindex $op 0] {
257 set std_prefix "-std=gnu++"
258 if { [string match "*-std=*" [lindex $op 2]] } {
262 "dg-additional-options" {
263 if { [string match "*-std=*" [lindex $op 2]] } {
272 foreach x $MOD_STD_LIST {
273 lappend option_list "${std_prefix}$x"
276 lappend option_list ""
282 # cleanup any detritus from previous run
283 cleanup_module_files [find $DEFAULT_REPO *.gcm]
285 # not grouped tests, sadly tcl doesn't have negated glob
286 foreach test [prune [lsort [find $srcdir/$subdir {*.[CH]}]] \
287 "$srcdir/$subdir/*_?.\[CH\]"] {
288 if [runtest_file_p $runtests $test] {
289 set nshort [file tail [file dirname $test]]/[file tail $test]
291 set std_list [module-init $test]
292 foreach std $std_list {
295 verbose "Testing $nshort $std" 1
296 dg-test $test "$std" $DEFAULT_MODFLAGS
297 set testcase [string range $test [string length "$srcdir/"] end]
298 cleanup_module_files [module_cmi_p $testcase $module_cmis]
304 foreach src [lsort [find $srcdir/$subdir {*_a.[CHX}]] {
305 # use the FOO_a.C name as the parallelization key
306 if [runtest_file_p $runtests $src] {
307 set tests [lsort [find [file dirname $src] \
308 [regsub {_a.[CHX]$} [file tail $src] {_[a-z].[CHX]}]]]
310 set std_list [module-init $src]
311 foreach std $std_list {
314 set module_do {"compile" "P"}
318 file_on_host delete $DEFAULT_REPO
319 foreach test $tests {
320 if { [lindex $module_do 1] != "N" } {
323 set nshort [file tail [file dirname $test]]/[file tail $test]
324 verbose "Testing $nshort $std" 1
325 switch [file extension $test] {
327 lappend asm_list [file rootname [file tail $test]].s
333 dg-test -keep-output $test "$std" $DEFAULT_MODFLAGS
334 set testcase [string range $test [string length "$srcdir/"] end]
335 lappend mod_files [module_cmi_p $testcase $module_cmis]
338 set testcase [regsub {_a.[CH]} $src {}]
340 [string range $testcase [string length "$srcdir/"] end]
341 module_do_it $module_do $testcase $std $asm_list
342 foreach asm $asm_list {
343 file_on_host delete $asm
346 set mod_files [find $DEFAULT_REPO *.gcm]
348 cleanup_module_files $mod_files