1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2013, 2014, 2015 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2014, 2015 Mark H Weaver <mhw@netris.org>
5 ;;; This file is part of GNU Guix.
7 ;;; GNU Guix is free software; you can redistribute it and/or modify it
8 ;;; under the terms of the GNU General Public License as published by
9 ;;; the Free Software Foundation; either version 3 of the License, or (at
10 ;;; your option) any later version.
12 ;;; GNU Guix is distributed in the hope that it will be useful, but
13 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;;; GNU General Public License for more details.
17 ;;; You should have received a copy of the GNU General Public License
18 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
20 (define-module (gnu packages cross-base)
21 #:use-module (guix licenses)
22 #:use-module (gnu packages)
23 #:use-module (gnu packages gcc)
24 #:use-module (gnu packages base)
25 #:use-module (gnu packages commencement)
26 #:use-module (gnu packages linux)
27 #:use-module (guix packages)
28 #:use-module (guix download)
29 #:use-module (guix utils)
30 #:use-module (guix build-system gnu)
31 #:use-module (guix build-system trivial)
32 #:use-module (srfi srfi-1)
33 #:use-module (srfi srfi-26)
34 #:use-module (ice-9 match)
35 #:export (cross-binutils
40 ;; GCC package used as the basis for cross-compilation. It doesn't have to
41 ;; be 'gcc' and can be a specific variant such as 'gcc-4.8'.
44 (define (cross p target)
46 (name (string-append (package-name p) "-cross-" target))
48 (substitute-keyword-arguments (package-arguments p)
49 ((#:configure-flags flags)
50 `(cons ,(string-append "--target=" target)
53 (define (package-with-patch original patch)
54 "Return package ORIGINAL with PATCH applied."
55 (package (inherit original)
56 (source (origin (inherit (package-source original))
57 (patches (list patch))))))
59 (define (cross-binutils target)
60 "Return a cross-Binutils for TARGET."
61 (let ((binutils (package (inherit binutils)
63 (substitute-keyword-arguments (package-arguments
65 ((#:configure-flags flags)
66 ;; Build with `--with-sysroot' so that ld honors
67 ;; DT_RUNPATH entries when searching for a needed
68 ;; library. This works because as a side effect
69 ;; `genscripts.sh' sets `USE_LIBPATH=yes', which tells
70 ;; elf32.em to use DT_RUNPATH in its search list.
71 ;; See <http://sourceware.org/ml/binutils/2013-05/msg00312.html>.
73 ;; In theory choosing / as the sysroot could lead ld
74 ;; to pick up native libs instead of target ones. In
75 ;; practice the RUNPATH of target libs only refers to
76 ;; target libs, not native libs, so this is safe.
77 `(cons "--with-sysroot=/" ,flags)))))))
79 ;; For Xtensa, apply Qualcomm's patch.
80 (cross (if (string-prefix? "xtensa-" target)
81 (package-with-patch binutils
83 "ath9k-htc-firmware-binutils.patch"))
87 (define (cross-gcc-arguments target libc)
88 "Return build system arguments for a cross-gcc for TARGET, using LIBC (which
89 may be either a libc package or #f.)"
90 ;; Set the current target system so that 'glibc-dynamic-linker' returns the
92 (parameterize ((%current-target-system target))
93 ;; Disable stripping as this can break binaries, with object files of
94 ;; libgcc.a showing up as having an unknown architecture. See
95 ;; <http://lists.fedoraproject.org/pipermail/arm/2010-August/000663.html>
97 (let ((args `(#:strip-binaries? #f
98 ,@(package-arguments %xgcc))))
99 (substitute-keyword-arguments args
100 ((#:configure-flags flags)
101 `(append (list ,(string-append "--target=" target)
103 `( ;; Disable libcilkrts because it is not
104 ;; ported to GNU/Hurd.
105 "--disable-libcilkrts")
106 `( ;; Disable features not needed at this stage.
107 "--disable-shared" "--enable-static"
109 ;; Disable C++ because libstdc++'s configure
110 ;; script otherwise fails with "Link tests are not
111 ;; allowed after GCC_NO_EXECUTABLES."
112 "--enable-languages=c"
114 "--disable-threads" ;libgcc, would need libc
115 "--disable-libatomic"
116 "--disable-libmudflap"
119 "--disable-libquadmath"
120 "--disable-decimal-float" ;would need libc
121 "--disable-libcilkrts"
126 `(remove (cut string-match "--enable-languages.*" <>)
128 ((#:make-flags flags)
130 `(let ((libc (assoc-ref %build-inputs "libc")))
131 ;; FLAGS_FOR_TARGET are needed for the target libraries to receive
132 ;; the -Bxxx for the startfiles.
133 (cons (string-append "FLAGS_FOR_TARGET=-B" libc "/lib")
139 'install 'make-cross-binutils-visible
140 (lambda* (#:key outputs inputs #:allow-other-keys)
141 (let* ((out (assoc-ref outputs "out"))
142 (libexec (string-append out "/libexec/gcc/"
144 (binutils (string-append
145 (assoc-ref inputs "binutils-cross")
146 "/bin/" ,target "-"))
147 (wrapper (string-append
148 (assoc-ref inputs "ld-wrapper-cross")
149 "/bin/" ,target "-ld")))
150 (for-each (lambda (file)
151 (symlink (string-append binutils file)
152 (string-append libexec "/"
155 (symlink wrapper (string-append libexec "/ld"))
160 ;; Unlike our 'strip' phase, this will do the right thing
161 ;; for cross-compilers.
162 (zero? (system* "make" "install-strip")))
166 'configure 'set-cross-path
167 (lambda* (#:key inputs #:allow-other-keys)
168 ;; Add the cross Linux headers to CROSS_CPATH, and remove them
170 (let ((libc (assoc-ref inputs "libc"))
171 (linux (assoc-ref inputs "xlinux-headers")))
173 ;; Return #t if X is a cross-libc or cross Linux.
174 (or (string-prefix? libc x)
175 (string-prefix? linux x)))
177 (setenv "CROSS_CPATH"
178 (string-append libc "/include:"
180 (setenv "CROSS_LIBRARY_PATH"
181 (string-append libc "/lib"))
183 (let ((cpath (search-path-as-string->list
185 (libpath (search-path-as-string->list
186 (getenv "LIBRARY_PATH"))))
188 (list->search-path-as-string
189 (remove cross? cpath) ":"))
190 (setenv "LIBRARY_PATH"
191 (list->search-path-as-string
192 (remove cross? libpath) ":"))
197 (define (cross-gcc-patches target)
198 "Return GCC patches needed for TARGET."
199 (cond ((string-prefix? "xtensa-" target)
200 ;; Patch by Qualcomm needed to build the ath9k-htc firmware.
201 (list (search-patch "ath9k-htc-firmware-gcc.patch")))
204 (define* (cross-gcc target
205 #:optional (xbinutils (cross-binutils target)) libc)
206 "Return a cross-compiler for TARGET, where TARGET is a GNU triplet. Use
207 XBINUTILS as the associated cross-Binutils. If LIBC is false, then build a
208 GCC that does not target a libc; otherwise, target that libc."
209 (package (inherit %xgcc)
210 (name (string-append "gcc-cross-"
211 (if libc "" "sans-libc-")
213 (source (origin (inherit (package-source %xgcc))
216 (origin-patches (package-source %xgcc))
217 (cons (search-patch "gcc-cross-environment-variables.patch")
218 (cross-gcc-patches target))))))
220 ;; For simplicity, use a single output. Otherwise libgcc_s & co. are not
221 ;; found by default, etc.
225 `(#:implicit-inputs? #f
226 #:modules ((guix build gnu-build-system)
232 ,@(cross-gcc-arguments target libc)))
235 `(("ld-wrapper-cross" ,(make-ld-wrapper
236 (string-append "ld-wrapper-" target)
238 #:binutils xbinutils))
239 ("binutils-cross" ,xbinutils)
241 ;; Call it differently so that the builder can check whether the "libc"
243 ("libc-native" ,@(assoc-ref %final-inputs "libc"))
246 ,@(let ((inputs (append (package-inputs %xgcc)
247 (alist-delete "libc" %final-inputs))))
250 ("xlinux-headers" ;the target headers
251 ,@(assoc-ref (package-propagated-inputs libc)
258 ;; Only search target inputs, not host inputs.
260 (list (search-path-specification
261 (variable "CROSS_CPATH")
262 (files '("include")))
263 (search-path-specification
264 (variable "CROSS_LIBRARY_PATH")
265 (files '("lib" "lib64")))))
266 (native-search-paths '())))
268 (define* (cross-libc target
270 (xgcc (cross-gcc target))
271 (xbinutils (cross-binutils target)))
272 "Return a libc cross-built for TARGET, a GNU triplet. Use XGCC and
273 XBINUTILS and the cross tool chain."
274 (define xlinux-headers
275 (package (inherit linux-libre-headers)
276 (name (string-append (package-name linux-libre-headers)
279 (substitute-keyword-arguments
280 `(#:implicit-cross-inputs? #f
281 ,@(package-arguments linux-libre-headers))
286 (setenv "ARCH" ,(system->linux-architecture target))
287 (format #t "`ARCH' set to `~a' (cross compiling)~%" (getenv "ARCH"))
289 (and (zero? (system* "make" "defconfig"))
290 (zero? (system* "make" "mrproper" "headers_check"))))
292 (native-inputs `(("cross-gcc" ,xgcc)
293 ("cross-binutils" ,xbinutils)
294 ,@(package-native-inputs linux-libre-headers)))))
296 (package (inherit glibc)
297 (name (string-append "glibc-cross-" target))
299 (substitute-keyword-arguments
300 `(;; Disable stripping (see above.)
303 ;; This package is used as a target input, but it should not have
304 ;; the usual cross-compilation inputs since that would include
306 #:implicit-cross-inputs? #f
308 ,@(package-arguments glibc))
309 ((#:configure-flags flags)
310 `(cons ,(string-append "--host=" target)
314 'configure 'set-cross-linux-headers-path
315 (lambda* (#:key inputs #:allow-other-keys)
316 (let ((linux (assoc-ref inputs "linux-headers")))
317 (setenv "CROSS_CPATH"
318 (string-append linux "/include"))
322 ;; Shadow the native "linux-headers" because glibc's recipe expects the
323 ;; "linux-headers" input to point to the right thing.
324 (propagated-inputs `(("linux-headers" ,xlinux-headers)))
326 ;; FIXME: 'static-bash' should really be an input, not a native input, but
327 ;; to do that will require building an intermediate cross libc.
330 (native-inputs `(("cross-gcc" ,xgcc)
331 ("cross-binutils" ,xbinutils)
332 ,@(package-inputs glibc) ;FIXME: static-bash
333 ,@(package-native-inputs glibc)))))
337 ;;; Concrete cross toolchains.
340 (define-public xgcc-mips64el
341 (let* ((triplet "mips64el-linux-gnuabi64") ;N64 ABI
342 (xgcc (cross-gcc triplet
343 (cross-binutils triplet)
344 (cross-libc triplet))))
345 ;; Don't attempt to build this cross-compiler on i686;
346 ;; see <http://bugs.gnu.org/19598>.
347 (package (inherit xgcc)
348 (supported-systems (fold delete
349 (package-supported-systems xgcc)
350 '("mips64el-linux" "i686-linux"))))))
352 (define-public xgcc-avr
353 ;; AVR cross-compiler, used to build AVR-Libc.
354 (let ((triplet "avr"))
356 (cross-binutils triplet))))
358 (define-public xgcc-xtensa
359 ;; Bare-bones Xtensa cross-compiler, used to build the Atheros firmware.
360 (cross-gcc "xtensa-elf"))
362 (define-public xgcc-armhf
363 (let* ((triplet "arm-linux-gnueabihf")
364 (xgcc (cross-gcc triplet
365 (cross-binutils triplet)
366 (cross-libc triplet))))
367 (package (inherit xgcc)
368 (supported-systems (delete "armhf-linux" %supported-systems)))))
370 ;; (define-public xgcc-armel
371 ;; (let ((triplet "armel-linux-gnueabi"))
372 ;; (cross-gcc triplet
373 ;; (cross-binutils triplet)
374 ;; (cross-libc triplet))))