summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile.am4
-rw-r--r--guix/build/union.scm116
-rw-r--r--tests/union.scm103
3 files changed, 222 insertions, 1 deletions
diff --git a/Makefile.am b/Makefile.am
index 10f3b617021..75e479ddc49 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -34,6 +34,7 @@ MODULES = \
34 guix/build/ftp.scm \ 34 guix/build/ftp.scm \
35 guix/build/http.scm \ 35 guix/build/http.scm \
36 guix/build/utils.scm \ 36 guix/build/utils.scm \
37 guix/build/union.scm \
37 guix/packages.scm \ 38 guix/packages.scm \
38 guix.scm \ 39 guix.scm \
39 distro.scm \ 40 distro.scm \
@@ -116,7 +117,8 @@ TESTS = \
116 tests/derivations.scm \ 117 tests/derivations.scm \
117 tests/utils.scm \ 118 tests/utils.scm \
118 tests/build-utils.scm \ 119 tests/build-utils.scm \
119 tests/packages.scm 120 tests/packages.scm \
121 tests/union.scm
120 122
121LOG_COMPILER = \ 123LOG_COMPILER = \
122 $(top_builddir)/pre-inst-env \ 124 $(top_builddir)/pre-inst-env \
diff --git a/guix/build/union.scm b/guix/build/union.scm
new file mode 100644
index 00000000000..ffd367917ae
--- /dev/null
+++ b/guix/build/union.scm
@@ -0,0 +1,116 @@
1;;; Guix --- Nix package management from Guile. -*- coding: utf-8 -*-
2;;; Copyright (C) 2012 Ludovic Courtès <ludo@gnu.org>
3;;;
4;;; This file is part of Guix.
5;;;
6;;; 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;;; 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 Guix. If not, see <http://www.gnu.org/licenses/>.
18
19(define-module (guix build union)
20 #:use-module (ice-9 ftw)
21 #:use-module (ice-9 match)
22 #:use-module (srfi srfi-1)
23 #:use-module (srfi srfi-26)
24 #:export (tree-union
25 union-build))
26
27;;; Commentary:
28;;;
29;;; Build a directory that is the union of a set of directories, using
30;;; symbolic links.
31;;;
32;;; Code:
33
34(define (tree-union trees)
35 "Return a tree that is the union of the trees listed in TREES. Each
36tree has the form (PARENT LEAVES ...) or just LEAF, where each leaf is
37itself a tree. "
38 (let loop ((trees trees))
39 (match trees
40 (() ; nothing left
41 '())
42 (_
43 (let ((dirs (filter pair? trees))
44 (leaves (remove pair? trees)))
45 `(,@leaves
46 ,@(fold (lambda (dir result)
47 (cons `(,dir
48 ,@(loop
49 (concatenate
50 (filter-map (match-lambda
51 ((head children ...)
52 (and (equal? head dir)
53 children)))
54 dirs))))
55 result))
56 '()
57 (delete-duplicates (map car dirs)))))))))
58
59(define* (union-build output directories)
60 "Build in the OUTPUT directory a symlink tree that is the union of all
61the DIRECTORIES."
62 (define (file-tree dir)
63 ;; Return the contents of DIR as a tree.
64 (match (file-system-fold (const #t)
65 (lambda (file stat result) ; leaf
66 (match result
67 (((siblings ...) rest ...)
68 `((,file ,@siblings) ,@rest))))
69 (lambda (dir stat result) ; down
70 `(() ,@result))
71 (lambda (dir stat result) ; up
72 (match result
73 (((leaves ...) (siblings ...) rest ...)
74 `(((,(basename dir) ,@leaves) ,@siblings)
75 ,@rest))))
76 (const #f) ; skip
77 (lambda (file stat errno result)
78 (format (current-error-port) "union-build: ~a: ~a~%"
79 file (strerror errno)))
80 '(())
81 dir)
82 (((tree)) tree)
83 (() #f)))
84
85 (define tree-leaves
86 ;; Return the leaves of the given tree.
87 (match-lambda
88 (((? string?) leaves ...)
89 leaves)))
90
91 (setvbuf (current-output-port) _IOLBF)
92 (setvbuf (current-error-port) _IOLBF)
93
94 (mkdir output)
95 (let loop ((tree (tree-union (append-map (compose tree-leaves file-tree)
96 directories)))
97 (dir '()))
98 (match tree
99 ((? string?)
100 ;; A leaf: create a symlink.
101 (let* ((dir (string-join dir "/"))
102 (target (string-append output "/" dir "/" (basename tree))))
103 (format (current-error-port) "`~a' ~~> `~a'~%"
104 tree target)
105 (symlink tree target)))
106 (((? string? subdir) leaves ...)
107 ;; A sub-directory: create it in OUTPUT, and iterate over LEAVES.
108 (let ((dir (string-join dir "/")))
109 (mkdir (string-append output "/" dir "/" subdir)))
110 (for-each (cute loop <> `(,@dir ,subdir))
111 leaves))
112 ((leaves ...)
113 ;; A series of leaves: iterate over them.
114 (for-each (cut loop <> dir) leaves)))))
115
116;;; union.scm ends here
diff --git a/tests/union.scm b/tests/union.scm
new file mode 100644
index 00000000000..f8a58d59527
--- /dev/null
+++ b/tests/union.scm
@@ -0,0 +1,103 @@
1;;; Guix --- Nix package management from Guile. -*- coding: utf-8 -*-
2;;; Copyright (C) 2012 Ludovic Courtès <ludo@gnu.org>
3;;;
4;;; This file is part of Guix.
5;;;
6;;; 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;;; 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 Guix. If not, see <http://www.gnu.org/licenses/>.
18
19
20(define-module (test-union)
21 #:use-module (guix store)
22 #:use-module (guix utils)
23 #:use-module (guix derivations)
24 #:use-module (guix packages)
25 #:use-module (guix build union)
26 #:use-module ((guix build utils)
27 #:select (with-directory-excursion directory-exists?))
28 #:use-module (srfi srfi-64)
29 #:use-module (ice-9 match))
30
31;; Exercise the (guix build union) module.
32
33(define %store
34 (false-if-exception (open-connection)))
35
36(define %bootstrap-guile
37 (@@ (distro packages base) %bootstrap-guile))
38
39(when %store
40 ;; By default, use %BOOTSTRAP-GUILE for the current system.
41 (let ((drv (package-derivation %store %bootstrap-guile)))
42 (%guile-for-build drv)))
43
44
45(test-begin "union")
46
47(test-equal "tree-union, empty"
48 '()
49 (tree-union '()))
50
51(test-equal "tree-union, leaves only"
52 '(a b c d)
53 (tree-union '(a b c d)))
54
55(test-equal "tree-union, simple"
56 '((bin ls touch make awk gawk))
57 (tree-union '((bin ls touch)
58 (bin make)
59 (bin awk gawk))))
60
61(test-equal "tree-union, several levels"
62 '((share (doc (make README) (coreutils README)))
63 (bin ls touch make))
64 (tree-union '((bin ls touch)
65 (share (doc (coreutils README)))
66 (bin make)
67 (share (doc (make README))))))
68
69(test-skip (if %store 0 1))
70
71(test-assert "union-build"
72 (let* ((inputs (map (match-lambda
73 ((name package)
74 `(,name ,(package-derivation %store package))))
75 (@@ (distro packages base) %bootstrap-inputs)))
76 (builder `(begin
77 (use-modules (guix build union))
78 (union-build (assoc-ref %outputs "out")
79 (map cdr %build-inputs))))
80 (drv
81 (build-expression->derivation %store "union-test"
82 (%current-system)
83 builder inputs
84 #:modules '((guix build union)))))
85 (and (build-derivations %store (list (pk 'drv drv)))
86 (with-directory-excursion (derivation-path->output-path drv)
87 (and (file-exists? "bin/touch")
88 (file-exists? "bin/gcc")
89 (file-exists? "bin/ld")
90 (file-exists? "lib/libc.so")
91 (directory-exists? "lib/gcc")
92 (file-exists? "include/unistd.h"))))))
93
94(test-end)
95
96
97(exit (= (test-runner-fail-count (test-runner-current)) 0))
98
99;;; Local Variables:
100;;; eval: (put 'test-assert 'scheme-indent-function 1)
101;;; eval: (put 'test-equal 'scheme-indent-function 1)
102;;; eval: (put 'call-with-input-string 'scheme-indent-function 1)
103;;; End: