gnu: par2cmdline: Update to 0.7.1.
[guix.git] / build-aux / build-self.scm
bloba1335fea1d1a26986bab42b269b5b5e0e9c7ad6a
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2014, 2016, 2017 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/>.
19 (define-module (build-self)
20   #:use-module (gnu)
21   #:use-module (guix)
22   #:use-module (guix config)
23   #:use-module (srfi srfi-1)
24   #:use-module (srfi srfi-19)
25   #:use-module (ice-9 match)
26   #:export (build))
28 ;;; Commentary:
29 ;;;
30 ;;; When loaded, this module returns a monadic procedure of at least one
31 ;;; argument: the source tree to build.  It returns a derivation that
32 ;;; builds it.
33 ;;;
34 ;;; This file uses modules provided by the already-installed Guix.  Those
35 ;;; modules may be arbitrarily old compared to the version we want to
36 ;;; build.  Because of that, it must rely on the smallest set of features
37 ;;; that are likely to be provided by the (guix) and (gnu) modules, and by
38 ;;; Guile itself, forever and ever.
39 ;;;
40 ;;; Code:
43 ;; The dependencies.  Don't refer explicitly to the variables because they
44 ;; could be renamed or shuffled around in modules over time.  Conversely,
45 ;; 'find-best-packages-by-name' is expected to always have the same semantics.
47 (define libgcrypt
48   (first (find-best-packages-by-name "libgcrypt" #f)))
50 (define zlib
51   (first (find-best-packages-by-name "zlib" #f)))
53 (define gzip
54   (first (find-best-packages-by-name "gzip" #f)))
56 (define bzip2
57   (first (find-best-packages-by-name "bzip2" #f)))
59 (define xz
60   (first (find-best-packages-by-name "xz" #f)))
62 (define (false-if-wrong-guile package)
63   "Return #f if PACKAGE depends on the \"wrong\" major version of Guile (e.g.,
64 2.0 instead of 2.2), otherwise return PACKAGE."
65   (let ((guile (any (match-lambda
66                       ((label (? package? dep) _ ...)
67                        (and (string=? (package-name dep) "guile")
68                             dep)))
69                     (package-direct-inputs package))))
70     (and (or (not guile)
71              (string-prefix? (effective-version)
72                              (package-version guile)))
73          package)))
75 (define (package-for-current-guile . names)
76   "Return the package with one of the given NAMES that depends on the current
77 Guile major version (2.0 or 2.2), or #f if none of the packages matches."
78   (let loop ((names names))
79     (match names
80       (()
81        #f)
82       ((name rest ...)
83        (match (find-best-packages-by-name name #f)
84          (()
85           (loop rest))
86          ((first _ ...)
87           (or (false-if-wrong-guile first)
88               (loop rest))))))))
90 (define guile-json
91   (package-for-current-guile "guile-json"
92                              "guile2.2-json"
93                              "guile2.0-json"))
95 (define guile-ssh
96   (package-for-current-guile "guile-ssh"
97                              "guile2.2-ssh"
98                              "guile2.0-ssh"))
101 ;; The actual build procedure.
103 (define (top-source-directory)
104   "Return the name of the top-level directory of this source tree."
105   (and=> (assoc-ref (current-source-location) 'filename)
106          (lambda (file)
107            (string-append (dirname file) "/.."))))
110 (define (date-version-string)
111   "Return the current date and hour in UTC timezone, for use as a poor
112 person's version identifier."
113   ;; XXX: Replace with a Git commit id.
114   (date->string (current-date 0) "~Y~m~d.~H"))
116 (define (guile-for-build)
117   "Return a derivation for Guile 2.0 or 2.2, whichever matches the currently
118 running Guile."
119   (package->derivation (cond-expand
120                          (guile-2.2
121                           (canonical-package
122                            (specification->package "guile@2.2")))
123                          (else
124                           (canonical-package
125                            (specification->package "guile@2.0"))))))
127 ;; The procedure below is our return value.
128 (define* (build source
129                 #:key verbose? (version (date-version-string))
130                 #:allow-other-keys
131                 #:rest rest)
132   "Return a derivation that unpacks SOURCE into STORE and compiles Scheme
133 files."
134   ;; The '%xxxdir' variables were added to (guix config) in July 2016 so we
135   ;; cannot assume that they are defined.  Try to guess their value when
136   ;; they're undefined (XXX: we get an incorrect guess when environment
137   ;; variables such as 'NIX_STATE_DIR' are defined!).
138   (define storedir
139     (if (defined? '%storedir) %storedir %store-directory))
140   (define localstatedir
141     (if (defined? '%localstatedir) %localstatedir (dirname %state-directory)))
142   (define sysconfdir
143     (if (defined? '%sysconfdir) %sysconfdir (dirname %config-directory)))
144   (define sbindir
145     (if (defined? '%sbindir) %sbindir (dirname %guix-register-program)))
147   (define builder
148     #~(begin
149         (use-modules (guix build pull))
151         (let ((json (string-append #$guile-json "/share/guile/site/"
152                                    #$(effective-version))))
153           (set! %load-path
154             (cons* json
155                    (string-append #$guile-ssh "/share/guile/site/"
156                                   #$(effective-version))
157                    %load-path))
158           (set! %load-compiled-path
159             (cons* json
160                    (string-append #$guile-ssh "/lib/guile/"
161                                   #$(effective-version)
162                                   "/site-ccache")
163                    %load-compiled-path)))
165         ;; XXX: The 'guile-ssh' package prior to Guix commit 92b7258 was
166         ;; broken: libguile-ssh could not be found.  Work around that.
167         ;; FIXME: We want Guile-SSH 0.10.2 or later anyway.
168         #$(if (string-prefix? "0.9." (package-version guile-ssh))
169               #~(setenv "LTDL_LIBRARY_PATH" (string-append #$guile-ssh "/lib"))
170               #t)
172         (build-guix #$output #$source
174                     #:system #$%system
175                     #:storedir #$storedir
176                     #:localstatedir #$localstatedir
177                     #:sysconfdir #$sysconfdir
178                     #:sbindir #$sbindir
180                     #:package-name #$%guix-package-name
181                     #:package-version #$version
182                     #:bug-report-address #$%guix-bug-report-address
183                     #:home-page-url #$%guix-home-page-url
185                     #:libgcrypt #$libgcrypt
186                     #:zlib #$zlib
187                     #:gzip #$gzip
188                     #:bzip2 #$bzip2
189                     #:xz #$xz
191                     ;; XXX: This is not perfect, enabling VERBOSE? means
192                     ;; building a different derivation.
193                     #:debug-port (if #$verbose?
194                                      (current-error-port)
195                                      (%make-void-port "w")))))
197   (mlet %store-monad ((guile (guile-for-build)))
198     (gexp->derivation "guix-latest" builder
199                       #:modules '((guix build pull)
200                                   (guix build utils)
202                                   ;; Closure of (guix modules).
203                                   (guix modules)
204                                   (guix memoization)
205                                   (guix sets))
207                       ;; Arrange so that our own (guix build …) modules are
208                       ;; used.
209                       #:module-path (list (top-source-directory))
211                       #:guile-for-build guile)))
213 ;; This file is loaded by 'guix pull'; return it the build procedure.
214 build
216 ;; Local Variables:
217 ;; eval: (put 'with-load-path 'scheme-indent-function 1)
218 ;; End:
220 ;;; build-self.scm ends here