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