cf434dfa25319ceb4132e2d4ec00352f27b3292e
[binutils-gdb.git] / ld / testsuite / ld-scripts / sysroot-prefix.exp
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". We ignore that X can actually be set to
31 # interfere with the tests, as that's unlikely to be useful, and
32 # assume that the build-directory (aka. $base_dir) does not contain X.
33 set with_sysroot [check_sysroot_available]
34 verbose -log "Has (non-empty) sysroot support: $with_sysroot"
35
36 # Entries in the array-tables:
37 # 0: Testtype (1: only non-sysroot, 2: only sysroot, 3: either).
38 # 1: Description, forming part of the dejagnu test-name.
39 # 2: Replacement for @p@.
40 # 3: Option to pass to linker (usually something with --sysroot).
41 # 4: Message substring; a substring to match against the error message
42 # if an error is expected, or empty if no error is expected.
43 #
44 # If the replacement or option contains @cwd@, that'll be replaced by
45 # "$base_dir/tmpdir", the full path to the location of the script
46 # (with the actual files in the "sysroot" subdirectory). If the
47 # description contains @cwd@, that will be replaced by "<CWD>".
48
49 set sysroot_prefix_tests {
50 {3 "plain -Lpath" "sysroot/" {} ""}
51 {3 "root-anchored but -Lpath" "/sysroot/" {} "cannot find"}
52 {3 "full-path" "@cwd@/sysroot/" {} ""}
53 {3 "root-anchored =-prefixed -Lpath" "=/sysroot/" {} "cannot find"}
54 {3 "full-path =-prefixed with empty" "=@cwd@/sysroot/" "--sysroot=" ""}
55 {3 "plain =-prefixed with empty" "=sysroot/" "--sysroot=" ""}
56 {2 "root-anchored but script outside sysroot" "/" "--sysroot=@cwd@/sysroot" "cannot find"}
57 {2 "root-anchored and script inside sysroot" "/sysroot/" "--sysroot=@cwd@" ""}
58 {2 "root-anchored =-prefixed script outside" "=/" "--sysroot=@cwd@/sysroot" ""}
59 {2 "root-anchored =-prefixed script inside" "=/sysroot/" "--sysroot=@cwd@" ""}
60 {2 "plain =-prefixed without but -Lpath" "=sysroot/" {} "cannot find"}
61 {2 "full-path =-prefixed without" "=@cwd@/sysroot/" {} "cannot find"}
62 {1 "plain =-prefixed -Lpath" "=sysroot/" {} ""}
63 {1 "full-path =-prefixed without" "=@cwd@/sysroot/" {} ""}
64 }
65
66 # May have to provide a target-specific assembler option for some targets.
67 set gasopt ""
68
69 # Intentionally similar to the ubiquitous glibc libc.so script.
70 set template "GROUP ( @q@@p@tmp/ldtest-xyzzy/libx.a@q@ AS_NEEDED ( @q@@p@tmp/ldtest-xyzzy/liby.a@q@ ) )"
71
72 # Set up everything from the variables above.
73 proc sysroot_prefix_test_setup { } {
74 global as gasopt srcdir subdir ar
75
76 if {![ld_assemble_flags $as $gasopt $srcdir/$subdir/pr14962a.s tmpdir/main.o]} {
77 error "Error assembling a trivial file for sysroot-prefix tests framework"
78 return 0
79 }
80
81 # We need somewhere under tmpdir to point the sysroot, a subdirectory
82 # that is benevolent if it escapes into "/".
83 remote_exec host "mkdir -p tmpdir/sysroot/tmp/ldtest-xyzzy"
84
85 # 0: a "main" object that needs a symbol (x) (most portably by
86 # using a pre-existing file). 1: a library with an object that
87 # provides that symbol and needs another one (y). 2: another
88 # library with a third object providing that other symbol.
89 set sysroot_prefix_tests_framework_objects {
90 {"pr14962a.s" "main" ""}
91 {"sysroot-prefix-x.s" "x" "x"}
92 {"sysroot-prefix-y.s" "y" "y"}
93 }
94
95 foreach test_object $sysroot_prefix_tests_framework_objects {
96 set sname [lindex $test_object 0]
97 set onamebase [lindex $test_object 1]
98 set oname "tmpdir/$onamebase.o"
99 set libnamebase [lindex $test_object 2]
100
101 if ![ld_assemble_flags $as $gasopt $srcdir/$subdir/$sname $oname] {
102 error "Error assembling trivial file $sname for sysroot-prefix tests framework"
103 return 0
104 }
105
106 if { [string length $libnamebase] != 0 &&
107 ![ar_simple_create $ar "" tmpdir/sysroot/tmp/ldtest-xyzzy/lib$libnamebase.a $oname] } {
108 error "Error creating archive $libnamebase for sysroot-prefix tests framework"
109 return 0
110 }
111 }
112
113 return 1
114 }
115
116 # Run a single linker test.
117 proc single_sysroot_prefix_test { type xtestname finalscript ldopt errstr } {
118 global ld exec_output with_sysroot
119 set scriptname "tmpdir/libsysroottest.a"
120 set testname "sysroot-prefix $xtestname"
121
122 if { ($type & ($with_sysroot + 1)) == 0 } {
123 unsupported $testname
124 return
125 }
126
127 if [catch { set ofd [open $scriptname w] } x] {
128 perror "$x"
129 unresolved $testname
130 return
131 }
132
133 puts $ofd "$finalscript"
134 close $ofd
135 verbose -log "script: $finalscript"
136
137 set res [ld_simple_link $ld tmpdir/output "$ldopt tmpdir/main.o -Ltmpdir -lsysroottest"]
138 set ld_output "$exec_output"
139 set expect_success [expr [string length $errstr] == 0]
140
141 if { $res == $expect_success &&
142 ($expect_success || [regexp "$errstr" $ld_output]) } {
143 pass $testname
144 catch "exec rm -f $scriptname"
145 } {
146 fail $testname
147 }
148 }
149
150 # Run all interesting variants from an option-and-path combination.
151 proc run_sysroot_prefix_test { type name templ p ldopt errstr } {
152 global base_dir
153 set qlist { { "quoted" "\"" } { "unquoted" {} } }
154
155 regsub -all "@p@" $templ $p templ
156 regsub -all "@cwd@" $templ "$base_dir/tmpdir" templ
157 regsub -all "@cwd@" $ldopt "$base_dir/tmpdir" ldopt
158 regsub -all "@cwd@" $name "<CWD>" name
159
160 foreach qitems $qlist {
161 regsub -all "@q@" $templ [lindex $qitems 1] finalscript
162 single_sysroot_prefix_test $type "$name, [lindex $qitems 0]" \
163 $finalscript $ldopt $errstr
164 }
165 }
166
167 # Run a list of option-and-path test-combinations.
168 proc run_sysroot_prefix_tests { descr templ items } {
169 foreach item $items {
170 set type [lindex $item 0]
171 set name [lindex $item 1]
172 set p [lindex $item 2]
173 set ldopt [lindex $item 3]
174 set errstr [lindex $item 4]
175 run_sysroot_prefix_test $type "$descr $name" $templ "$p" "$ldopt" "$errstr"
176 }
177 }
178
179 if ![sysroot_prefix_test_setup] {
180 return
181 }
182
183 run_sysroot_prefix_tests "common" $template $sysroot_prefix_tests