1# Copyright 2004 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 2 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 this program; if not, write to the Free Software
15# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
16# USA.
17
18load_lib libgloss.exp
19
20# FIXME:brobecker/2004-03-31:
21# The following function should eventually be part of dejagnu. Even after
22# this function becomes available in dejagnu, we will keep for a while
23# a copy of this function here in order to avoid increasing the dejagnu
24# version requirement.
25
26proc gdb_find_gnatmake {} {
27    global tool_root_dir
28
29    set root "$tool_root_dir/gcc"
30    set GM ""
31
32    if ![is_remote host] {
33        set file [lookfor_file $root gnatmake]
34        if { $file != "" } {
35            set GM "$file -I$root/ada/rts --GCC=$root/xgcc --GNATBIND=$root/gnatbind --GNATLINK=$root/gnatlink -cargs -B$root -largs --GCC=$root/xgcc -margs";
36        }
37    }
38
39    if {$GM == ""} {
40        set GM [transform gnatmake]
41    }
42
43    return $GM
44}
45
46# FIXME:brobecker/2004-03-31:
47# The following function is a copy of the function of the same name provided
48# by dejagnu, except that it has been modified to add support for building
49# Ada programs.  This copy is temporarily placed here until the changes
50# are merged into the dejagnu official release. Once the changes are merged,
51# we will likely keep this local copy for a while to avoid increasing the
52# dejagnu version requirement.
53
54proc gdb_default_target_compile {source destfile type options} {
55    global target_triplet
56    global tool_root_dir
57    global CFLAGS_FOR_TARGET
58    global compiler_flags
59
60    if { $destfile == "" && $type != "preprocess" && $type != "none" } {
61	error "Must supply an output filename for the compile to default_target_compile"
62    }
63
64    set add_flags ""
65    set libs ""
66    set compiler_type "c"
67    set compiler ""
68    set ldflags ""
69    set dest [target_info name]
70
71    if [info exists CFLAGS_FOR_TARGET] {
72	append add_flags " $CFLAGS_FOR_TARGET"
73    }
74
75    if [info exists target_info(host,name)] {
76	set host [host_info name];
77    } else {
78	set host "unix";
79    }
80
81    foreach i $options {
82	if { $i == "ada" } {
83	    set compiler_type "ada"
84	    if [board_info $dest exists adaflags] {
85		append add_flags " [target_info adaflags]"
86	    }
87	    # append add_flags " [gnatmake_include_flags]";
88	    if [board_info $dest exists gnatmake] {
89		set compiler [target_info gnatmake];
90	    } else {
91		set compiler [find_gnatmake];
92	    }
93	}
94
95	if { $i == "c++" } {
96	    set compiler_type "c++"
97	    if [board_info $dest exists cxxflags] {
98		append add_flags " [target_info cxxflags]"
99	    }
100	    append add_flags " [g++_include_flags]";
101	    if [board_info $dest exists c++compiler] {
102		set compiler [target_info c++compiler];
103	    } else {
104		set compiler [find_g++];
105	    }
106	}
107
108	if { $i == "f77" } {
109	    set compiler_type "f77"
110	    if [board_info $dest exists f77flags] {
111		append add_flags " [target_info f77flags]"
112	    }
113#	    append add_flags " [f77_include_flags]"
114	    if [board_info $dest exists f77compiler] {
115		set compiler [target_info f77compiler]
116	    } else {
117		set compiler [find_g77]
118	    }
119	}
120
121	if [regexp "^dest=" $i] {
122	    regsub "^dest=" $i "" tmp
123	    if [board_info $tmp exists name] {
124		set dest [board_info $tmp name];
125	    } else {
126		set dest $tmp;
127	    }
128	}
129	if [regexp "^compiler=" $i] {
130	    regsub "^compiler=" $i "" tmp
131	    set compiler $tmp
132	}
133	if [regexp "^additional_flags=" $i] {
134	    regsub "^additional_flags=" $i "" tmp
135	    append add_flags " $tmp"
136	}
137	if [regexp "^ldflags=" $i] {
138	    regsub "^ldflags=" $i "" tmp
139	    append ldflags " $tmp"
140	}
141	if [regexp "^libs=" $i] {
142	    regsub "^libs=" $i "" tmp
143	    append libs " $tmp"
144	}
145	if [regexp "^incdir=" $i] {
146	    regsub "^incdir=" $i "-I" tmp
147	    append add_flags " $tmp"
148	}
149	if [regexp "^libdir=" $i] {
150	    regsub "^libdir=" $i "-L" tmp
151	    append add_flags " $tmp"
152	}
153	if [regexp "^ldscript=" $i] {
154	    regsub "^ldscript=" $i "" ldscript
155	}
156 	if [regexp "^redirect=" $i] {
157 	    regsub "^redirect=" $i "" redirect
158 	}
159 	if [regexp "^optimize=" $i] {
160 	    regsub "^optimize=" $i "" optimize
161 	}
162 	if [regexp "^timeout=" $i] {
163 	    regsub "^timeout=" $i "" timeout
164 	}
165    }
166
167    if [board_info $host exists cflags_for_target] {
168	append add_flags " [board_info $host cflags_for_target]";
169    }
170
171    global CC_FOR_TARGET
172    global CXX_FOR_TARGET
173    global F77_FOR_TARGET
174    global GNATMAKE_FOR_TARGET
175
176    if [info exists CC_FOR_TARGET] {
177	if { $compiler == "" } {
178	    set compiler $CC_FOR_TARGET
179	}
180    }
181
182    if [info exists CXX_FOR_TARGET] {
183	if { $compiler_type == "c++" } {
184	    set compiler $CXX_FOR_TARGET
185	}
186    }
187
188    if [info exists F77_FOR_TARGET] {
189	if { $compiler_type == "f77" } {
190	    set compiler $F77_FOR_TARGET
191	}
192    }
193
194    if [info exists GNATMAKE_FOR_TARGET] {
195	if { $compiler_type == "ada" } {
196	    set compiler $GNATMAKE_FOR_TARGET
197	}
198    }
199
200    if { $compiler == "" } {
201	set compiler [board_info $dest compiler];
202	if { $compiler == "" } {
203	    return "default_target_compile: No compiler to compile with";
204	}
205    }
206
207    if ![is_remote host] {
208	if { [which $compiler] == 0 } {
209	    return "default_target_compile: Can't find $compiler."
210	}
211    }
212
213    if {$type == "object"} {
214	append add_flags " -c"
215    }
216
217    if { $type == "preprocess" } {
218	append add_flags " -E"
219    }
220
221    if { $type == "assembly" } {
222	append add_flags " -S"
223    }
224
225    if [board_info $dest exists cflags] {
226	append add_flags " [board_info $dest cflags]"
227    }
228
229    if { $type == "executable" } {
230	# This must be added here.
231	# if [board_info $dest exists ldscript] {
232	#     append add_flags " [board_info $dest ldscript]"
233	# }
234
235	if [board_info $dest exists ldflags] {
236	    append add_flags " [board_info $dest ldflags]"
237	}
238	if { $compiler_type == "c++" } {
239	    append add_flags " [g++_link_flags]";
240	}
241	if [isnative] {
242	    # This is a lose.
243	    catch "glob -nocomplain $tool_root_dir/libstdc++/libstdc++.so* $tool_root_dir/libstdc++/libstdc++.sl" tmp
244	    if { ${tmp} != "" } {
245		if [regexp ".*solaris2.*" $target_triplet] {
246		    # Solaris 2
247		    append add_flags " -R$tool_root_dir/libstdc++"
248		} elseif [regexp ".*(osf|irix5|linux).*" $target_triplet] {
249		    # OSF/1 or Irix5
250		    append add_flags " -Wl,-rpath,$tool_root_dir/libstdc++"
251		} elseif [regexp ".*hppa.*" $target_triplet] {
252		    # HP/UX
253		    append add_flags " -Wl,-a,shared_archive"
254		}
255	    }
256	}
257    }
258
259    if ![info exists ldscript] {
260	set ldscript [board_info $dest ldscript]
261    }
262
263    foreach i $options {
264	if { $i == "debug" } {
265	    if [board_info $dest exists debug_flags] {
266		append add_flags " [board_info $dest debug_flags]";
267	    } else {
268		append add_flags " -g"
269	    }
270	}
271    }
272
273    if [info exists optimize] {
274	append add_flags " $optimize";
275    }
276
277    if { $type == "executable" } {
278	append add_flags " $ldflags"
279	foreach x $libs {
280	    if [file exists $x] {
281		append source " $x"
282	    } else {
283		append add_flags " $x";
284	    }
285	}
286
287	if [board_info $dest exists libs] {
288	    append add_flags " [board_info $dest libs]"
289	}
290
291	# This probably isn't such a good idea, but it avoids nasty
292	# hackiness in the testsuites.
293	# The math library must be linked in before the C library.  The C
294	# library is linked in by the linker script, so this must be before
295	# the linker script.
296	if [board_info $dest exists mathlib] {
297	    append add_flags " [board_info $dest mathlib]"
298	} else {
299	    append add_flags " -lm"
300	}
301
302	# This must be added here.
303	append add_flags " $ldscript";
304
305	if [board_info $dest exists remote_link] {
306	    # Relink option.
307	    append add_flags " -Wl,-r"
308	}
309	if [board_info $dest exists output_format] {
310	    append add_flags " -Wl,-oformat,[board_info $dest output_format]";
311	}
312    }
313
314    if [board_info $dest exists multilib_flags] {
315	append add_flags " [board_info $dest multilib_flags]";
316    }
317
318    verbose "doing compile"
319
320    set sources ""
321    if [is_remote host] {
322	foreach x $source {
323	    set file [remote_download host $x];
324	    if { $file == "" } {
325		warning "Unable to download $x to host."
326		return "Unable to download $x to host."
327	    } else {
328		append sources " $file";
329	    }
330	}
331    } else {
332	set sources $source
333    }
334
335    if [is_remote host] {
336	append add_flags " -o a.out"
337	remote_file host delete a.out;
338    } else {
339	if { $destfile != "" } {
340	    append add_flags " -o $destfile";
341	}
342    }
343
344    # This is obscure: we put SOURCES at the end when building an
345    # object, because otherwise, in some situations, libtool will
346    # become confused about the name of the actual source file.
347    if {$type == "object"} {
348        set opts "$add_flags $sources"
349    } else {
350        set opts "$sources $add_flags"
351    }
352
353    if [is_remote host] {
354	if [host_info exists use_at] {
355	    set fid [open "atfile" "w"];
356	    puts $fid "$opts";
357	    close $fid;
358	    set opts "@[remote_download host atfile]"
359	    remote_file build delete atfile
360	}
361    }
362
363    verbose "Invoking the compiler as $compiler $opts" 2
364
365    if [info exists redirect] {
366	verbose "Redirecting output to $redirect" 2
367	set status [remote_exec host "$compiler $opts" "" "" $redirect];
368    } else {
369	if [info exists timeout] {
370	    verbose "Setting timeout to $timeout" 2
371	    set status [remote_exec host "$compiler $opts" "" "" "" $timeout];
372	} else {
373	    set status [remote_exec host "$compiler $opts"];
374	}
375    }
376
377    set compiler_flags $opts
378    if [is_remote host] {
379	remote_upload host a.out $destfile;
380	remote_file host delete a.out;
381    }
382    set comp_output [prune_warnings [lindex $status 1]];
383    regsub "^\[\r\n\]+" $comp_output "" comp_output;
384    if { [lindex $status 0] != 0 } {
385	verbose -log "compiler exited with status [lindex $status 0]";
386    }
387    if { [lindex $status 1] != "" } {
388	verbose -log "output is:\n[lindex $status 1]" 2;
389    }
390    if { [lindex $status 0] != 0 && "${comp_output}" == "" } {
391	set comp_output "exit status is [lindex $status 0]";
392    }
393    return ${comp_output};
394}
395
396# See if the version of dejaGNU being used to run the testsuite is
397# recent enough to contain support for building Ada programs or not.
398# If not, then use the functions above in place of the ones provided
399# by dejaGNU. This is only temporary (brobecker/2004-03-31).
400
401if {[info procs find_gnatmake] == ""} {
402  proc find_gnatmake { } { return [gdb_find_gnatmake] }
403  proc default_target_compile { source destfile type options } {
404    return [gdb_default_target_compile $source $destfile $type $options]
405  }
406}
407
408# Compile some Ada code.
409
410proc gdb_compile_ada {source dest type options} {
411
412    set objdir [file dirname $dest]
413
414    append options " ada"
415    append options " additional_flags=-P$objdir/gnat_ada"
416
417    set result [target_compile $source $dest $type $options]
418
419    # The Ada build always produces some output, even when the build
420    # succeeds. Thus, we can not use the output the same way we do in
421    # gdb_compile to determine whether the build has succeeded or not.
422    # We therefore simply check whether the dest file has been created
423    # or not. Unless not present, the build has succeeded.
424    if ![file exists $dest] {
425        unsupported "Ada compilation failed: $result"
426        return "Ada compilation failed."
427    }
428}
429
430