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