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