Merge remote-tracking branch 'origin/master' into core-updates
[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 match))
31
32 (define %guile-executable
33 (match (false-if-exception (readlink "/proc/self/exe"))
34 ((? string? program)
35 (and (file-exists? program) (elf-file? program)
36 program))
37 (_
38 #f)))
39
40 (define read-elf
41 (compose parse-elf get-bytevector-all))
42
43 (define c-compiler
44 (or (which "gcc") (which "cc") (which "g++")))
45
46 \f
47 (test-begin "gremlin")
48
49 (unless %guile-executable (test-skip 1))
50 (test-assert "elf-dynamic-info-needed, executable"
51 (let* ((elf (call-with-input-file %guile-executable read-elf))
52 (dyninfo (elf-dynamic-info elf)))
53 (or (not dyninfo) ;static executable
54 (lset<= string=?
55 (list (string-append "libguile-" (effective-version))
56 "libc")
57 (map (lambda (lib)
58 (string-take lib (string-contains lib ".so")))
59 (elf-dynamic-info-needed dyninfo))))))
60
61 (test-equal "expand-origin"
62 '("OOO/../lib"
63 "OOO"
64 "../OOO/bar/OOO/baz"
65 "ORIGIN/foo")
66 (map (cut expand-origin <> "OOO")
67 '("$ORIGIN/../lib"
68 "${ORIGIN}"
69 "../${ORIGIN}/bar/$ORIGIN/baz"
70 "ORIGIN/foo")))
71
72 (unless c-compiler
73 (test-skip 1))
74 (test-equal "strip-runpath"
75 "hello\n"
76 (call-with-temporary-directory
77 (lambda (directory)
78 (with-directory-excursion directory
79 (call-with-output-file "t.c"
80 (lambda (port)
81 (display "int main () { puts(\"hello\"); }" port)))
82 (invoke c-compiler "t.c"
83 "-Wl,--enable-new-dtags" "-Wl,-rpath=/foo" "-Wl,-rpath=/bar")
84 (let* ((dyninfo (elf-dynamic-info
85 (parse-elf (call-with-input-file "a.out"
86 get-bytevector-all))))
87 (old (elf-dynamic-info-runpath dyninfo))
88 (new (strip-runpath "a.out"))
89 (new* (strip-runpath "a.out")))
90 (validate-needed-in-runpath "a.out")
91 (and (member "/foo" old) (member "/bar" old)
92 (not (member "/foo" new))
93 (not (member "/bar" new))
94 (equal? new* new)
95 (let* ((pipe (open-input-pipe "./a.out"))
96 (str (get-string-all pipe)))
97 (close-pipe pipe)
98 str)))))))
99
100 (unless c-compiler
101 (test-skip 1))
102 (test-equal "set-file-runpath + file-runpath"
103 "hello\n"
104 (call-with-temporary-directory
105 (lambda (directory)
106 (with-directory-excursion directory
107 (call-with-output-file "t.c"
108 (lambda (port)
109 (display "int main () { puts(\"hello\"); }" port)))
110
111 (invoke c-compiler "t.c"
112 "-Wl,--enable-new-dtags" "-Wl,-rpath=/xxxxxxxxx")
113
114 (let ((original-runpath (file-runpath "a.out")))
115 (and (member "/xxxxxxxxx" original-runpath)
116 (guard (c ((runpath-too-long-error? c)
117 (string=? "a.out" (runpath-too-long-error-file c))))
118 (set-file-runpath "a.out" (list (make-string 777 #\y))))
119 (let ((runpath (delete "/xxxxxxxxx" original-runpath)))
120 (set-file-runpath "a.out" runpath)
121 (equal? runpath (file-runpath "a.out")))
122 (let* ((pipe (open-input-pipe "./a.out"))
123 (str (get-string-all pipe)))
124 (close-pipe pipe)
125 str)))))))
126
127 (test-end "gremlin")