1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2013, 2014, 2015, 2016, 2017, 2018, 2020 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2014, 2015, 2018 Mark H Weaver <mhw@netris.org>
4 ;;; Copyright © 2016, 2019 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
5 ;;; Copyright © 2016 Manolis Fragkiskos Ragkousis <manolis837@gmail.com>
6 ;;; Copyright © 2018 Tobias Geerinckx-Rice <me@tobias.gr>
7 ;;; Copyright © 2019, 2020 Marius Bakke <mbakke@fastmail.com>
8 ;;; Copyright © 2019 Carl Dong <contact@carldong.me>
9 ;;; Copyright © 2020 Mathieu Othacehe <m.othacehe@gmail.com>
11 ;;; This file is part of GNU Guix.
13 ;;; GNU Guix is free software; you can redistribute it and/or modify it
14 ;;; under the terms of the GNU General Public License as published by
15 ;;; the Free Software Foundation; either version 3 of the License, or (at
16 ;;; your option) any later version.
18 ;;; GNU Guix is distributed in the hope that it will be useful, but
19 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;;; GNU General Public License for more details.
23 ;;; You should have received a copy of the GNU General Public License
24 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
26 (define-module (gnu packages cross-base)
27 #:use-module (gnu packages)
28 #:use-module (gnu packages gcc)
29 #:use-module (gnu packages base)
30 #:use-module (gnu packages linux)
31 #:use-module (gnu packages hurd)
32 #:use-module (gnu packages mingw)
33 #:use-module (guix packages)
34 #:use-module (guix download)
35 #:use-module (guix utils)
36 #:use-module (guix build-system gnu)
37 #:use-module (guix build-system trivial)
38 #:use-module (srfi srfi-1)
39 #:use-module (srfi srfi-26)
40 #:use-module (ice-9 match)
41 #:use-module (ice-9 regex)
42 #:export (cross-binutils
46 cross-kernel-headers))
49 ;; GCC package used as the basis for cross-compilation. It doesn't have to
50 ;; be 'gcc' and can be a specific variant such as 'gcc-4.8'.
52 ;; Note: This is a macro so that we do not refer to 'gcc' from the top
53 ;; level, which would lead to circular-dependency issues.
54 (identifier-syntax gcc))
56 (define %gcc-include-paths
57 ;; Environment variables for header search paths.
58 ;; Note: See <http://bugs.gnu.org/22186> for why not 'CPATH'.
62 "OBJCPLUS_INCLUDE_PATH"))
64 (define %gcc-cross-include-paths
65 ;; Search path for target headers when cross-compiling.
66 (map (cut string-append "CROSS_" <>) %gcc-include-paths))
68 (define (cross p target)
70 (name (string-append (package-name p) "-cross-" target))
72 (substitute-keyword-arguments (package-arguments p)
73 ((#:configure-flags flags)
74 `(cons ,(string-append "--target=" target)
77 (define* (cross-binutils target #:optional (binutils binutils))
78 "Return a cross-Binutils for TARGET using BINUTILS."
79 (let ((binutils (package (inherit binutils)
81 (substitute-keyword-arguments (package-arguments
83 ((#:configure-flags flags)
84 ;; Build with `--with-sysroot' so that ld honors
85 ;; DT_RUNPATH entries when searching for a needed
86 ;; library. This works because as a side effect
87 ;; `genscripts.sh' sets `USE_LIBPATH=yes', which tells
88 ;; elf32.em to use DT_RUNPATH in its search list.
89 ;; See <http://sourceware.org/ml/binutils/2013-05/msg00312.html>.
91 ;; In theory choosing / as the sysroot could lead ld
92 ;; to pick up native libs instead of target ones. In
93 ;; practice the RUNPATH of target libs only refers to
94 ;; target libs, not native libs, so this is safe.
95 `(cons "--with-sysroot=/" ,flags)))))))
97 ;; For Xtensa, apply Qualcomm's patch.
98 (cross (cond ((string-prefix? "xtensa-" target)
99 (package-with-patches binutils
101 "ath9k-htc-firmware-binutils.patch")))
102 ((target-mingw? target)
103 (package-with-extra-patches
105 (search-patches "binutils-mingw-w64-timestamp.patch"
106 "binutils-mingw-w64-deterministic.patch")))
110 (define (cross-gcc-arguments target xgcc libc)
111 "Return build system arguments for a cross-gcc for TARGET, using XGCC as the
112 base compiler and using LIBC (which may be either a libc package or #f.)"
113 ;; Set the current target system so that 'glibc-dynamic-linker' returns the
115 (parameterize ((%current-target-system target))
116 ;; Disable stripping as this can break binaries, with object files of
117 ;; libgcc.a showing up as having an unknown architecture. See
118 ;; <http://lists.fedoraproject.org/pipermail/arm/2010-August/000663.html>
120 (let ((args `(#:strip-binaries? #f
121 ,@(package-arguments xgcc))))
122 (substitute-keyword-arguments args
123 ((#:configure-flags flags)
124 `(append (list ,(string-append "--target=" target)
126 `( ;; Disable libcilkrts because it is not
127 ;; ported to GNU/Hurd.
128 "--disable-libcilkrts"
129 ;; When building a cross compiler, --with-sysroot is
130 ;; implicitly set to "$gcc_tooldir/sys-root". This does
131 ;; not work for us, because --with-native-system-header-dir
132 ;; is searched for relative to this location. Thus, we set
133 ;; it to "/" so GCC is able to find the target libc headers.
134 ;; This is safe because in practice GCC uses CROSS_CPATH
135 ;; & co to separate target and host libraries.
137 `( ;; Disable features not needed at this stage.
138 "--disable-shared" "--enable-static"
139 "--enable-languages=c,c++"
141 ;; libstdc++ cannot be built at this stage
142 ;; ("Link tests are not allowed after
143 ;; GCC_NO_EXECUTABLES.").
144 "--disable-libstdc++-v3"
146 "--disable-threads" ;libgcc, would need libc
147 "--disable-libatomic"
148 "--disable-libmudflap"
152 "--disable-libquadmath"
153 "--disable-decimal-float" ;would need libc
154 "--disable-libcilkrts"
156 ;; When target is any OS other than 'none' these
157 ;; libraries will fail if there is no libc
159 ;; <https://lists.gnu.org/archive/html/guix-devel/2016-02/msg01311.html>
162 "--disable-libsanitizer"
165 ;; Install cross-built libraries such as libgcc_s.so in
168 `((string-append "--with-toolexeclibdir="
169 (assoc-ref %outputs "lib")
172 ;; For a newlib (non-glibc) target
173 ,@(if (cross-newlib? target)
179 `(remove (cut string-match "--enable-languages.*" <>)
181 ((#:make-flags flags)
183 `(let ((libc (assoc-ref %build-inputs "libc")))
184 ;; FLAGS_FOR_TARGET are needed for the target libraries to receive
185 ;; the -Bxxx for the startfiles.
186 (cons (string-append "FLAGS_FOR_TARGET=-B" libc "/lib")
190 `(cross-gcc-build-phases ,target ,phases))))))
192 (define (cross-gcc-patches xgcc target)
193 "Return GCC patches needed for XGCC and TARGET."
194 (cond ((string-prefix? "xtensa-" target)
195 ;; Patch by Qualcomm needed to build the ath9k-htc firmware.
196 (search-patches "ath9k-htc-firmware-gcc.patch"))
197 ((target-mingw? target)
198 (append (search-patches "gcc-4.9.3-mingw-gthr-default.patch")
199 (if (version>=? (package-version xgcc) "7.0")
200 (search-patches "gcc-7-cross-mingw.patch")
204 (define (cross-gcc-snippet target)
205 "Return GCC snippet needed for TARGET."
207 ,@(if (target-mingw? target)
208 '((copy-recursively "libstdc++-v3/config/os/mingw32-w64"
209 "libstdc++-v3/config/os/newlib"))
211 ;; TOOLDIR_BASE_PREFIX is erroneous when using a separate "lib"
212 ;; output. Specify it correctly, otherwise GCC won't find its shared
213 ;; libraries installed in the "lib" output. See:
214 ;; https://lists.gnu.org/archive/html/bug-guix/2020-03/msg00196.html.
215 (substitute* "gcc/Makefile.in"
216 (("-DTOOLDIR_BASE_PREFIX=[^ ]*")
217 "-DTOOLDIR_BASE_PREFIX=\\\"../../../../\\\""))
220 (define* (cross-gcc target
223 (xbinutils (cross-binutils target))
225 "Return a cross-compiler for TARGET, where TARGET is a GNU triplet. Use
226 XGCC as the base compiler. Use XBINUTILS as the associated cross-Binutils.
227 If LIBC is false, then build a GCC that does not target a libc; otherwise,
229 (package (inherit xgcc)
230 (name (string-append "gcc-cross-"
231 (if libc "" "sans-libc-")
235 (inherit (package-source xgcc))
238 (origin-patches (package-source xgcc))
240 ((version>=? (package-version xgcc) "8.0")
241 (search-patches "gcc-8-cross-environment-variables.patch"))
242 ((version>=? (package-version xgcc) "6.0")
243 (search-patches "gcc-7-cross-toolexeclibdir.patch"
244 "gcc-6-cross-environment-variables.patch"))
246 (search-patches "gcc-cross-environment-variables.patch")))
247 (cross-gcc-patches xgcc target))))
248 (modules '((guix build utils)))
250 (cross-gcc-snippet target))))
252 (outputs '("out" "lib"))
255 `(#:implicit-inputs? #f
256 #:imported-modules ((gnu build cross-toolchain)
257 ,@%gnu-build-system-modules)
258 #:modules ((guix build gnu-build-system)
260 (gnu build cross-toolchain)
265 ,@(cross-gcc-arguments target xgcc libc)))
268 `(("ld-wrapper-cross" ,(make-ld-wrapper
269 (string-append "ld-wrapper-" target)
270 #:target (const target)
271 #:binutils xbinutils))
272 ("binutils-cross" ,xbinutils)
274 ,@(let ((inputs (append (package-inputs xgcc)
275 (fold alist-delete (%final-inputs)
276 '("libc" "libc:static"))
278 ;; Call it differently so that the builder can
279 ;; check whether the "libc" input is #f.
281 ,@(assoc-ref (%final-inputs) "libc"))
282 ("libc-native:static"
283 ,@(assoc-ref (%final-inputs)
286 ((target-mingw? target)
291 ("mingw-source" ,(package-source mingw-w64)))))
295 ("libc:static" ,libc "static")
296 ("xkernel-headers" ;the target headers
297 ,@(assoc-ref (package-propagated-inputs libc)
303 ;; Only search target inputs, not host inputs.
304 (search-paths (cons (search-path-specification
305 (variable "CROSS_LIBRARY_PATH")
306 (files '("lib" "lib64")))
307 (map (lambda (variable)
308 (search-path-specification
311 ;; Add 'include/c++' here so that <cstdlib>'s
312 ;; "#include_next <stdlib.h>" finds GCC's
313 ;; <stdlib.h>, not libc's.
314 (files (match variable
315 ("CROSS_CPLUS_INCLUDE_PATH"
316 '("include/c++" "include"))
319 %gcc-cross-include-paths)))
320 (native-search-paths '())))
322 (define* (cross-kernel-headers target
324 (linux-headers linux-libre-headers)
325 (xgcc (cross-gcc target))
326 (xbinutils (cross-binutils target)))
327 "Return headers depending on TARGET."
329 (define xlinux-headers
330 (package (inherit linux-headers)
331 (name (string-append (package-name linux-headers)
334 (substitute-keyword-arguments
335 `(#:implicit-cross-inputs? #f
336 ,@(package-arguments linux-headers))
341 (setenv "ARCH" ,(system->linux-architecture target))
342 (format #t "`ARCH' set to `~a' (cross compiling)~%" (getenv "ARCH"))
344 (invoke "make" ,(system->defconfig target))
345 (invoke "make" "mrproper" "headers_check"))
347 (native-inputs `(("cross-gcc" ,xgcc)
348 ("cross-binutils" ,xbinutils)
349 ,@(package-native-inputs linux-headers)))))
351 (define xgnumach-headers
352 (package (inherit gnumach-headers)
353 (name (string-append (package-name gnumach-headers)
356 (native-inputs `(("cross-gcc" ,xgcc)
357 ("cross-binutils" ,xbinutils)
358 ,@(package-native-inputs gnumach-headers)))))
361 (package (inherit mig)
362 (name (string-append "mig-cross"))
364 `(#:modules ((guix build gnu-build-system)
367 #:phases (modify-phases %standard-phases
368 (add-before 'configure 'set-cross-headers-path
369 (lambda* (#:key inputs #:allow-other-keys)
370 (let* ((mach (assoc-ref inputs "cross-gnumach-headers"))
371 (cpath (string-append mach "/include")))
372 (for-each (cut setenv <> cpath)
373 ',%gcc-cross-include-paths)
375 #:configure-flags (list ,(string-append "--target=" target))
378 (propagated-inputs `(("cross-gnumach-headers" ,xgnumach-headers)))
379 (native-inputs `(("cross-gcc" ,xgcc)
380 ("cross-binutils" ,xbinutils)
381 ,@(package-native-inputs mig)))))
383 (define xhurd-headers
384 (package (inherit hurd-headers)
385 (name (string-append (package-name hurd-headers)
388 (native-inputs `(("cross-gcc" ,xgcc)
389 ("cross-binutils" ,xbinutils)
391 ,@(alist-delete "mig"(package-native-inputs hurd-headers))))))
393 (define xglibc/hurd-headers
394 (package (inherit glibc/hurd-headers)
395 (name (string-append (package-name glibc/hurd-headers)
399 (substitute-keyword-arguments
400 `(#:modules ((guix build gnu-build-system)
403 ,@(package-arguments glibc/hurd-headers))
405 `(modify-phases ,phases
406 (add-after 'unpack 'set-cross-headers-path
407 (lambda* (#:key inputs #:allow-other-keys)
408 (let* ((mach (assoc-ref inputs "gnumach-headers"))
409 (hurd (assoc-ref inputs "hurd-headers"))
410 (cpath (string-append mach "/include:"
412 (for-each (cut setenv <> cpath)
413 ',%gcc-cross-include-paths)
416 (propagated-inputs `(("gnumach-headers" ,xgnumach-headers)
417 ("hurd-headers" ,xhurd-headers)))
419 (native-inputs `(("cross-gcc" ,xgcc)
420 ("cross-binutils" ,xbinutils)
422 ,@(alist-delete "mig"(package-native-inputs glibc/hurd-headers))))))
424 (define xhurd-minimal
425 (package (inherit hurd-minimal)
426 (name (string-append (package-name hurd-minimal)
429 (substitute-keyword-arguments
430 `(#:modules ((guix build gnu-build-system)
433 ,@(package-arguments hurd-minimal))
435 `(modify-phases ,phases
436 (add-before 'configure 'set-cross-headers-path
437 (lambda* (#:key inputs #:allow-other-keys)
438 (let* ((glibc-headers (assoc-ref inputs "cross-glibc-hurd-headers"))
439 (cpath (string-append glibc-headers "/include")))
440 (for-each (cut setenv <> cpath)
441 ',%gcc-cross-include-paths)
444 (inputs `(("cross-glibc-hurd-headers" ,xglibc/hurd-headers)))
446 (native-inputs `(("cross-gcc" ,xgcc)
447 ("cross-binutils" ,xbinutils)
449 ,@(alist-delete "mig"(package-native-inputs hurd-minimal))))))
451 (define xhurd-core-headers
452 (package (inherit hurd-core-headers)
453 (name (string-append (package-name hurd-core-headers)
456 (inputs `(("gnumach-headers" ,xgnumach-headers)
457 ("hurd-headers" ,xhurd-headers)
458 ("hurd-minimal" ,xhurd-minimal)))
460 (native-inputs `(("cross-gcc" ,xgcc)
461 ("cross-binutils" ,xbinutils)
463 ,@(package-native-inputs hurd-core-headers)))))
466 ((or "i586-pc-gnu" "i586-gnu") xhurd-core-headers)
469 (define* (cross-libc target
472 (xgcc (cross-gcc target))
473 (xbinutils (cross-binutils target))
474 (xheaders (cross-kernel-headers target)))
475 "Return LIBC cross-built for TARGET, a GNU triplet. Use XGCC and XBINUTILS
476 and the cross tool chain."
477 (if (cross-newlib? target libc)
478 (native-libc target libc
480 #:xbinutils xbinutils)
483 (name (string-append "glibc-cross-" target))
485 (substitute-keyword-arguments
486 `( ;; Disable stripping (see above.)
489 ;; This package is used as a target input, but it should not have
490 ;; the usual cross-compilation inputs since that would include
492 #:implicit-cross-inputs? #f
495 #:modules ((guix build gnu-build-system)
499 ,@(package-arguments libc))
500 ((#:configure-flags flags)
501 `(cons ,(string-append "--host=" target)
502 ,(if (hurd-triplet? target)
503 `(cons "--disable-werror" ,flags)
506 `(modify-phases ,phases
507 (add-before 'configure 'set-cross-kernel-headers-path
508 (lambda* (#:key inputs #:allow-other-keys)
509 (let* ((kernel (assoc-ref inputs "kernel-headers"))
510 (cpath (string-append kernel "/include")))
511 (for-each (cut setenv <> cpath)
512 ',%gcc-cross-include-paths)
513 (setenv "CROSS_LIBRARY_PATH"
514 (string-append kernel "/lib")) ; for Hurd's libihash
516 ,@(if (hurd-triplet? target)
517 '((add-after 'install 'augment-libc.so
518 (lambda* (#:key outputs #:allow-other-keys)
519 (let* ((out (assoc-ref outputs "out")))
520 (substitute* (string-append out "/lib/libc.so")
521 (("/[^ ]+/lib/libc.so.0.3")
522 (string-append out "/lib/libc.so.0.3"
523 " libmachuser.so libhurduser.so"))))
525 ;; TODO: move to glibc in the next rebuild cycle
526 (add-after 'unpack 'patch-libc/hurd
527 (lambda* (#:key inputs #:allow-other-keys)
530 (let ((patch (assoc-ref inputs name)))
531 (invoke "patch" "-p1" "--force" "-i" patch)))
532 '("hurd-mach-print.patch"
533 "hurd-gettyent.patch")))))
536 ;; Shadow the native "kernel-headers" because glibc's recipe expects the
537 ;; "kernel-headers" input to point to the right thing.
538 (propagated-inputs `(("kernel-headers" ,xheaders)))
540 ;; FIXME: 'static-bash' should really be an input, not a native input, but
541 ;; to do that will require building an intermediate cross libc.
544 (native-inputs `(("cross-gcc" ,xgcc)
545 ("cross-binutils" ,xbinutils)
546 ,@(if (hurd-triplet? target)
548 ,@(assoc-ref (package-native-inputs xheaders)
550 ;; TODO: move to glibc in the next rebuild cycle
551 ("hurd-mach-print.patch"
552 ,@(search-patches "glibc-hurd-mach-print.patch"))
553 ("hurd-gettyent.patch"
554 ,@(search-patches "glibc-hurd-gettyent.patch")))
556 ,@(package-inputs libc) ;FIXME: static-bash
557 ,@(package-native-inputs libc))))))
559 (define* (native-libc target
565 (if (target-mingw? target)
566 (let ((machine (substring target 0 (string-index target #\-))))
567 (make-mingw-w64 machine
569 #:xbinutils xbinutils))
572 (define* (cross-newlib? target
575 (not (eq? (native-libc target libc) libc)))
578 ;;; Concrete cross tool chains are instantiated like this:
580 ;; (define-public xgcc-armhf
581 ;; (let ((triplet "arm-linux-gnueabihf"))
582 ;; (cross-gcc triplet
583 ;; #:xbinutils (cross-binutils triplet)
584 ;; #:libc (cross-libc triplet))))
586 ;;; We don't do that here because we'd be referring to bindings from (gnu
587 ;;; packages gcc) from the top level, which doesn't play well with circular
588 ;;; dependencies among modules.