gnu: cross-base: Apply gcc 8 patch to gcc >= 8.
[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
44 (define-syntax %xgcc
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'.
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))
51
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
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 (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.
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)))))))
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)))
106
107 (define (cross-gcc-arguments target xgcc libc)
108 "Return build system arguments for a cross-gcc for TARGET, using XGCC as the
109 base compiler and using LIBC (which may be either a libc package or #f.)"
110 ;; Set the current target system so that 'glibc-dynamic-linker' returns the
111 ;; right name.
112 (parameterize ((%current-target-system target))
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
118 ,@(package-arguments xgcc))))
119 (substitute-keyword-arguments args
120 ((#:configure-flags flags)
121 `(append (list ,(string-append "--target=" target)
122 ,@(if libc
123 `( ;; Disable libcilkrts because it is not
124 ;; ported to GNU/Hurd.
125 "--disable-libcilkrts")
126 `( ;; Disable features not needed at this stage.
127 "--disable-shared" "--enable-static"
128 "--enable-languages=c,c++"
129
130 ;; libstdc++ cannot be built at this stage
131 ;; ("Link tests are not allowed after
132 ;; GCC_NO_EXECUTABLES.").
133 "--disable-libstdc++-v3"
134
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
142 "--disable-libcilkrts"
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"
151 ))
152
153 ;; For a newlib (non-glibc) target
154 ,@(if (cross-newlib? target)
155 '("--with-newlib")
156 '()))
157
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)
171 `(cross-gcc-build-phases ,target ,phases))))))
172
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.
177 (search-patches "ath9k-htc-firmware-gcc.patch"))
178 ((target-mingw? target)
179 (search-patches "gcc-4.9.3-mingw-gthr-default.patch"))
180 (else '())))
181
182 (define (cross-gcc-snippet target)
183 "Return GCC snippet needed for TARGET."
184 (cond ((target-mingw? target)
185 '(begin
186 (copy-recursively "libstdc++-v3/config/os/mingw32-w64"
187 "libstdc++-v3/config/os/newlib")
188 #t))
189 (else #f)))
190
191 (define* (cross-gcc target
192 #:key
193 (xgcc %xgcc)
194 (xbinutils (cross-binutils target))
195 (libc #f))
196 "Return a cross-compiler for TARGET, where TARGET is a GNU triplet. Use
197 XGCC as the base compiler. Use XBINUTILS as the associated cross-Binutils.
198 If LIBC is false, then build a GCC that does not target a libc; otherwise,
199 target that libc."
200 (package (inherit xgcc)
201 (name (string-append "gcc-cross-"
202 (if libc "" "sans-libc-")
203 target))
204 (source (origin (inherit (package-source xgcc))
205 (patches
206 (append
207 (origin-patches (package-source xgcc))
208 (cons (cond
209 ((version>=? (package-version xgcc) "8.0") (search-patch "gcc-8-cross-environment-variables.patch"))
210 ((version>=? (package-version xgcc) "6.0") (search-patch "gcc-6-cross-environment-variables.patch"))
211 (else (search-patch "gcc-cross-environment-variables.patch")))
212 (cross-gcc-patches target))))
213 (modules '((guix build utils)))
214 (snippet
215 (cross-gcc-snippet target))))
216
217 ;; For simplicity, use a single output. Otherwise libgcc_s & co. are not
218 ;; found by default, etc.
219 (outputs '("out"))
220
221 (arguments
222 `(#:implicit-inputs? #f
223 #:imported-modules ((gnu build cross-toolchain)
224 ,@%gnu-build-system-modules)
225 #:modules ((guix build gnu-build-system)
226 (guix build utils)
227 (gnu build cross-toolchain)
228 (srfi srfi-1)
229 (srfi srfi-26)
230 (ice-9 regex))
231
232 ,@(cross-gcc-arguments target xgcc libc)))
233
234 (native-inputs
235 `(("ld-wrapper-cross" ,(make-ld-wrapper
236 (string-append "ld-wrapper-" target)
237 #:target (const target)
238 #:binutils xbinutils))
239 ("binutils-cross" ,xbinutils)
240
241 ;; Call it differently so that the builder can check whether the "libc"
242 ;; input is #f.
243 ("libc-native" ,@(assoc-ref (%final-inputs) "libc"))
244
245 ;; Remaining inputs.
246 ,@(let ((inputs (append (package-inputs xgcc)
247 (alist-delete "libc" (%final-inputs)))))
248 (cond
249 ((target-mingw? target)
250 (if libc
251 `(("libc" ,mingw-w64)
252 ,@inputs)
253 `(("mingw-source" ,(package-source mingw-w64))
254 ,@inputs)))
255 (libc
256 `(("libc" ,libc)
257 ("libc:static" ,libc "static")
258 ("xkernel-headers" ;the target headers
259 ,@(assoc-ref (package-propagated-inputs libc)
260 "kernel-headers"))
261 ,@inputs))
262 (else inputs)))))
263
264 (inputs '())
265
266 ;; Only search target inputs, not host inputs.
267 (search-paths (cons (search-path-specification
268 (variable "CROSS_LIBRARY_PATH")
269 (files '("lib" "lib64")))
270 (map (lambda (variable)
271 (search-path-specification
272 (variable variable)
273 (files '("include"))))
274 %gcc-cross-include-paths)))
275 (native-search-paths '())))
276
277 (define* (cross-kernel-headers target
278 #:optional
279 (xgcc (cross-gcc target))
280 (xbinutils (cross-binutils target)))
281 "Return headers depending on TARGET."
282
283 (define xlinux-headers
284 (package (inherit linux-libre-headers)
285 (name (string-append (package-name linux-libre-headers)
286 "-cross-" target))
287 (arguments
288 (substitute-keyword-arguments
289 `(#:implicit-cross-inputs? #f
290 ,@(package-arguments linux-libre-headers))
291 ((#:phases phases)
292 `(alist-replace
293 'build
294 (lambda _
295 (setenv "ARCH" ,(system->linux-architecture target))
296 (format #t "`ARCH' set to `~a' (cross compiling)~%" (getenv "ARCH"))
297
298 (invoke "make" ,(system->defconfig target))
299 (invoke "make" "mrproper" "headers_check"))
300 ,phases))))
301 (native-inputs `(("cross-gcc" ,xgcc)
302 ("cross-binutils" ,xbinutils)
303 ,@(package-native-inputs linux-libre-headers)))))
304
305 (define xgnumach-headers
306 (package (inherit gnumach-headers)
307 (name (string-append (package-name gnumach-headers)
308 "-cross-" target))
309
310 (native-inputs `(("cross-gcc" ,xgcc)
311 ("cross-binutils" ,xbinutils)
312 ,@(package-native-inputs gnumach-headers)))))
313
314 (define xmig
315 (package (inherit mig)
316 (name (string-append "mig-cross"))
317 (arguments
318 `(#:modules ((guix build gnu-build-system)
319 (guix build utils)
320 (srfi srfi-26))
321 #:phases (modify-phases %standard-phases
322 (add-before 'configure 'set-cross-headers-path
323 (lambda* (#:key inputs #:allow-other-keys)
324 (let* ((mach (assoc-ref inputs "cross-gnumach-headers"))
325 (cpath (string-append mach "/include")))
326 (for-each (cut setenv <> cpath)
327 ',%gcc-cross-include-paths)
328 #t))))
329 #:configure-flags (list ,(string-append "--target=" target))
330 ,@(package-arguments mig)))
331
332 (propagated-inputs `(("cross-gnumach-headers" ,xgnumach-headers)))
333 (native-inputs `(("cross-gcc" ,xgcc)
334 ("cross-binutils" ,xbinutils)
335 ,@(package-native-inputs mig)))))
336
337 (define xhurd-headers
338 (package (inherit hurd-headers)
339 (name (string-append (package-name hurd-headers)
340 "-cross-" target))
341
342 (native-inputs `(("cross-gcc" ,xgcc)
343 ("cross-binutils" ,xbinutils)
344 ("cross-mig" ,xmig)
345 ,@(alist-delete "mig"(package-native-inputs hurd-headers))))))
346
347 (define xglibc/hurd-headers
348 (package (inherit glibc/hurd-headers)
349 (name (string-append (package-name glibc/hurd-headers)
350 "-cross-" target))
351
352 (arguments
353 (substitute-keyword-arguments
354 `(#:modules ((guix build gnu-build-system)
355 (guix build utils)
356 (srfi srfi-26))
357 ,@(package-arguments glibc/hurd-headers))
358 ((#:phases phases)
359 `(modify-phases ,phases
360 (add-after 'unpack 'set-cross-headers-path
361 (lambda* (#:key inputs #:allow-other-keys)
362 (let* ((mach (assoc-ref inputs "gnumach-headers"))
363 (hurd (assoc-ref inputs "hurd-headers"))
364 (cpath (string-append mach "/include:"
365 hurd "/include")))
366 (for-each (cut setenv <> cpath)
367 ',%gcc-cross-include-paths)
368 #t)))))))
369
370 (propagated-inputs `(("gnumach-headers" ,xgnumach-headers)
371 ("hurd-headers" ,xhurd-headers)))
372
373 (native-inputs `(("cross-gcc" ,xgcc)
374 ("cross-binutils" ,xbinutils)
375 ("cross-mig" ,xmig)
376 ,@(alist-delete "mig"(package-native-inputs glibc/hurd-headers))))))
377
378 (define xhurd-minimal
379 (package (inherit hurd-minimal)
380 (name (string-append (package-name hurd-minimal)
381 "-cross-" target))
382 (arguments
383 (substitute-keyword-arguments
384 `(#:modules ((guix build gnu-build-system)
385 (guix build utils)
386 (srfi srfi-26))
387 ,@(package-arguments hurd-minimal))
388 ((#:phases phases)
389 `(modify-phases ,phases
390 (add-before 'configure 'set-cross-headers-path
391 (lambda* (#:key inputs #:allow-other-keys)
392 (let* ((glibc-headers (assoc-ref inputs "cross-glibc-hurd-headers"))
393 (cpath (string-append glibc-headers "/include")))
394 (for-each (cut setenv <> cpath)
395 ',%gcc-cross-include-paths)
396 #t)))))))
397
398 (inputs `(("cross-glibc-hurd-headers" ,xglibc/hurd-headers)))
399
400 (native-inputs `(("cross-gcc" ,xgcc)
401 ("cross-binutils" ,xbinutils)
402 ("cross-mig" ,xmig)
403 ,@(alist-delete "mig"(package-native-inputs hurd-minimal))))))
404
405 (define xhurd-core-headers
406 (package (inherit hurd-core-headers)
407 (name (string-append (package-name hurd-core-headers)
408 "-cross-" target))
409
410 (inputs `(("gnumach-headers" ,xgnumach-headers)
411 ("hurd-headers" ,xhurd-headers)
412 ("hurd-minimal" ,xhurd-minimal)))
413
414 (native-inputs `(("cross-gcc" ,xgcc)
415 ("cross-binutils" ,xbinutils)
416 ("cross-mig" ,xmig)
417 ,@(package-native-inputs hurd-core-headers)))))
418
419 (match target
420 ((or "i586-pc-gnu" "i586-gnu") xhurd-core-headers)
421 (_ xlinux-headers)))
422
423 (define* (cross-libc target
424 #:optional
425 (xgcc (cross-gcc target))
426 (xbinutils (cross-binutils target))
427 (xheaders (cross-kernel-headers target)))
428 "Return a libc cross-built for TARGET, a GNU triplet. Use XGCC and
429 XBINUTILS and the cross tool chain."
430 (if (cross-newlib? target)
431 (native-libc target)
432 (let ((libc glibc))
433 (package (inherit libc)
434 (name (string-append "glibc-cross-" target))
435 (arguments
436 (substitute-keyword-arguments
437 `(;; Disable stripping (see above.)
438 #:strip-binaries? #f
439
440 ;; This package is used as a target input, but it should not have
441 ;; the usual cross-compilation inputs since that would include
442 ;; itself.
443 #:implicit-cross-inputs? #f
444
445 ;; We need SRFI 26.
446 #:modules ((guix build gnu-build-system)
447 (guix build utils)
448 (srfi srfi-26))
449
450 ,@(package-arguments libc))
451 ((#:configure-flags flags)
452 `(cons ,(string-append "--host=" target)
453 ,(if (hurd-triplet? target)
454 `(cons "--disable-werror" ,flags)
455 flags)))
456 ((#:phases phases)
457 `(modify-phases ,phases
458 ;; XXX: The hack below allows us to make sure the
459 ;; 'apply-hurd-patch' phase gets added in the first
460 ;; cross-libc, but does *not* get added twice subsequently
461 ;; when cross-building another libc.
462 ,@(if (and (hurd-triplet? target)
463 (not (hurd-target?)))
464 `((add-after 'unpack 'apply-hurd-patch
465 (lambda* (#:key inputs native-inputs
466 #:allow-other-keys)
467 ;; TODO: Move this to 'patches' field.
468 (let ((patch (or (assoc-ref native-inputs
469 "hurd-magic-pid-patch")
470 (assoc-ref inputs
471 "hurd-magic-pid-patch"))))
472 (invoke "patch" "-p1" "--force" "--input"
473 patch)))))
474 '())
475 (add-before 'configure 'set-cross-kernel-headers-path
476 (lambda* (#:key inputs #:allow-other-keys)
477 (let* ((kernel (assoc-ref inputs "kernel-headers"))
478 (cpath (string-append kernel "/include")))
479 (for-each (cut setenv <> cpath)
480 ',%gcc-cross-include-paths)
481 (setenv "CROSS_LIBRARY_PATH"
482 (string-append kernel "/lib")) ; for Hurd's libihash
483 #t)))))))
484
485 ;; Shadow the native "kernel-headers" because glibc's recipe expects the
486 ;; "kernel-headers" input to point to the right thing.
487 (propagated-inputs `(("kernel-headers" ,xheaders)))
488
489 ;; FIXME: 'static-bash' should really be an input, not a native input, but
490 ;; to do that will require building an intermediate cross libc.
491 (inputs '())
492
493 (native-inputs `(("cross-gcc" ,xgcc)
494 ("cross-binutils" ,xbinutils)
495 ,@(if (hurd-triplet? target)
496 `(("cross-mig"
497 ,@(assoc-ref (package-native-inputs xheaders)
498 "cross-mig"))
499 ("hurd-magic-pid-patch"
500 ,(search-patch "glibc-hurd-magic-pid.patch")))
501 '())
502 ,@(package-inputs libc) ;FIXME: static-bash
503 ,@(package-native-inputs libc)))))))
504
505 (define (native-libc target)
506 (if (target-mingw? target)
507 mingw-w64
508 glibc))
509
510 (define (cross-newlib? target)
511 (not (eq? (native-libc target) glibc)))
512
513 \f
514 ;;; Concrete cross tool chains are instantiated like this:
515 ;;
516 ;; (define-public xgcc-armhf
517 ;; (let ((triplet "arm-linux-gnueabihf"))
518 ;; (cross-gcc triplet
519 ;; #:xbinutils (cross-binutils triplet)
520 ;; #:libc (cross-libc triplet))))
521 ;;
522 ;;; We don't do that here because we'd be referring to bindings from (gnu
523 ;;; packages gcc) from the top level, which doesn't play well with circular
524 ;;; dependencies among modules.