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