1# Copyright (C) 2014 Free Software Foundation, Inc.
2#
3# This file is part of the GNU Binutils.
4#
5# This program is free software; you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation; either version 3 of the License, or
8# (at your option) any later version.
9#
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with this program; if not, write to the Free Software
17# Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
18# MA 02110-1301, USA.
19
20# Check that scripts using the "=" sysroot-prefix work, for both
21# toolchains with and without --sysroot support.
22
23# We test this by emitting archives into a subdirectory and expect
24# constructs such as GROUP and AS_NEEDED (the only two constructs
25# actually tested) to find them (or not); both quoted and unquoted
26# paths, with different prefixes on the path and with --sysroot
27# present or not, with different arguments.
28
29# Find out if the linker supports sysroot; if it was configured
30# "--with-sysroot X" where X is a non-empty string.
31set with_sysroot [check_sysroot_available]
32verbose -log "Has (non-empty) sysroot support: $with_sysroot; \"$ld_sysroot\""
33
34# We also need to know if the sysroot is "/" (a common use) as some of
35# the tests prepend sysroot to the current directory and on most
36# common systems "//dir/path" is handled as "/dir/path".
37if {$ld_sysroot == "/"} {
38    # Use a modified test-subset for testing.
39    set with_sysroot 3
40}
41
42# Entries in the array-tables:
43# 0: Testtype; an inclusive bitmask indicating that the test should run on a
44# build configured for: 1: non-sysroot, 2: sysroot != "/", 4: sysroot == "/".
45# 1: Description, forming part of the dejagnu test-name.
46# 2: Replacement for @p@.
47# 3: Option to pass to linker (usually something with --sysroot).
48# 4: Message substring; a substring to match against the error message
49# if an error is expected, or empty if no error is expected.
50#
51# If the replacement or option contains @cwd@, that'll be replaced by
52# "$base_dir/tmpdir", the full path to the location of the script
53# (with the actual files in the "sysroot" subdirectory).  If the
54# description contains @cwd@, that will be replaced by "<CWD>".
55
56set sysroot_prefix_tests {
57    {7 "plain -Lpath" "sysroot/" {} ""}
58    {7 "root-anchored but -Lpath" "/sysroot/" {} "cannot find"}
59    {7 "full-path" "@cwd@/sysroot/" {} ""}
60    {7 "root-anchored =-prefixed -Lpath" "=/sysroot/" {} "cannot find"}
61    {7 "full-path =-prefixed with empty" "=@cwd@/sysroot/" "--sysroot=" ""}
62    {7 "plain =-prefixed with empty" "=sysroot/" "--sysroot=" ""}
63    {6 "root-anchored but script outside sysroot" "/" "--sysroot=@cwd@/sysroot" "cannot find"}
64    {6 "root-anchored and script inside sysroot" "/sysroot/" "--sysroot=@cwd@" ""}
65    {6 "root-anchored =-prefixed script outside" "=/" "--sysroot=@cwd@/sysroot" ""}
66    {6 "root-anchored =-prefixed script inside" "=/sysroot/" "--sysroot=@cwd@" ""}
67    {2 "plain =-prefixed without but -Lpath" "=sysroot/" {} "cannot find"}
68    {2 "full-path =-prefixed without" "=@cwd@/sysroot/" {} "cannot find"}
69    {1 "plain =-prefixed -Lpath" "=sysroot/" {} ""}
70    {1 "full-path =-prefixed without" "=@cwd@/sysroot/" {} ""}
71}
72
73# May have to provide a target-specific assembler option for some targets.
74set gasopt ""
75
76# Intentionally similar to the ubiquitous glibc libc.so script.
77set template "GROUP ( @q@@p@tmp/ldtest-xyzzy/libx.a@q@ AS_NEEDED ( @q@@p@tmp/ldtest-xyzzy/liby.a@q@ ) )"
78
79# Set up everything from the variables above.
80proc sysroot_prefix_test_setup { } {
81    global as gasopt srcdir subdir ar
82
83    if {![ld_assemble_flags $as $gasopt $srcdir/$subdir/pr14962a.s tmpdir/main.o]} {
84	error "Error assembling a trivial file for sysroot-prefix tests�framework"
85	return 0
86    }
87
88    # We need somewhere under tmpdir to point the sysroot, a subdirectory
89    # that is benevolent if it escapes into "/".
90    remote_exec host "mkdir -p tmpdir/sysroot/tmp/ldtest-xyzzy"
91
92    # 0: a "main" object that needs a symbol (x) (most portably by
93    # using a pre-existing file).  1: a library with an object that
94    # provides that symbol and needs another one (y).  2: another
95    # library with a third object providing that other symbol.
96    set sysroot_prefix_tests_framework_objects  {
97	{"pr14962a.s" "main" ""}
98	{"sysroot-prefix-x.s" "x" "x"}
99	{"sysroot-prefix-y.s" "y"  "y"}
100    }
101
102    foreach test_object $sysroot_prefix_tests_framework_objects {
103	set sname [lindex $test_object 0]
104	set onamebase [lindex $test_object 1]
105	set oname "tmpdir/$onamebase.o"
106	set libnamebase [lindex $test_object 2]
107
108	if ![ld_assemble_flags $as $gasopt $srcdir/$subdir/$sname $oname] {
109	    error "Error assembling trivial file $sname for sysroot-prefix tests�framework"
110	    return 0
111	}
112
113	if { [string length $libnamebase] != 0 &&
114	     ![ar_simple_create $ar "" tmpdir/sysroot/tmp/ldtest-xyzzy/lib$libnamebase.a $oname] } {
115	    error "Error creating archive $libnamebase for sysroot-prefix tests�framework"
116	    return 0
117	}
118    }
119
120    return 1
121}
122
123# Run a single linker test.
124proc single_sysroot_prefix_test { type xtestname finalscript ldopt errstr } {
125    global ld exec_output with_sysroot
126    set scriptname "tmpdir/libsysroottest.a"
127    set testname "sysroot-prefix $xtestname"
128
129    if { ($type & ($with_sysroot + 1)) == 0 } {
130	unsupported $testname
131	return
132    }
133
134    if [catch { set ofd [open $scriptname w] } x] {
135	perror "$x"
136	unresolved $testname
137	return
138    }
139
140    puts $ofd "$finalscript"
141    close $ofd
142    verbose -log "script: $finalscript"
143
144    set res [ld_simple_link $ld tmpdir/output "$ldopt tmpdir/main.o -Ltmpdir -lsysroottest"]
145    set ld_output "$exec_output"
146    set expect_success [expr [string length $errstr] == 0]
147
148    if { $res == $expect_success &&
149         ($expect_success || [regexp "$errstr" $ld_output]) } {
150	pass $testname
151	catch "exec rm -f $scriptname"
152    } {
153	fail $testname
154    }
155}
156
157# Run all interesting variants from an option-and-path combination.
158proc run_sysroot_prefix_test { type name templ p ldopt errstr } {
159    global base_dir
160    set qlist { { "quoted" "\"" } { "unquoted" {} } }
161
162    regsub -all "@p@" $templ $p templ
163    regsub -all "@cwd@" $templ "$base_dir/tmpdir" templ
164    regsub -all "@cwd@" $ldopt "$base_dir/tmpdir" ldopt
165    regsub -all "@cwd@" $name "<CWD>" name
166
167    foreach qitems $qlist {
168	regsub -all "@q@" $templ [lindex $qitems 1] finalscript
169	single_sysroot_prefix_test $type "$name, [lindex $qitems 0]" \
170		$finalscript $ldopt $errstr
171    }
172}
173
174# Run a list of option-and-path test-combinations.
175proc run_sysroot_prefix_tests { descr templ items } {
176    foreach item $items {
177	set type [lindex $item 0]
178	set name [lindex $item 1]
179	set p [lindex $item 2]
180	set ldopt [lindex $item 3]
181	set errstr [lindex $item 4]
182	run_sysroot_prefix_test $type "$descr $name" $templ "$p" "$ldopt" "$errstr"
183    }
184}
185
186if ![sysroot_prefix_test_setup] {
187    return
188}
189
190run_sysroot_prefix_tests "common" $template $sysroot_prefix_tests
191