gnu: cross-base: Move phases to (gnu build cross-toolchain).
[jackhill/guix/guix.git] / gnu / packages / cross-base.scm
index f947e7a..763bbf5 100644 (file)
@@ -1,6 +1,8 @@
 ;;; GNU Guix --- Functional package management for GNU
-;;; Copyright © 2013, 2014, 2015 Ludovic Courtès <ludo@gnu.org>
+;;; Copyright © 2013, 2014, 2015, 2016 Ludovic Courtès <ludo@gnu.org>
 ;;; Copyright © 2014, 2015 Mark H Weaver <mhw@netris.org>
+;;; Copyright © 2016 Jan Nieuwenhuizen <janneke@gnu.org>
+;;; Copyright © 2016 Manolis Fragkiskos Ragkousis <manolis837@gmail.com>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
 ;;; along with GNU Guix.  If not, see <http://www.gnu.org/licenses/>.
 
 (define-module (gnu packages cross-base)
-  #:use-module (guix licenses)
   #:use-module (gnu packages)
   #:use-module (gnu packages gcc)
   #:use-module (gnu packages base)
-  #:use-module (gnu packages commencement)
   #:use-module (gnu packages linux)
+  #:use-module (gnu packages hurd)
+  #:use-module (gnu packages mingw)
   #:use-module (guix packages)
   #:use-module (guix download)
   #:use-module (guix utils)
   #:use-module (srfi srfi-1)
   #:use-module (srfi srfi-26)
   #:use-module (ice-9 match)
+  #:use-module (ice-9 regex)
   #:export (cross-binutils
             cross-libc
-            cross-gcc))
+            cross-gcc
+            cross-newlib?))
 
 (define %xgcc
   ;; GCC package used as the basis for cross-compilation.  It doesn't have to
   ;; be 'gcc' and can be a specific variant such as 'gcc-4.8'.
   gcc)
 
+(define %gcc-include-paths
+  ;; Environment variables for header search paths.
+  ;; Note: See <http://bugs.gnu.org/22186> for why not 'CPATH'.
+  '("C_INCLUDE_PATH"
+    "CPLUS_INCLUDE_PATH"
+    "OBJC_INCLUDE_PATH"
+    "OBJCPLUS_INCLUDE_PATH"))
+
+(define %gcc-cross-include-paths
+  ;; Search path for target headers when cross-compiling.
+  (map (cut string-append "CROSS_" <>) %gcc-include-paths))
+
 (define (cross p target)
   (package (inherit p)
     (name (string-append (package-name p) "-cross-" target))
@@ -101,7 +117,7 @@ may be either a libc package or #f.)"
         `(append (list ,(string-append "--target=" target)
                        ,@(if libc
                              `( ;; Disable libcilkrts because it is not
-                                ;; ported to GNU/Hurd. 
+                                ;; ported to GNU/Hurd.
                                "--disable-libcilkrts")
                              `( ;; Disable features not needed at this stage.
                                "--disable-shared" "--enable-static"
@@ -120,7 +136,20 @@ may be either a libc package or #f.)"
                                "--disable-libquadmath"
                                "--disable-decimal-float" ;would need libc
                                "--disable-libcilkrts"
-                               )))
+
+                               ;; When target is any OS other than 'none' these
+                               ;; libraries will fail if there is no libc
+                               ;; present. See
+                               ;; <https://lists.gnu.org/archive/html/guix-devel/2016-02/msg01311.html>
+                               "--disable-libitm"
+                               "--disable-libvtv"
+                               "--disable-libsanitizer"
+                                ))
+
+                       ;; For a newlib (non-glibc) target
+                       ,@(if (cross-newlib? target)
+                             '("--with-newlib")
+                             '()))
 
                  ,(if libc
                       flags
@@ -135,73 +164,24 @@ may be either a libc package or #f.)"
                      ,flags))
             flags))
        ((#:phases phases)
-        (let ((phases
-               `(alist-cons-after
-                 'install 'make-cross-binutils-visible
-                 (lambda* (#:key outputs inputs #:allow-other-keys)
-                   (let* ((out      (assoc-ref outputs "out"))
-                          (libexec  (string-append out "/libexec/gcc/"
-                                                   ,target))
-                          (binutils (string-append
-                                     (assoc-ref inputs "binutils-cross")
-                                     "/bin/" ,target "-"))
-                          (wrapper  (string-append
-                                     (assoc-ref inputs "ld-wrapper-cross")
-                                     "/bin/" ,target "-ld")))
-                     (for-each (lambda (file)
-                                 (symlink (string-append binutils file)
-                                          (string-append libexec "/"
-                                                         file)))
-                               '("as" "nm"))
-                     (symlink wrapper (string-append libexec "/ld"))
-                     #t))
-                 (alist-replace
-                  'install
-                  (lambda _
-                    ;; Unlike our 'strip' phase, this will do the right thing
-                    ;; for cross-compilers.
-                    (zero? (system* "make" "install-strip")))
-                  ,phases))))
-          (if libc
-              `(alist-cons-before
-                'configure 'set-cross-path
-                (lambda* (#:key inputs #:allow-other-keys)
-                  ;; Add the cross Linux headers to CROSS_CPATH, and remove them
-                  ;; from CPATH.
-                  (let ((libc  (assoc-ref inputs "libc"))
-                        (linux (assoc-ref inputs "xlinux-headers")))
-                    (define (cross? x)
-                      ;; Return #t if X is a cross-libc or cross Linux.
-                      (or (string-prefix? libc x)
-                          (string-prefix? linux x)))
-
-                    (setenv "CROSS_CPATH"
-                            (string-append libc "/include:"
-                                           linux "/include"))
-                    (setenv "CROSS_LIBRARY_PATH"
-                            (string-append libc "/lib"))
-
-                    (let ((cpath   (search-path-as-string->list
-                                    (getenv "CPATH")))
-                          (libpath (search-path-as-string->list
-                                    (getenv "LIBRARY_PATH"))))
-                      (setenv "CPATH"
-                              (list->search-path-as-string
-                               (remove cross? cpath) ":"))
-                      (setenv "LIBRARY_PATH"
-                              (list->search-path-as-string
-                               (remove cross? libpath) ":"))
-                      #t)))
-                ,phases)
-              phases)))))))
+        `(cross-gcc-build-phases ,target ,phases))))))
 
 (define (cross-gcc-patches target)
   "Return GCC patches needed for TARGET."
   (cond ((string-prefix? "xtensa-" target)
          ;; Patch by Qualcomm needed to build the ath9k-htc firmware.
-         (list (search-patch "ath9k-htc-firmware-gcc.patch")))
+         (search-patches "ath9k-htc-firmware-gcc.patch"))
+        ((target-mingw? target)
+         (search-patches "gcc-4.9.3-mingw-gthr-default.patch"))
         (else '())))
 
+(define (cross-gcc-snippet target)
+  "Return GCC snippet needed for TARGET."
+  (cond ((target-mingw? target)
+         '(copy-recursively "libstdc++-v3/config/os/mingw32-w64"
+                            "libstdc++-v3/config/os/newlib"))
+        (else #f)))
+
 (define* (cross-gcc target
                     #:optional (xbinutils (cross-binutils target)) libc)
   "Return a cross-compiler for TARGET, where TARGET is a GNU triplet.  Use
@@ -216,7 +196,10 @@ GCC that does not target a libc; otherwise, target that libc."
                (append
                 (origin-patches (package-source %xgcc))
                 (cons (search-patch "gcc-cross-environment-variables.patch")
-                      (cross-gcc-patches target))))))
+                      (cross-gcc-patches target))))
+              (modules '((guix build utils)))
+              (snippet
+               (cross-gcc-snippet target))))
 
     ;; For simplicity, use a single output.  Otherwise libgcc_s & co. are not
     ;; found by default, etc.
@@ -224,54 +207,65 @@ GCC that does not target a libc; otherwise, target that libc."
 
     (arguments
      `(#:implicit-inputs? #f
+       #:imported-modules ((gnu build cross-toolchain)
+                           ,@%gnu-build-system-modules)
        #:modules ((guix build gnu-build-system)
                   (guix build utils)
-                  (ice-9 regex)
+                  (gnu build cross-toolchain)
                   (srfi srfi-1)
-                  (srfi srfi-26))
+                  (srfi srfi-26)
+                  (ice-9 regex))
 
        ,@(cross-gcc-arguments target libc)))
 
     (native-inputs
      `(("ld-wrapper-cross" ,(make-ld-wrapper
                              (string-append "ld-wrapper-" target)
-                             #:target target
+                             #:target (const target)
                              #:binutils xbinutils))
        ("binutils-cross" ,xbinutils)
 
        ;; Call it differently so that the builder can check whether the "libc"
        ;; input is #f.
-       ("libc-native" ,@(assoc-ref %final-inputs "libc"))
+       ("libc-native" ,@(assoc-ref (%final-inputs) "libc"))
 
        ;; Remaining inputs.
        ,@(let ((inputs (append (package-inputs %xgcc)
-                               (alist-delete "libc" %final-inputs))))
-           (if libc
-               `(("libc" ,libc)
-                 ("xlinux-headers"                ;the target headers
-                  ,@(assoc-ref (package-propagated-inputs libc)
-                               "linux-headers"))
-                 ,@inputs)
-               inputs))))
+                               (alist-delete "libc" (%final-inputs)))))
+           (cond
+            ((target-mingw? target)
+             (if libc
+                 `(("libc" ,mingw-w64)
+                   ,@inputs)
+                 `(("mingw-source" ,(package-source mingw-w64))
+                   ,@inputs)))
+            (libc
+             `(("libc" ,libc)
+               ("xkernel-headers"                ;the target headers
+                ,@(assoc-ref (package-propagated-inputs libc)
+                             "kernel-headers"))
+               ,@inputs))
+            (else inputs)))))
 
     (inputs '())
 
     ;; Only search target inputs, not host inputs.
-    (search-paths
-     (list (search-path-specification
-            (variable "CROSS_CPATH")
-            (files '("include")))
-           (search-path-specification
-            (variable "CROSS_LIBRARY_PATH")
-            (files '("lib" "lib64")))))
+    (search-paths (cons (search-path-specification
+                         (variable "CROSS_LIBRARY_PATH")
+                         (files '("lib" "lib64")))
+                        (map (lambda (variable)
+                               (search-path-specification
+                                (variable variable)
+                                (files '("include"))))
+                             %gcc-cross-include-paths)))
     (native-search-paths '())))
 
-(define* (cross-libc target
-                     #:optional
-                     (xgcc (cross-gcc target))
-                     (xbinutils (cross-binutils target)))
-  "Return a libc cross-built for TARGET, a GNU triplet.  Use XGCC and
-XBINUTILS and the cross tool chain."
+(define* (cross-kernel-headers target
+                               #:optional
+                               (xgcc (cross-gcc target))
+                               (xbinutils (cross-binutils target)))
+  "Return headers depending on TARGET."
+
   (define xlinux-headers
     (package (inherit linux-libre-headers)
       (name (string-append (package-name linux-libre-headers)
@@ -294,82 +288,211 @@ XBINUTILS and the cross tool chain."
                        ("cross-binutils" ,xbinutils)
                        ,@(package-native-inputs linux-libre-headers)))))
 
-  (package (inherit glibc)
-    (name (string-append "glibc-cross-" target))
-    (arguments
-     (substitute-keyword-arguments
-         `(;; Disable stripping (see above.)
-           #:strip-binaries? #f
+  (define xgnumach-headers
+    (package (inherit gnumach-headers)
+      (name (string-append (package-name gnumach-headers)
+                           "-cross-" target))
+
+      (native-inputs `(("cross-gcc" ,xgcc)
+                       ("cross-binutils" ,xbinutils)
+                       ,@(package-native-inputs gnumach-headers)))))
 
-           ;; This package is used as a target input, but it should not have
-           ;; the usual cross-compilation inputs since that would include
-           ;; itself.
-           #:implicit-cross-inputs? #f
+  (define xmig
+    (package (inherit mig)
+      (name (string-append "mig-cross"))
+      (arguments
+       `(#:modules ((guix build gnu-build-system)
+                    (guix build utils)
+                    (srfi srfi-26))
+         #:phases (alist-cons-before
+                   'configure 'set-cross-headers-path
+                   (lambda* (#:key inputs #:allow-other-keys)
+                     (let* ((mach (assoc-ref inputs "cross-gnumach-headers"))
+                            (cpath (string-append mach "/include")))
+                       (for-each (cut setenv <> cpath)
+                                 ',%gcc-cross-include-paths)))
+                   %standard-phases)
+         #:configure-flags (list ,(string-append "--target=" target))
+         ,@(package-arguments mig)))
+
+      (propagated-inputs `(("cross-gnumach-headers" ,xgnumach-headers)))
+      (native-inputs `(("cross-gcc" ,xgcc)
+                       ("cross-binutils" ,xbinutils)
+                       ,@(package-native-inputs mig)))))
 
-           ,@(package-arguments glibc))
-       ((#:configure-flags flags)
-        `(cons ,(string-append "--host=" target)
-               ,flags))
-       ((#:phases phases)
-        `(alist-cons-before
-          'configure 'set-cross-linux-headers-path
-          (lambda* (#:key inputs #:allow-other-keys)
-            (let ((linux (assoc-ref inputs "linux-headers")))
-              (setenv "CROSS_CPATH"
-                      (string-append linux "/include"))
-              #t))
-          ,phases))))
-
-    ;; Shadow the native "linux-headers" because glibc's recipe expects the
-    ;; "linux-headers" input to point to the right thing.
-    (propagated-inputs `(("linux-headers" ,xlinux-headers)))
-
-    ;; FIXME: 'static-bash' should really be an input, not a native input, but
-    ;; to do that will require building an intermediate cross libc.
-    (inputs '())
+  (define xhurd-headers
+    (package (inherit hurd-headers)
+      (name (string-append (package-name hurd-headers)
+                           "-cross-" target))
 
-    (native-inputs `(("cross-gcc" ,xgcc)
-                     ("cross-binutils" ,xbinutils)
-                     ,@(package-inputs glibc)     ;FIXME: static-bash
-                     ,@(package-native-inputs glibc)))))
+      (native-inputs `(("cross-gcc" ,xgcc)
+                       ("cross-binutils" ,xbinutils)
+                       ("cross-mig" ,xmig)
+                       ,@(alist-delete "mig"(package-native-inputs hurd-headers))))))
 
-\f
-;;;
-;;; Concrete cross toolchains.
-;;;
+   (define xglibc/hurd-headers
+    (package (inherit glibc/hurd-headers)
+      (name (string-append (package-name glibc/hurd-headers)
+                           "-cross-" target))
+
+      (arguments
+       (substitute-keyword-arguments
+           `(#:modules ((guix build gnu-build-system)
+                        (guix build utils)
+                        (srfi srfi-26))
+             ,@(package-arguments glibc/hurd-headers))
+         ((#:phases phases)
+          `(alist-cons-before
+            'pre-configure 'set-cross-headers-path
+            (lambda* (#:key inputs #:allow-other-keys)
+              (let* ((mach (assoc-ref inputs "gnumach-headers"))
+                     (hurd (assoc-ref inputs "hurd-headers"))
+                     (cpath (string-append mach "/include:"
+                                           hurd "/include")))
+                (for-each (cut setenv <> cpath)
+                          ',%gcc-cross-include-paths)))
+            ,phases))))
+
+      (propagated-inputs `(("gnumach-headers" ,xgnumach-headers)
+                           ("hurd-headers" ,xhurd-headers)))
+
+      (native-inputs `(("cross-gcc" ,xgcc)
+                       ("cross-binutils" ,xbinutils)
+                       ("cross-mig" ,xmig)
+                       ,@(alist-delete "mig"(package-native-inputs glibc/hurd-headers))))))
+
+  (define xhurd-minimal
+    (package (inherit hurd-minimal)
+      (name (string-append (package-name hurd-minimal)
+                           "-cross-" target))
+      (arguments
+       (substitute-keyword-arguments
+         `(#:modules ((guix build gnu-build-system)
+                      (guix build utils)
+                      (srfi srfi-26))
+           ,@(package-arguments hurd-minimal))
+         ((#:phases phases)
+          `(alist-cons-before
+            'configure 'set-cross-headers-path
+            (lambda* (#:key inputs #:allow-other-keys)
+              (let* ((glibc-headers (assoc-ref inputs "cross-glibc-hurd-headers"))
+                    (cpath (string-append glibc-headers "/include")))
+                (for-each (cut setenv <> cpath)
+                          ',%gcc-cross-include-paths)))
+            ,phases))))
+
+      (inputs `(("cross-glibc-hurd-headers" ,xglibc/hurd-headers)))
+
+      (native-inputs `(("cross-gcc" ,xgcc)
+                       ("cross-binutils" ,xbinutils)
+                       ("cross-mig" ,xmig)
+                       ,@(alist-delete "mig"(package-native-inputs hurd-minimal))))))
 
-(define-public xgcc-mips64el
-  (let* ((triplet "mips64el-linux-gnuabi64")      ;N64 ABI
-         (xgcc    (cross-gcc triplet
-                             (cross-binutils triplet)
-                             (cross-libc triplet))))
-    ;; Don't attempt to build this cross-compiler on i686;
-    ;; see <http://bugs.gnu.org/19598>.
-    (package (inherit xgcc)
-      (supported-systems (fold delete
-                               (package-supported-systems xgcc)
-                               '("mips64el-linux" "i686-linux"))))))
-
-(define-public xgcc-avr
-  ;; AVR cross-compiler, used to build AVR-Libc.
-  (let ((triplet "avr"))
-    (cross-gcc triplet
-               (cross-binutils triplet))))
-
-(define-public xgcc-xtensa
-  ;; Bare-bones Xtensa cross-compiler, used to build the Atheros firmware.
-  (cross-gcc "xtensa-elf"))
-
-(define-public xgcc-armhf
-  (let* ((triplet "arm-linux-gnueabihf")
-         (xgcc    (cross-gcc triplet
-                             (cross-binutils triplet)
-                             (cross-libc triplet))))
-    (package (inherit xgcc)
-      (supported-systems (delete "armhf-linux" %supported-systems)))))
-
-;; (define-public xgcc-armel
-;;   (let ((triplet "armel-linux-gnueabi"))
+  (define xhurd-core-headers
+    (package (inherit hurd-core-headers)
+      (name (string-append (package-name hurd-core-headers)
+                           "-cross-" target))
+
+      (inputs `(("gnumach-headers" ,xgnumach-headers)
+                ("hurd-headers" ,xhurd-headers)
+                ("hurd-minimal" ,xhurd-minimal)))
+
+      (native-inputs `(("cross-gcc" ,xgcc)
+                       ("cross-binutils" ,xbinutils)
+                       ("cross-mig" ,xmig)
+                       ,@(package-native-inputs hurd-core-headers)))))
+
+  (match target
+    ((or "i586-pc-gnu" "i586-gnu") xhurd-core-headers)
+    (_ xlinux-headers)))
+
+(define* (cross-libc target
+                     #:optional
+                     (xgcc (cross-gcc target))
+                     (xbinutils (cross-binutils target))
+                     (xheaders (cross-kernel-headers target)))
+  "Return a libc cross-built for TARGET, a GNU triplet.  Use XGCC and
+XBINUTILS and the cross tool chain."
+  (define (cross-libc-for-target target)
+    "Return libc depending on TARGET."
+    (match target
+      ((or "i586-pc-gnu" "i586-gnu") glibc/hurd)
+      (_ glibc/linux)))
+
+  ;; Use (cross-libc-for-target ...) to determine the correct libc to use.
+
+  (if (cross-newlib? target)
+      (native-libc target)
+      (let ((libc (cross-libc-for-target target)))
+        (package (inherit libc)
+          (name (string-append "glibc-cross-" target))
+          (arguments
+           (substitute-keyword-arguments
+               `(;; Disable stripping (see above.)
+                 #:strip-binaries? #f
+
+                 ;; This package is used as a target input, but it should not have
+                 ;; the usual cross-compilation inputs since that would include
+                 ;; itself.
+                 #:implicit-cross-inputs? #f
+
+                 ;; We need SRFI 26.
+                 #:modules ((guix build gnu-build-system)
+                            (guix build utils)
+                            (srfi srfi-26))
+
+                 ,@(package-arguments libc))
+             ((#:configure-flags flags)
+              `(cons ,(string-append "--host=" target)
+                   ,flags))
+             ((#:phases phases)
+              `(alist-cons-before
+                'configure 'set-cross-kernel-headers-path
+                (lambda* (#:key inputs #:allow-other-keys)
+                  (let* ((kernel (assoc-ref inputs "kernel-headers"))
+                         (cpath (string-append kernel "/include")))
+                    (for-each (cut setenv <> cpath)
+                              ',%gcc-cross-include-paths)
+                    (setenv "CROSS_LIBRARY_PATH"
+                            (string-append kernel "/lib")) ;for Hurd's libihash
+                    #t))
+                ,phases))))
+
+          ;; Shadow the native "kernel-headers" because glibc's recipe expects the
+          ;; "kernel-headers" input to point to the right thing.
+          (propagated-inputs `(("kernel-headers" ,xheaders)))
+
+          ;; FIXME: 'static-bash' should really be an input, not a native input, but
+          ;; to do that will require building an intermediate cross libc.
+          (inputs '())
+
+          (native-inputs `(("cross-gcc" ,xgcc)
+                           ("cross-binutils" ,xbinutils)
+                           ,@(if (string-match (or "i586-pc-gnu" "i586-gnu") target)
+                                 `(("cross-mig"
+                                    ,@(assoc-ref (package-native-inputs xheaders)
+                                                 "cross-mig")))
+                                 '())
+                           ,@(package-inputs libc)     ;FIXME: static-bash
+                           ,@(package-native-inputs libc)))))))
+
+(define (native-libc target)
+  (if (target-mingw? target)
+      mingw-w64
+      glibc))
+
+(define (cross-newlib? target)
+  (not (eq? (native-libc target) glibc)))
+
+\f
+;;; Concrete cross tool chains are instantiated like this:
+;;
+;; (define-public xgcc-armhf
+;;   (let ((triplet "arm-linux-gnueabihf"))
 ;;     (cross-gcc triplet
 ;;                (cross-binutils triplet)
 ;;                (cross-libc triplet))))
+;;
+;;; We don't do that here because we'd be referring to bindings from (gnu
+;;; packages gcc) from the top level, which doesn't play well with circular
+;;; dependencies among modules.