gnu: All snippets report errors using exceptions, else return #t.
[jackhill/guix/guix.git] / gnu / packages / cross-base.scm
CommitLineData
827d2891 1;;; GNU Guix --- Functional package management for GNU
be99170d 2;;; Copyright © 2013, 2014, 2015, 2016, 2017 Ludovic Courtès <ludo@gnu.org>
1e5005e2 3;;; Copyright © 2014, 2015, 2018 Mark H Weaver <mhw@netris.org>
efc4eb14 4;;; Copyright © 2016 Jan Nieuwenhuizen <janneke@gnu.org>
2d558e31 5;;; Copyright © 2016 Manolis Fragkiskos Ragkousis <manolis837@gmail.com>
273cab96 6;;; Copyright © 2018 Tobias Geerinckx-Rice <me@tobias.gr>
827d2891
LC
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)
827d2891 24 #:use-module (gnu packages)
f594028a 25 #:use-module (gnu packages gcc)
827d2891
LC
26 #:use-module (gnu packages base)
27 #:use-module (gnu packages linux)
2d558e31 28 #:use-module (gnu packages hurd)
cba36e64 29 #:use-module (gnu packages mingw)
827d2891
LC
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)
264218a4 37 #:use-module (ice-9 match)
2d558e31 38 #:use-module (ice-9 regex)
264218a4
LC
39 #:export (cross-binutils
40 cross-libc
cba36e64
JN
41 cross-gcc
42 cross-newlib?))
827d2891 43
be99170d 44(define-syntax %xgcc
c92f1c0a
LC
45 ;; GCC package used as the basis for cross-compilation. It doesn't have to
46 ;; be 'gcc' and can be a specific variant such as 'gcc-4.8'.
be99170d
LC
47 ;;
48 ;; Note: This is a macro so that we do not refer to 'gcc' from the top
49 ;; level, which would lead to circular-dependency issues.
50 (identifier-syntax gcc))
c92f1c0a 51
cba36e64
JN
52(define %gcc-include-paths
53 ;; Environment variables for header search paths.
54 ;; Note: See <http://bugs.gnu.org/22186> for why not 'CPATH'.
55 '("C_INCLUDE_PATH"
56 "CPLUS_INCLUDE_PATH"
57 "OBJC_INCLUDE_PATH"
58 "OBJCPLUS_INCLUDE_PATH"))
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
827d2891
LC
64(define (cross p target)
65 (package (inherit p)
827d2891
LC
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
1306b0b0
LC
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
47e74d6e
LC
79(define (cross-binutils target)
80 "Return a cross-Binutils for TARGET."
81 (let ((binutils (package (inherit binutils)
82 (arguments
83 (substitute-keyword-arguments (package-arguments
84 binutils)
85 ((#:configure-flags flags)
86 ;; Build with `--with-sysroot' so that ld honors
87 ;; DT_RUNPATH entries when searching for a needed
88 ;; library. This works because as a side effect
89 ;; `genscripts.sh' sets `USE_LIBPATH=yes', which tells
90 ;; elf32.em to use DT_RUNPATH in its search list.
c8fa51f2
LC
91 ;; See <http://sourceware.org/ml/binutils/2013-05/msg00312.html>.
92 ;;
93 ;; In theory choosing / as the sysroot could lead ld
94 ;; to pick up native libs instead of target ones. In
95 ;; practice the RUNPATH of target libs only refers to
96 ;; target libs, not native libs, so this is safe.
97 `(cons "--with-sysroot=/" ,flags)))))))
1306b0b0
LC
98
99 ;; For Xtensa, apply Qualcomm's patch.
100 (cross (if (string-prefix? "xtensa-" target)
101 (package-with-patch binutils
102 (search-patch
103 "ath9k-htc-firmware-binutils.patch"))
104 binutils)
105 target)))
827d2891 106
7b3318e3
RW
107(define (cross-gcc-arguments target xgcc libc)
108 "Return build system arguments for a cross-gcc for TARGET, using XGCC as the
109base compiler and using LIBC (which may be either a libc package or #f.)"
b4469d8c
LC
110 ;; Set the current target system so that 'glibc-dynamic-linker' returns the
111 ;; right name.
112 (parameterize ((%current-target-system target))
2a1552c6
LC
113 ;; Disable stripping as this can break binaries, with object files of
114 ;; libgcc.a showing up as having an unknown architecture. See
115 ;; <http://lists.fedoraproject.org/pipermail/arm/2010-August/000663.html>
116 ;; for instance.
117 (let ((args `(#:strip-binaries? #f
7b3318e3 118 ,@(package-arguments xgcc))))
2a1552c6
LC
119 (substitute-keyword-arguments args
120 ((#:configure-flags flags)
121 `(append (list ,(string-append "--target=" target)
122 ,@(if libc
8fd857f5 123 `( ;; Disable libcilkrts because it is not
3009a9e4 124 ;; ported to GNU/Hurd.
8fd857f5 125 "--disable-libcilkrts")
2a1552c6
LC
126 `( ;; Disable features not needed at this stage.
127 "--disable-shared" "--enable-static"
ca7ef4d4 128 "--enable-languages=c,c++"
cdb4b4b3 129
ca7ef4d4
RW
130 ;; libstdc++ cannot be built at this stage
131 ;; ("Link tests are not allowed after
132 ;; GCC_NO_EXECUTABLES.").
133 "--disable-libstdc++-v3"
cdb4b4b3 134
2a1552c6
LC
135 "--disable-threads" ;libgcc, would need libc
136 "--disable-libatomic"
137 "--disable-libmudflap"
138 "--disable-libgomp"
139 "--disable-libssp"
140 "--disable-libquadmath"
141 "--disable-decimal-float" ;would need libc
8fd857f5 142 "--disable-libcilkrts"
9a745d76
MR
143
144 ;; When target is any OS other than 'none' these
145 ;; libraries will fail if there is no libc
146 ;; present. See
147 ;; <https://lists.gnu.org/archive/html/guix-devel/2016-02/msg01311.html>
148 "--disable-libitm"
149 "--disable-libvtv"
150 "--disable-libsanitizer"
cba36e64
JN
151 ))
152
153 ;; For a newlib (non-glibc) target
154 ,@(if (cross-newlib? target)
155 '("--with-newlib")
156 '()))
cdb4b4b3 157
2a1552c6
LC
158 ,(if libc
159 flags
160 `(remove (cut string-match "--enable-languages.*" <>)
161 ,flags))))
162 ((#:make-flags flags)
163 (if libc
164 `(let ((libc (assoc-ref %build-inputs "libc")))
165 ;; FLAGS_FOR_TARGET are needed for the target libraries to receive
166 ;; the -Bxxx for the startfiles.
167 (cons (string-append "FLAGS_FOR_TARGET=-B" libc "/lib")
168 ,flags))
169 flags))
170 ((#:phases phases)
3593e5d5 171 `(cross-gcc-build-phases ,target ,phases))))))
cdb4b4b3 172
1306b0b0
LC
173(define (cross-gcc-patches target)
174 "Return GCC patches needed for TARGET."
175 (cond ((string-prefix? "xtensa-" target)
176 ;; Patch by Qualcomm needed to build the ath9k-htc firmware.
fc1adab1 177 (search-patches "ath9k-htc-firmware-gcc.patch"))
cba36e64
JN
178 ((target-mingw? target)
179 (search-patches "gcc-4.9.3-mingw-gthr-default.patch"))
1306b0b0
LC
180 (else '())))
181
cba36e64
JN
182(define (cross-gcc-snippet target)
183 "Return GCC snippet needed for TARGET."
184 (cond ((target-mingw? target)
6cbee49d
MW
185 '(begin
186 (copy-recursively "libstdc++-v3/config/os/mingw32-w64"
187 "libstdc++-v3/config/os/newlib")
188 #t))
cba36e64
JN
189 (else #f)))
190
827d2891 191(define* (cross-gcc target
7b3318e3
RW
192 #:key
193 (xgcc %xgcc)
194 (xbinutils (cross-binutils target))
195 (libc #f))
827d2891 196 "Return a cross-compiler for TARGET, where TARGET is a GNU triplet. Use
7b3318e3
RW
197XGCC as the base compiler. Use XBINUTILS as the associated cross-Binutils.
198If LIBC is false, then build a GCC that does not target a libc; otherwise,
199target that libc."
200 (package (inherit xgcc)
827d2891
LC
201 (name (string-append "gcc-cross-"
202 (if libc "" "sans-libc-")
203 target))
7b3318e3 204 (source (origin (inherit (package-source xgcc))
01eafd38 205 (patches
1421afa9 206 (append
7b3318e3 207 (origin-patches (package-source xgcc))
e32d3024
DM
208 (cons (if (version>=? (package-version xgcc) "6.0")
209 (search-patch "gcc-6-cross-environment-variables.patch")
210 (search-patch "gcc-cross-environment-variables.patch"))
cba36e64
JN
211 (cross-gcc-patches target))))
212 (modules '((guix build utils)))
213 (snippet
214 (cross-gcc-snippet target))))
a49c57a7
LC
215
216 ;; For simplicity, use a single output. Otherwise libgcc_s & co. are not
217 ;; found by default, etc.
218 (outputs '("out"))
219
827d2891
LC
220 (arguments
221 `(#:implicit-inputs? #f
3593e5d5
LC
222 #:imported-modules ((gnu build cross-toolchain)
223 ,@%gnu-build-system-modules)
827d2891
LC
224 #:modules ((guix build gnu-build-system)
225 (guix build utils)
3593e5d5 226 (gnu build cross-toolchain)
827d2891 227 (srfi srfi-1)
3593e5d5
LC
228 (srfi srfi-26)
229 (ice-9 regex))
827d2891 230
7b3318e3 231 ,@(cross-gcc-arguments target xgcc libc)))
0de71c23
LC
232
233 (native-inputs
4a740d0f
LC
234 `(("ld-wrapper-cross" ,(make-ld-wrapper
235 (string-append "ld-wrapper-" target)
5bde4503 236 #:target (const target)
4a740d0f
LC
237 #:binutils xbinutils))
238 ("binutils-cross" ,xbinutils)
827d2891
LC
239
240 ;; Call it differently so that the builder can check whether the "libc"
241 ;; input is #f.
fb77c614 242 ("libc-native" ,@(assoc-ref (%final-inputs) "libc"))
827d2891
LC
243
244 ;; Remaining inputs.
7b3318e3 245 ,@(let ((inputs (append (package-inputs xgcc)
fb77c614 246 (alist-delete "libc" (%final-inputs)))))
cba36e64
JN
247 (cond
248 ((target-mingw? target)
249 (if libc
250 `(("libc" ,mingw-w64)
251 ,@inputs)
252 `(("mingw-source" ,(package-source mingw-w64))
253 ,@inputs)))
254 (libc
255 `(("libc" ,libc)
6dff905e 256 ("libc:static" ,libc "static")
cba36e64
JN
257 ("xkernel-headers" ;the target headers
258 ,@(assoc-ref (package-propagated-inputs libc)
259 "kernel-headers"))
260 ,@inputs))
261 (else inputs)))))
17bb886f 262
0de71c23
LC
263 (inputs '())
264
17bb886f 265 ;; Only search target inputs, not host inputs.
cba36e64
JN
266 (search-paths (cons (search-path-specification
267 (variable "CROSS_LIBRARY_PATH")
268 (files '("lib" "lib64")))
269 (map (lambda (variable)
270 (search-path-specification
271 (variable variable)
272 (files '("include"))))
273 %gcc-cross-include-paths)))
17bb886f 274 (native-search-paths '())))
827d2891 275
2d558e31
MR
276(define* (cross-kernel-headers target
277 #:optional
278 (xgcc (cross-gcc target))
279 (xbinutils (cross-binutils target)))
280 "Return headers depending on TARGET."
281
827d2891
LC
282 (define xlinux-headers
283 (package (inherit linux-libre-headers)
284 (name (string-append (package-name linux-libre-headers)
285 "-cross-" target))
286 (arguments
0d5a559f
LC
287 (substitute-keyword-arguments
288 `(#:implicit-cross-inputs? #f
289 ,@(package-arguments linux-libre-headers))
827d2891
LC
290 ((#:phases phases)
291 `(alist-replace
292 'build
293 (lambda _
294 (setenv "ARCH" ,(system->linux-architecture target))
295 (format #t "`ARCH' set to `~a' (cross compiling)~%" (getenv "ARCH"))
296
1e5005e2
MW
297 (invoke "make" ,(system->defconfig target))
298 (invoke "make" "mrproper" "headers_check"))
827d2891 299 ,phases))))
0de71c23
LC
300 (native-inputs `(("cross-gcc" ,xgcc)
301 ("cross-binutils" ,xbinutils)
302 ,@(package-native-inputs linux-libre-headers)))))
827d2891 303
2d558e31
MR
304 (define xgnumach-headers
305 (package (inherit gnumach-headers)
306 (name (string-append (package-name gnumach-headers)
307 "-cross-" target))
308
309 (native-inputs `(("cross-gcc" ,xgcc)
310 ("cross-binutils" ,xbinutils)
311 ,@(package-native-inputs gnumach-headers)))))
312
313 (define xmig
314 (package (inherit mig)
315 (name (string-append "mig-cross"))
316 (arguments
317 `(#:modules ((guix build gnu-build-system)
318 (guix build utils)
319 (srfi srfi-26))
273cab96
TGR
320 #:phases (modify-phases %standard-phases
321 (add-before 'configure 'set-cross-headers-path
322 (lambda* (#:key inputs #:allow-other-keys)
323 (let* ((mach (assoc-ref inputs "cross-gnumach-headers"))
324 (cpath (string-append mach "/include")))
325 (for-each (cut setenv <> cpath)
326 ',%gcc-cross-include-paths)
327 #t))))
2d558e31
MR
328 #:configure-flags (list ,(string-append "--target=" target))
329 ,@(package-arguments mig)))
330
331 (propagated-inputs `(("cross-gnumach-headers" ,xgnumach-headers)))
332 (native-inputs `(("cross-gcc" ,xgcc)
333 ("cross-binutils" ,xbinutils)
334 ,@(package-native-inputs mig)))))
335
336 (define xhurd-headers
337 (package (inherit hurd-headers)
338 (name (string-append (package-name hurd-headers)
339 "-cross-" target))
340
2d558e31
MR
341 (native-inputs `(("cross-gcc" ,xgcc)
342 ("cross-binutils" ,xbinutils)
343 ("cross-mig" ,xmig)
344 ,@(alist-delete "mig"(package-native-inputs hurd-headers))))))
345
346 (define xglibc/hurd-headers
347 (package (inherit glibc/hurd-headers)
348 (name (string-append (package-name glibc/hurd-headers)
349 "-cross-" target))
350
351 (arguments
352 (substitute-keyword-arguments
353 `(#:modules ((guix build gnu-build-system)
354 (guix build utils)
355 (srfi srfi-26))
356 ,@(package-arguments glibc/hurd-headers))
357 ((#:phases phases)
273cab96
TGR
358 `(modify-phases ,phases
359 (add-before 'pre-configure 'set-cross-headers-path
360 (lambda* (#:key inputs #:allow-other-keys)
361 (let* ((mach (assoc-ref inputs "gnumach-headers"))
362 (hurd (assoc-ref inputs "hurd-headers"))
363 (cpath (string-append mach "/include:"
364 hurd "/include")))
365 (for-each (cut setenv <> cpath)
366 ',%gcc-cross-include-paths)
367 #t)))))))
2d558e31
MR
368
369 (propagated-inputs `(("gnumach-headers" ,xgnumach-headers)
370 ("hurd-headers" ,xhurd-headers)))
371
372 (native-inputs `(("cross-gcc" ,xgcc)
373 ("cross-binutils" ,xbinutils)
374 ("cross-mig" ,xmig)
375 ,@(alist-delete "mig"(package-native-inputs glibc/hurd-headers))))))
376
377 (define xhurd-minimal
378 (package (inherit hurd-minimal)
379 (name (string-append (package-name hurd-minimal)
380 "-cross-" target))
381 (arguments
382 (substitute-keyword-arguments
383 `(#:modules ((guix build gnu-build-system)
384 (guix build utils)
385 (srfi srfi-26))
386 ,@(package-arguments hurd-minimal))
387 ((#:phases phases)
273cab96
TGR
388 `(modify-phases ,phases
389 (add-before 'configure 'set-cross-headers-path
390 (lambda* (#:key inputs #:allow-other-keys)
391 (let* ((glibc-headers (assoc-ref inputs "cross-glibc-hurd-headers"))
392 (cpath (string-append glibc-headers "/include")))
393 (for-each (cut setenv <> cpath)
394 ',%gcc-cross-include-paths)
395 #t)))))))
2d558e31
MR
396
397 (inputs `(("cross-glibc-hurd-headers" ,xglibc/hurd-headers)))
398
399 (native-inputs `(("cross-gcc" ,xgcc)
400 ("cross-binutils" ,xbinutils)
401 ("cross-mig" ,xmig)
402 ,@(alist-delete "mig"(package-native-inputs hurd-minimal))))))
403
404 (define xhurd-core-headers
405 (package (inherit hurd-core-headers)
406 (name (string-append (package-name hurd-core-headers)
407 "-cross-" target))
408
409 (inputs `(("gnumach-headers" ,xgnumach-headers)
410 ("hurd-headers" ,xhurd-headers)
411 ("hurd-minimal" ,xhurd-minimal)))
412
413 (native-inputs `(("cross-gcc" ,xgcc)
414 ("cross-binutils" ,xbinutils)
415 ("cross-mig" ,xmig)
416 ,@(package-native-inputs hurd-core-headers)))))
417
418 (match target
419 ((or "i586-pc-gnu" "i586-gnu") xhurd-core-headers)
420 (_ xlinux-headers)))
421
422(define* (cross-libc target
423 #:optional
424 (xgcc (cross-gcc target))
425 (xbinutils (cross-binutils target))
426 (xheaders (cross-kernel-headers target)))
427 "Return a libc cross-built for TARGET, a GNU triplet. Use XGCC and
428XBINUTILS and the cross tool chain."
429 (define (cross-libc-for-target target)
430 "Return libc depending on TARGET."
431 (match target
432 ((or "i586-pc-gnu" "i586-gnu") glibc/hurd)
433 (_ glibc/linux)))
434
d76181a7 435 ;; Use (cross-libc-for-target ...) to determine the correct libc to use.
0158ca7f 436
cba36e64
JN
437 (if (cross-newlib? target)
438 (native-libc target)
439 (let ((libc (cross-libc-for-target target)))
440 (package (inherit libc)
441 (name (string-append "glibc-cross-" target))
442 (arguments
443 (substitute-keyword-arguments
444 `(;; Disable stripping (see above.)
445 #:strip-binaries? #f
446
447 ;; This package is used as a target input, but it should not have
448 ;; the usual cross-compilation inputs since that would include
449 ;; itself.
450 #:implicit-cross-inputs? #f
451
452 ;; We need SRFI 26.
453 #:modules ((guix build gnu-build-system)
454 (guix build utils)
455 (srfi srfi-26))
456
457 ,@(package-arguments libc))
458 ((#:configure-flags flags)
459 `(cons ,(string-append "--host=" target)
460 ,flags))
461 ((#:phases phases)
273cab96
TGR
462 `(modify-phases ,phases
463 (add-before 'configure 'set-cross-kernel-headers-path
464 (lambda* (#:key inputs #:allow-other-keys)
465 (let* ((kernel (assoc-ref inputs "kernel-headers"))
466 (cpath (string-append kernel "/include")))
467 (for-each (cut setenv <> cpath)
468 ',%gcc-cross-include-paths)
469 (setenv "CROSS_LIBRARY_PATH"
470 (string-append kernel "/lib")) ; for Hurd's libihash
471 #t)))))))
cba36e64
JN
472
473 ;; Shadow the native "kernel-headers" because glibc's recipe expects the
474 ;; "kernel-headers" input to point to the right thing.
475 (propagated-inputs `(("kernel-headers" ,xheaders)))
476
477 ;; FIXME: 'static-bash' should really be an input, not a native input, but
478 ;; to do that will require building an intermediate cross libc.
479 (inputs '())
480
481 (native-inputs `(("cross-gcc" ,xgcc)
482 ("cross-binutils" ,xbinutils)
62596a15 483 ,@(if (hurd-triplet? target)
cba36e64
JN
484 `(("cross-mig"
485 ,@(assoc-ref (package-native-inputs xheaders)
486 "cross-mig")))
487 '())
488 ,@(package-inputs libc) ;FIXME: static-bash
489 ,@(package-native-inputs libc)))))))
490
491(define (native-libc target)
492 (if (target-mingw? target)
493 mingw-w64
494 glibc))
495
496(define (cross-newlib? target)
497 (not (eq? (native-libc target) glibc)))
827d2891
LC
498
499\f
bd2e1a8c
LC
500;;; Concrete cross tool chains are instantiated like this:
501;;
502;; (define-public xgcc-armhf
503;; (let ((triplet "arm-linux-gnueabihf"))
827d2891 504;; (cross-gcc triplet
e0775d2a
MW
505;; #:xbinutils (cross-binutils triplet)
506;; #:libc (cross-libc triplet))))
bd2e1a8c
LC
507;;
508;;; We don't do that here because we'd be referring to bindings from (gnu
509;;; packages gcc) from the top level, which doesn't play well with circular
510;;; dependencies among modules.