1# Copyright (C) 2017-2020 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 GCC; see the file COPYING3.  If not see
15# <http://www.gnu.org/licenses/>.
16
17#
18# DejaGnu does not have proper library search paths for load_lib.
19# We have to explicitly load everything that gdc.exp wants to load.
20#
21
22proc load_gcc_lib { filename } {
23    global srcdir loaded_libs
24
25    load_file $srcdir/../../gcc/testsuite/lib/$filename
26    set loaded_libs($filename) ""
27}
28
29load_lib dg.exp
30load_lib libgloss.exp
31
32load_gcc_lib target-supports.exp
33load_gcc_lib target-supports-dg.exp
34load_gcc_lib dg-options.exp
35load_gcc_lib target-libpath.exp
36load_gcc_lib timeout.exp
37load_gcc_lib wrapper.exp
38load_gcc_lib target-utils.exp
39load_gcc_lib gcc-defs.exp
40
41set TESTING_IN_BUILD_TREE 1
42
43# Define libphobos callbacks for dg.exp.
44
45proc libphobos-dg-test { prog do_what extra_tool_flags } {
46    set compile_type ""
47    set output_file ""
48
49    global libphobos_test_name
50    upvar name name
51    if { $libphobos_test_name != "" } {
52	set name $libphobos_test_name
53    }
54
55    # Set up the compiler flags, based on what we're going to do.
56    switch $do_what {
57	"run" {
58	    set compile_type "executable"
59	    # FIXME: "./" is to cope with "." not being in $PATH.
60	    # Should this be handled elsewhere?
61	    # YES.
62	    set output_file "./[file rootname [file tail $prog]].exe"
63	    # This is the only place where we care if an executable was
64	    # created or not.  If it was, dg.exp will try to run it.
65	    catch { remote_file build delete $output_file }
66	}
67	"link" {
68	    set compile_type "executable"
69	    set output_file "./[file rootname [file tail $prog]].exe"
70	}
71	default {
72	    perror "$do_what: not a valid dg-do keyword"
73	    return ""
74	}
75    }
76
77    set select_compile "libphobos_target_compile"
78    set options ""
79    if { $extra_tool_flags != "" } {
80	lappend options "additional_flags=$extra_tool_flags"
81    }
82
83    set unsupported_message [libphobos_skipped_test_p $name]
84    if { $unsupported_message != "" } {
85	return [list "::unsupported::$unsupported_message" $output_file]
86    }
87
88    set comp_output [$select_compile "$prog" "$output_file" "$compile_type" $options]
89    return [list $comp_output $output_file]
90}
91
92proc libphobos-dg-prune { system text } {
93    return $text
94}
95
96#
97# libphobos_init
98#
99
100proc libphobos_init { args } {
101    global env
102    global srcdir blddir objdir tool_root_dir
103    global exeext
104    global gdc gdcflags
105    global gdcpaths gdcldflags
106    global gluefile wrap_flags
107    global ld_library_path
108    global DEFAULT_DFLAGS
109
110    # If a testcase doesn't have special options, use these.
111    if ![info exists DEFAULT_DFLAGS] then {
112	set DEFAULT_DFLAGS ""
113    }
114
115    # By default, we assume we want to run program images.
116    global dg-do-what-default
117    if [isnative] {
118	set dg-do-what-default "run"
119    } else {
120	set dg-do-what-default "link"
121    }
122
123    # What arguments to pass to run program images.
124    global libphobos_run_args
125    set libphobos_run_args ""
126
127    # If the name of the test should be something else.
128    global libphobos_test_name
129    set libphobos_test_name ""
130
131    global libphobos_skip_tests
132    set libphobos_skip_tests { }
133
134    # Default settings.
135    set blddir [lookfor_file [get_multilibs] libphobos]
136    set flags_file "${blddir}/testsuite/testsuite_flags"
137    set shlib_ext [get_shlib_extension]
138
139    set gdc [transform "gdc"]
140    set gdcflags "-fmessage-length=0"
141    set gdcpaths "-I${srcdir}"
142    set gdcldflags ""
143
144    if { [file exists $flags_file] } {
145	set gdc [exec sh $flags_file --gdc]
146	set gdcflags [exec sh $flags_file --gdcflags]
147	set gdcpaths [exec sh $flags_file --gdcpaths]
148	set gdcldflags [exec sh $flags_file --gdcldflags]
149    }
150
151    set exeext ""
152    if [info exists env(EXEEXT)] {
153	set exeext $env(EXEEXT)
154    }
155
156    # Compute what needs to be added to the existing LD_LIBRARY_PATH.
157    set ld_library_path "."
158
159    set gccdir [lookfor_file $tool_root_dir gcc/libgcc.a]
160    if {$gccdir != ""} {
161	set gccdir [file dirname $gccdir]
162	append ld_library_path ":${gccdir}"
163    }
164
165    if { [file exists "${blddir}/src/.libs/libgphobos.${shlib_ext}"] } {
166	append ld_library_path ":${blddir}/src/.libs"
167    }
168
169    # Compute what needs to be added to the existing LD_LIBRARY_PATH.
170    if {$gccdir != ""} {
171	set compiler ${gccdir}/gdc
172
173	if { [is_remote host] == 0 && [which $compiler] != 0 } {
174	  foreach i "[exec $compiler --print-multi-lib]" {
175	    set mldir ""
176	    regexp -- "\[a-z0-9=_/\.-\]*;" $i mldir
177	    set mldir [string trimright $mldir "\;@"]
178	    if { "$mldir" == "." } {
179	      continue
180	    }
181	    if { [llength [glob -nocomplain ${gccdir}/${mldir}/libgcc_s*.so.*]] >= 1 } {
182	      append ld_library_path ":${gccdir}/${mldir}"
183	    }
184	  }
185	}
186    }
187
188    set_ld_library_path_env_vars
189
190    libphobos_maybe_build_wrapper "${objdir}/testglue.o"
191}
192
193#
194# libphobos_target_compile -- compile a source file.
195#
196
197proc libphobos_target_compile { source dest type options } {
198    global gdc gdcflags
199    global gdcpaths gdcldflags
200    global gluefile wrap_flags
201
202    lappend options "additional_flags=-fno-diagnostics-show-caret -fdiagnostics-color=never"
203
204    if { [target_info needs_status_wrapper] != "" && [info exists gluefile] } {
205	lappend options "libs=${gluefile}"
206	lappend options "ldflags=${wrap_flags}"
207    }
208
209    # Flag setting based on type argument.
210    if { $type == "executable" } {
211	# Link the support objects into executables.
212	lappend options "additional_flags=$gdcldflags"
213    }
214
215    # Set the compiler, only add D flags and paths if building D sources.
216    set gdc_final $gdc
217    if [regexp ".*\.d\$" $source] {
218	set gdc_final [concat $gdc_final $gdcflags]
219	set gdc_final [concat $gdc_final $gdcpaths]
220    }
221
222    lappend options "compiler=$gdc_final"
223    lappend options "timeout=[timeout_value]"
224
225    set options [dg-additional-files-options $options $source]
226    set comp_output [target_compile $source $dest $type $options]
227
228    return $comp_output
229}
230
231#
232# Helper used by libphobos and libdruntime unittest runner, filters out
233# D sources that may contain a unittest function.
234#
235
236proc filter_libphobos_unittests { list } {
237    set res {}
238    foreach filename $list {
239	set fid [open $filename r]
240	if [regexp -- {unittest} [read $fid [file size $filename]]] {
241	    lappend res $filename
242	}
243	close $fid
244    }
245    return $res
246}
247
248# Skip the unittest (report it as UNSUPPORTED) if the module exists in
249# libphobos_skip_tests and its target list is matched by dg-process-target.
250#
251# The expected format of the libphobos_skip_tests file is:
252# { test { targets } }
253proc libphobos_skipped_test_p { test } {
254    global libphobos_skip_tests
255
256    set row [lsearch -inline -index 0 $libphobos_skip_tests $test]
257    if { $row eq "" } {
258	return ""
259    }
260
261    if { [llength $row] != 2 } {
262	error "syntax error in libphobos_skip_tests: $row"
263    }
264
265    set selector [list target [lindex $row 1]]
266    if { [dg-process-target-1 $selector] != "S" } {
267	return ""
268    }
269
270    return "skipped test"
271}
272
273# Return true if the curl library is supported on the target.
274proc check_effective_target_libcurl_available { } {
275    return [check_no_compiler_messages libcurl_available executable {
276	int main (void) { return 0; }
277    } "-lcurl"]
278}
279
280# Return true if the target is linux version < 2.6.39
281proc check_effective_target_linux_pre_2639 { } {
282    if { ![istarget *-*-linux*] } {
283	return 0
284    }
285
286    if { [check_no_compiler_messages linux_pre_2639 assembly {
287	#include <linux/version.h>
288	#if !defined LINUX_VERSION_CODE || LINUX_VERSION_CODE < KERNEL_VERSION(2,6,39)
289	#error Yes, it is.
290	#endif
291    }] } {
292	return 0
293    }
294    return 1
295}
296