aboutsummaryrefslogtreecommitdiff
path: root/libjava/testsuite/lib/libjava.exp
blob: fad617503da0d28e3195321e03b87a639e125821 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
# Copyright (C) 1998, 1999 Cygnus Solutions

load_lib "libgloss.exp"

# GCJ_UNDER_TEST is the compiler under test.

global tmpdir

if ![info exists tmpdir] {
    set tmpdir "/tmp"
}

# Find `jv-scan'.  FIXME: this relies on DejaGnu internals.  These
# should probably be exposed in a better way.
proc find_jvscan {} {
    global tool_root_dir

    set file [lookfor_file $tool_root_dir jv-scan]
    if { $file == "" } {
	set file [lookfor_file $tool_root_dir gcc/jv-scan];
    }
    if {$file == ""} {
	set file jv-scan
    }
    return $file
}

proc bytecompile_file { file objdir {classpath {}} } {
    global env
    set dirname  [file dirname $file];

    catch {unset env(CLASSPATH)}
    if {$classpath != ""} then {
        set env(CLASSPATH) $classpath
    }
    if {[catch {system "cd $dirname; javac $file -d $objdir"} msg]} then {
	verbose "couldn't compile $file: $msg"
	set r 0
    } else {
	set r 1
    }
    return $r
}

set libjava_initialized 0

#
# Build the status wrapper library as needed.
#
proc libjava_init { args } {
    global wrapper_file;
    global wrap_compile_flags;
    global libjava_initialized
    global GCJ_UNDER_TEST
    global TOOL_EXECUTABLE

    if { $libjava_initialized == 1 } { return; }

    if ![info exists GCJ_UNDER_TEST] {
	if [info exists TOOL_EXECUTABLE] {
	    set GCJ_UNDER_TEST $TOOL_EXECUTABLE;
	} else {
	    set GCJ_UNDER_TEST "[find_gcj]"
	}
    }

    set wrapper_file "";
    set wrap_compile_flags "";
    if [target_info exists needs_status_wrapper] {
	set result [build_wrapper "testglue.o"];
	if { $result != "" } {
	    set wrapper_file [lindex $result 0];
	    set wrap_compile_flags [lindex $result 1];
	} else {
	    warning "Status wrapper failed to build."
	}
    }

    set libjava_initialized 1
}

# Find a library.  We know where libtool puts the actual libraries,
# and we look there.  The implementation is fairly hacky.  We can't
# compile with -nodefaultlibs, because that will also eliminate the
# system libraries we need.  In order to have gcj still work, it must
# find the appropriate libraries so we must add -L options for their
# paths.  However we can't simply use those libraries; we still need
# libtool for linking.
proc libjava_find_lib {dir name} {
    global base_dir
    set gp [get_multilibs]
    foreach sub {.libs _libs} {
	if {$gp != ""} {
	    if {[file exists $gp/$dir/$sub/lib${name}.a]} then {
		return "$gp/$dir/lib${name}.la -L$gp/$dir/$sub"
	    }
	}
	set lib [findfile $base_dir/../../$dir/$sub/lib${name}.a \
		   "$base_dir/../../$dir/lib${name}.la -L$base_dir/../../$dir/$sub" \
		   ""]
	if {$lib != ""} {
	    return $lib
	}
    }
    return ""
}

# Compute arguments needed for compiler.  MODE is a libtool mode:
# either compile or link.
proc libjava_arguments {{mode compile}} {
    global base_dir
    global LIBJAVA
    global LIBGC
    global srcdir subdir objdir
    global TOOL_OPTIONS
    global GCJ_UNDER_TEST
    global tmpdir
    global runtests

    if [info exists LIBJAVA] {
	set libjava $LIBJAVA;
    } else {
	set libjava [libjava_find_lib libjava gcj]
    }

    if [info exists LIBGC] {
	set libgc $LIBGC;
    } else {
	set libgc [libjava_find_lib boehm-gc gcjgc]
    }

    if [info exists LIBQTHREADS] {
	set libqthreads $LIBQTHREADS
    } else {
	set libqthreads [libjava_find_lib qthreads gcjcoop]
    }

    # FIXME: there's no way to determine whether -lpthread is
    # required.  We should get this info from configure, or it should
    # just be in the compiler driver.

    verbose "using LIBJAVA = $libjava" 2
    verbose "using LIBGC = $libgc" 2
    verbose "using LIBQTHREADS = $libqthreads" 2
    set args ""

    # Basically we want to build up a colon separated path list from
    # the value of $libjava.

    # First strip away any -L arguments.
    regsub -all -- "-L" $libjava "" ld_library_path

    # Then remove any -lgcj argument.
    regsub -all -- " -lgcj.*" $ld_library_path "" ld_library_path

    # First strip away any -L arguments.
    regsub -all -- "-L" $libgc $ld_library_path ld_library_path

    # Then remove any -lgcjgc argument.
    regsub -all -- " -lgcjgc.*" $ld_library_path "" ld_library_path

    # That's enough to make things work for the normal case.
    # If we wanted to handle an arbitrary value of libjava,
    # then we'd have to do a lot more work.

    # Set variables the dynamic linker looks at.
    setenv LD_LIBRARY_PATH $ld_library_path
    setenv SHLIB_PATH $ld_library_path

    # Set the CLASSPATH environment variable
    verbose "CLASSPATH is $objdir/.."
    global env
    set env(CLASSPATH) "$objdir/.."

    global wrapper_file wrap_compile_flags;
    lappend args "additional_flags=$wrap_compile_flags";
    lappend args "libs=$wrapper_file";
    lappend args "libs=$libjava";
    lappend args "libs=$libgc";
    lappend args "libs=$libqthreads"
    lappend args debug

    if { [target_info needs_status_wrapper]!="" && [info exists gluefile] } {
	lappend args "libs=${gluefile}"
	lappend args "ldflags=$wrap_flags"
    }

    if [info exists TOOL_OPTIONS] {
	lappend args "additional_flags=$TOOL_OPTIONS"
    }

    # Search for libtool.  We need it to link.
    set d [absolute $objdir]
    foreach x {. .. ../.. ../../..} {
	if {[file exists $d/$x/libtool]} then {
	    # We have to run silently to avoid DejaGNU lossage.
	    lappend args \
	      "compiler=$d/$x/libtool --silent --mode=$mode $GCJ_UNDER_TEST"
	    break
	}
    }

    return $args
}


#
# Run the test specified by srcfile and resultfile. compile_args and
# exec_args are additional arguments to be passed in when compiling and
# running the testcase, respectively.
#
proc test_libjava_from_source { options srcfile compile_args inpfile resultfile exec_args } {
    global base_dir
    global LIBJAVA
    global LIBGC
    global srcdir subdir objdir
    global TOOL_OPTIONS
    global GCJ_UNDER_TEST
    global tmpdir
    global runtests

    set errname [file rootname [file tail $srcfile]]
    if {! [runtest_file_p $runtests $errname]} {
        return
    }

    set args [libjava_arguments link]
    # Add the --main flag
    lappend args "additional_flags=--main=[file rootname [file tail $srcfile]]"
    if { $compile_args != "" } {
	lappend args "additional_flags=$compile_args"
    }

    regsub "^.*/(\[^/.\]+)\[.\]\[^/]*$" "$srcfile" "\\1" out
    set executable "${objdir}/$out"
    if { $compile_args != "" } {
	set errname "$errname $compile_args"
    }

    set x [target_compile $srcfile "$executable" executable $args]
    if { $x != "" } {
	verbose "target_compile failed: $x" 2
	fail "$errname compilation from source"
	if {$exec_args != "no-exec"} {
	    setup_xfail "*-*-*"
	    fail "$errname execution from source compiled test"
	    setup_xfail "*-*-*"
	    fail "$errname output from source compiled test"
	}
	return;
    }
    pass "$errname compilation from source"

    if { $exec_args == "no-exec" } {
	return
    }

    set result [libjava_load $executable "$exec_args" "$inpfile"];
    set status [lindex $result 0];
    set output [lindex $result 1];
    $status "$errname execution from source compiled test"
    if { $status != "pass" } {
	setup_xfail "*-*-*"
	fail "$errname output from source compiled test"
	return;
    }

    verbose "resultfile is $resultfile"
    set id [open $resultfile r];
    set expected ""
    append expected [read $id];
    regsub -all "\r" "$output" "" output;
    regsub "\n*$" $expected "" expected
    regsub "\n*$" $output "" output
    regsub "^\n*" $expected "" expected
    regsub "^\n*" $output "" output
    regsub -all "\[ \t\]\[ \t\]*" $expected " " expected
    regsub -all "\[ \t\]*\n\n*" $expected "\n" expected
    regsub -all "\[ \t\]\[ \t\]*" $output " " output
    regsub -all "\[ \t\]*\n\n*" $output "\n" output
    verbose "expected is $expected"
    verbose "actual is $output"
    set passed 0;
    if {$options == "regexp_match"} {
	if [regexp $expected $output] {
	    set passed 1;
	}
    } else {
	if { $expected == $output } {
	    set passed 1;
	}
    }
    if { $passed == 1 } {
	pass "$errname output from source compiled test"
    } else {
	clone_output "expected was $expected"
	clone_output "output was $output"
	fail "$errname output from source compiled test"
    }
    close $id;
}

#
# Run the test specified by srcfile and resultfile. compile_args and
# exec_args are additional arguments to be passed in when compiling and
# running the testcase, respectively.
#
proc test_libjava_from_javac { options srcfile compile_args inpfile resultfile exec_args } {
    global base_dir
    global LIBJAVA
    global LIBGC
    global srcdir subdir objdir
    global TOOL_OPTIONS
    global GCJ_UNDER_TEST
    global tmpdir
    global runtests

    set errname [file rootname [file tail $srcfile]]
    if {! [runtest_file_p $runtests $errname]} {
        return
    }

    # bytecompile files with Sun's compiler for now.
    set bc_ok [bytecompile_file $srcfile $objdir]
    if {! $bc_ok} then {
	fail "$errname byte compilation"
	setup_xfail "*-*-*"
	fail "$errname compilation from bytecode"
	if {$exec_args != "no-exec"} {
	    setup_xfail "*-*-*"
	    fail "$errname execution from bytecode->native test"
	    setup_xfail "*-*-*"
	    fail "$errname output from bytecode->native test"
	}
	return
    }
    pass "$errname byte compilation"

    # Find name to use for --main, and name of all class files.
    set jvscan [find_jvscan]
    verbose "jvscan is $jvscan"
    set main_name [string trim \
		     [target_compile $srcfile "" none \
			"compiler=$jvscan additional_flags=--print-main"]]
    verbose "main name is $main_name"
    set class_out [string trim \
		     [target_compile $srcfile "" none \
			"compiler=$jvscan additional_flags=--list-class"]]
    verbose "class list is $class_out"
    # FIXME: As of Wed Feb 24 1999, `jv-scan --list-class' prints
    # nothing if the file contains an interface and not a class.  I
    # believe this is a jv-scan bug.
    if {$class_out == ""} then {
	set class_files [file rootname [file tail $srcfile]].class
    } else {
	# Turn "a b" into "a.class b.class".
	set class_files [join [split [string trim $class_out]] ".class "].class
    }

    # Usually it is an error for a test program not to have a `main'
    # method.  However, for no-exec tests it is ok.
    set largs {}
    if {$main_name == ""} {
	if {$exec_args != "no-exec"} {
	    perror "No `main' given in program $errname"
	    return
	} else {
	    set type object
	    set executable [file rootname [file tail $srcfile]].o
	    set mode compile
	}
    } else {
	set type executable
	lappend largs "additional_flags=--main=$main_name"
	set executable "${objdir}/$main_name"
	set mode link
    }

    # Initial arguments.
    set args [libjava_arguments $mode]
    eval lappend args $largs

    if { $compile_args != "" } {
	lappend args "additional_flags=$compile_args"
    }

    if { $compile_args != "" } {
	set errname "$errname $compile_args"
    }

    verbose "compilation command = $args" 2
    set x [target_compile $class_files "$executable" $type $args]
    if { $x != "" } {
	verbose "target_compile failed: $x" 2
	fail "$errname compilation from bytecode"
	setup_xfail "*-*-*"
	if {$exec_args != "no-exec"} {
	    fail "$errname execution from bytecode->native test"
	    setup_xfail "*-*-*"
	    fail "$errname output from bytecode->native test"
	}
	return;
    }
    pass "$errname compilation from bytecode"

    if { $exec_args == "no-exec" } {
	return
    }

    set result [libjava_load $executable "$exec_args" "$inpfile"];
    set status [lindex $result 0];
    set output [lindex $result 1];
    $status "$errname execution from bytecode->native test"
    if { $status != "pass" } {
	setup_xfail "*-*-*"
	fail "$errname output from bytecode->native test"
	return;
    }

    verbose "resultfile is $resultfile"
    set id [open $resultfile r];
    set expected ""
    append expected [read $id];
    regsub -all "\r" "$output" "" output;
    regsub "\n*$" $expected "" expected
    regsub "\n*$" $output "" output
    regsub "^\n*" $expected "" expected
    regsub "^\n*" $output "" output
    regsub -all "\[ \t\]\[ \t\]*" $expected " " expected
    regsub -all "\[ \t\]*\n\n*" $expected "\n" expected
    regsub -all "\[ \t\]\[ \t\]*" $output " " output
    regsub -all "\[ \t\]*\n\n*" $output "\n" output
    verbose "expected is $expected"
    verbose "actual is $output"
    set passed 0;
    if {$options == "regexp_match"} {
	if [regexp $expected $output] {
	    set passed 1;
	}
    } else {
	if { $expected == $output } {
	    set passed 1;
	}
    }
    if { $passed == 1 } {
	pass "$errname output from bytecode->native test"
    } else {
	clone_output "expected was $expected"
	clone_output "output was $output"
	fail "$errname output from bytecode->native test"
    }
    close $id;
}

#
# Run the test specified by srcfile and resultfile. compile_args and
# exec_args are additional arguments to be passed in when compiling and
# running the testcase, respectively.
#
proc test_libjava { options srcfile compile_args inpfile resultfile exec_args } {
    test_libjava_from_source $options $srcfile $compile_args $inpfile $resultfile $exec_args
    test_libjava_from_javac $options $srcfile $compile_args $inpfile $resultfile $exec_args
 }

#
# libjava_version -- extract and print the version number of libjavap
#
proc default_libjava_version {} {
}

proc default_libjava_start { } {
}