dc63dc9462ae0c7f4f4eec8eecbf7ed43f39030c
[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 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 Marius Bakke <mbakke@fastmail.com>
8 ;;; Copyright © 2019 Carl Dong <contact@carldong.me>
9 ;;;
10 ;;; This file is part of GNU Guix.
11 ;;;
12 ;;; GNU Guix is free software; you can redistribute it and/or modify it
13 ;;; under the terms of the GNU General Public License as published by
14 ;;; the Free Software Foundation; either version 3 of the License, or (at
15 ;;; your option) any later version.
16 ;;;
17 ;;; GNU Guix is distributed in the hope that it will be useful, but
18 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;;; GNU General Public License for more details.
21 ;;;
22 ;;; You should have received a copy of the GNU General Public License
23 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
24
25 (define-module (gnu packages cross-base)
26 #:use-module (gnu packages)
27 #:use-module (gnu packages gcc)
28 #:use-module (gnu packages base)
29 #:use-module (gnu packages linux)
30 #:use-module (gnu packages hurd)
31 #:use-module (gnu packages mingw)
32 #:use-module (guix packages)
33 #:use-module (guix download)
34 #:use-module (guix utils)
35 #:use-module (guix build-system gnu)
36 #:use-module (guix build-system trivial)
37 #:use-module (srfi srfi-1)
38 #:use-module (srfi srfi-26)
39 #:use-module (ice-9 match)
40 #:use-module (ice-9 regex)
41 #:export (cross-binutils
42 cross-libc
43 cross-gcc
44 cross-newlib?
45 cross-kernel-headers))
46
47 (define-syntax %xgcc
48 ;; GCC package used as the basis for cross-compilation. It doesn't have to
49 ;; be 'gcc' and can be a specific variant such as 'gcc-4.8'.
50 ;;
51 ;; Note: This is a macro so that we do not refer to 'gcc' from the top
52 ;; level, which would lead to circular-dependency issues.
53 (identifier-syntax gcc))
54
55 (define %gcc-include-paths
56 ;; Environment variables for header search paths.
57 ;; Note: See <http://bugs.gnu.org/30756> for why not 'C_INCLUDE_PATH' & co.
58 '("CPATH"))
59
60 (define %gcc-cross-include-paths
61 ;; Search path for target headers when cross-compiling.
62 (map (cut string-append "CROSS_" <>) %gcc-include-paths))
63
64 (define (cross p target)
65 (package (inherit p)
66 (name (string-append (package-name p) "-cross-" target))
67 (arguments
68 (substitute-keyword-arguments (package-arguments p)
69 ((#:configure-flags flags)
70 `(cons ,(string-append "--target=" target)
71 ,flags))))))
72
73 (define (package-with-patch original patch)
74 "Return package ORIGINAL with PATCH applied."
75 (package (inherit original)
76 (source (origin (inherit (package-source original))
77 (patches (list patch))))))
78
79 (define (package-with-extra-patches original patches)
80 "Return package ORIGINAL with all PATCHES appended to its list of patches."
81 (package-with-patch original
82 `(,@(origin-patches (package-source original))
83 ,@patches)))
84
85 (define (cross-binutils target)
86 "Return a cross-Binutils for TARGET."
87 (let ((binutils (package (inherit binutils)
88 (arguments
89 (substitute-keyword-arguments (package-arguments
90 binutils)
91 ((#:configure-flags flags)
92 ;; Build with `--with-sysroot' so that ld honors
93 ;; DT_RUNPATH entries when searching for a needed
94 ;; library. This works because as a side effect
95 ;; `genscripts.sh' sets `USE_LIBPATH=yes', which tells
96 ;; elf32.em to use DT_RUNPATH in its search list.
97 ;; See <http://sourceware.org/ml/binutils/2013-05/msg00312.html>.
98 ;;
99 ;; In theory choosing / as the sysroot could lead ld
100 ;; to pick up native libs instead of target ones. In
101 ;; practice the RUNPATH of target libs only refers to
102 ;; target libs, not native libs, so this is safe.
103 `(cons "--with-sysroot=/" ,flags)))))))
104
105 ;; For Xtensa, apply Qualcomm's patch.
106 (cross (cond ((string-prefix? "xtensa-" target)
107 (package-with-patch binutils
108 (search-patch
109 "ath9k-htc-firmware-binutils.patch")))
110 ((target-mingw? target)
111 (package-with-extra-patches
112 binutils
113 (search-patches
114 "binutils-mingw-w64-specify-timestamp.patch"
115 "binutils-mingw-w64-reproducible-import-libraries.patch")))
116 (else binutils))
117 target)))
118
119 (define (cross-gcc-arguments target xgcc libc)
120 "Return build system arguments for a cross-gcc for TARGET, using XGCC as the
121 base compiler and using LIBC (which may be either a libc package or #f.)"
122 ;; Set the current target system so that 'glibc-dynamic-linker' returns the
123 ;; right name.
124 (parameterize ((%current-target-system target))
125 ;; Disable stripping as this can break binaries, with object files of
126 ;; libgcc.a showing up as having an unknown architecture. See
127 ;; <http://lists.fedoraproject.org/pipermail/arm/2010-August/000663.html>
128 ;; for instance.
129 (let ((args `(#:strip-binaries? #f
130 ,@(package-arguments xgcc))))
131 (substitute-keyword-arguments args
132 ((#:configure-flags flags)
133 `(append (list ,(string-append "--target=" target)
134 ,@(if libc
135 `( ;; Disable libcilkrts because it is not
136 ;; ported to GNU/Hurd.
137 "--disable-libcilkrts"
138 ;; When building a cross compiler, --with-sysroot is
139 ;; implicitly set to "$gcc_tooldir/sys-root". This does
140 ;; not work for us, because --with-native-system-header-dir
141 ;; is searched for relative to this location. Thus, we set
142 ;; it to "/" so GCC is able to find the target libc headers.
143 ;; This is safe because in practice GCC uses CROSS_CPATH
144 ;; & co to separate target and host libraries.
145 "--with-sysroot=/")
146 `( ;; Disable features not needed at this stage.
147 "--disable-shared" "--enable-static"
148 "--enable-languages=c,c++"
149
150 ;; libstdc++ cannot be built at this stage
151 ;; ("Link tests are not allowed after
152 ;; GCC_NO_EXECUTABLES.").
153 "--disable-libstdc++-v3"
154
155 "--disable-threads" ;libgcc, would need libc
156 "--disable-libatomic"
157 "--disable-libmudflap"
158 "--disable-libgomp"
159 "--disable-libmpx"
160 "--disable-libssp"
161 "--disable-libquadmath"
162 "--disable-decimal-float" ;would need libc
163 "--disable-libcilkrts"
164
165 ;; When target is any OS other than 'none' these
166 ;; libraries will fail if there is no libc
167 ;; present. See
168 ;; <https://lists.gnu.org/archive/html/guix-devel/2016-02/msg01311.html>
169 "--disable-libitm"
170 "--disable-libvtv"
171 "--disable-libsanitizer"
172 ))
173
174 ;; For a newlib (non-glibc) target
175 ,@(if (cross-newlib? target)
176 '("--with-newlib")
177 '()))
178
179 ,(if libc
180 flags
181 `(remove (cut string-match "--enable-languages.*" <>)
182 ,flags))))
183 ((#:make-flags flags)
184 (if libc
185 `(let ((libc (assoc-ref %build-inputs "libc")))
186 ;; FLAGS_FOR_TARGET are needed for the target libraries to receive
187 ;; the -Bxxx for the startfiles.
188 (cons (string-append "FLAGS_FOR_TARGET=-B" libc "/lib")
189 ,flags))
190 flags))
191 ((#:phases phases)
192 `(cross-gcc-build-phases
193 ,target
194 (modify-phases ,phases
195 (add-before 'configure 'treat-glibc-as-system-header
196 (lambda* (#:key inputs #:allow-other-keys)
197 (let ((libc (assoc-ref inputs "libc")))
198 (when libc
199 ;; For GCC6 and later, make sure Glibc is treated as a "system
200 ;; header" such that #include_next does the right thing.
201 (for-each (lambda (var)
202 (setenv var (string-append libc "/include")))
203 '("CROSS_C_INCLUDE_PATH" "CROSS_CPLUS_INCLUDE_PATH")))
204 #t))))))))))
205
206 (define (cross-gcc-patches xgcc target)
207 "Return GCC patches needed for XGCC and TARGET."
208 (cond ((string-prefix? "xtensa-" target)
209 ;; Patch by Qualcomm needed to build the ath9k-htc firmware.
210 (search-patches "ath9k-htc-firmware-gcc.patch"))
211 ((target-mingw? target)
212 (append (search-patches "gcc-4.9.3-mingw-gthr-default.patch")
213 (if (version>=? (package-version xgcc) "7.0")
214 (search-patches "gcc-7-cross-mingw.patch")
215 '())))
216 (else '())))
217
218 (define (cross-gcc-snippet target)
219 "Return GCC snippet needed for TARGET."
220 (cond ((target-mingw? target)
221 '(begin
222 (copy-recursively "libstdc++-v3/config/os/mingw32-w64"
223 "libstdc++-v3/config/os/newlib")
224 #t))
225 (else #f)))
226
227 (define* (cross-gcc target
228 #:key
229 (xgcc %xgcc)
230 (xbinutils (cross-binutils target))
231 (libc #f))
232 "Return a cross-compiler for TARGET, where TARGET is a GNU triplet. Use
233 XGCC as the base compiler. Use XBINUTILS as the associated cross-Binutils.
234 If LIBC is false, then build a GCC that does not target a libc; otherwise,
235 target that libc."
236 (package (inherit xgcc)
237 (name (string-append "gcc-cross-"
238 (if libc "" "sans-libc-")
239 target))
240 (source (origin (inherit (package-source xgcc))
241 (patches
242 (append
243 (origin-patches (package-source xgcc))
244 (cons (cond
245 ((version>=? (package-version xgcc) "8.0") (search-patch "gcc-8-cross-environment-variables.patch"))
246 ((version>=? (package-version xgcc) "6.0") (search-patch "gcc-6-cross-environment-variables.patch"))
247 (else (search-patch "gcc-cross-environment-variables.patch")))
248 (cross-gcc-patches xgcc target))))
249 (modules '((guix build utils)))
250 (snippet
251 (cross-gcc-snippet target))))
252
253 ;; For simplicity, use a single output. Otherwise libgcc_s & co. are not
254 ;; found by default, etc.
255 (outputs '("out"))
256
257 (arguments
258 `(#:implicit-inputs? #f
259 #:imported-modules ((gnu build cross-toolchain)
260 ,@%gnu-build-system-modules)
261 #:modules ((guix build gnu-build-system)
262 (guix build utils)
263 (gnu build cross-toolchain)
264 (srfi srfi-1)
265 (srfi srfi-26)
266 (ice-9 regex))
267
268 ,@(cross-gcc-arguments target xgcc libc)))
269
270 (native-inputs
271 `(("ld-wrapper-cross" ,(make-ld-wrapper
272 (string-append "ld-wrapper-" target)
273 #:target (const target)
274 #:binutils xbinutils))
275 ("binutils-cross" ,xbinutils)
276
277 ;; Call it differently so that the builder can check whether the "libc"
278 ;; input is #f.
279 ("libc-native" ,@(assoc-ref (%final-inputs) "libc"))
280
281 ;; Remaining inputs.
282 ,@(let ((inputs (append (package-inputs xgcc)
283 (alist-delete "libc" (%final-inputs)))))
284 (cond
285 ((target-mingw? target)
286 (if libc
287 `(("libc" ,libc)
288 ,@inputs)
289 `(("mingw-source" ,(package-source mingw-w64))
290 ,@inputs)))
291 (libc
292 `(("libc" ,libc)
293 ("libc:static" ,libc "static")
294 ("xkernel-headers" ;the target headers
295 ,@(assoc-ref (package-propagated-inputs libc)
296 "kernel-headers"))
297 ,@inputs))
298 (else inputs)))))
299
300 (inputs '())
301
302 ;; Only search target inputs, not host inputs.
303 (search-paths (cons (search-path-specification
304 (variable "CROSS_LIBRARY_PATH")
305 (files '("lib" "lib64")))
306 (map (lambda (variable)
307 (search-path-specification
308 (variable variable)
309 (files '("include"))))
310 %gcc-cross-include-paths)))
311 (native-search-paths '())))
312
313 (define* (cross-kernel-headers target
314 #:optional
315 (linux-headers linux-libre-headers)
316 (xgcc (cross-gcc target))
317 (xbinutils (cross-binutils target)))
318 "Return headers depending on TARGET."
319
320 (define xlinux-headers
321 (package (inherit linux-headers)
322 (name (string-append (package-name linux-headers)
323 "-cross-" target))
324 (arguments
325 (substitute-keyword-arguments
326 `(#:implicit-cross-inputs? #f
327 ,@(package-arguments linux-headers))
328 ((#:phases phases)
329 `(alist-replace
330 'build
331 (lambda _
332 (setenv "ARCH" ,(system->linux-architecture target))
333 (format #t "`ARCH' set to `~a' (cross compiling)~%" (getenv "ARCH"))
334
335 (invoke "make" ,(system->defconfig target))
336 (invoke "make" "mrproper" "headers_check"))
337 ,phases))))
338 (native-inputs `(("cross-gcc" ,xgcc)
339 ("cross-binutils" ,xbinutils)
340 ,@(package-native-inputs linux-headers)))))
341
342 (define xgnumach-headers
343 (package (inherit gnumach-headers)
344 (name (string-append (package-name gnumach-headers)
345 "-cross-" target))
346
347 (native-inputs `(("cross-gcc" ,xgcc)
348 ("cross-binutils" ,xbinutils)
349 ,@(package-native-inputs gnumach-headers)))))
350
351 (define xmig
352 (package (inherit mig)
353 (name (string-append "mig-cross"))
354 (arguments
355 `(#:modules ((guix build gnu-build-system)
356 (guix build utils)
357 (srfi srfi-26))
358 #:phases (modify-phases %standard-phases
359 (add-before 'configure 'set-cross-headers-path
360 (lambda* (#:key inputs #:allow-other-keys)
361 (let* ((mach (assoc-ref inputs "cross-gnumach-headers"))
362 (cpath (string-append mach "/include")))
363 (for-each (cut setenv <> cpath)
364 ',%gcc-cross-include-paths)
365 #t))))
366 #:configure-flags (list ,(string-append "--target=" target))
367 ,@(package-arguments mig)))
368
369 (propagated-inputs `(("cross-gnumach-headers" ,xgnumach-headers)))
370 (native-inputs `(("cross-gcc" ,xgcc)
371 ("cross-binutils" ,xbinutils)
372 ,@(package-native-inputs mig)))))
373
374 (define xhurd-headers
375 (package (inherit hurd-headers)
376 (name (string-append (package-name hurd-headers)
377 "-cross-" target))
378
379 (native-inputs `(("cross-gcc" ,xgcc)
380 ("cross-binutils" ,xbinutils)
381 ("cross-mig" ,xmig)
382 ,@(alist-delete "mig"(package-native-inputs hurd-headers))))))
383
384 (define xglibc/hurd-headers
385 (package (inherit glibc/hurd-headers)
386 (name (string-append (package-name glibc/hurd-headers)
387 "-cross-" target))
388
389 (arguments
390 (substitute-keyword-arguments
391 `(#:modules ((guix build gnu-build-system)
392 (guix build utils)
393 (srfi srfi-26))
394 ,@(package-arguments glibc/hurd-headers))
395 ((#:phases phases)
396 `(modify-phases ,phases
397 (add-after 'unpack 'set-cross-headers-path
398 (lambda* (#:key inputs #:allow-other-keys)
399 (let* ((mach (assoc-ref inputs "gnumach-headers"))
400 (hurd (assoc-ref inputs "hurd-headers"))
401 (cpath (string-append mach "/include:"
402 hurd "/include")))
403 (for-each (cut setenv <> cpath)
404 ',%gcc-cross-include-paths)
405 #t)))))))
406
407 (propagated-inputs `(("gnumach-headers" ,xgnumach-headers)
408 ("hurd-headers" ,xhurd-headers)))
409
410 (native-inputs `(("cross-gcc" ,xgcc)
411 ("cross-binutils" ,xbinutils)
412 ("cross-mig" ,xmig)
413 ,@(alist-delete "mig"(package-native-inputs glibc/hurd-headers))))))
414
415 (define xhurd-minimal
416 (package (inherit hurd-minimal)
417 (name (string-append (package-name hurd-minimal)
418 "-cross-" target))
419 (arguments
420 (substitute-keyword-arguments
421 `(#:modules ((guix build gnu-build-system)
422 (guix build utils)
423 (srfi srfi-26))
424 ,@(package-arguments hurd-minimal))
425 ((#:phases phases)
426 `(modify-phases ,phases
427 (add-before 'configure 'set-cross-headers-path
428 (lambda* (#:key inputs #:allow-other-keys)
429 (let* ((glibc-headers (assoc-ref inputs "cross-glibc-hurd-headers"))
430 (cpath (string-append glibc-headers "/include")))
431 (for-each (cut setenv <> cpath)
432 ',%gcc-cross-include-paths)
433 #t)))))))
434
435 (inputs `(("cross-glibc-hurd-headers" ,xglibc/hurd-headers)))
436
437 (native-inputs `(("cross-gcc" ,xgcc)
438 ("cross-binutils" ,xbinutils)
439 ("cross-mig" ,xmig)
440 ,@(alist-delete "mig"(package-native-inputs hurd-minimal))))))
441
442 (define xhurd-core-headers
443 (package (inherit hurd-core-headers)
444 (name (string-append (package-name hurd-core-headers)
445 "-cross-" target))
446
447 (inputs `(("gnumach-headers" ,xgnumach-headers)
448 ("hurd-headers" ,xhurd-headers)
449 ("hurd-minimal" ,xhurd-minimal)))
450
451 (native-inputs `(("cross-gcc" ,xgcc)
452 ("cross-binutils" ,xbinutils)
453 ("cross-mig" ,xmig)
454 ,@(package-native-inputs hurd-core-headers)))))
455
456 (match target
457 ((or "i586-pc-gnu" "i586-gnu") xhurd-core-headers)
458 (_ xlinux-headers)))
459
460 (define* (cross-libc target
461 #:optional
462 (libc glibc)
463 (xgcc (cross-gcc target))
464 (xbinutils (cross-binutils target))
465 (xheaders (cross-kernel-headers target)))
466 "Return LIBC cross-built for TARGET, a GNU triplet. Use XGCC and XBINUTILS
467 and the cross tool chain."
468 (if (cross-newlib? target libc)
469 (native-libc target libc
470 #:xgcc xgcc
471 #:xbinutils xbinutils)
472 (let ((libc libc))
473 (package (inherit libc)
474 (name (string-append "glibc-cross-" target))
475 (arguments
476 (substitute-keyword-arguments
477 `(;; Disable stripping (see above.)
478 #:strip-binaries? #f
479
480 ;; This package is used as a target input, but it should not have
481 ;; the usual cross-compilation inputs since that would include
482 ;; itself.
483 #:implicit-cross-inputs? #f
484
485 ;; We need SRFI 26.
486 #:modules ((guix build gnu-build-system)
487 (guix build utils)
488 (srfi srfi-26))
489
490 ,@(package-arguments libc))
491 ((#:configure-flags flags)
492 `(cons ,(string-append "--host=" target)
493 ,(if (hurd-triplet? target)
494 `(cons "--disable-werror" ,flags)
495 flags)))
496 ((#:phases phases)
497 `(modify-phases ,phases
498 (add-before 'configure 'set-cross-kernel-headers-path
499 (lambda* (#:key inputs #:allow-other-keys)
500 (let* ((kernel (assoc-ref inputs "kernel-headers"))
501 (cpath (string-append kernel "/include")))
502 (for-each (cut setenv <> cpath)
503 ',%gcc-cross-include-paths)
504 (setenv "CROSS_LIBRARY_PATH"
505 (string-append kernel "/lib")) ; for Hurd's libihash
506 #t)))
507 ,@(if (hurd-triplet? target)
508 '((add-after 'install 'augment-libc.so
509 (lambda* (#:key outputs #:allow-other-keys)
510 (let* ((out (assoc-ref outputs "out")))
511 (substitute* (string-append out "/lib/libc.so")
512 (("/[^ ]+/lib/libc.so.0.3")
513 (string-append out "/lib/libc.so.0.3"
514 " libmachuser.so libhurduser.so"))))
515 #t)))
516 '())))))
517
518 ;; Shadow the native "kernel-headers" because glibc's recipe expects the
519 ;; "kernel-headers" input to point to the right thing.
520 (propagated-inputs `(("kernel-headers" ,xheaders)))
521
522 ;; FIXME: 'static-bash' should really be an input, not a native input, but
523 ;; to do that will require building an intermediate cross libc.
524 (inputs '())
525
526 (native-inputs `(("cross-gcc" ,xgcc)
527 ("cross-binutils" ,xbinutils)
528 ,@(if (hurd-triplet? target)
529 `(("cross-mig"
530 ,@(assoc-ref (package-native-inputs xheaders)
531 "cross-mig")))
532 '())
533 ,@(package-inputs libc) ;FIXME: static-bash
534 ,@(package-native-inputs libc)))))))
535
536 (define* (native-libc target
537 #:optional
538 (libc glibc)
539 #:key
540 xgcc
541 xbinutils)
542 (if (target-mingw? target)
543 (let ((machine (substring target 0 (string-index target #\-))))
544 (make-mingw-w64 machine
545 #:xgcc xgcc
546 #:xbinutils xbinutils))
547 libc))
548
549 (define* (cross-newlib? target
550 #:optional
551 (libc glibc))
552 (not (eq? (native-libc target libc) libc)))
553
554 \f
555 ;;; Concrete cross tool chains are instantiated like this:
556 ;;
557 ;; (define-public xgcc-armhf
558 ;; (let ((triplet "arm-linux-gnueabihf"))
559 ;; (cross-gcc triplet
560 ;; #:xbinutils (cross-binutils triplet)
561 ;; #:libc (cross-libc triplet))))
562 ;;
563 ;;; We don't do that here because we'd be referring to bindings from (gnu
564 ;;; packages gcc) from the top level, which doesn't play well with circular
565 ;;; dependencies among modules.