1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2013, 2014, 2015, 2016, 2017, 2018 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2014, 2015, 2018 Mark H Weaver <mhw@netris.org>
4 ;;; Copyright © 2016 Jan Nieuwenhuizen <janneke@gnu.org>
5 ;;; Copyright © 2016 Manolis Fragkiskos Ragkousis <manolis837@gmail.com>
6 ;;; Copyright © 2018 Tobias Geerinckx-Rice <me@tobias.gr>
8 ;;; This file is part of GNU Guix.
10 ;;; GNU Guix is free software; you can redistribute it and/or modify it
11 ;;; under the terms of the GNU General Public License as published by
12 ;;; the Free Software Foundation; either version 3 of the License, or (at
13 ;;; your option) any later version.
15 ;;; GNU Guix is distributed in the hope that it will be useful, but
16 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;;; GNU General Public License for more details.
20 ;;; You should have received a copy of the GNU General Public License
21 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
23 (define-module (gnu packages cross-base)
24 #:use-module (gnu packages)
25 #:use-module (gnu packages gcc)
26 #:use-module (gnu packages base)
27 #:use-module (gnu packages linux)
28 #:use-module (gnu packages hurd)
29 #:use-module (gnu packages mingw)
30 #:use-module (guix packages)
31 #:use-module (guix download)
32 #:use-module (guix utils)
33 #:use-module (guix build-system gnu)
34 #:use-module (guix build-system trivial)
35 #:use-module (srfi srfi-1)
36 #:use-module (srfi srfi-26)
37 #:use-module (ice-9 match)
38 #:use-module (ice-9 regex)
39 #:export (cross-binutils
43 cross-kernel-headers))
46 ;; GCC package used as the basis for cross-compilation. It doesn't have to
47 ;; be 'gcc' and can be a specific variant such as 'gcc-4.8'.
49 ;; Note: This is a macro so that we do not refer to 'gcc' from the top
50 ;; level, which would lead to circular-dependency issues.
51 (identifier-syntax gcc))
53 (define %gcc-include-paths
54 ;; Environment variables for header search paths.
55 ;; Note: See <http://bugs.gnu.org/22186> for why not 'CPATH'.
59 "OBJCPLUS_INCLUDE_PATH"))
61 (define %gcc-cross-include-paths
62 ;; Search path for target headers when cross-compiling.
63 (map (cut string-append "CROSS_" <>) %gcc-include-paths))
65 (define (cross p target)
67 (name (string-append (package-name p) "-cross-" target))
69 (substitute-keyword-arguments (package-arguments p)
70 ((#:configure-flags flags)
71 `(cons ,(string-append "--target=" target)
74 (define (package-with-patch original patch)
75 "Return package ORIGINAL with PATCH applied."
76 (package (inherit original)
77 (source (origin (inherit (package-source original))
78 (patches (list patch))))))
80 (define (cross-binutils target)
81 "Return a cross-Binutils for TARGET."
82 (let ((binutils (package (inherit binutils)
84 (substitute-keyword-arguments (package-arguments
86 ((#:configure-flags flags)
87 ;; Build with `--with-sysroot' so that ld honors
88 ;; DT_RUNPATH entries when searching for a needed
89 ;; library. This works because as a side effect
90 ;; `genscripts.sh' sets `USE_LIBPATH=yes', which tells
91 ;; elf32.em to use DT_RUNPATH in its search list.
92 ;; See <http://sourceware.org/ml/binutils/2013-05/msg00312.html>.
94 ;; In theory choosing / as the sysroot could lead ld
95 ;; to pick up native libs instead of target ones. In
96 ;; practice the RUNPATH of target libs only refers to
97 ;; target libs, not native libs, so this is safe.
98 `(cons "--with-sysroot=/" ,flags)))))))
100 ;; For Xtensa, apply Qualcomm's patch.
101 (cross (if (string-prefix? "xtensa-" target)
102 (package-with-patch binutils
104 "ath9k-htc-firmware-binutils.patch"))
108 (define (cross-gcc-arguments target xgcc libc)
109 "Return build system arguments for a cross-gcc for TARGET, using XGCC as the
110 base compiler and using LIBC (which may be either a libc package or #f.)"
111 ;; Set the current target system so that 'glibc-dynamic-linker' returns the
113 (parameterize ((%current-target-system target))
114 ;; Disable stripping as this can break binaries, with object files of
115 ;; libgcc.a showing up as having an unknown architecture. See
116 ;; <http://lists.fedoraproject.org/pipermail/arm/2010-August/000663.html>
118 (let ((args `(#:strip-binaries? #f
119 ,@(package-arguments xgcc))))
120 (substitute-keyword-arguments args
121 ((#:configure-flags flags)
122 `(append (list ,(string-append "--target=" target)
124 `( ;; Disable libcilkrts because it is not
125 ;; ported to GNU/Hurd.
126 "--disable-libcilkrts")
127 `( ;; Disable features not needed at this stage.
128 "--disable-shared" "--enable-static"
129 "--enable-languages=c,c++"
131 ;; libstdc++ cannot be built at this stage
132 ;; ("Link tests are not allowed after
133 ;; GCC_NO_EXECUTABLES.").
134 "--disable-libstdc++-v3"
136 "--disable-threads" ;libgcc, would need libc
137 "--disable-libatomic"
138 "--disable-libmudflap"
141 "--disable-libquadmath"
142 "--disable-decimal-float" ;would need libc
143 "--disable-libcilkrts"
145 ;; When target is any OS other than 'none' these
146 ;; libraries will fail if there is no libc
148 ;; <https://lists.gnu.org/archive/html/guix-devel/2016-02/msg01311.html>
151 "--disable-libsanitizer"
154 ;; For a newlib (non-glibc) target
155 ,@(if (cross-newlib? target)
161 `(remove (cut string-match "--enable-languages.*" <>)
163 ((#:make-flags flags)
165 `(let ((libc (assoc-ref %build-inputs "libc")))
166 ;; FLAGS_FOR_TARGET are needed for the target libraries to receive
167 ;; the -Bxxx for the startfiles.
168 (cons (string-append "FLAGS_FOR_TARGET=-B" libc "/lib")
172 `(cross-gcc-build-phases ,target ,phases))))))
174 (define (cross-gcc-patches target)
175 "Return GCC patches needed for TARGET."
176 (cond ((string-prefix? "xtensa-" target)
177 ;; Patch by Qualcomm needed to build the ath9k-htc firmware.
178 (search-patches "ath9k-htc-firmware-gcc.patch"))
179 ((target-mingw? target)
180 (search-patches "gcc-4.9.3-mingw-gthr-default.patch"))
183 (define (cross-gcc-snippet target)
184 "Return GCC snippet needed for TARGET."
185 (cond ((target-mingw? target)
187 (copy-recursively "libstdc++-v3/config/os/mingw32-w64"
188 "libstdc++-v3/config/os/newlib")
192 (define* (cross-gcc target
195 (xbinutils (cross-binutils target))
197 "Return a cross-compiler for TARGET, where TARGET is a GNU triplet. Use
198 XGCC as the base compiler. Use XBINUTILS as the associated cross-Binutils.
199 If LIBC is false, then build a GCC that does not target a libc; otherwise,
201 (package (inherit xgcc)
202 (name (string-append "gcc-cross-"
203 (if libc "" "sans-libc-")
205 (source (origin (inherit (package-source xgcc))
208 (origin-patches (package-source xgcc))
210 ((version>=? (package-version xgcc) "8.0") (search-patch "gcc-8-cross-environment-variables.patch"))
211 ((version>=? (package-version xgcc) "6.0") (search-patch "gcc-6-cross-environment-variables.patch"))
212 (else (search-patch "gcc-cross-environment-variables.patch")))
213 (cross-gcc-patches target))))
214 (modules '((guix build utils)))
216 (cross-gcc-snippet target))))
218 ;; For simplicity, use a single output. Otherwise libgcc_s & co. are not
219 ;; found by default, etc.
223 `(#:implicit-inputs? #f
224 #:imported-modules ((gnu build cross-toolchain)
225 ,@%gnu-build-system-modules)
226 #:modules ((guix build gnu-build-system)
228 (gnu build cross-toolchain)
233 ,@(cross-gcc-arguments target xgcc libc)))
236 `(("ld-wrapper-cross" ,(make-ld-wrapper
237 (string-append "ld-wrapper-" target)
238 #:target (const target)
239 #:binutils xbinutils))
240 ("binutils-cross" ,xbinutils)
242 ;; Call it differently so that the builder can check whether the "libc"
244 ("libc-native" ,@(assoc-ref (%final-inputs) "libc"))
247 ,@(let ((inputs (append (package-inputs xgcc)
248 (alist-delete "libc" (%final-inputs)))))
250 ((target-mingw? target)
252 `(("libc" ,mingw-w64)
254 `(("mingw-source" ,(package-source mingw-w64))
258 ("libc:static" ,libc "static")
259 ("xkernel-headers" ;the target headers
260 ,@(assoc-ref (package-propagated-inputs libc)
267 ;; Only search target inputs, not host inputs.
268 (search-paths (cons (search-path-specification
269 (variable "CROSS_LIBRARY_PATH")
270 (files '("lib" "lib64")))
271 (map (lambda (variable)
272 (search-path-specification
274 (files '("include"))))
275 %gcc-cross-include-paths)))
276 (native-search-paths '())))
278 (define* (cross-kernel-headers target
280 (linux-headers linux-libre-headers)
281 (xgcc (cross-gcc target))
282 (xbinutils (cross-binutils target)))
283 "Return headers depending on TARGET."
285 (define xlinux-headers
286 (package (inherit linux-headers)
287 (name (string-append (package-name linux-headers)
290 (substitute-keyword-arguments
291 `(#:implicit-cross-inputs? #f
292 ,@(package-arguments linux-headers))
297 (setenv "ARCH" ,(system->linux-architecture target))
298 (format #t "`ARCH' set to `~a' (cross compiling)~%" (getenv "ARCH"))
300 (invoke "make" ,(system->defconfig target))
301 (invoke "make" "mrproper" "headers_check"))
303 (native-inputs `(("cross-gcc" ,xgcc)
304 ("cross-binutils" ,xbinutils)
305 ,@(package-native-inputs linux-headers)))))
307 (define xgnumach-headers
308 (package (inherit gnumach-headers)
309 (name (string-append (package-name gnumach-headers)
312 (native-inputs `(("cross-gcc" ,xgcc)
313 ("cross-binutils" ,xbinutils)
314 ,@(package-native-inputs gnumach-headers)))))
317 (package (inherit mig)
318 (name (string-append "mig-cross"))
320 `(#:modules ((guix build gnu-build-system)
323 #:phases (modify-phases %standard-phases
324 (add-before 'configure 'set-cross-headers-path
325 (lambda* (#:key inputs #:allow-other-keys)
326 (let* ((mach (assoc-ref inputs "cross-gnumach-headers"))
327 (cpath (string-append mach "/include")))
328 (for-each (cut setenv <> cpath)
329 ',%gcc-cross-include-paths)
331 #:configure-flags (list ,(string-append "--target=" target))
332 ,@(package-arguments mig)))
334 (propagated-inputs `(("cross-gnumach-headers" ,xgnumach-headers)))
335 (native-inputs `(("cross-gcc" ,xgcc)
336 ("cross-binutils" ,xbinutils)
337 ,@(package-native-inputs mig)))))
339 (define xhurd-headers
340 (package (inherit hurd-headers)
341 (name (string-append (package-name hurd-headers)
344 (native-inputs `(("cross-gcc" ,xgcc)
345 ("cross-binutils" ,xbinutils)
347 ,@(alist-delete "mig"(package-native-inputs hurd-headers))))))
349 (define xglibc/hurd-headers
350 (package (inherit glibc/hurd-headers)
351 (name (string-append (package-name glibc/hurd-headers)
355 (substitute-keyword-arguments
356 `(#:modules ((guix build gnu-build-system)
359 ,@(package-arguments glibc/hurd-headers))
361 `(modify-phases ,phases
362 (add-after 'unpack 'set-cross-headers-path
363 (lambda* (#:key inputs #:allow-other-keys)
364 (let* ((mach (assoc-ref inputs "gnumach-headers"))
365 (hurd (assoc-ref inputs "hurd-headers"))
366 (cpath (string-append mach "/include:"
368 (for-each (cut setenv <> cpath)
369 ',%gcc-cross-include-paths)
372 (propagated-inputs `(("gnumach-headers" ,xgnumach-headers)
373 ("hurd-headers" ,xhurd-headers)))
375 (native-inputs `(("cross-gcc" ,xgcc)
376 ("cross-binutils" ,xbinutils)
378 ,@(alist-delete "mig"(package-native-inputs glibc/hurd-headers))))))
380 (define xhurd-minimal
381 (package (inherit hurd-minimal)
382 (name (string-append (package-name hurd-minimal)
385 (substitute-keyword-arguments
386 `(#:modules ((guix build gnu-build-system)
389 ,@(package-arguments hurd-minimal))
391 `(modify-phases ,phases
392 (add-before 'configure 'set-cross-headers-path
393 (lambda* (#:key inputs #:allow-other-keys)
394 (let* ((glibc-headers (assoc-ref inputs "cross-glibc-hurd-headers"))
395 (cpath (string-append glibc-headers "/include")))
396 (for-each (cut setenv <> cpath)
397 ',%gcc-cross-include-paths)
400 (inputs `(("cross-glibc-hurd-headers" ,xglibc/hurd-headers)))
402 (native-inputs `(("cross-gcc" ,xgcc)
403 ("cross-binutils" ,xbinutils)
405 ,@(alist-delete "mig"(package-native-inputs hurd-minimal))))))
407 (define xhurd-core-headers
408 (package (inherit hurd-core-headers)
409 (name (string-append (package-name hurd-core-headers)
412 (inputs `(("gnumach-headers" ,xgnumach-headers)
413 ("hurd-headers" ,xhurd-headers)
414 ("hurd-minimal" ,xhurd-minimal)))
416 (native-inputs `(("cross-gcc" ,xgcc)
417 ("cross-binutils" ,xbinutils)
419 ,@(package-native-inputs hurd-core-headers)))))
422 ((or "i586-pc-gnu" "i586-gnu") xhurd-core-headers)
425 (define* (cross-libc target
428 (xgcc (cross-gcc target))
429 (xbinutils (cross-binutils target))
430 (xheaders (cross-kernel-headers target)))
431 "Return LIBC cross-built for TARGET, a GNU triplet. Use XGCC and XBINUTILS
432 and the cross tool chain."
433 (if (cross-newlib? target libc)
434 (native-libc target libc)
436 (package (inherit libc)
437 (name (string-append "glibc-cross-" target))
439 (substitute-keyword-arguments
440 `(;; Disable stripping (see above.)
443 ;; This package is used as a target input, but it should not have
444 ;; the usual cross-compilation inputs since that would include
446 #:implicit-cross-inputs? #f
449 #:modules ((guix build gnu-build-system)
453 ,@(package-arguments libc))
454 ((#:configure-flags flags)
455 `(cons ,(string-append "--host=" target)
456 ,(if (hurd-triplet? target)
457 `(cons "--disable-werror" ,flags)
460 `(modify-phases ,phases
461 ;; XXX: The hack below allows us to make sure the
462 ;; 'apply-hurd-patch' phase gets added in the first
463 ;; cross-libc, but does *not* get added twice subsequently
464 ;; when cross-building another libc.
465 ,@(if (and (hurd-triplet? target)
466 (not (hurd-target?)))
467 `((add-after 'unpack 'apply-hurd-patch
468 (lambda* (#:key inputs native-inputs
470 ;; TODO: Move this to 'patches' field.
471 (let ((patch (or (assoc-ref native-inputs
472 "hurd-magic-pid-patch")
474 "hurd-magic-pid-patch"))))
475 (invoke "patch" "-p1" "--force" "--input"
478 (add-before 'configure 'set-cross-kernel-headers-path
479 (lambda* (#:key inputs #:allow-other-keys)
480 (let* ((kernel (assoc-ref inputs "kernel-headers"))
481 (cpath (string-append kernel "/include")))
482 (for-each (cut setenv <> cpath)
483 ',%gcc-cross-include-paths)
484 (setenv "CROSS_LIBRARY_PATH"
485 (string-append kernel "/lib")) ; for Hurd's libihash
488 ;; Shadow the native "kernel-headers" because glibc's recipe expects the
489 ;; "kernel-headers" input to point to the right thing.
490 (propagated-inputs `(("kernel-headers" ,xheaders)))
492 ;; FIXME: 'static-bash' should really be an input, not a native input, but
493 ;; to do that will require building an intermediate cross libc.
496 (native-inputs `(("cross-gcc" ,xgcc)
497 ("cross-binutils" ,xbinutils)
498 ,@(if (hurd-triplet? target)
500 ,@(assoc-ref (package-native-inputs xheaders)
502 ("hurd-magic-pid-patch"
503 ,(search-patch "glibc-hurd-magic-pid.patch")))
505 ,@(package-inputs libc) ;FIXME: static-bash
506 ,@(package-native-inputs libc)))))))
508 (define* (native-libc target
511 (if (target-mingw? target)
515 (define* (cross-newlib? target
518 (not (eq? (native-libc target libc) libc)))
521 ;;; Concrete cross tool chains are instantiated like this:
523 ;; (define-public xgcc-armhf
524 ;; (let ((triplet "arm-linux-gnueabihf"))
525 ;; (cross-gcc triplet
526 ;; #:xbinutils (cross-binutils triplet)
527 ;; #:libc (cross-libc triplet))))
529 ;;; We don't do that here because we'd be referring to bindings from (gnu
530 ;;; packages gcc) from the top level, which doesn't play well with circular
531 ;;; dependencies among modules.