gnu: sra-tools: Update to 2.9.6.
[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 Jan Nieuwenhuizen <janneke@gnu.org>
5 ;;; Copyright © 2016 Manolis Fragkiskos Ragkousis <manolis837@gmail.com>
6 ;;; Copyright © 2018 Tobias Geerinckx-Rice <me@tobias.gr>
7 ;;;
8 ;;; This file is part of GNU Guix.
9 ;;;
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.
14 ;;;
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.
19 ;;;
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/>.
22
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
40 cross-libc
41 cross-gcc
42 cross-newlib?
43 cross-kernel-headers))
44
45 (define-syntax %xgcc
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'.
48 ;;
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))
52
53 (define %gcc-include-paths
54 ;; Environment variables for header search paths.
55 ;; Note: See <http://bugs.gnu.org/22186> for why not 'CPATH'.
56 '("C_INCLUDE_PATH"
57 "CPLUS_INCLUDE_PATH"
58 "OBJC_INCLUDE_PATH"
59 "OBJCPLUS_INCLUDE_PATH"))
60
61 (define %gcc-cross-include-paths
62 ;; Search path for target headers when cross-compiling.
63 (map (cut string-append "CROSS_" <>) %gcc-include-paths))
64
65 (define (cross p target)
66 (package (inherit p)
67 (name (string-append (package-name p) "-cross-" target))
68 (arguments
69 (substitute-keyword-arguments (package-arguments p)
70 ((#:configure-flags flags)
71 `(cons ,(string-append "--target=" target)
72 ,flags))))))
73
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))))))
79
80 (define (cross-binutils target)
81 "Return a cross-Binutils for TARGET."
82 (let ((binutils (package (inherit binutils)
83 (arguments
84 (substitute-keyword-arguments (package-arguments
85 binutils)
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>.
93 ;;
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)))))))
99
100 ;; For Xtensa, apply Qualcomm's patch.
101 (cross (if (string-prefix? "xtensa-" target)
102 (package-with-patch binutils
103 (search-patch
104 "ath9k-htc-firmware-binutils.patch"))
105 binutils)
106 target)))
107
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
112 ;; right name.
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>
117 ;; for instance.
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)
123 ,@(if libc
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++"
130
131 ;; libstdc++ cannot be built at this stage
132 ;; ("Link tests are not allowed after
133 ;; GCC_NO_EXECUTABLES.").
134 "--disable-libstdc++-v3"
135
136 "--disable-threads" ;libgcc, would need libc
137 "--disable-libatomic"
138 "--disable-libmudflap"
139 "--disable-libgomp"
140 "--disable-libssp"
141 "--disable-libquadmath"
142 "--disable-decimal-float" ;would need libc
143 "--disable-libcilkrts"
144
145 ;; When target is any OS other than 'none' these
146 ;; libraries will fail if there is no libc
147 ;; present. See
148 ;; <https://lists.gnu.org/archive/html/guix-devel/2016-02/msg01311.html>
149 "--disable-libitm"
150 "--disable-libvtv"
151 "--disable-libsanitizer"
152 ))
153
154 ;; For a newlib (non-glibc) target
155 ,@(if (cross-newlib? target)
156 '("--with-newlib")
157 '()))
158
159 ,(if libc
160 flags
161 `(remove (cut string-match "--enable-languages.*" <>)
162 ,flags))))
163 ((#:make-flags flags)
164 (if libc
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")
169 ,flags))
170 flags))
171 ((#:phases phases)
172 `(cross-gcc-build-phases ,target ,phases))))))
173
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"))
181 (else '())))
182
183 (define (cross-gcc-snippet target)
184 "Return GCC snippet needed for TARGET."
185 (cond ((target-mingw? target)
186 '(begin
187 (copy-recursively "libstdc++-v3/config/os/mingw32-w64"
188 "libstdc++-v3/config/os/newlib")
189 #t))
190 (else #f)))
191
192 (define* (cross-gcc target
193 #:key
194 (xgcc %xgcc)
195 (xbinutils (cross-binutils target))
196 (libc #f))
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,
200 target that libc."
201 (package (inherit xgcc)
202 (name (string-append "gcc-cross-"
203 (if libc "" "sans-libc-")
204 target))
205 (source (origin (inherit (package-source xgcc))
206 (patches
207 (append
208 (origin-patches (package-source xgcc))
209 (cons (cond
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)))
215 (snippet
216 (cross-gcc-snippet target))))
217
218 ;; For simplicity, use a single output. Otherwise libgcc_s & co. are not
219 ;; found by default, etc.
220 (outputs '("out"))
221
222 (arguments
223 `(#:implicit-inputs? #f
224 #:imported-modules ((gnu build cross-toolchain)
225 ,@%gnu-build-system-modules)
226 #:modules ((guix build gnu-build-system)
227 (guix build utils)
228 (gnu build cross-toolchain)
229 (srfi srfi-1)
230 (srfi srfi-26)
231 (ice-9 regex))
232
233 ,@(cross-gcc-arguments target xgcc libc)))
234
235 (native-inputs
236 `(("ld-wrapper-cross" ,(make-ld-wrapper
237 (string-append "ld-wrapper-" target)
238 #:target (const target)
239 #:binutils xbinutils))
240 ("binutils-cross" ,xbinutils)
241
242 ;; Call it differently so that the builder can check whether the "libc"
243 ;; input is #f.
244 ("libc-native" ,@(assoc-ref (%final-inputs) "libc"))
245
246 ;; Remaining inputs.
247 ,@(let ((inputs (append (package-inputs xgcc)
248 (alist-delete "libc" (%final-inputs)))))
249 (cond
250 ((target-mingw? target)
251 (if libc
252 `(("libc" ,mingw-w64)
253 ,@inputs)
254 `(("mingw-source" ,(package-source mingw-w64))
255 ,@inputs)))
256 (libc
257 `(("libc" ,libc)
258 ("libc:static" ,libc "static")
259 ("xkernel-headers" ;the target headers
260 ,@(assoc-ref (package-propagated-inputs libc)
261 "kernel-headers"))
262 ,@inputs))
263 (else inputs)))))
264
265 (inputs '())
266
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
273 (variable variable)
274 (files '("include"))))
275 %gcc-cross-include-paths)))
276 (native-search-paths '())))
277
278 (define* (cross-kernel-headers target
279 #:optional
280 (linux-headers linux-libre-headers)
281 (xgcc (cross-gcc target))
282 (xbinutils (cross-binutils target)))
283 "Return headers depending on TARGET."
284
285 (define xlinux-headers
286 (package (inherit linux-headers)
287 (name (string-append (package-name linux-headers)
288 "-cross-" target))
289 (arguments
290 (substitute-keyword-arguments
291 `(#:implicit-cross-inputs? #f
292 ,@(package-arguments linux-headers))
293 ((#:phases phases)
294 `(alist-replace
295 'build
296 (lambda _
297 (setenv "ARCH" ,(system->linux-architecture target))
298 (format #t "`ARCH' set to `~a' (cross compiling)~%" (getenv "ARCH"))
299
300 (invoke "make" ,(system->defconfig target))
301 (invoke "make" "mrproper" "headers_check"))
302 ,phases))))
303 (native-inputs `(("cross-gcc" ,xgcc)
304 ("cross-binutils" ,xbinutils)
305 ,@(package-native-inputs linux-headers)))))
306
307 (define xgnumach-headers
308 (package (inherit gnumach-headers)
309 (name (string-append (package-name gnumach-headers)
310 "-cross-" target))
311
312 (native-inputs `(("cross-gcc" ,xgcc)
313 ("cross-binutils" ,xbinutils)
314 ,@(package-native-inputs gnumach-headers)))))
315
316 (define xmig
317 (package (inherit mig)
318 (name (string-append "mig-cross"))
319 (arguments
320 `(#:modules ((guix build gnu-build-system)
321 (guix build utils)
322 (srfi srfi-26))
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)
330 #t))))
331 #:configure-flags (list ,(string-append "--target=" target))
332 ,@(package-arguments mig)))
333
334 (propagated-inputs `(("cross-gnumach-headers" ,xgnumach-headers)))
335 (native-inputs `(("cross-gcc" ,xgcc)
336 ("cross-binutils" ,xbinutils)
337 ,@(package-native-inputs mig)))))
338
339 (define xhurd-headers
340 (package (inherit hurd-headers)
341 (name (string-append (package-name hurd-headers)
342 "-cross-" target))
343
344 (native-inputs `(("cross-gcc" ,xgcc)
345 ("cross-binutils" ,xbinutils)
346 ("cross-mig" ,xmig)
347 ,@(alist-delete "mig"(package-native-inputs hurd-headers))))))
348
349 (define xglibc/hurd-headers
350 (package (inherit glibc/hurd-headers)
351 (name (string-append (package-name glibc/hurd-headers)
352 "-cross-" target))
353
354 (arguments
355 (substitute-keyword-arguments
356 `(#:modules ((guix build gnu-build-system)
357 (guix build utils)
358 (srfi srfi-26))
359 ,@(package-arguments glibc/hurd-headers))
360 ((#:phases phases)
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:"
367 hurd "/include")))
368 (for-each (cut setenv <> cpath)
369 ',%gcc-cross-include-paths)
370 #t)))))))
371
372 (propagated-inputs `(("gnumach-headers" ,xgnumach-headers)
373 ("hurd-headers" ,xhurd-headers)))
374
375 (native-inputs `(("cross-gcc" ,xgcc)
376 ("cross-binutils" ,xbinutils)
377 ("cross-mig" ,xmig)
378 ,@(alist-delete "mig"(package-native-inputs glibc/hurd-headers))))))
379
380 (define xhurd-minimal
381 (package (inherit hurd-minimal)
382 (name (string-append (package-name hurd-minimal)
383 "-cross-" target))
384 (arguments
385 (substitute-keyword-arguments
386 `(#:modules ((guix build gnu-build-system)
387 (guix build utils)
388 (srfi srfi-26))
389 ,@(package-arguments hurd-minimal))
390 ((#:phases phases)
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)
398 #t)))))))
399
400 (inputs `(("cross-glibc-hurd-headers" ,xglibc/hurd-headers)))
401
402 (native-inputs `(("cross-gcc" ,xgcc)
403 ("cross-binutils" ,xbinutils)
404 ("cross-mig" ,xmig)
405 ,@(alist-delete "mig"(package-native-inputs hurd-minimal))))))
406
407 (define xhurd-core-headers
408 (package (inherit hurd-core-headers)
409 (name (string-append (package-name hurd-core-headers)
410 "-cross-" target))
411
412 (inputs `(("gnumach-headers" ,xgnumach-headers)
413 ("hurd-headers" ,xhurd-headers)
414 ("hurd-minimal" ,xhurd-minimal)))
415
416 (native-inputs `(("cross-gcc" ,xgcc)
417 ("cross-binutils" ,xbinutils)
418 ("cross-mig" ,xmig)
419 ,@(package-native-inputs hurd-core-headers)))))
420
421 (match target
422 ((or "i586-pc-gnu" "i586-gnu") xhurd-core-headers)
423 (_ xlinux-headers)))
424
425 (define* (cross-libc target
426 #:optional
427 (libc glibc)
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)
435 (let ((libc libc))
436 (package (inherit libc)
437 (name (string-append "glibc-cross-" target))
438 (arguments
439 (substitute-keyword-arguments
440 `(;; Disable stripping (see above.)
441 #:strip-binaries? #f
442
443 ;; This package is used as a target input, but it should not have
444 ;; the usual cross-compilation inputs since that would include
445 ;; itself.
446 #:implicit-cross-inputs? #f
447
448 ;; We need SRFI 26.
449 #:modules ((guix build gnu-build-system)
450 (guix build utils)
451 (srfi srfi-26))
452
453 ,@(package-arguments libc))
454 ((#:configure-flags flags)
455 `(cons ,(string-append "--host=" target)
456 ,(if (hurd-triplet? target)
457 `(cons "--disable-werror" ,flags)
458 flags)))
459 ((#:phases phases)
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
469 #:allow-other-keys)
470 ;; TODO: Move this to 'patches' field.
471 (let ((patch (or (assoc-ref native-inputs
472 "hurd-magic-pid-patch")
473 (assoc-ref inputs
474 "hurd-magic-pid-patch"))))
475 (invoke "patch" "-p1" "--force" "--input"
476 patch)))))
477 '())
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
486 #t)))))))
487
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)))
491
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.
494 (inputs '())
495
496 (native-inputs `(("cross-gcc" ,xgcc)
497 ("cross-binutils" ,xbinutils)
498 ,@(if (hurd-triplet? target)
499 `(("cross-mig"
500 ,@(assoc-ref (package-native-inputs xheaders)
501 "cross-mig"))
502 ("hurd-magic-pid-patch"
503 ,(search-patch "glibc-hurd-magic-pid.patch")))
504 '())
505 ,@(package-inputs libc) ;FIXME: static-bash
506 ,@(package-native-inputs libc)))))))
507
508 (define* (native-libc target
509 #:optional
510 (libc glibc))
511 (if (target-mingw? target)
512 mingw-w64
513 libc))
514
515 (define* (cross-newlib? target
516 #:optional
517 (libc glibc))
518 (not (eq? (native-libc target libc) libc)))
519
520 \f
521 ;;; Concrete cross tool chains are instantiated like this:
522 ;;
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))))
528 ;;
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.