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