gnu: r-fields: Update to 11.4.
[jackhill/guix/guix.git] / gnu / packages / cross-base.scm
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>
10 ;;;
11 ;;; This file is part of GNU Guix.
12 ;;;
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.
17 ;;;
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.
22 ;;;
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/>.
25
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
43 cross-libc
44 cross-gcc
45 cross-newlib?
46 cross-kernel-headers))
47
48 (define-syntax %xgcc
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'.
51 ;;
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))
55
56 (define %gcc-include-paths
57 ;; Environment variables for header search paths.
58 ;; Note: See <http://bugs.gnu.org/22186> for why not 'CPATH'.
59 '("C_INCLUDE_PATH"
60 "CPLUS_INCLUDE_PATH"
61 "OBJC_INCLUDE_PATH"
62 "OBJCPLUS_INCLUDE_PATH"))
63
64 (define %gcc-cross-include-paths
65 ;; Search path for target headers when cross-compiling.
66 (map (cut string-append "CROSS_" <>) %gcc-include-paths))
67
68 (define (cross p target)
69 (package (inherit p)
70 (name (string-append (package-name p) "-cross-" target))
71 (arguments
72 (substitute-keyword-arguments (package-arguments p)
73 ((#:configure-flags flags)
74 `(cons ,(string-append "--target=" target)
75 ,flags))))))
76
77 (define* (cross-binutils target #:optional (binutils binutils))
78 "Return a cross-Binutils for TARGET using BINUTILS."
79 (let ((binutils (package (inherit binutils)
80 (arguments
81 (substitute-keyword-arguments (package-arguments
82 binutils)
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>.
90 ;;
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)))))))
96
97 ;; For Xtensa, apply Qualcomm's patch.
98 (cross (cond ((string-prefix? "xtensa-" target)
99 (package-with-patches binutils
100 (search-patches
101 "ath9k-htc-firmware-binutils.patch")))
102 ((target-mingw? target)
103 (package-with-extra-patches
104 binutils
105 (search-patches "binutils-mingw-w64-timestamp.patch"
106 "binutils-mingw-w64-deterministic.patch")))
107 (else binutils))
108 target)))
109
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
114 ;; right name.
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>
119 ;; for instance.
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)
125 ,@(if libc
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.
136 "--with-sysroot=/")
137 `( ;; Disable features not needed at this stage.
138 "--disable-shared" "--enable-static"
139 "--enable-languages=c,c++"
140
141 ;; libstdc++ cannot be built at this stage
142 ;; ("Link tests are not allowed after
143 ;; GCC_NO_EXECUTABLES.").
144 "--disable-libstdc++-v3"
145
146 "--disable-threads" ;libgcc, would need libc
147 "--disable-libatomic"
148 "--disable-libmudflap"
149 "--disable-libgomp"
150 "--disable-libmpx"
151 "--disable-libssp"
152 "--disable-libquadmath"
153 "--disable-decimal-float" ;would need libc
154 "--disable-libcilkrts"
155
156 ;; When target is any OS other than 'none' these
157 ;; libraries will fail if there is no libc
158 ;; present. See
159 ;; <https://lists.gnu.org/archive/html/guix-devel/2016-02/msg01311.html>
160 "--disable-libitm"
161 "--disable-libvtv"
162 "--disable-libsanitizer"
163 ))
164
165 ;; Install cross-built libraries such as libgcc_s.so in
166 ;; the "lib" output.
167 ,@(if libc
168 `((string-append "--with-toolexeclibdir="
169 (assoc-ref %outputs "lib")
170 "/" ,target "/lib"))
171 '())
172 ;; For a newlib (non-glibc) target
173 ,@(if (cross-newlib? target)
174 '("--with-newlib")
175 '()))
176
177 ,(if libc
178 flags
179 `(remove (cut string-match "--enable-languages.*" <>)
180 ,flags))))
181 ((#:make-flags flags)
182 (if libc
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")
187 ,flags))
188 flags))
189 ((#:phases phases)
190 `(cross-gcc-build-phases ,target ,phases))))))
191
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")
201 '())))
202 (else '())))
203
204 (define (cross-gcc-snippet target)
205 "Return GCC snippet needed for TARGET."
206 `(begin
207 ,@(if (target-mingw? target)
208 '((copy-recursively "libstdc++-v3/config/os/mingw32-w64"
209 "libstdc++-v3/config/os/newlib"))
210 '())
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=\\\"../../../../\\\""))
218 #t))
219
220 (define* (cross-gcc target
221 #:key
222 (xgcc %xgcc)
223 (xbinutils (cross-binutils target))
224 (libc #f))
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,
228 target that libc."
229 (package (inherit xgcc)
230 (name (string-append "gcc-cross-"
231 (if libc "" "sans-libc-")
232 target))
233 (source
234 (origin
235 (inherit (package-source xgcc))
236 (patches
237 (append
238 (origin-patches (package-source xgcc))
239 (append (cond
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"))
245 (else
246 (search-patches "gcc-cross-environment-variables.patch")))
247 (cross-gcc-patches xgcc target))))
248 (modules '((guix build utils)))
249 (snippet
250 (cross-gcc-snippet target))))
251
252 (outputs '("out" "lib"))
253
254 (arguments
255 `(#:implicit-inputs? #f
256 #:imported-modules ((gnu build cross-toolchain)
257 ,@%gnu-build-system-modules)
258 #:modules ((guix build gnu-build-system)
259 (guix build utils)
260 (gnu build cross-toolchain)
261 (srfi srfi-1)
262 (srfi srfi-26)
263 (ice-9 regex))
264
265 ,@(cross-gcc-arguments target xgcc libc)))
266
267 (native-inputs
268 `(("ld-wrapper-cross" ,(make-ld-wrapper
269 (string-append "ld-wrapper-" target)
270 #:target (const target)
271 #:binutils xbinutils))
272 ("binutils-cross" ,xbinutils)
273
274 ,@(let ((inputs (append (package-inputs xgcc)
275 (fold alist-delete (%final-inputs)
276 '("libc" "libc:static"))
277
278 ;; Call it differently so that the builder can
279 ;; check whether the "libc" input is #f.
280 `(("libc-native"
281 ,@(assoc-ref (%final-inputs) "libc"))
282 ("libc-native:static"
283 ,@(assoc-ref (%final-inputs)
284 "libc:static"))))))
285 (cond
286 ((target-mingw? target)
287 (if libc
288 `(,@inputs
289 ("libc" ,libc))
290 `(,@inputs
291 ("mingw-source" ,(package-source mingw-w64)))))
292 (libc
293 `(,@inputs
294 ("libc" ,libc)
295 ("libc:static" ,libc "static")
296 ("xkernel-headers" ;the target headers
297 ,@(assoc-ref (package-propagated-inputs libc)
298 "kernel-headers"))))
299 (else inputs)))))
300
301 (inputs '())
302
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
309 (variable variable)
310
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"))
317 (_
318 '("include"))))))
319 %gcc-cross-include-paths)))
320 (native-search-paths '())))
321
322 (define* (cross-kernel-headers target
323 #:optional
324 (linux-headers linux-libre-headers)
325 (xgcc (cross-gcc target))
326 (xbinutils (cross-binutils target)))
327 "Return headers depending on TARGET."
328
329 (define xlinux-headers
330 (package (inherit linux-headers)
331 (name (string-append (package-name linux-headers)
332 "-cross-" target))
333 (arguments
334 (substitute-keyword-arguments
335 `(#:implicit-cross-inputs? #f
336 ,@(package-arguments linux-headers))
337 ((#:phases phases)
338 `(alist-replace
339 'build
340 (lambda _
341 (setenv "ARCH" ,(system->linux-architecture target))
342 (format #t "`ARCH' set to `~a' (cross compiling)~%" (getenv "ARCH"))
343
344 (invoke "make" ,(system->defconfig target))
345 (invoke "make" "mrproper" "headers_check"))
346 ,phases))))
347 (native-inputs `(("cross-gcc" ,xgcc)
348 ("cross-binutils" ,xbinutils)
349 ,@(package-native-inputs linux-headers)))))
350
351 (define xgnumach-headers
352 (package (inherit gnumach-headers)
353 (name (string-append (package-name gnumach-headers)
354 "-cross-" target))
355
356 (native-inputs `(("cross-gcc" ,xgcc)
357 ("cross-binutils" ,xbinutils)
358 ,@(package-native-inputs gnumach-headers)))))
359
360 (define xmig
361 (package (inherit mig)
362 (name (string-append "mig-cross"))
363 (arguments
364 `(#:modules ((guix build gnu-build-system)
365 (guix build utils)
366 (srfi srfi-26))
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)
374 #t))))
375 #:configure-flags (list ,(string-append "--target=" target))
376 #:tests? #f))
377
378 (propagated-inputs `(("cross-gnumach-headers" ,xgnumach-headers)))
379 (native-inputs `(("cross-gcc" ,xgcc)
380 ("cross-binutils" ,xbinutils)
381 ,@(package-native-inputs mig)))))
382
383 (define xhurd-headers
384 (package (inherit hurd-headers)
385 (name (string-append (package-name hurd-headers)
386 "-cross-" target))
387
388 (native-inputs `(("cross-gcc" ,xgcc)
389 ("cross-binutils" ,xbinutils)
390 ("cross-mig" ,xmig)
391 ,@(alist-delete "mig"(package-native-inputs hurd-headers))))))
392
393 (define xglibc/hurd-headers
394 (package (inherit glibc/hurd-headers)
395 (name (string-append (package-name glibc/hurd-headers)
396 "-cross-" target))
397
398 (arguments
399 (substitute-keyword-arguments
400 `(#:modules ((guix build gnu-build-system)
401 (guix build utils)
402 (srfi srfi-26))
403 ,@(package-arguments glibc/hurd-headers))
404 ((#:phases phases)
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:"
411 hurd "/include")))
412 (for-each (cut setenv <> cpath)
413 ',%gcc-cross-include-paths)
414 #t)))))))
415
416 (propagated-inputs `(("gnumach-headers" ,xgnumach-headers)
417 ("hurd-headers" ,xhurd-headers)))
418
419 (native-inputs `(("cross-gcc" ,xgcc)
420 ("cross-binutils" ,xbinutils)
421 ("cross-mig" ,xmig)
422 ,@(alist-delete "mig"(package-native-inputs glibc/hurd-headers))))))
423
424 (define xhurd-minimal
425 (package (inherit hurd-minimal)
426 (name (string-append (package-name hurd-minimal)
427 "-cross-" target))
428 (arguments
429 (substitute-keyword-arguments
430 `(#:modules ((guix build gnu-build-system)
431 (guix build utils)
432 (srfi srfi-26))
433 ,@(package-arguments hurd-minimal))
434 ((#:phases phases)
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)
442 #t)))))))
443
444 (inputs `(("cross-glibc-hurd-headers" ,xglibc/hurd-headers)))
445
446 (native-inputs `(("cross-gcc" ,xgcc)
447 ("cross-binutils" ,xbinutils)
448 ("cross-mig" ,xmig)
449 ,@(alist-delete "mig"(package-native-inputs hurd-minimal))))))
450
451 (define xhurd-core-headers
452 (package (inherit hurd-core-headers)
453 (name (string-append (package-name hurd-core-headers)
454 "-cross-" target))
455
456 (inputs `(("gnumach-headers" ,xgnumach-headers)
457 ("hurd-headers" ,xhurd-headers)
458 ("hurd-minimal" ,xhurd-minimal)))
459
460 (native-inputs `(("cross-gcc" ,xgcc)
461 ("cross-binutils" ,xbinutils)
462 ("cross-mig" ,xmig)
463 ,@(package-native-inputs hurd-core-headers)))))
464
465 (match target
466 ((or "i586-pc-gnu" "i586-gnu") xhurd-core-headers)
467 (_ xlinux-headers)))
468
469 (define* (cross-libc target
470 #:optional
471 (libc glibc)
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
479 #:xgcc xgcc
480 #:xbinutils xbinutils)
481 (package
482 (inherit libc)
483 (name (string-append "glibc-cross-" target))
484 (arguments
485 (substitute-keyword-arguments
486 `( ;; Disable stripping (see above.)
487 #:strip-binaries? #f
488
489 ;; This package is used as a target input, but it should not have
490 ;; the usual cross-compilation inputs since that would include
491 ;; itself.
492 #:implicit-cross-inputs? #f
493
494 ;; We need SRFI 26.
495 #:modules ((guix build gnu-build-system)
496 (guix build utils)
497 (srfi srfi-26))
498
499 ,@(package-arguments libc))
500 ((#:configure-flags flags)
501 `(cons ,(string-append "--host=" target)
502 ,(if (hurd-triplet? target)
503 `(cons "--disable-werror" ,flags)
504 flags)))
505 ((#:phases phases)
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
515 #t)))
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"))))
524 #t))
525 ;; TODO: move to glibc in the next rebuild cycle
526 (add-after 'unpack 'patch-libc/hurd
527 (lambda* (#:key inputs #:allow-other-keys)
528 (for-each
529 (lambda (name)
530 (let ((patch (assoc-ref inputs name)))
531 (invoke "patch" "-p1" "--force" "-i" patch)))
532 '("hurd-mach-print.patch"
533 "hurd-gettyent.patch")))))
534 '())))))
535
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)))
539
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.
542 (inputs '())
543
544 (native-inputs `(("cross-gcc" ,xgcc)
545 ("cross-binutils" ,xbinutils)
546 ,@(if (hurd-triplet? target)
547 `(("cross-mig"
548 ,@(assoc-ref (package-native-inputs xheaders)
549 "cross-mig"))
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")))
555 '())
556 ,@(package-inputs libc) ;FIXME: static-bash
557 ,@(package-native-inputs libc))))))
558
559 (define* (native-libc target
560 #:optional
561 (libc glibc)
562 #:key
563 xgcc
564 xbinutils)
565 (if (target-mingw? target)
566 (let ((machine (substring target 0 (string-index target #\-))))
567 (make-mingw-w64 machine
568 #:xgcc xgcc
569 #:xbinutils xbinutils))
570 libc))
571
572 (define* (cross-newlib? target
573 #:optional
574 (libc glibc))
575 (not (eq? (native-libc target libc) libc)))
576
577 \f
578 ;;; Concrete cross tool chains are instantiated like this:
579 ;;
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))))
585 ;;
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.