1# Copyright (C) 2005-2023 Free Software Foundation, Inc.
2#
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.
7#
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.
12#
13# You should have received a copy of the GNU General Public License
14# along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
16# Miscellaneous CRIS simulator testcases testing syscall sequences.
17
18sim_init
19
20global global_cc_works
21global global_cc_os
22
23set CFLAGS_FOR_TARGET "-O2"
24if [istarget cris-*-*] {
25    set mach "crisv10"
26} {
27    set mach "crisv32"
28}
29
30# Make sure we're using the right runtime for simulator runs.  If the
31# cris-sim dejagnu baseboard is used, -sim3 will be duplicated, but
32# that's ok.  For e.g. cris*-linux-gnu, neither -sim not -sim3 are
33# supported options and likely not other targets too.
34set saved_CFLAGS_FOR_TARGET $CFLAGS_FOR_TARGET
35if { $global_cc_os == "newlib" } {
36    append CFLAGS_FOR_TARGET " -sim3"
37}
38
39# Using target_compile, since it is less noisy,
40if { $global_cc_works == 1 } {
41    # Now check if we can link a program dynamically, and where
42    # libc.so is located.  If it is, we provide a sym link to the
43    # directory (which must end in /lib) in [pwd], so /lib/ld.so.1 is
44    # found (which must reside along libc.so).  We don't bother
45    # replacing the board ldflags like below as we don't care about
46    # detrimental effects on the executable from the specs and
47    # -static in the board ldflags, we just add -Bdynamic.
48    global objdir
49    if [regexp "(.*/lib)/libc.so" \
50	    [target_compile $srcdir/lib/compilercheck.c $objdir/compilercheck.x \
51		 "executable" \
52		 "ldflags=-print-file-name=libc.so -Wl,-Bdynamic"] \
53	    xxx libcsodir]  {
54	file delete lib
55	verbose -log "Creating link to $libcsodir in [pwd]"
56	file link lib $libcsodir
57    }
58    file delete $objdir/compilercheck.x
59}
60
61# Like istarget, except take a list of targets as a string.
62proc anytarget { targets } {
63    set targetlist [split $targets]
64    set argc [llength $targetlist]
65    for { set i 0 } { $i < $argc } { incr i } {
66	if [istarget [lindex $targetlist $i]] {
67	    return 1
68	}
69    }
70    return 0
71}
72
73global sim_path
74foreach src [lsort [glob -nocomplain $srcdir/$subdir/*.c]] {
75    set orig_ldflags ""
76
77    if ![runtest_file_p $runtests $src] {
78	continue
79    }
80    set testname "[file tail $src]"
81
82    set opt_array [slurp_options $src]
83    if { $opt_array == -1 } {
84	unresolved $testname
85	return
86    }
87
88    # And again, to simplify specifying tests.
89    if ![runtest_file_p $runtests $src] {
90	continue
91    }
92
93    # Note absence of CC in results, but don't make a big fuss over it.
94    if { $global_cc_works == 0 } {
95	untested $testname
96	continue
97    }
98
99    if ![file exists $sim_path] {
100	untested $testname
101	return 0
102    }
103
104    # Clear default options
105    set opts(cc) ""
106    set opts(sim) ""
107    set opts(output) ""
108    set opts(progoptions) ""
109    set opts(progos) ""
110    set opts(timeout) ""
111    set opts(mach) ""
112    set opts(xerror) "no"
113    set opts(kfail) ""
114    set opts(xfail) ""
115    set opts(target) ""
116    set opts(notarget) ""
117
118    # Clear any machine specific options specified in a previous test case
119    if [info exists opts(sim,$mach)] {
120	unset opts(sim,$mach)
121    }
122
123    foreach i $opt_array {
124	set opt_name [lindex $i 0]
125	set opt_machs [lindex $i 1]
126	set opt_val [lindex $i 2]
127	if ![info exists opts($opt_name)] {
128	    perror "unknown option $opt_name in file $src"
129	    unresolved $testname
130	    return
131	}
132
133	# Multiple of these options concatenate, they don't override.
134	if { $opt_name == "output" || $opt_name == "progoptions" } {
135	    set opt_val "$opts($opt_name)$opt_val"
136	}
137
138	# Similar with "xfail", "kfail", "target" and "notarget", but
139	# arguments are space-separated.
140	if { $opt_name == "xfail" || $opt_name == "kfail" \
141		 || $opt_name == "target"  || $opt_name == "notarget" } {
142	    if { $opts($opt_name) != "" } {
143		set opt_val "$opts($opt_name) $opt_val"
144	    }
145	}
146
147	foreach m $opt_machs {
148	    set opts($opt_name,$m) $opt_val
149	}
150	if { "$opt_machs" == "" } {
151	    set opts($opt_name) $opt_val
152	}
153    }
154
155    if { $opts(output) == "" } {
156	if { "$opts(xerror)" == "no" } {
157	    set opts(output) "pass\n"
158	} else {
159	    set opts(output) "fail\n"
160	}
161    }
162
163    if { $opts(target) != "" && ![anytarget $opts(target)] } {
164	continue
165    }
166
167    if { $opts(notarget) != "" && [anytarget $opts(notarget)] } {
168	continue
169    }
170
171    if { $opts(progos) != "" && $opts(progos) != $global_cc_os } {
172	untested $testname
173	continue
174    }
175
176    # If no machine specific options, default to the general version.
177    if ![info exists opts(sim,$mach)] {
178	set opts(sim,$mach) $opts(sim)
179    }
180
181    # Change \n sequences to newline chars.
182    regsub -all "\\\\n" $opts(output) "\n" opts(output)
183
184    verbose -log "Compiling $src with $opts(cc)"
185
186    if { [target_compile $src "$objdir/$testname.x" "executable" "$opts(cc)" ] != "" } {
187	fail "$mach $testname (compilation)"
188	continue
189    }
190
191    if { $orig_ldflags != "" } {
192	set board_info([target_info name],ldflags) $orig_ldflags
193    }
194
195    verbose -log "Simulating $src with $opts(sim,$mach)"
196
197    # Time to setup xfailures and kfailures.
198    if { "$opts(xfail)" != "" } {
199	verbose -log "xfail: $opts(xfail)"
200	# Using eval to make $opts(xfail) appear as individual
201	# arguments.
202	eval setup_xfail $opts(xfail)
203    }
204    if { "$opts(kfail)" != "" } {
205	verbose -log "kfail: $opts(kfail)"
206	eval setup_kfail $opts(kfail)
207    }
208
209    set result [sim_run "$objdir/$testname.x" "$opts(sim,$mach)" "$opts(progoptions)" \
210	"" ""]
211    set return_code [lindex $result 0]
212    set output [lindex $result 1]
213
214    set status fail
215    if { $return_code == 0 } {
216	set status pass
217    }
218
219    if { "$status" == "pass" } {
220	if { "$opts(xerror)" == "no" } {
221	    if [string match $opts(output) $output] {
222		pass "$mach $testname"
223	    } else {
224		verbose -log "output:  $output" 3
225		verbose -log "pattern: $opts(output)" 3
226		fail "$mach $testname (execution)"
227	    }
228	} else {
229	    verbose -log "`pass' return code when expecting failure" 3
230	    fail "$mach $testname (execution)"
231	}
232    } elseif { "$status" == "fail" } {
233	if { "$opts(xerror)" == "no" } {
234	    fail "$mach $testname (execution)"
235	} else {
236	    if [string match $opts(output) $output] {
237		pass "$mach $testname"
238	    } else {
239		verbose -log "output:  $output" 3
240		verbose -log "pattern: $opts(output)" 3
241		fail "$mach $testname (execution)"
242	    }
243	}
244    } else {
245	$status "$mach $testname"
246    }
247}
248
249set CFLAGS_FOR_TARGET $saved_CFLAGS_FOR_TARGET
250