gnu: Add graphios.
[jackhill/guix/guix.git] / gnu / packages / cross-base.scm
CommitLineData
827d2891 1;;; GNU Guix --- Functional package management for GNU
6ee01481 2;;; Copyright © 2013, 2014, 2015 Ludovic Courtès <ludo@gnu.org>
3f00ff8b 3;;; Copyright © 2014, 2015 Mark H Weaver <mhw@netris.org>
827d2891
LC
4;;;
5;;; This file is part of GNU Guix.
6;;;
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.
11;;;
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.
16;;;
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/>.
19
20(define-module (gnu packages cross-base)
21 #:use-module (guix licenses)
22 #:use-module (gnu packages)
f594028a 23 #:use-module (gnu packages gcc)
827d2891 24 #:use-module (gnu packages base)
bdb36958 25 #:use-module (gnu packages commencement)
827d2891
LC
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)
264218a4
LC
34 #:use-module (ice-9 match)
35 #:export (cross-binutils
36 cross-libc
37 cross-gcc))
827d2891 38
c92f1c0a
LC
39(define %xgcc
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'.
42 gcc)
43
827d2891
LC
44(define (cross p target)
45 (package (inherit p)
827d2891
LC
46 (name (string-append (package-name p) "-cross-" target))
47 (arguments
48 (substitute-keyword-arguments (package-arguments p)
49 ((#:configure-flags flags)
50 `(cons ,(string-append "--target=" target)
51 ,flags))))))
52
1306b0b0
LC
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))))))
58
47e74d6e
LC
59(define (cross-binutils target)
60 "Return a cross-Binutils for TARGET."
61 (let ((binutils (package (inherit binutils)
62 (arguments
63 (substitute-keyword-arguments (package-arguments
64 binutils)
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.
c8fa51f2
LC
71 ;; See <http://sourceware.org/ml/binutils/2013-05/msg00312.html>.
72 ;;
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)))))))
1306b0b0
LC
78
79 ;; For Xtensa, apply Qualcomm's patch.
80 (cross (if (string-prefix? "xtensa-" target)
81 (package-with-patch binutils
82 (search-patch
83 "ath9k-htc-firmware-binutils.patch"))
84 binutils)
85 target)))
827d2891 86
cdb4b4b3
LC
87(define (cross-gcc-arguments target libc)
88 "Return build system arguments for a cross-gcc for TARGET, using LIBC (which
89may be either a libc package or #f.)"
b4469d8c
LC
90 ;; Set the current target system so that 'glibc-dynamic-linker' returns the
91 ;; right name.
92 (parameterize ((%current-target-system target))
2a1552c6
LC
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>
96 ;; for instance.
97 (let ((args `(#:strip-binaries? #f
c92f1c0a 98 ,@(package-arguments %xgcc))))
2a1552c6
LC
99 (substitute-keyword-arguments args
100 ((#:configure-flags flags)
101 `(append (list ,(string-append "--target=" target)
102 ,@(if libc
103 '()
104 `( ;; Disable features not needed at this stage.
105 "--disable-shared" "--enable-static"
cdb4b4b3 106
2a1552c6
LC
107 ;; Disable C++ because libstdc++'s configure
108 ;; script otherwise fails with "Link tests are not
109 ;; allowed after GCC_NO_EXECUTABLES."
110 "--enable-languages=c"
cdb4b4b3 111
2a1552c6
LC
112 "--disable-threads" ;libgcc, would need libc
113 "--disable-libatomic"
114 "--disable-libmudflap"
115 "--disable-libgomp"
116 "--disable-libssp"
117 "--disable-libquadmath"
118 "--disable-decimal-float" ;would need libc
119 )))
cdb4b4b3 120
2a1552c6
LC
121 ,(if libc
122 flags
123 `(remove (cut string-match "--enable-languages.*" <>)
124 ,flags))))
125 ((#:make-flags flags)
126 (if libc
127 `(let ((libc (assoc-ref %build-inputs "libc")))
128 ;; FLAGS_FOR_TARGET are needed for the target libraries to receive
129 ;; the -Bxxx for the startfiles.
130 (cons (string-append "FLAGS_FOR_TARGET=-B" libc "/lib")
131 ,flags))
132 flags))
133 ((#:phases phases)
134 (let ((phases
135 `(alist-cons-after
136 'install 'make-cross-binutils-visible
137 (lambda* (#:key outputs inputs #:allow-other-keys)
138 (let* ((out (assoc-ref outputs "out"))
139 (libexec (string-append out "/libexec/gcc/"
140 ,target))
141 (binutils (string-append
142 (assoc-ref inputs "binutils-cross")
143 "/bin/" ,target "-"))
144 (wrapper (string-append
145 (assoc-ref inputs "ld-wrapper-cross")
146 "/bin/" ,target "-ld")))
147 (for-each (lambda (file)
148 (symlink (string-append binutils file)
149 (string-append libexec "/"
150 file)))
151 '("as" "nm"))
152 (symlink wrapper (string-append libexec "/ld"))
153 #t))
154 (alist-replace
155 'install
156 (lambda _
157 ;; Unlike our 'strip' phase, this will do the right thing
158 ;; for cross-compilers.
159 (zero? (system* "make" "install-strip")))
160 ,phases))))
161 (if libc
162 `(alist-cons-before
163 'configure 'set-cross-path
164 (lambda* (#:key inputs #:allow-other-keys)
165 ;; Add the cross Linux headers to CROSS_CPATH, and remove them
166 ;; from CPATH.
167 (let ((libc (assoc-ref inputs "libc"))
aa27987f 168 (linux (assoc-ref inputs "xlinux-headers")))
2a1552c6
LC
169 (define (cross? x)
170 ;; Return #t if X is a cross-libc or cross Linux.
171 (or (string-prefix? libc x)
172 (string-prefix? linux x)))
cdb4b4b3 173
2a1552c6
LC
174 (setenv "CROSS_CPATH"
175 (string-append libc "/include:"
176 linux "/include"))
177 (setenv "CROSS_LIBRARY_PATH"
178 (string-append libc "/lib"))
cdb4b4b3 179
2a1552c6
LC
180 (let ((cpath (search-path-as-string->list
181 (getenv "CPATH")))
182 (libpath (search-path-as-string->list
183 (getenv "LIBRARY_PATH"))))
184 (setenv "CPATH"
185 (list->search-path-as-string
186 (remove cross? cpath) ":"))
187 (setenv "LIBRARY_PATH"
188 (list->search-path-as-string
189 (remove cross? libpath) ":"))
190 #t)))
191 ,phases)
192 phases)))))))
cdb4b4b3 193
1306b0b0
LC
194(define (cross-gcc-patches target)
195 "Return GCC patches needed for TARGET."
196 (cond ((string-prefix? "xtensa-" target)
197 ;; Patch by Qualcomm needed to build the ath9k-htc firmware.
198 (list (search-patch "ath9k-htc-firmware-gcc.patch")))
199 (else '())))
200
827d2891
LC
201(define* (cross-gcc target
202 #:optional (xbinutils (cross-binutils target)) libc)
203 "Return a cross-compiler for TARGET, where TARGET is a GNU triplet. Use
204XBINUTILS as the associated cross-Binutils. If LIBC is false, then build a
205GCC that does not target a libc; otherwise, target that libc."
c92f1c0a 206 (package (inherit %xgcc)
827d2891
LC
207 (name (string-append "gcc-cross-"
208 (if libc "" "sans-libc-")
209 target))
c92f1c0a 210 (source (origin (inherit (package-source %xgcc))
01eafd38 211 (patches
1421afa9 212 (append
c92f1c0a 213 (origin-patches (package-source %xgcc))
1421afa9
MW
214 (cons (search-patch "gcc-cross-environment-variables.patch")
215 (cross-gcc-patches target))))))
a49c57a7
LC
216
217 ;; For simplicity, use a single output. Otherwise libgcc_s & co. are not
218 ;; found by default, etc.
219 (outputs '("out"))
220
827d2891
LC
221 (arguments
222 `(#:implicit-inputs? #f
223 #:modules ((guix build gnu-build-system)
224 (guix build utils)
225 (ice-9 regex)
226 (srfi srfi-1)
227 (srfi srfi-26))
827d2891 228
cdb4b4b3 229 ,@(cross-gcc-arguments target libc)))
0de71c23
LC
230
231 (native-inputs
4a740d0f
LC
232 `(("ld-wrapper-cross" ,(make-ld-wrapper
233 (string-append "ld-wrapper-" target)
234 #:target target
235 #:binutils xbinutils))
236 ("binutils-cross" ,xbinutils)
827d2891
LC
237
238 ;; Call it differently so that the builder can check whether the "libc"
239 ;; input is #f.
240 ("libc-native" ,@(assoc-ref %final-inputs "libc"))
241
242 ;; Remaining inputs.
c92f1c0a 243 ,@(let ((inputs (append (package-inputs %xgcc)
827d2891
LC
244 (alist-delete "libc" %final-inputs))))
245 (if libc
246 `(("libc" ,libc)
aa27987f
LC
247 ("xlinux-headers" ;the target headers
248 ,@(assoc-ref (package-propagated-inputs libc)
249 "linux-headers"))
827d2891 250 ,@inputs)
17bb886f
LC
251 inputs))))
252
0de71c23
LC
253 (inputs '())
254
17bb886f
LC
255 ;; Only search target inputs, not host inputs.
256 (search-paths
257 (list (search-path-specification
258 (variable "CROSS_CPATH")
af070955 259 (files '("include")))
17bb886f
LC
260 (search-path-specification
261 (variable "CROSS_LIBRARY_PATH")
af070955 262 (files '("lib" "lib64")))))
17bb886f 263 (native-search-paths '())))
827d2891
LC
264
265(define* (cross-libc target
266 #:optional
267 (xgcc (cross-gcc target))
268 (xbinutils (cross-binutils target)))
269 "Return a libc cross-built for TARGET, a GNU triplet. Use XGCC and
270XBINUTILS and the cross tool chain."
271 (define xlinux-headers
272 (package (inherit linux-libre-headers)
273 (name (string-append (package-name linux-libre-headers)
274 "-cross-" target))
275 (arguments
0d5a559f
LC
276 (substitute-keyword-arguments
277 `(#:implicit-cross-inputs? #f
278 ,@(package-arguments linux-libre-headers))
827d2891
LC
279 ((#:phases phases)
280 `(alist-replace
281 'build
282 (lambda _
283 (setenv "ARCH" ,(system->linux-architecture target))
284 (format #t "`ARCH' set to `~a' (cross compiling)~%" (getenv "ARCH"))
285
286 (and (zero? (system* "make" "defconfig"))
287 (zero? (system* "make" "mrproper" "headers_check"))))
288 ,phases))))
0de71c23
LC
289 (native-inputs `(("cross-gcc" ,xgcc)
290 ("cross-binutils" ,xbinutils)
291 ,@(package-native-inputs linux-libre-headers)))))
827d2891
LC
292
293 (package (inherit glibc)
294 (name (string-append "glibc-cross-" target))
295 (arguments
296 (substitute-keyword-arguments
0d5a559f
LC
297 `(;; Disable stripping (see above.)
298 #:strip-binaries? #f
299
300 ;; This package is used as a target input, but it should not have
301 ;; the usual cross-compilation inputs since that would include
302 ;; itself.
303 #:implicit-cross-inputs? #f
304
827d2891
LC
305 ,@(package-arguments glibc))
306 ((#:configure-flags flags)
307 `(cons ,(string-append "--host=" target)
308 ,flags))
309 ((#:phases phases)
310 `(alist-cons-before
311 'configure 'set-cross-linux-headers-path
312 (lambda* (#:key inputs #:allow-other-keys)
0d5a559f 313 (let ((linux (assoc-ref inputs "linux-headers")))
827d2891
LC
314 (setenv "CROSS_CPATH"
315 (string-append linux "/include"))
316 #t))
317 ,phases))))
a4627d49 318
c6d33a9d 319 ;; Shadow the native "linux-headers" because glibc's recipe expects the
0d5a559f
LC
320 ;; "linux-headers" input to point to the right thing.
321 (propagated-inputs `(("linux-headers" ,xlinux-headers)))
322
12b0dbd4
LC
323 ;; FIXME: 'static-bash' should really be an input, not a native input, but
324 ;; to do that will require building an intermediate cross libc.
325 (inputs '())
326
0de71c23
LC
327 (native-inputs `(("cross-gcc" ,xgcc)
328 ("cross-binutils" ,xbinutils)
12b0dbd4 329 ,@(package-inputs glibc) ;FIXME: static-bash
0de71c23 330 ,@(package-native-inputs glibc)))))
827d2891
LC
331
332\f
333;;;
334;;; Concrete cross toolchains.
335;;;
336
337(define-public xgcc-mips64el
6ee01481
LC
338 (let* ((triplet "mips64el-linux-gnuabi64") ;N64 ABI
339 (xgcc (cross-gcc triplet
340 (cross-binutils triplet)
341 (cross-libc triplet))))
342 ;; Don't attempt to build this cross-compiler on i686;
343 ;; see <http://bugs.gnu.org/19598>.
344 (package (inherit xgcc)
9fdd80e8
LC
345 (supported-systems (fold delete
346 (package-supported-systems xgcc)
347 '("mips64el-linux" "i686-linux"))))))
827d2891 348
a5b60e3c
MR
349(define-public xgcc-avr
350 ;; AVR cross-compiler, used to build AVR-Libc.
351 (let ((triplet "avr"))
352 (cross-gcc triplet
353 (cross-binutils triplet))))
354
9d307460
LC
355(define-public xgcc-xtensa
356 ;; Bare-bones Xtensa cross-compiler, used to build the Atheros firmware.
357 (cross-gcc "xtensa-elf"))
358
3f00ff8b 359(define-public xgcc-armhf
9fdd80e8
LC
360 (let* ((triplet "arm-linux-gnueabihf")
361 (xgcc (cross-gcc triplet
362 (cross-binutils triplet)
363 (cross-libc triplet))))
364 (package (inherit xgcc)
365 (supported-systems (delete "armhf-linux" %supported-systems)))))
3f00ff8b 366
827d2891
LC
367;; (define-public xgcc-armel
368;; (let ((triplet "armel-linux-gnueabi"))
369;; (cross-gcc triplet
370;; (cross-binutils triplet)
371;; (cross-libc triplet))))