gnu: texlive-latex-amsmath: Declare a source file-name.
[jackhill/guix/guix.git] / build-aux / build-self.scm
index cc70249..4c85c09 100644 (file)
@@ -1,5 +1,5 @@
 ;;; GNU Guix --- Functional package management for GNU
-;;; Copyright © 2014, 2016 Ludovic Courtès <ludo@gnu.org>
+;;; Copyright © 2014, 2016, 2017 Ludovic Courtès <ludo@gnu.org>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -22,6 +22,7 @@
   #:use-module (guix config)
   #:use-module (srfi srfi-1)
   #:use-module (srfi srfi-19)
+  #:use-module (ice-9 match)
   #:export (build))
 
 ;;; Commentary:
@@ -43,6 +44,9 @@
 ;; could be renamed or shuffled around in modules over time.  Conversely,
 ;; 'find-best-packages-by-name' is expected to always have the same semantics.
 
+(define guix
+  (first (find-best-packages-by-name "guix" #f)))
+
 (define libgcrypt
   (first (find-best-packages-by-name "libgcrypt" #f)))
 
 (define xz
   (first (find-best-packages-by-name "xz" #f)))
 
+(define (false-if-wrong-guile package)
+  "Return #f if PACKAGE depends on the \"wrong\" major version of Guile (e.g.,
+2.0 instead of 2.2), otherwise return PACKAGE."
+  (let ((guile (any (match-lambda
+                      ((label (? package? dep) _ ...)
+                       (and (string=? (package-name dep) "guile")
+                            dep)))
+                    (package-direct-inputs package))))
+    (and (or (not guile)
+             (string-prefix? (effective-version)
+                             (package-version guile)))
+         package)))
+
+(define (package-for-current-guile . names)
+  "Return the package with one of the given NAMES that depends on the current
+Guile major version (2.0 or 2.2), or #f if none of the packages matches."
+  (let loop ((names names))
+    (match names
+      (()
+       #f)
+      ((name rest ...)
+       (match (find-best-packages-by-name name #f)
+         (()
+          (loop rest))
+         ((first _ ...)
+          (or (false-if-wrong-guile first)
+              (loop rest))))))))
+
 (define guile-json
-  (first (find-best-packages-by-name "guile-json" #f)))
+  (package-for-current-guile "guile-json"
+                             "guile2.2-json"
+                             "guile2.0-json"))
 
 (define guile-ssh
-  (first (find-best-packages-by-name "guile-ssh" #f)))
+  (package-for-current-guile "guile-ssh"
+                             "guile2.2-ssh"
+                             "guile2.0-ssh"))
 
+(define guile-git
+  (package-for-current-guile "guile-git"
+                             "guile2.0-git"))
+
+(define guile-bytestructures
+  (package-for-current-guile "guile-bytestructures"
+                             "guile2.0-bytestructures"))
 \f
 ;; The actual build procedure.
 
@@ -80,6 +123,34 @@ person's version identifier."
   ;; XXX: Replace with a Git commit id.
   (date->string (current-date 0) "~Y~m~d.~H"))
 
+(define (matching-guile-2.2)
+  "Return a Guile 2.2 with the same version as the current one or immediately
+older than then current one.  This is so that we do not build ABI-incompatible
+objects.  See <https://bugs.gnu.org/29570>."
+  (let loop ((packages (find-packages-by-name "guile" "2.2"))
+             (best     #f))
+    (match packages
+      (()
+       best)
+      ((head tail ...)
+       (if (string=? (package-version head) (version))
+           head
+           (if best
+               (if (version>? (package-version head) (version))
+                   (loop tail best)
+                   (loop tail head))
+               (loop tail head)))))))
+
+(define (guile-for-build)
+  "Return a derivation for Guile 2.0 or 2.2, whichever matches the currently
+running Guile."
+  (package->derivation (cond-expand
+                         (guile-2.2
+                          (canonical-package (matching-guile-2.2)))
+                         (else
+                          (canonical-package
+                           (specification->package "guile@2.0"))))))
+
 ;; The procedure below is our return value.
 (define* (build source
                 #:key verbose? (version (date-version-string))
@@ -97,22 +168,47 @@ files."
     (if (defined? '%localstatedir) %localstatedir (dirname %state-directory)))
   (define sysconfdir
     (if (defined? '%sysconfdir) %sysconfdir (dirname %config-directory)))
-  (define sbindir
-    (if (defined? '%sbindir) %sbindir (dirname %guix-register-program)))
 
   (define builder
     #~(begin
         (use-modules (guix build pull))
 
-        (let ((json (string-append #$guile-json "/share/guile/site/2.0")))
+        (letrec-syntax ((maybe-load-path
+                         (syntax-rules ()
+                           ((_ item rest ...)
+                            (let ((tail (maybe-load-path rest ...)))
+                              (if (string? item)
+                                  (cons (string-append item
+                                                       "/share/guile/site/"
+                                                       #$(effective-version))
+                                        tail)
+                                  tail)))
+                           ((_)
+                            '()))))
           (set! %load-path
-                (cons* json
-                       (string-append #$guile-ssh "/share/guile/site/2.0")
-                       %load-path))
+                (append
+                 (maybe-load-path #$guile-json #$guile-ssh
+                                  #$guile-git #$guile-bytestructures)
+                 %load-path)))
+
+        (letrec-syntax ((maybe-load-compiled-path
+                         (syntax-rules ()
+                           ((_ item rest ...)
+                            (let ((tail (maybe-load-compiled-path rest ...)))
+                              (if (string? item)
+                                  (cons (string-append item
+                                                       "/lib/guile/"
+                                                       #$(effective-version)
+                                                       "/site-ccache")
+                                        tail)
+                                  tail)))
+                           ((_)
+                            '()))))
           (set! %load-compiled-path
-                (cons* json
-                       (string-append #$guile-ssh "/lib/guile/2.0/site-ccache")
-                       %load-compiled-path)))
+                (append
+                 (maybe-load-compiled-path #$guile-json #$guile-ssh
+                                           #$guile-git #$guile-bytestructures)
+                 %load-compiled-path)))
 
         ;; XXX: The 'guile-ssh' package prior to Guix commit 92b7258 was
         ;; broken: libguile-ssh could not be found.  Work around that.
@@ -127,7 +223,7 @@ files."
                     #:storedir #$storedir
                     #:localstatedir #$localstatedir
                     #:sysconfdir #$sysconfdir
-                    #:sbindir #$sbindir
+                    #:sbindir (string-append #$guix "/sbin")
 
                     #:package-name #$%guix-package-name
                     #:package-version #$version
@@ -146,13 +242,40 @@ files."
                                      (current-error-port)
                                      (%make-void-port "w")))))
 
-  (gexp->derivation "guix-latest" builder
-                    #:modules '((guix build pull)
-                                (guix build utils))
+  (unless guile-git
+    ;; XXX: Guix before February 2017 lacks a 'guile-git' package altogether.
+    ;; If we try to upgrade anyway, the logic in (guix scripts pull) will not
+    ;; build (guix git), which will leave us with an unusable 'guix pull'.  To
+    ;; avoid that, fail early.
+    (format (current-error-port)
+            "\
+Your installation is too old and lacks a '~a' package.
+Please upgrade to an intermediate version first, for instance with:
+
+  guix pull --url=https://git.savannah.gnu.org/cgit/guix.git/snapshot/v0.13.0.tar.gz
+\n"
+            (match (effective-version)
+              ("2.0" "guile2.0-git")
+              (_     "guile-git")))
+    (exit 1))
+
+  (mlet %store-monad ((guile (guile-for-build)))
+    (gexp->derivation "guix-latest" builder
+                      #:modules '((guix build pull)
+                                  (guix build utils)
+                                  (guix build compile)
+
+                                  ;; Closure of (guix modules).
+                                  (guix modules)
+                                  (guix memoization)
+                                  (guix profiling)
+                                  (guix sets))
+
+                      ;; Arrange so that our own (guix build …) modules are
+                      ;; used.
+                      #:module-path (list (top-source-directory))
 
-                    ;; Arrange so that our own (guix build …) modules are
-                    ;; used.
-                    #:module-path (list (top-source-directory))))
+                      #:guile-for-build guile)))
 
 ;; This file is loaded by 'guix pull'; return it the build procedure.
 build