gnu: texlive: Update to 2013.
[jackhill/guix/guix.git] / gnu / packages / cross-base.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2013 Ludovic Courtès <ludo@gnu.org>
3 ;;;
4 ;;; This file is part of GNU Guix.
5 ;;;
6 ;;; GNU Guix is free software; you can redistribute it and/or modify it
7 ;;; under the terms of the GNU General Public License as published by
8 ;;; the Free Software Foundation; either version 3 of the License, or (at
9 ;;; your option) any later version.
10 ;;;
11 ;;; GNU Guix is distributed in the hope that it will be useful, but
12 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;;; GNU General Public License for more details.
15 ;;;
16 ;;; You should have received a copy of the GNU General Public License
17 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
18
19 (define-module (gnu packages cross-base)
20 #:use-module (guix licenses)
21 #:use-module (gnu packages)
22 #:use-module (gnu packages gcc)
23 #:use-module (gnu packages base)
24 #:use-module (gnu packages linux)
25 #:use-module (guix packages)
26 #:use-module (guix download)
27 #:use-module (guix utils)
28 #:use-module (guix build-system gnu)
29 #:use-module (guix build-system trivial)
30 #:use-module (srfi srfi-1)
31 #:use-module (srfi srfi-26)
32 #:use-module (ice-9 match)
33 #:export (cross-binutils
34 cross-libc
35 cross-gcc))
36
37 (define (cross p target)
38 (package (inherit p)
39 (location (source-properties->location (current-source-location)))
40 (name (string-append (package-name p) "-cross-" target))
41 (arguments
42 (substitute-keyword-arguments (package-arguments p)
43 ((#:configure-flags flags)
44 `(cons ,(string-append "--target=" target)
45 ,flags))))))
46
47 (define (cross-binutils target)
48 "Return a cross-Binutils for TARGET."
49 (let ((binutils (package (inherit binutils)
50 (arguments
51 (substitute-keyword-arguments (package-arguments
52 binutils)
53 ((#:configure-flags flags)
54 ;; Build with `--with-sysroot' so that ld honors
55 ;; DT_RUNPATH entries when searching for a needed
56 ;; library. This works because as a side effect
57 ;; `genscripts.sh' sets `USE_LIBPATH=yes', which tells
58 ;; elf32.em to use DT_RUNPATH in its search list.
59 ;; See <http://sourceware.org/ml/binutils/2013-05/msg00312.html>.
60 ;;
61 ;; In theory choosing / as the sysroot could lead ld
62 ;; to pick up native libs instead of target ones. In
63 ;; practice the RUNPATH of target libs only refers to
64 ;; target libs, not native libs, so this is safe.
65 `(cons "--with-sysroot=/" ,flags)))))))
66 (cross binutils target)))
67
68 (define* (cross-gcc target
69 #:optional (xbinutils (cross-binutils target)) libc)
70 "Return a cross-compiler for TARGET, where TARGET is a GNU triplet. Use
71 XBINUTILS as the associated cross-Binutils. If LIBC is false, then build a
72 GCC that does not target a libc; otherwise, target that libc."
73 (package (inherit gcc-4.7)
74 (name (string-append "gcc-cross-"
75 (if libc "" "sans-libc-")
76 target))
77 (arguments
78 `(#:implicit-inputs? #f
79 #:modules ((guix build gnu-build-system)
80 (guix build utils)
81 (ice-9 regex)
82 (srfi srfi-1)
83 (srfi srfi-26))
84 #:patches (list (assoc-ref %build-inputs "patch/cross-env-vars"))
85
86 ,@(substitute-keyword-arguments (package-arguments gcc-4.7)
87 ((#:configure-flags flags)
88 `(append (list ,(string-append "--target=" target)
89 ,@(gcc-configure-flags-for-triplet target)
90 ,@(if libc
91 '()
92 `( ;; Disable features not needed at this stage.
93 "--disable-shared" "--enable-static"
94
95 ;; Disable C++ because libstdc++'s
96 ;; configure script otherwise fails with
97 ;; "Link tests are not allowed after
98 ;; GCC_NO_EXECUTABLES."
99 "--enable-languages=c"
100
101 "--disable-threads" ; libgcc, would need libc
102 "--disable-libmudflap"
103 "--disable-libgomp"
104 "--disable-libssp"
105 "--disable-libquadmath"
106 "--disable-decimal-float" ; would need libc
107 )))
108
109 ,(if libc
110 flags
111 `(remove (cut string-match "--enable-languages.*" <>)
112 ,flags))))
113 ((#:make-flags flags)
114 (if libc
115 `(let ((libc (assoc-ref %build-inputs "libc")))
116 ;; FLAGS_FOR_TARGET are needed for the target libraries to
117 ;; receive the -Bxxx for the startfiles.
118 (cons (string-append "FLAGS_FOR_TARGET=-B" libc "/lib")
119 ,flags))
120 flags))
121 ((#:phases phases)
122 (let ((phases
123 `(alist-cons-after
124 'install 'make-cross-binutils-visible
125 (lambda* (#:key outputs inputs #:allow-other-keys)
126 (let* ((out (assoc-ref outputs "out"))
127 (libexec (string-append out "/libexec/gcc/"
128 ,target))
129 (binutils (string-append
130 (assoc-ref inputs "binutils-cross")
131 "/bin/" ,target "-")))
132 (for-each (lambda (file)
133 (symlink (string-append binutils file)
134 (string-append libexec "/"
135 file)))
136 '("as" "ld" "nm"))
137 #t))
138 ,phases)))
139 (if libc
140 `(alist-cons-before
141 'configure 'set-cross-path
142 (lambda* (#:key inputs #:allow-other-keys)
143 ;; Add the cross Linux headers to CROSS_CPATH, and remove
144 ;; them from CPATH.
145 (let ((libc (assoc-ref inputs "libc"))
146 (linux (assoc-ref inputs
147 "libc/cross-linux-headers")))
148 (define (cross? x)
149 ;; Return #t if X is a cross-libc or cross Linux.
150 (or (string-prefix? libc x)
151 (string-prefix? linux x)))
152
153 (setenv "CROSS_CPATH"
154 (string-append libc "/include:"
155 linux "/include"))
156 (setenv "CROSS_LIBRARY_PATH"
157 (string-append libc "/lib"))
158
159 (let ((cpath (search-path-as-string->list
160 (getenv "CPATH")))
161 (libpath (search-path-as-string->list
162 (getenv "LIBRARY_PATH"))))
163 (setenv "CPATH"
164 (list->search-path-as-string
165 (remove cross? cpath) ":"))
166 (setenv "LIBRARY_PATH"
167 (list->search-path-as-string
168 (remove cross? libpath) ":"))
169 #t)))
170 ,phases)
171 phases)))
172 ((#:strip-binaries? _)
173 ;; Disable stripping as this can break binaries, with object files
174 ;; of libgcc.a showing up as having an unknown architecture. See
175 ;; <http://lists.fedoraproject.org/pipermail/arm/2010-August/000663.html>
176 ;; for instance.
177 #f))))
178
179 (native-inputs
180 `(("patch/cross-env-vars"
181 ,(search-patch "gcc-cross-environment-variables.patch"))
182
183 ("binutils-cross" ,xbinutils)
184
185 ;; Call it differently so that the builder can check whether the "libc"
186 ;; input is #f.
187 ("libc-native" ,@(assoc-ref %final-inputs "libc"))
188
189 ;; Remaining inputs.
190 ,@(let ((inputs (append (package-inputs gcc-4.7)
191 (alist-delete "libc" %final-inputs))))
192 (if libc
193 `(("libc" ,libc)
194 ,@inputs)
195 inputs))))
196
197 (inputs '())
198
199 ;; Only search target inputs, not host inputs.
200 (search-paths
201 (list (search-path-specification
202 (variable "CROSS_CPATH")
203 (directories '("include")))
204 (search-path-specification
205 (variable "CROSS_LIBRARY_PATH")
206 (directories '("lib" "lib64")))))
207 (native-search-paths '())))
208
209 (define* (cross-libc target
210 #:optional
211 (xgcc (cross-gcc target))
212 (xbinutils (cross-binutils target)))
213 "Return a libc cross-built for TARGET, a GNU triplet. Use XGCC and
214 XBINUTILS and the cross tool chain."
215 (define xlinux-headers
216 (package (inherit linux-libre-headers)
217 (name (string-append (package-name linux-libre-headers)
218 "-cross-" target))
219 (arguments
220 (substitute-keyword-arguments (package-arguments linux-libre-headers)
221 ((#:phases phases)
222 `(alist-replace
223 'build
224 (lambda _
225 (setenv "ARCH" ,(system->linux-architecture target))
226 (format #t "`ARCH' set to `~a' (cross compiling)~%" (getenv "ARCH"))
227
228 (and (zero? (system* "make" "defconfig"))
229 (zero? (system* "make" "mrproper" "headers_check"))))
230 ,phases))))
231 (native-inputs `(("cross-gcc" ,xgcc)
232 ("cross-binutils" ,xbinutils)
233 ,@(package-native-inputs linux-libre-headers)))))
234
235 (package (inherit glibc)
236 (name (string-append "glibc-cross-" target))
237 (arguments
238 (substitute-keyword-arguments
239 `(#:strip-binaries? #f ; disable stripping (see above)
240 ,@(package-arguments glibc))
241 ((#:configure-flags flags)
242 `(cons ,(string-append "--host=" target)
243 ,flags))
244 ((#:phases phases)
245 `(alist-cons-before
246 'configure 'set-cross-linux-headers-path
247 (lambda* (#:key inputs #:allow-other-keys)
248 (let ((linux (assoc-ref inputs "cross-linux-headers")))
249 (setenv "CROSS_CPATH"
250 (string-append linux "/include"))
251 #t))
252 ,phases))))
253
254 (propagated-inputs `(("cross-linux-headers" ,xlinux-headers)))
255 (native-inputs `(("cross-gcc" ,xgcc)
256 ("cross-binutils" ,xbinutils)
257 ,@(package-native-inputs glibc)))))
258
259 \f
260 ;;;
261 ;;; Concrete cross toolchains.
262 ;;;
263
264 (define-public xgcc-mips64el
265 (let ((triplet "mips64el-linux-gnuabi64")) ; N64 ABI
266 (cross-gcc triplet
267 (cross-binutils triplet)
268 (cross-libc triplet))))
269
270 ;; (define-public xgcc-armel
271 ;; (let ((triplet "armel-linux-gnueabi"))
272 ;; (cross-gcc triplet
273 ;; (cross-binutils triplet)
274 ;; (cross-libc triplet))))