1# Copyright (C) 2009-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#
18# go support library routines
19#
20load_lib prune.exp
21load_lib gcc-defs.exp
22load_lib timeout.exp
23load_lib target-libpath.exp
24
25#
26# GOC_UNDER_TEST is the compiler under test.
27#
28
29
30set gpp_compile_options ""
31
32
33#
34# go_version -- extract and print the version number of the compiler
35#
36
37proc go_version { } {
38    global GOC_UNDER_TEST
39
40    go_init
41
42    # ignore any arguments after the command
43    set compiler [lindex $GOC_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# go_include_flags -- include flags for the gcc tree structure
68#
69
70proc go_include_flags { paths } {
71    global srcdir
72    global TESTING_IN_BUILD_TREE
73
74    set flags ""
75
76    if { [is_remote host] || ![info exists TESTING_IN_BUILD_TREE] } {
77	return "${flags}"
78    }
79
80    set gccpath ${paths}
81
82    if { $gccpath != "" } {
83	if [file exists "${gccpath}/libgo/os.gox"] {
84	    append flags "-I${gccpath}/libgo "
85	}
86    }
87}
88
89#
90# go_link_flags -- linker flags for the gcc tree structure
91#
92
93proc go_link_flags { paths } {
94    global srcdir
95    global ld_library_path
96    global GOC_UNDER_TEST
97    global shlib_ext
98
99    set gccpath ${paths}
100    set libio_dir ""
101    set flags ""
102    set ld_library_path "."
103    set shlib_ext [get_shlib_extension]
104    verbose "shared lib extension: $shlib_ext"
105
106    if { $gccpath != "" } {
107      if [file exists "${gccpath}/libgo/libgobegin.a"] {
108	  append flags "-L${gccpath}/libgo "
109      }
110      if { [file exists "${gccpath}/libgo/.libs/libgo.a"] \
111	   || [file exists "${gccpath}/libgo/.libs/libgo.${shlib_ext}"] } {
112          append flags "-L${gccpath}/libgo/.libs "
113          append ld_library_path ":${gccpath}/libgo/.libs"
114      }
115      if [file exists "${gccpath}/libiberty/libiberty.a"] {
116          append flags "-L${gccpath}/libiberty "
117      }
118      append ld_library_path \
119	[gcc-set-multilib-library-path $GOC_UNDER_TEST]
120    }
121
122    set_ld_library_path_env_vars
123
124    return "$flags"
125}
126
127#
128# go_init -- called at the start of each subdir of tests
129#
130
131proc go_init { args } {
132    global subdir
133    global gpp_initialized
134    global base_dir
135    global tmpdir
136    global libdir
137    global gluefile wrap_flags
138    global objdir srcdir
139    global ALWAYS_GOCFLAGS
140    global TOOL_EXECUTABLE TOOL_OPTIONS
141    global GOC_UNDER_TEST
142    global TESTING_IN_BUILD_TREE
143    global TEST_ALWAYS_FLAGS
144
145    # We set LC_ALL and LANG to C so that we get the same error messages as expected.
146    setenv LC_ALL C
147    setenv LANG C
148
149    if ![info exists GOC_UNDER_TEST] then {
150	if [info exists TOOL_EXECUTABLE] {
151	    set GOC_UNDER_TEST $TOOL_EXECUTABLE
152	} else {
153	    if { [is_remote host] || ! [info exists TESTING_IN_BUILD_TREE] } {
154		set GOC_UNDER_TEST [transform gccgo]
155	    } else {
156		set GOC_UNDER_TEST [findfile $base_dir/../../gccgo "$base_dir/../../gccgo -B$base_dir/../../" [findfile $base_dir/gccgo "$base_dir/gccgo -B$base_dir/" [transform gccgo]]]
157	    }
158	}
159    }
160
161    if ![is_remote host] {
162	if { [which $GOC_UNDER_TEST] == 0 } then {
163	    perror "GOC_UNDER_TEST ($GOC_UNDER_TEST) does not exist"
164	    exit 1
165	}
166    }
167    if ![info exists tmpdir] {
168	set tmpdir "/tmp"
169    }
170
171    if [info exists gluefile] {
172	unset gluefile
173    }
174
175    go_maybe_build_wrapper "${tmpdir}/go-testglue.o"
176
177    set ALWAYS_GOCFLAGS ""
178
179    # TEST_ALWAYS_FLAGS are flags that should be passed to every
180    # compilation.  They are passed first to allow individual
181    # tests to override them.
182    if [info exists TEST_ALWAYS_FLAGS] {
183	lappend ALWAYS_GOCFLAGS "additional_flags=$TEST_ALWAYS_FLAGS"
184    }
185
186    if ![is_remote host] {
187	if [info exists TOOL_OPTIONS] {
188	    lappend ALWAYS_GOCFLAGS "additional_flags=[go_include_flags [get_multilibs ${TOOL_OPTIONS}] ]"
189	    lappend ALWAYS_GOCFLAGS "ldflags=[go_link_flags [get_multilibs ${TOOL_OPTIONS}] ]"
190	} else {
191	    lappend ALWAYS_GOCFLAGS "additional_flags=[go_include_flags [get_multilibs] ]"
192	    lappend ALWAYS_GOCFLAGS "ldflags=[go_link_flags [get_multilibs] ]"
193	}
194    }
195
196    if [info exists TOOL_OPTIONS] {
197	lappend ALWAYS_GOCFLAGS "additional_flags=$TOOL_OPTIONS"
198    }
199
200    verbose -log "ALWAYS_GOCFLAGS set to $ALWAYS_GOCFLAGS"
201
202    verbose "go is initialized" 3
203}
204
205#
206# go_target_compile -- compile a source file
207#
208
209proc go_target_compile { source dest type options } {
210    global tmpdir
211    global gluefile wrap_flags
212    global ALWAYS_GOCFLAGS
213    global GOC_UNDER_TEST
214
215    if { [target_info needs_status_wrapper] != "" && [info exists gluefile] } {
216	lappend options "libs=${gluefile}"
217	lappend options "ldflags=${wrap_flags}"
218    }
219
220    lappend options "timeout=[timeout_value]"
221    lappend options "compiler=$GOC_UNDER_TEST"
222
223    set options [concat "$ALWAYS_GOCFLAGS" $options]
224    set options [dg-additional-files-options $options $source]
225    return [target_compile $source $dest $type $options]
226}
227