1 # Copyright (C) 2012-2024 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/>.
18 # gdc support library routines
24 load_lib target-libpath.exp
27 # GDC_UNDER_TEST is the compiler under test.
30 set gdc_compile_options ""
34 # gdc_version -- extract and print the version number of the compiler
37 proc gdc_version { } {
42 # ignore any arguments after the command
43 set compiler [lindex $GDC_UNDER_TEST 0]
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 {
53 clone_output "$compiler $version\n"
55 clone_output "[which $compiler] $version\n"
58 clone_output "Couldn't determine version of [which $compiler]\n"
61 # compiler does not exist (this should have already been detected)
62 warning "$compiler does not exist"
67 # gdc_include_flags -- include flags for the gcc tree structure
70 proc gdc_include_flags { paths } {
72 global GDC_INCLUDE_CXX_FLAGS
73 global TESTING_IN_BUILD_TREE
77 if { [is_remote host] || ![info exists TESTING_IN_BUILD_TREE] } {
81 if [info exists GDC_INCLUDE_CXX_FLAGS] {
82 set include_cxx_flags $GDC_INCLUDE_CXX_FLAGS
84 set include_cxx_flags 0
89 if { $gccpath != "" } {
90 if [file exists "${gccpath}/libphobos/libdruntime"] {
91 append flags "-I${gccpath}/libphobos/libdruntime "
94 append flags "-I${srcdir}/../../libphobos/libdruntime "
95 append flags "-I${srcdir}/../../libphobos/src "
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]
111 # gdc_link_flags -- linker flags for the gcc tree structure
114 proc gdc_link_flags { paths } {
116 global ld_library_path
117 global GDC_UNDER_TEST
118 global GDC_INCLUDE_CXX_FLAGS
125 set ld_library_path "."
126 set shlib_ext [get_shlib_extension]
128 verbose "shared lib extension: $shlib_ext"
130 if [info exists GDC_INCLUDE_CXX_FLAGS] {
131 set include_cxx_flags $GDC_INCLUDE_CXX_FLAGS
133 set include_cxx_flags 0
136 if { $gccpath != "" } {
137 # Path to libgphobos.spec.
138 append flags "-B${gccpath}/libphobos/src "
139 # Path to drtbegin.o/drtend.o.
140 if { [file exists "${gccpath}/libphobos/libdruntime/gcc/drtbegin.o"] } {
141 append flags "-B${gccpath}/libphobos/libdruntime/gcc "
144 if { [file exists "${gccpath}/libphobos/src/.libs/libgphobos.a"] \
145 || [file exists "${gccpath}/libphobos/src/.libs/libgphobos.${shlib_ext}"] } {
146 append flags "-L${gccpath}/libphobos/src/.libs "
147 append ld_library_path ":${gccpath}/libphobos/src/.libs"
149 # Static linking is default. If only the shared lib is available adjust
150 # flags to always use it. If both are available, set SHARED_OPTION which
151 # will be added to PERMUTE_ARGS
152 if { [file exists "${gccpath}/libphobos/src/.libs/libgphobos.${shlib_ext}"] } {
153 if { [file exists "${gccpath}/libphobos/src/.libs/libgphobos.a"] } {
154 set SHARED_OPTION "-shared-libphobos"
156 append flags "-shared-libphobos "
159 if [file exists "${gccpath}/libiberty/libiberty.a"] {
160 append flags "-L${gccpath}/libiberty "
162 # For the tests that mix C++ and D, need to know where library is located.
163 if $include_cxx_flags {
164 if { [file exists "${gccpath}/libstdc++-v3/src/.libs/libstdc++.a"] \
165 || [file exists "${gccpath}/libstdc++-v3/src/.libs/libstdc++.${shlib_ext}"] } {
166 append flags "-L${gccpath}/libstdc++-v3/src/.libs "
167 append ld_library_path ":${gccpath}/libstdc++-v3/src/.libs"
170 append ld_library_path [gcc-set-multilib-library-path $GDC_UNDER_TEST]
174 set libphobos [lookfor_file ${tool_root_dir} libgphobos]
175 if { $libphobos != "" } {
176 append flags "-B${libphobos} -L${libphobos} "
177 append ld_library_path ":${libphobos}"
179 set libiberty [lookfor_file ${tool_root_dir} libiberty]
180 if { $libiberty != "" } {
181 append flags "-L${libiberty} "
183 if $include_cxx_flags {
184 set libstdcpp [lookfor_file ${tool_root_dir} libstdc++]
185 if { $libstdcpp != "" } {
186 append flags "-L${libstdcpp} "
187 append ld_library_path ":${libstdcpp}"
192 set_ld_library_path_env_vars
198 # gdc_init -- called at the start of each subdir of tests
201 proc gdc_init { args } {
202 global gdc_initialized
206 global gluefile wrap_flags
207 global TOOL_EXECUTABLE
208 global GDC_UNDER_TEST
209 global TESTING_IN_BUILD_TREE
210 global gcc_warning_prefix
211 global gcc_error_prefix
213 # We set LC_ALL and LANG to C so that we get the same error messages as expected.
217 if ![info exists GDC_UNDER_TEST] then {
218 if [info exists TOOL_EXECUTABLE] {
219 set GDC_UNDER_TEST $TOOL_EXECUTABLE
221 if { [is_remote host] || ! [info exists TESTING_IN_BUILD_TREE] } {
222 set GDC_UNDER_TEST [transform gdc]
224 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]]]
229 if ![is_remote host] {
230 if { [which $GDC_UNDER_TEST] == 0 } then {
231 perror "GDC_UNDER_TEST ($GDC_UNDER_TEST) does not exist"
235 if ![info exists tmpdir] {
239 if [info exists gluefile] {
243 gdc_maybe_build_wrapper "${tmpdir}/d-testglue.o"
245 set gcc_warning_prefix "warning:"
246 set gcc_error_prefix "(fatal )?error:"
248 verbose "gdc is initialized" 3
252 # gdc_target_compile -- compile a source file
255 proc gdc_target_compile { source dest type options } {
257 global gluefile wrap_flags
258 global GDC_UNDER_TEST
260 global TEST_ALWAYS_FLAGS
262 if { [target_info needs_status_wrapper] != "" && [info exists gluefile] } {
263 lappend options "libs=${gluefile}"
264 lappend options "ldflags=${wrap_flags}"
269 # TEST_ALWAYS_FLAGS are flags that should be passed to every
270 # compilation. They are passed first to allow individual
271 # tests to override them.
272 if [info exists TEST_ALWAYS_FLAGS] {
273 lappend always_dflags "additional_flags=$TEST_ALWAYS_FLAGS"
276 if ![is_remote host] {
277 if [info exists TOOL_OPTIONS] {
278 lappend always_dflags "additional_flags=[gdc_include_flags [get_multilibs ${TOOL_OPTIONS}] ]"
279 lappend always_dflags "ldflags=[gdc_link_flags [get_multilibs ${TOOL_OPTIONS}] ]"
281 lappend always_dflags "additional_flags=[gdc_include_flags [get_multilibs] ]"
282 lappend always_dflags "ldflags=[gdc_link_flags [get_multilibs] ]"
286 if [info exists TOOL_OPTIONS] {
287 lappend always_dflags "additional_flags=$TOOL_OPTIONS"
290 verbose "always_dflags set to: $always_dflags"
292 lappend options "timeout=[timeout_value]"
293 lappend options "compiler=$GDC_UNDER_TEST"
295 set options [concat "$always_dflags" $options]
296 set options [dg-additional-files-options $options $source]
297 return [target_compile $source $dest $type $options]