tests: Fix file-needed/recursive on powerpc64le-linux.
[jackhill/guix/guix.git] / tests / gremlin.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2015, 2018, 2020 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2022 Chris Marusich <cmmarusich@gmail.com>
4 ;;;
5 ;;; This file is part of GNU Guix.
6 ;;;
7 ;;; GNU Guix is free software; you can redistribute it and/or modify it
8 ;;; under the terms of the GNU General Public License as published by
9 ;;; the Free Software Foundation; either version 3 of the License, or (at
10 ;;; your option) any later version.
11 ;;;
12 ;;; GNU Guix is distributed in the hope that it will be useful, but
13 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;;; GNU General Public License for more details.
16 ;;;
17 ;;; You should have received a copy of the GNU General Public License
18 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
19
20 (define-module (test-gremlin)
21 #:use-module (guix elf)
22 #:use-module (guix tests)
23 #:use-module ((guix utils) #:select (call-with-temporary-directory))
24 #:use-module (guix build utils)
25 #:use-module (guix build gremlin)
26 #:use-module (srfi srfi-1)
27 #:use-module (srfi srfi-26)
28 #:use-module (srfi srfi-34)
29 #:use-module (srfi srfi-64)
30 #:use-module (rnrs io ports)
31 #:use-module (ice-9 popen)
32 #:use-module (ice-9 rdelim)
33 #:use-module (ice-9 regex)
34 #:use-module (ice-9 match))
35
36 (define %guile-executable
37 (match (false-if-exception (readlink "/proc/self/exe"))
38 ((? string? program)
39 (and (file-exists? program) (elf-file? program)
40 program))
41 (_
42 #f)))
43
44 (define read-elf
45 (compose parse-elf get-bytevector-all))
46
47 (define c-compiler
48 (or (which "gcc") (which "cc") (which "g++")))
49
50 \f
51 (test-begin "gremlin")
52
53 (unless %guile-executable (test-skip 1))
54 (test-assert "elf-dynamic-info-needed, executable"
55 (let* ((elf (call-with-input-file %guile-executable read-elf))
56 (dyninfo (elf-dynamic-info elf)))
57 (or (not dyninfo) ;static executable
58 (lset<= string=?
59 (list (string-append "libguile-" (effective-version))
60 "libc")
61 (map (lambda (lib)
62 (string-take lib (string-contains lib ".so")))
63 (elf-dynamic-info-needed dyninfo))))))
64
65 (unless (and %guile-executable (not (getenv "LD_LIBRARY_PATH"))
66 (file-needed %guile-executable) ;statically linked?
67 ;; When Guix has been built on a foreign distro, using a
68 ;; toolchain and libraries from that foreign distro, it is not
69 ;; unusual for the runpath to be empty.
70 (pair? (file-runpath %guile-executable)))
71 (test-skip 1))
72 (test-assert "file-needed/recursive"
73 (let* ((needed (file-needed/recursive %guile-executable))
74 (pipe (dynamic-wind
75 (lambda ()
76 ;; Tell ld.so to list loaded objects, like 'ldd' does.
77 (setenv "LD_TRACE_LOADED_OBJECTS" "yup"))
78 (lambda ()
79 (open-pipe* OPEN_READ %guile-executable))
80 (lambda ()
81 (unsetenv "LD_TRACE_LOADED_OBJECTS")))))
82 (define ldd-rx
83 (make-regexp "^[[:blank:]]+([[:graph:]]+ => )?([[:graph:]]+) .*$"))
84
85 (define (read-ldd-output port)
86 ;; Read from PORT output in GNU ldd format.
87 (let loop ((result '()))
88 (match (read-line port)
89 ((? eof-object?)
90 (reverse result))
91 ((= (cut regexp-exec ldd-rx <>) m)
92 (if m
93 (loop (cons (match:substring m 2) result))
94 (loop result))))))
95 (define ground-truth
96 (remove (lambda (entry)
97 (or (string-prefix? "linux-vdso.so" entry)
98 (string-prefix? "linux-vdso64.so" entry)))
99 (read-ldd-output pipe)))
100
101 (and (zero? (close-pipe pipe))
102 ;; It's OK if file-needed/recursive returns multiple entries that are
103 ;; different strings referring to the same file. This appears to be a
104 ;; benign edge case. See: https://issues.guix.gnu.org/52940
105 (lset= file=? (pk 'truth ground-truth) (pk 'needed needed)))))
106
107 (test-equal "expand-origin"
108 '("OOO/../lib"
109 "OOO"
110 "../OOO/bar/OOO/baz"
111 "ORIGIN/foo")
112 (map (cut expand-origin <> "OOO")
113 '("$ORIGIN/../lib"
114 "${ORIGIN}"
115 "../${ORIGIN}/bar/$ORIGIN/baz"
116 "ORIGIN/foo")))
117
118 (unless c-compiler
119 (test-skip 1))
120 (test-equal "strip-runpath"
121 "hello\n"
122 (call-with-temporary-directory
123 (lambda (directory)
124 (with-directory-excursion directory
125 (call-with-output-file "t.c"
126 (lambda (port)
127 (display "int main () { puts(\"hello\"); }" port)))
128 (invoke c-compiler "t.c"
129 "-Wl,--enable-new-dtags" "-Wl,-rpath=/foo" "-Wl,-rpath=/bar")
130 (let* ((dyninfo (elf-dynamic-info
131 (parse-elf (call-with-input-file "a.out"
132 get-bytevector-all))))
133 (old (elf-dynamic-info-runpath dyninfo))
134 (new (strip-runpath "a.out"))
135 (new* (strip-runpath "a.out")))
136 (validate-needed-in-runpath "a.out")
137 (and (member "/foo" old) (member "/bar" old)
138 (not (member "/foo" new))
139 (not (member "/bar" new))
140 (equal? new* new)
141 (let* ((pipe (open-input-pipe "./a.out"))
142 (str (get-string-all pipe)))
143 (close-pipe pipe)
144 str)))))))
145
146 (unless c-compiler
147 (test-skip 1))
148 (test-equal "set-file-runpath + file-runpath"
149 "hello\n"
150 (call-with-temporary-directory
151 (lambda (directory)
152 (with-directory-excursion directory
153 (call-with-output-file "t.c"
154 (lambda (port)
155 (display "int main () { puts(\"hello\"); }" port)))
156
157 (invoke c-compiler "t.c"
158 "-Wl,--enable-new-dtags" "-Wl,-rpath=/xxxxxxxxx")
159
160 (let ((original-runpath (file-runpath "a.out")))
161 (and (member "/xxxxxxxxx" original-runpath)
162 (guard (c ((runpath-too-long-error? c)
163 (string=? "a.out" (runpath-too-long-error-file c))))
164 (set-file-runpath "a.out" (list (make-string 777 #\y))))
165 (let ((runpath (delete "/xxxxxxxxx" original-runpath)))
166 (set-file-runpath "a.out" runpath)
167 (equal? runpath (file-runpath "a.out")))
168 (let* ((pipe (open-input-pipe "./a.out"))
169 (str (get-string-all pipe)))
170 (close-pipe pipe)
171 str)))))))
172
173 (unless c-compiler
174 (test-skip 1))
175 (test-equal "elf-dynamic-info-soname"
176 "libfoo.so.2"
177 (call-with-temporary-directory
178 (lambda (directory)
179 (with-directory-excursion directory
180 (call-with-output-file "t.c"
181 (lambda (port)
182 (display "// empty file" port)))
183 (invoke c-compiler "t.c"
184 "-shared" "-Wl,-soname,libfoo.so.2")
185 (let* ((dyninfo (elf-dynamic-info
186 (parse-elf (call-with-input-file "a.out"
187 get-bytevector-all))))
188 (soname (elf-dynamic-info-soname dyninfo)))
189 soname)))))
190
191 (test-end "gremlin")