Debian Codegraph @

gcc/testsuite/lib/gdc.exp

raw
1 # Copyright (C) 2012-2025 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 # gdc support library routines
19 #
20
21 load_lib prune.exp
22 load_lib gcc-defs.exp
23 load_lib timeout.exp
24 load_lib target-libpath.exp
25
26 #
27 # GDC_UNDER_TEST is the compiler under test.
28 #
29
30 set gdc_compile_options ""
31
32
33 #
34 # gdc_version -- extract and print the version number of the compiler
35 #
36
37 proc gdc_version { } {
38 global GDC_UNDER_TEST
39
40 gdc_init
41
42 # ignore any arguments after the command
43 set compiler [lindex $GDC_UNDER_TEST 0]
44
45 # verify that the compiler exists
46 if { [is_remote host] || [which $compiler] != 0 } then {
47 set tmp [remote_exec host "$compiler -v"]
48 set status [lindex $tmp 0]
49 set output [lindex $tmp 1]
50 regexp " version \[^\n\r\]*" $output version
51 if { $status == 0 && [info exists version] } then {
52 if [is_remote host] {
53 clone_output "$compiler $version\n"
54 } else {
55 clone_output "[which $compiler] $version\n"
56 }
57 } else {
58 clone_output "Couldn't determine version of [which $compiler]\n"
59 }
60 } else {
61 # compiler does not exist (this should have already been detected)
62 warning "$compiler does not exist"
63 }
64 }
65
66 #
67 # gdc_include_flags -- include flags for the gcc tree structure
68 #
69
70 proc gdc_include_flags { paths } {
71 global srcdir
72 global GDC_INCLUDE_CXX_FLAGS
73 global TESTING_IN_BUILD_TREE
74
75 set flags ""
76
77 if { [is_remote host] || ![info exists TESTING_IN_BUILD_TREE] } {
78 return "${flags}"
79 }
80
81 if [info exists GDC_INCLUDE_CXX_FLAGS] {
82 set include_cxx_flags $GDC_INCLUDE_CXX_FLAGS
83 } else {
84 set include_cxx_flags 0
85 }
86
87 set gccpath ${paths}
88
89 if { $gccpath != "" } {
90 if [file exists "${gccpath}/libphobos/libdruntime"] {
91 append flags "-I${gccpath}/libphobos/libdruntime "
92 }
93 }
94 append flags "-I${srcdir}/../../libphobos/libdruntime "
95 append flags "-I${srcdir}/../../libphobos/src "
96
97 # For the tests that mix C++ and D, need to know where headers are located.
98 if $include_cxx_flags {
99 set odir [lookfor_file ${gccpath} libstdc++-v3]
100 if { ${odir} != "" && [file exists ${odir}/scripts/testsuite_flags] } {
101 set cxxflags [exec sh ${odir}/scripts/testsuite_flags --build-includes]
102 set idx [lsearch $cxxflags "-nostdinc++"]
103 append flags [lreplace $cxxflags $idx $idx]
104 }
105 }
106
107 return "$flags"
108 }
109
110 #
111 # gdc_link_flags -- linker flags for the gcc tree structure
112 #
113
114 proc gdc_link_flags { paths } {
115 global srcdir
116 global ld_library_path
117 global GDC_UNDER_TEST
118 global GDC_INCLUDE_CXX_FLAGS
119 global shlib_ext
120 global SHARED_OPTION
121
122 set gccpath ${paths}
123 set libio_dir ""
124 set flags ""
125 set ld_library_path "."
126 set shlib_ext [get_shlib_extension]
127 set SHARED_OPTION ""
128 verbose "shared lib extension: $shlib_ext"
129
130 if [info exists GDC_INCLUDE_CXX_FLAGS] {
131 set include_cxx_flags $GDC_INCLUDE_CXX_FLAGS
132 } else {
133 set include_cxx_flags 0
134 }
135
136 # We need to add options to locate libgphobos and libstdc++
137 # Usually '-L' options are added to point to the relevant directories for
138 # the uninstalled libraries.
139
140 # In cases where libraries are available as both shared and convenience
141 # some additional checks are made.
142
143 # For some targets -static-xxxx options are handled by specs substitution
144 # and need a '-B' option rather than '-L'. For Darwin, when embedded
145 # runpaths are in use (the default for all versions after macOS 10.11),
146 # '-B' is also needed to provide the runpath.
147 # When '-B' is used, this results in a '-L' for each path that exists (so
148 # that appending a '-L' as well is a needless duplicate). There are also
149 # cases where tools warn for duplicates, leading to spurious fails.
150 # Therefore the objective of the code below is to add just one '-L' or
151 # '-B' for each of the libraries.
152
153 set target_wants_B_option 0
154 if { [istarget *-*-darwin9* ] || [istarget *-*-darwin\[12\]* ] } {
155 set target_wants_B_option 1
156 }
157
158 if { $gccpath != "" } {
159 # Path to libgphobos.spec.
160 append flags "-B${gccpath}/libphobos/src "
161 # Path to drtbegin.o/drtend.o.
162 if { [file exists "${gccpath}/libphobos/libdruntime/gcc/drtbegin.o"] } {
163 append flags "-B${gccpath}/libphobos/libdruntime/gcc "
164 }
165
166 if { [file exists "${gccpath}/libphobos/src/.libs/libgphobos.a"] \
167 || [file exists "${gccpath}/libphobos/src/.libs/libgphobos.${shlib_ext}"] } {
168 if { $target_wants_B_option } {
169 append flags "-B${gccpath}/libphobos/src/.libs "
170 } else {
171 append flags "-L${gccpath}/libphobos/src/.libs "
172 }
173 append ld_library_path ":${gccpath}/libphobos/src/.libs"
174 }
175 # Static linking is default. If only the shared lib is available adjust
176 # flags to always use it. If both are available, set SHARED_OPTION which
177 # will be added to PERMUTE_ARGS
178 if { [file exists "${gccpath}/libphobos/src/.libs/libgphobos.${shlib_ext}"] } {
179 if { [file exists "${gccpath}/libphobos/src/.libs/libgphobos.a"] } {
180 set SHARED_OPTION "-shared-libphobos"
181 } else {
182 append flags "-shared-libphobos "
183 }
184 }
185 if [file exists "${gccpath}/libiberty/libiberty.a"] {
186 append flags "-L${gccpath}/libiberty "
187 }
188 # For the tests that mix C++ and D, need to know where library is located.
189 if $include_cxx_flags {
190 if { [file exists "${gccpath}/libstdc++-v3/src/.libs/libstdc++.a"] \
191 || [file exists "${gccpath}/libstdc++-v3/src/.libs/libstdc++.${shlib_ext}"] } {
192 if { $target_wants_B_option } {
193 append flags "-B${gccpath}/libstdc++-v3/src/.libs "
194 } else {
195 append flags "-L${gccpath}/libstdc++-v3/src/.libs "
196 }
197 append ld_library_path ":${gccpath}/libstdc++-v3/src/.libs"
198 }
199 }
200 append ld_library_path [gcc-set-multilib-library-path $GDC_UNDER_TEST]
201 } else {
202 global tool_root_dir
203
204 set libphobos [lookfor_file ${tool_root_dir} libgphobos]
205 if { $libphobos != "" } {
206 if { $target_wants_B_option } {
207 append flags "-B${libphobos} "
208 } else {
209 append flags " -L${libphobos} "
210 }
211 append ld_library_path ":${libphobos}"
212 }
213 set libiberty [lookfor_file ${tool_root_dir} libiberty]
214 if { $libiberty != "" } {
215 append flags "-L${libiberty} "
216 }
217 if $include_cxx_flags {
218 set libstdcpp [lookfor_file ${tool_root_dir} libstdc++]
219 if { $libstdcpp != "" } {
220 append flags "-L${libstdcpp} "
221 append ld_library_path ":${libstdcpp}"
222 }
223 }
224 }
225
226 set_ld_library_path_env_vars
227
228 return "$flags"
229 }
230
231 #
232 # gdc_init -- called at the start of each subdir of tests
233 #
234
235 proc gdc_init { args } {
236 global gdc_initialized
237 global base_dir
238 global tmpdir
239 global libdir
240 global gluefile wrap_flags
241 global TOOL_EXECUTABLE
242 global GDC_UNDER_TEST
243 global TESTING_IN_BUILD_TREE
244 global gcc_warning_prefix
245 global gcc_error_prefix
246
247 # We set LC_ALL and LANG to C so that we get the same error messages as expected.
248 setenv LC_ALL C
249 setenv LANG C
250
251 if ![info exists GDC_UNDER_TEST] then {
252 if [info exists TOOL_EXECUTABLE] {
253 set GDC_UNDER_TEST $TOOL_EXECUTABLE
254 } else {
255 if { [is_remote host] || ! [info exists TESTING_IN_BUILD_TREE] } {
256 set GDC_UNDER_TEST [transform gdc]
257 } else {
258 set GDC_UNDER_TEST [findfile $base_dir/../../gdc "$base_dir/../../gdc -B$base_dir/../../" [findfile $base_dir/gdc "$base_dir/gdc -B$base_dir/" [transform gdc]]]
259 }
260 }
261 }
262
263 if ![is_remote host] {
264 if { [which $GDC_UNDER_TEST] == 0 } then {
265 perror "GDC_UNDER_TEST ($GDC_UNDER_TEST) does not exist"
266 exit 1
267 }
268 }
269 if ![info exists tmpdir] {
270 set tmpdir "/tmp"
271 }
272
273 if [info exists gluefile] {
274 unset gluefile
275 }
276
277 gdc_maybe_build_wrapper "${tmpdir}/d-testglue.o"
278
279 set gcc_warning_prefix "warning:"
280 set gcc_error_prefix "(fatal )?error:"
281
282 verbose "gdc is initialized" 3
283 }
284
285 #
286 # gdc_target_compile -- compile a source file
287 #
288
289 proc gdc_target_compile { source dest type options } {
290 global tmpdir
291 global gluefile wrap_flags
292 global GDC_UNDER_TEST
293 global TOOL_OPTIONS
294 global TEST_ALWAYS_FLAGS
295
296 if { [target_info needs_status_wrapper] != "" && [info exists gluefile] } {
297 lappend options "libs=${gluefile}"
298 lappend options "ldflags=${wrap_flags}"
299 }
300
301 set always_dflags ""
302
303 # TEST_ALWAYS_FLAGS are flags that should be passed to every
304 # compilation. They are passed first to allow individual
305 # tests to override them.
306 if [info exists TEST_ALWAYS_FLAGS] {
307 lappend always_dflags "additional_flags=$TEST_ALWAYS_FLAGS"
308 }
309
310 if ![is_remote host] {
311 if [info exists TOOL_OPTIONS] {
312 lappend always_dflags "additional_flags=[gdc_include_flags [get_multilibs ${TOOL_OPTIONS}] ]"
313 lappend always_dflags "ldflags=[gdc_link_flags [get_multilibs ${TOOL_OPTIONS}] ]"
314 } else {
315 lappend always_dflags "additional_flags=[gdc_include_flags [get_multilibs] ]"
316 lappend always_dflags "ldflags=[gdc_link_flags [get_multilibs] ]"
317 }
318 }
319
320 if [info exists TOOL_OPTIONS] {
321 lappend always_dflags "additional_flags=$TOOL_OPTIONS"
322 }
323
324 verbose "always_dflags set to: $always_dflags"
325
326 lappend options "timeout=[timeout_value]"
327 lappend options "compiler=$GDC_UNDER_TEST"
328
329 set options [concat "$always_dflags" $options]
330 set options [dg-additional-files-options $options $source $dest $type]
331 return [target_compile $source $dest $type $options]
332 }