gnu: commencement: Add support for a native GNU/Hurd system.
[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>
827d2891
LC
5;;;
6;;; This file is part of GNU Guix.
7;;;
8;;; GNU Guix is free software; you can redistribute it and/or modify it
9;;; under the terms of the GNU General Public License as published by
10;;; the Free Software Foundation; either version 3 of the License, or (at
11;;; your option) any later version.
12;;;
13;;; GNU Guix is distributed in the hope that it will be useful, but
14;;; WITHOUT ANY WARRANTY; without even the implied warranty of
15;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16;;; GNU General Public License for more details.
17;;;
18;;; You should have received a copy of the GNU General Public License
19;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
20
21(define-module (gnu packages cross-base)
22 #:use-module (guix licenses)
23 #:use-module (gnu packages)
f594028a 24 #:use-module (gnu packages gcc)
827d2891 25 #:use-module (gnu packages base)
bdb36958 26 #:use-module (gnu packages commencement)
827d2891
LC
27 #:use-module (gnu packages linux)
28 #:use-module (guix packages)
29 #:use-module (guix download)
30 #:use-module (guix utils)
31 #:use-module (guix build-system gnu)
32 #:use-module (guix build-system trivial)
33 #:use-module (srfi srfi-1)
34 #:use-module (srfi srfi-26)
264218a4
LC
35 #:use-module (ice-9 match)
36 #:export (cross-binutils
37 cross-libc
38 cross-gcc))
827d2891 39
c92f1c0a
LC
40(define %xgcc
41 ;; GCC package used as the basis for cross-compilation. It doesn't have to
42 ;; be 'gcc' and can be a specific variant such as 'gcc-4.8'.
43 gcc)
44
827d2891
LC
45(define (cross p target)
46 (package (inherit p)
827d2891
LC
47 (name (string-append (package-name p) "-cross-" target))
48 (arguments
49 (substitute-keyword-arguments (package-arguments p)
50 ((#:configure-flags flags)
51 `(cons ,(string-append "--target=" target)
52 ,flags))))))
53
1306b0b0
LC
54(define (package-with-patch original patch)
55 "Return package ORIGINAL with PATCH applied."
56 (package (inherit original)
57 (source (origin (inherit (package-source original))
58 (patches (list patch))))))
59
47e74d6e
LC
60(define (cross-binutils target)
61 "Return a cross-Binutils for TARGET."
62 (let ((binutils (package (inherit binutils)
63 (arguments
64 (substitute-keyword-arguments (package-arguments
65 binutils)
66 ((#:configure-flags flags)
67 ;; Build with `--with-sysroot' so that ld honors
68 ;; DT_RUNPATH entries when searching for a needed
69 ;; library. This works because as a side effect
70 ;; `genscripts.sh' sets `USE_LIBPATH=yes', which tells
71 ;; elf32.em to use DT_RUNPATH in its search list.
c8fa51f2
LC
72 ;; See <http://sourceware.org/ml/binutils/2013-05/msg00312.html>.
73 ;;
74 ;; In theory choosing / as the sysroot could lead ld
75 ;; to pick up native libs instead of target ones. In
76 ;; practice the RUNPATH of target libs only refers to
77 ;; target libs, not native libs, so this is safe.
78 `(cons "--with-sysroot=/" ,flags)))))))
1306b0b0
LC
79
80 ;; For Xtensa, apply Qualcomm's patch.
81 (cross (if (string-prefix? "xtensa-" target)
82 (package-with-patch binutils
83 (search-patch
84 "ath9k-htc-firmware-binutils.patch"))
85 binutils)
86 target)))
827d2891 87
cdb4b4b3
LC
88(define (cross-gcc-arguments target libc)
89 "Return build system arguments for a cross-gcc for TARGET, using LIBC (which
90may be either a libc package or #f.)"
b4469d8c
LC
91 ;; Set the current target system so that 'glibc-dynamic-linker' returns the
92 ;; right name.
93 (parameterize ((%current-target-system target))
2a1552c6
LC
94 ;; Disable stripping as this can break binaries, with object files of
95 ;; libgcc.a showing up as having an unknown architecture. See
96 ;; <http://lists.fedoraproject.org/pipermail/arm/2010-August/000663.html>
97 ;; for instance.
98 (let ((args `(#:strip-binaries? #f
c92f1c0a 99 ,@(package-arguments %xgcc))))
2a1552c6
LC
100 (substitute-keyword-arguments args
101 ((#:configure-flags flags)
102 `(append (list ,(string-append "--target=" target)
103 ,@(if libc
8fd857f5
MR
104 `( ;; Disable libcilkrts because it is not
105 ;; ported to GNU/Hurd.
106 "--disable-libcilkrts")
2a1552c6
LC
107 `( ;; Disable features not needed at this stage.
108 "--disable-shared" "--enable-static"
ca7ef4d4 109 "--enable-languages=c,c++"
cdb4b4b3 110
ca7ef4d4
RW
111 ;; libstdc++ cannot be built at this stage
112 ;; ("Link tests are not allowed after
113 ;; GCC_NO_EXECUTABLES.").
114 "--disable-libstdc++-v3"
cdb4b4b3 115
2a1552c6
LC
116 "--disable-threads" ;libgcc, would need libc
117 "--disable-libatomic"
118 "--disable-libmudflap"
119 "--disable-libgomp"
120 "--disable-libssp"
121 "--disable-libquadmath"
122 "--disable-decimal-float" ;would need libc
8fd857f5 123 "--disable-libcilkrts"
9a745d76
MR
124
125 ;; When target is any OS other than 'none' these
126 ;; libraries will fail if there is no libc
127 ;; present. See
128 ;; <https://lists.gnu.org/archive/html/guix-devel/2016-02/msg01311.html>
129 "--disable-libitm"
130 "--disable-libvtv"
131 "--disable-libsanitizer"
2a1552c6 132 )))
cdb4b4b3 133
2a1552c6
LC
134 ,(if libc
135 flags
136 `(remove (cut string-match "--enable-languages.*" <>)
137 ,flags))))
138 ((#:make-flags flags)
139 (if libc
140 `(let ((libc (assoc-ref %build-inputs "libc")))
141 ;; FLAGS_FOR_TARGET are needed for the target libraries to receive
142 ;; the -Bxxx for the startfiles.
143 (cons (string-append "FLAGS_FOR_TARGET=-B" libc "/lib")
144 ,flags))
145 flags))
146 ((#:phases phases)
147 (let ((phases
148 `(alist-cons-after
149 'install 'make-cross-binutils-visible
150 (lambda* (#:key outputs inputs #:allow-other-keys)
151 (let* ((out (assoc-ref outputs "out"))
152 (libexec (string-append out "/libexec/gcc/"
153 ,target))
154 (binutils (string-append
155 (assoc-ref inputs "binutils-cross")
156 "/bin/" ,target "-"))
157 (wrapper (string-append
158 (assoc-ref inputs "ld-wrapper-cross")
159 "/bin/" ,target "-ld")))
160 (for-each (lambda (file)
161 (symlink (string-append binutils file)
162 (string-append libexec "/"
163 file)))
164 '("as" "nm"))
165 (symlink wrapper (string-append libexec "/ld"))
166 #t))
167 (alist-replace
168 'install
169 (lambda _
170 ;; Unlike our 'strip' phase, this will do the right thing
171 ;; for cross-compilers.
172 (zero? (system* "make" "install-strip")))
173 ,phases))))
174 (if libc
175 `(alist-cons-before
176 'configure 'set-cross-path
177 (lambda* (#:key inputs #:allow-other-keys)
55de892b 178 ;; Add the cross kernel headers to CROSS_CPATH, and remove them
2a1552c6
LC
179 ;; from CPATH.
180 (let ((libc (assoc-ref inputs "libc"))
55de892b 181 (kernel (assoc-ref inputs "xkernel-headers")))
2a1552c6
LC
182 (define (cross? x)
183 ;; Return #t if X is a cross-libc or cross Linux.
184 (or (string-prefix? libc x)
55de892b 185 (string-prefix? kernel x)))
efc4eb14
JN
186 (let ((cpath (string-append
187 libc "/include"
eb74eb41 188 ":" kernel "/include")))
efc4eb14
JN
189 (for-each (cut setenv <> cpath)
190 '("CROSS_C_INCLUDE_PATH"
191 "CROSS_CPLUS_INCLUDE_PATH"
192 "CROSS_OBJC_INCLUDE_PATH"
193 "CROSS_OBJCPLUS_INCLUDE_PATH")))
2a1552c6 194 (setenv "CROSS_LIBRARY_PATH"
8956d07a
MR
195 (string-append libc "/lib:"
196 kernel "/lib")) ;for Hurd's libihash
efc4eb14
JN
197 (for-each
198 (lambda (var)
199 (and=> (getenv var)
200 (lambda (value)
201 (let* ((path (search-path-as-string->list value))
202 (native-path (list->search-path-as-string
203 (remove cross? path) ":")))
204 (setenv var native-path)))))
205 '("C_INCLUDE_PATH"
206 "CPLUS_INCLUDE_PATH"
207 "OBJC_INCLUDE_PATH"
208 "OBJCPLUS_INCLUDE_PATH"
209 "LIBRARY_PATH"))
210 #t))
2a1552c6
LC
211 ,phases)
212 phases)))))))
cdb4b4b3 213
1306b0b0
LC
214(define (cross-gcc-patches target)
215 "Return GCC patches needed for TARGET."
216 (cond ((string-prefix? "xtensa-" target)
217 ;; Patch by Qualcomm needed to build the ath9k-htc firmware.
fc1adab1 218 (search-patches "ath9k-htc-firmware-gcc.patch"))
1306b0b0
LC
219 (else '())))
220
827d2891
LC
221(define* (cross-gcc target
222 #:optional (xbinutils (cross-binutils target)) libc)
223 "Return a cross-compiler for TARGET, where TARGET is a GNU triplet. Use
224XBINUTILS as the associated cross-Binutils. If LIBC is false, then build a
225GCC that does not target a libc; otherwise, target that libc."
c92f1c0a 226 (package (inherit %xgcc)
827d2891
LC
227 (name (string-append "gcc-cross-"
228 (if libc "" "sans-libc-")
229 target))
c92f1c0a 230 (source (origin (inherit (package-source %xgcc))
01eafd38 231 (patches
1421afa9 232 (append
c92f1c0a 233 (origin-patches (package-source %xgcc))
1421afa9
MW
234 (cons (search-patch "gcc-cross-environment-variables.patch")
235 (cross-gcc-patches target))))))
a49c57a7
LC
236
237 ;; For simplicity, use a single output. Otherwise libgcc_s & co. are not
238 ;; found by default, etc.
239 (outputs '("out"))
240
827d2891
LC
241 (arguments
242 `(#:implicit-inputs? #f
243 #:modules ((guix build gnu-build-system)
244 (guix build utils)
245 (ice-9 regex)
246 (srfi srfi-1)
247 (srfi srfi-26))
827d2891 248
cdb4b4b3 249 ,@(cross-gcc-arguments target libc)))
0de71c23
LC
250
251 (native-inputs
4a740d0f
LC
252 `(("ld-wrapper-cross" ,(make-ld-wrapper
253 (string-append "ld-wrapper-" target)
254 #:target target
255 #:binutils xbinutils))
256 ("binutils-cross" ,xbinutils)
827d2891
LC
257
258 ;; Call it differently so that the builder can check whether the "libc"
259 ;; input is #f.
260 ("libc-native" ,@(assoc-ref %final-inputs "libc"))
261
262 ;; Remaining inputs.
c92f1c0a 263 ,@(let ((inputs (append (package-inputs %xgcc)
827d2891
LC
264 (alist-delete "libc" %final-inputs))))
265 (if libc
266 `(("libc" ,libc)
55de892b 267 ("xkernel-headers" ;the target headers
aa27987f 268 ,@(assoc-ref (package-propagated-inputs libc)
55de892b 269 "kernel-headers"))
827d2891 270 ,@inputs)
17bb886f
LC
271 inputs))))
272
0de71c23
LC
273 (inputs '())
274
17bb886f 275 ;; Only search target inputs, not host inputs.
efc4eb14 276 ;; Note: See <http://bugs.gnu.org/22186> for why not 'CPATH'.
17bb886f
LC
277 (search-paths
278 (list (search-path-specification
efc4eb14
JN
279 (variable "CROSS_C_INCLUDE_PATH")
280 (files '("include")))
281 (search-path-specification
282 (variable "CROSS_CPLUS_INCLUDE_PATH")
283 (files '("include")))
284 (search-path-specification
285 (variable "CROSS_OBJC_INCLUDE_PATH")
286 (files '("include")))
287 (search-path-specification
288 (variable "CROSS_OBJCPLUS_INCLUDE_PATH")
af070955 289 (files '("include")))
17bb886f
LC
290 (search-path-specification
291 (variable "CROSS_LIBRARY_PATH")
af070955 292 (files '("lib" "lib64")))))
17bb886f 293 (native-search-paths '())))
827d2891
LC
294
295(define* (cross-libc target
296 #:optional
297 (xgcc (cross-gcc target))
298 (xbinutils (cross-binutils target)))
299 "Return a libc cross-built for TARGET, a GNU triplet. Use XGCC and
300XBINUTILS and the cross tool chain."
301 (define xlinux-headers
302 (package (inherit linux-libre-headers)
303 (name (string-append (package-name linux-libre-headers)
304 "-cross-" target))
305 (arguments
0d5a559f
LC
306 (substitute-keyword-arguments
307 `(#:implicit-cross-inputs? #f
308 ,@(package-arguments linux-libre-headers))
827d2891
LC
309 ((#:phases phases)
310 `(alist-replace
311 'build
312 (lambda _
313 (setenv "ARCH" ,(system->linux-architecture target))
314 (format #t "`ARCH' set to `~a' (cross compiling)~%" (getenv "ARCH"))
315
316 (and (zero? (system* "make" "defconfig"))
317 (zero? (system* "make" "mrproper" "headers_check"))))
318 ,phases))))
0de71c23
LC
319 (native-inputs `(("cross-gcc" ,xgcc)
320 ("cross-binutils" ,xbinutils)
321 ,@(package-native-inputs linux-libre-headers)))))
827d2891
LC
322
323 (package (inherit glibc)
324 (name (string-append "glibc-cross-" target))
325 (arguments
326 (substitute-keyword-arguments
0d5a559f
LC
327 `(;; Disable stripping (see above.)
328 #:strip-binaries? #f
329
330 ;; This package is used as a target input, but it should not have
331 ;; the usual cross-compilation inputs since that would include
332 ;; itself.
333 #:implicit-cross-inputs? #f
334
0158ca7f
JN
335 ;; We need SRFI 26.
336 #:modules ((guix build gnu-build-system)
337 (guix build utils)
338 (srfi srfi-26))
339
827d2891
LC
340 ,@(package-arguments glibc))
341 ((#:configure-flags flags)
342 `(cons ,(string-append "--host=" target)
343 ,flags))
344 ((#:phases phases)
345 `(alist-cons-before
55de892b 346 'configure 'set-cross-kernel-headers-path
827d2891 347 (lambda* (#:key inputs #:allow-other-keys)
eb74eb41
LF
348 (let* ((kernel (assoc-ref inputs "kernel-headers"))
349 (cpath (string-append kernel "/include")))
efc4eb14
JN
350 (for-each (cut setenv <> cpath)
351 '("CROSS_C_INCLUDE_PATH"
352 "CROSS_CPLUS_INCLUDE_PATH"
353 "CROSS_OBJC_INCLUDE_PATH"
354 "CROSS_OBJCPLUS_INCLUDE_PATH"))
827d2891
LC
355 #t))
356 ,phases))))
a4627d49 357
55de892b
MR
358 ;; Shadow the native "kernel-headers" because glibc's recipe expects the
359 ;; "kernel-headers" input to point to the right thing.
360 (propagated-inputs `(("kernel-headers" ,xlinux-headers)))
0d5a559f 361
12b0dbd4
LC
362 ;; FIXME: 'static-bash' should really be an input, not a native input, but
363 ;; to do that will require building an intermediate cross libc.
364 (inputs '())
365
0de71c23
LC
366 (native-inputs `(("cross-gcc" ,xgcc)
367 ("cross-binutils" ,xbinutils)
12b0dbd4 368 ,@(package-inputs glibc) ;FIXME: static-bash
0de71c23 369 ,@(package-native-inputs glibc)))))
827d2891
LC
370
371\f
372;;;
373;;; Concrete cross toolchains.
374;;;
375
376(define-public xgcc-mips64el
6ee01481
LC
377 (let* ((triplet "mips64el-linux-gnuabi64") ;N64 ABI
378 (xgcc (cross-gcc triplet
379 (cross-binutils triplet)
380 (cross-libc triplet))))
381 ;; Don't attempt to build this cross-compiler on i686;
382 ;; see <http://bugs.gnu.org/19598>.
383 (package (inherit xgcc)
9fdd80e8
LC
384 (supported-systems (fold delete
385 (package-supported-systems xgcc)
386 '("mips64el-linux" "i686-linux"))))))
827d2891 387
9d307460
LC
388(define-public xgcc-xtensa
389 ;; Bare-bones Xtensa cross-compiler, used to build the Atheros firmware.
390 (cross-gcc "xtensa-elf"))
391
3f00ff8b 392(define-public xgcc-armhf
9fdd80e8
LC
393 (let* ((triplet "arm-linux-gnueabihf")
394 (xgcc (cross-gcc triplet
395 (cross-binutils triplet)
396 (cross-libc triplet))))
397 (package (inherit xgcc)
398 (supported-systems (delete "armhf-linux" %supported-systems)))))
3f00ff8b 399
827d2891
LC
400;; (define-public xgcc-armel
401;; (let ((triplet "armel-linux-gnueabi"))
402;; (cross-gcc triplet
403;; (cross-binutils triplet)
404;; (cross-libc triplet))))