1# Copyright (C) 1992-2015 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# This file was written by Rob Savoye (rob@cygnus.com)
18# Many modifications by Jeffrey Wheat (cassidy@cygnus.com)
19# With modifications by Mike Stump <mrs@cygnus.com>.
20
21#
22# g++ support library routines
23#
24load_lib prune.exp
25load_lib gcc-defs.exp
26load_lib timeout.exp
27load_lib target-libpath.exp
28
29#
30# GXX_UNDER_TEST is the compiler under test.
31#
32
33
34set gpp_compile_options ""
35
36#
37# g++_version -- extract and print the version number of the compiler
38#
39
40proc g++_version { } {
41    global GXX_UNDER_TEST
42
43    g++_init
44
45    # ignore any arguments after the command
46    set compiler [lindex $GXX_UNDER_TEST 0]
47
48    # verify that the compiler exists
49    if { [is_remote host] || [which $compiler] != 0 } then {
50	set tmp [remote_exec host "$compiler -v"]
51	set status [lindex $tmp 0]
52	set output [lindex $tmp 1]
53	regexp " version \[^\n\r\]*" $output version
54	if { $status == 0 && [info exists version] } then {
55	    if [is_remote host] {
56		clone_output "$compiler $version\n"
57	    } else {
58		clone_output "[which $compiler] $version\n"
59	    }
60	} else {
61	    clone_output "Couldn't determine version of [which $compiler]\n"
62	}
63    } else {
64	# compiler does not exist (this should have already been detected)
65	warning "$compiler does not exist"
66    }
67}
68
69#
70# g++_include_flags -- provide new version of g++_include_flags
71# (originally from libgloss.exp) which knows about the gcc tree structure
72#
73proc g++_include_flags { paths } {
74    global srcdir
75    global HAVE_LIBSTDCXX_V3
76    global TESTING_IN_BUILD_TREE
77
78    set flags ""
79
80    if { [is_remote host] || ! [info exists TESTING_IN_BUILD_TREE] } {
81      return "${flags}"
82    }
83
84    set gccpath ${paths}
85
86    set odir [lookfor_file ${gccpath} libstdc++-v3]
87    if { ${odir} != "" } {
88      append flags [exec sh ${odir}/scripts/testsuite_flags --build-includes]
89    }
90
91    return "$flags"
92}
93
94#
95# g++_link_flags -- provide new version of g++_link_flags
96# (originally from libgloss.exp) which knows about the gcc tree structure
97#
98
99proc g++_link_flags { paths } {
100    global srcdir
101    global ld_library_path
102    global GXX_UNDER_TEST
103    global shlib_ext
104
105    set gccpath ${paths}
106    set libio_dir ""
107    set flags ""
108    set ld_library_path "."
109
110    set shlib_ext [get_shlib_extension]
111    verbose "shared lib extension: $shlib_ext"
112
113    if { $gccpath != "" } {
114      if [file exists "${gccpath}/lib/libstdc++.a"] {
115          append ld_library_path ":${gccpath}/lib"
116      }
117      if [file exists "${gccpath}/libg++/libg++.a"] {
118          append flags "-L${gccpath}/libg++ "
119          append ld_library_path ":${gccpath}/libg++"
120      }
121      if [file exists "${gccpath}/libstdc++/libstdc++.a"] {
122          append flags "-L${gccpath}/libstdc++ "
123          append ld_library_path ":${gccpath}/libstdc++"
124      }
125      if [file exists "${gccpath}/libstdc++-v3/src/.libs/libstdc++.a"] {
126          append flags " -L${gccpath}/libstdc++-v3/src/.libs "
127          # Some targets use libstdc++.a%s in their specs, so they need a
128          # -B option for uninstalled testing.
129          append flags " -B${gccpath}/libstdc++-v3/src/.libs "
130          append ld_library_path ":${gccpath}/libstdc++-v3/src/.libs"
131      }
132      # Look for libstdc++.${shlib_ext}.
133      if [file exists "${gccpath}/libstdc++-v3/src/.libs/libstdc++.${shlib_ext}"] {
134	  append flags " -L${gccpath}/libstdc++-v3/src/.libs "
135	  append ld_library_path ":${gccpath}/libstdc++-v3/src/.libs"
136      }
137
138      if [file exists "${gccpath}/libiberty/libiberty.a"] {
139          append flags "-L${gccpath}/libiberty "
140      }
141      if [file exists "${gccpath}/librx/librx.a"] {
142          append flags "-L${gccpath}/librx "
143      }
144      append ld_library_path [gcc-set-multilib-library-path $GXX_UNDER_TEST]
145    } else {
146      global tool_root_dir
147
148      set libgpp [lookfor_file ${tool_root_dir} libg++]
149      if { $libgpp != "" } {
150          append flags "-L${libgpp} "
151          append ld_library_path ":${libgpp}"
152      }
153      set libstdcpp [lookfor_file ${tool_root_dir} libstdc++]
154      if { $libstdcpp != "" } {
155          append flags "-L${libstdcpp} "
156          append ld_library_path ":${libstdcpp}"
157      }
158      set libiberty [lookfor_file ${tool_root_dir} libiberty]
159      if { $libiberty != "" } {
160          append flags "-L${libiberty} "
161      }
162      set librx [lookfor_file ${tool_root_dir} librx]
163      if { $librx != "" } {
164          append flags "-L${librx} "
165      }
166    }
167
168    set_ld_library_path_env_vars
169
170    return "$flags"
171}
172
173#
174# g++_init -- called at the start of each subdir of tests
175#
176
177proc g++_init { args } {
178    global subdir
179    global gpp_initialized
180    global base_dir
181    global tmpdir
182    global libdir
183    global gluefile wrap_flags
184    global objdir srcdir
185    global ALWAYS_CXXFLAGS
186    global CXXFLAGS
187    global TOOL_EXECUTABLE TOOL_OPTIONS
188    global GXX_UNDER_TEST
189    global TESTING_IN_BUILD_TREE
190    global gcc_warning_prefix
191    global gcc_error_prefix
192    global TEST_ALWAYS_FLAGS
193
194    # We set LC_ALL and LANG to C so that we get the same error messages as expected.
195    setenv LC_ALL C
196    setenv LANG C
197
198    # Many hosts now default to a non-ASCII C locale, however, so
199    # they can set a charset encoding here if they need.
200    if { [ishost "*-*-cygwin*"] } {
201      setenv LC_ALL C.ASCII
202      setenv LANG C.ASCII
203    }
204
205    if ![info exists GXX_UNDER_TEST] then {
206	if [info exists TOOL_EXECUTABLE] {
207	    set GXX_UNDER_TEST $TOOL_EXECUTABLE
208	} else {
209	    if { [is_remote host] || ! [info exists TESTING_IN_BUILD_TREE] } {
210		set GXX_UNDER_TEST [transform c++]
211	    } else {
212		set GXX_UNDER_TEST [findfile $base_dir/../../xg++ "$base_dir/../../xg++ -B$base_dir/../../" [findfile $base_dir/xg++ "$base_dir/xg++ -B$base_dir/" [transform c++]]]
213	    }
214	}
215    }
216
217    # Bleah, nasty. Bad taste.
218    if [ishost "*-dos-*" ] {
219	regsub "c\\+\\+" "$GXX_UNDER_TEST" "gcc" GXX_UNDER_TEST
220    }
221
222    if ![is_remote host] {
223	if { [which $GXX_UNDER_TEST] == 0 } then {
224	    perror "GXX_UNDER_TEST ($GXX_UNDER_TEST) does not exist"
225	    exit 1
226	}
227    }
228    if ![info exists tmpdir] {
229	set tmpdir "/tmp"
230    }
231
232    if [info exists gluefile] {
233	unset gluefile
234    }
235
236    g++_maybe_build_wrapper "${tmpdir}/g++-testglue.o" "-fexceptions"
237
238    if {![info exists CXXFLAGS]} {
239	set CXXFLAGS ""
240    }
241
242    set ALWAYS_CXXFLAGS ""
243
244    # TEST_ALWAYS_FLAGS are flags that should be passed to every
245    # compilation command.  They are passed first to allow individual
246    # tests to override them.
247    if [info exists TEST_ALWAYS_FLAGS] {
248	lappend ALWAYS_CXXFLAGS "additional_flags=$TEST_ALWAYS_FLAGS"
249    }
250
251    if ![is_remote host] {
252	if [info exists TOOL_OPTIONS] {
253	    lappend ALWAYS_CXXFLAGS "additional_flags=[g++_include_flags [get_multilibs ${TOOL_OPTIONS}] ]"
254	    lappend ALWAYS_CXXFLAGS "ldflags=[g++_link_flags [get_multilibs ${TOOL_OPTIONS}] ]"
255	} else {
256	    lappend ALWAYS_CXXFLAGS "additional_flags=[g++_include_flags [get_multilibs] ]"
257	    lappend ALWAYS_CXXFLAGS "ldflags=[g++_link_flags [get_multilibs] ]"
258	}
259    }
260
261    if [info exists TOOL_OPTIONS] {
262	lappend ALWAYS_CXXFLAGS "additional_flags=$TOOL_OPTIONS"
263    }
264
265    # Make sure that lines are not wrapped.  That can confuse the
266    # error-message parsing machinery.
267    lappend ALWAYS_CXXFLAGS "additional_flags=-fmessage-length=0"
268
269    set gcc_warning_prefix "warning:"
270    set gcc_error_prefix "(fatal )?error:"
271
272    if { [istarget *-*-darwin*] } {
273	lappend ALWAYS_CXXFLAGS "ldflags=-multiply_defined suppress"
274       }
275
276    verbose -log "ALWAYS_CXXFLAGS set to $ALWAYS_CXXFLAGS"
277
278    verbose "g++ is initialized" 3
279}
280
281#
282# g++_target_compile -- compile a source file
283#
284
285proc g++_target_compile { source dest type options } {
286    global tmpdir
287    global gpp_compile_options
288    global gluefile wrap_flags
289    global ALWAYS_CXXFLAGS
290    global GXX_UNDER_TEST
291    global flags_to_postpone
292    global board_info
293
294    if { [target_info needs_status_wrapper] != "" && [info exists gluefile] } {
295	lappend options "libs=${gluefile}"
296	lappend options "ldflags=${wrap_flags}"
297    }
298
299    global TEST_EXTRA_LIBS
300    if [info exists TEST_EXTRA_LIBS] {
301	lappend options "ldflags=$TEST_EXTRA_LIBS"
302    }
303
304    lappend options "additional_flags=[libio_include_flags]"
305    lappend options "compiler=$GXX_UNDER_TEST"
306    lappend options "timeout=[timeout_value]"
307
308    set options [concat $gpp_compile_options $options]
309
310    set options [concat "$ALWAYS_CXXFLAGS" $options]
311
312    if { [regexp "(^| )-frepo( |$)" $options] && \
313	 [regexp "\.o(|bj)$" $dest] } then {
314	regsub "\.o(|bj)$" $dest ".rpo" rponame
315	exec rm -f $rponame
316    }
317
318    # bind_pic_locally adds -fpie/-fPIE flags to flags_to_postpone and it is
319    # appended here to multilib_flags as it can be overridden by the latter
320    # if it was added earlier. After the target_compile, multilib_flags is
321    # restored to its orignal content.
322    set tboard [target_info name]
323    if {[board_info $tboard exists multilib_flags]} {
324        set orig_multilib_flags "[board_info [target_info name] multilib_flags]"
325        append board_info($tboard,multilib_flags) " $flags_to_postpone"
326    }
327
328    set options [dg-additional-files-options $options $source]
329
330    set result [target_compile $source $dest $type $options]
331
332    if {[board_info $tboard exists multilib_flags]} {
333        set board_info($tboard,multilib_flags) $orig_multilib_flags
334        set flags_to_postpone ""
335    }
336
337    return $result
338}
339
340#
341# ${tool}_option_help
342#
343# Changed "additional" to "extra" because runtest.exp treats --a* as --all.
344#
345# This shouldn't be necessary at all; it should be entirely redundant with
346# --tool_opts, except that --tool_opts currently breaks multilib: see
347# http://lists.gnu.org/archive/html/dejagnu/2002-10/msg00007.html
348
349proc ${tool}_option_help { } {
350    send_user " --extra_opts,OPTIONS\t\tUse OPTIONS to compile the testcase files. OPTIONS should be comma-separated.\n"
351}
352
353#
354# ${tool}_option_proc
355#
356
357proc ${tool}_option_proc { option } {
358    if [regexp "^--extra_opts," $option] {
359	global gpp_compile_options
360	regsub "^--extra_opts," $option "" option
361	foreach x [split $option ","] {
362	    lappend gpp_compile_options "additional_flags=$x"
363	}
364	verbose -log "gpp_compile_options set to $gpp_compile_options"
365	return 1
366    } else {
367	return 0
368    }
369}
370