gnu: cross-gcc: Change patch so that `CROSS_LIBRARY_PATH' is honored.
[jackhill/guix/guix.git] / gnu / packages / cross-base.scm
CommitLineData
827d2891
LC
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)
f594028a 22 #:use-module (gnu packages gcc)
827d2891
LC
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
34(define (cross p target)
35 (package (inherit p)
36 (location (source-properties->location (current-source-location)))
37 (name (string-append (package-name p) "-cross-" target))
38 (arguments
39 (substitute-keyword-arguments (package-arguments p)
40 ((#:configure-flags flags)
41 `(cons ,(string-append "--target=" target)
42 ,flags))))))
43
44(define cross-binutils
45 (cut cross binutils <>))
46
47(define* (cross-gcc target
48 #:optional (xbinutils (cross-binutils target)) libc)
49 "Return a cross-compiler for TARGET, where TARGET is a GNU triplet. Use
50XBINUTILS as the associated cross-Binutils. If LIBC is false, then build a
51GCC that does not target a libc; otherwise, target that libc."
52 (define args
53 ;; Get the arguments as if we were building for TARGET. In particular, we
54 ;; want `glibc-dynamic-linker' to return the right thing.
55 (parameterize ((%current-system (gnu-triplet->nix-system target)))
56 (package-arguments gcc-4.7)))
57
58 (package (inherit gcc-4.7)
59 (name (string-append "gcc-cross-"
60 (if libc "" "sans-libc-")
61 target))
62 (arguments
63 `(#:implicit-inputs? #f
64 #:modules ((guix build gnu-build-system)
65 (guix build utils)
66 (ice-9 regex)
67 (srfi srfi-1)
68 (srfi srfi-26))
69 #:patches (list (assoc-ref %build-inputs "patch/cross-env-vars"))
70
71 ,@(substitute-keyword-arguments args
72 ((#:configure-flags flags)
73 `(append (list ,(string-append "--target=" target)
74 ,@(if libc
75 '()
76 `( ;; Disable features not needed at this stage.
77 "--disable-shared" "--enable-static"
78
79 ;; Disable C++ because libstdc++'s
80 ;; configure script otherwise fails with
81 ;; "Link tests are not allowed after
82 ;; GCC_NO_EXECUTABLES."
83 "--enable-languages=c"
84
85 "--disable-threads" ; libgcc, would need libc
86 "--disable-libmudflap"
87 "--disable-libgomp"
88 "--disable-libssp"
89 "--disable-libquadmath"
90 "--disable-decimal-float" ; would need libc
91 )))
92
93 ,(if libc
94 flags
95 `(remove (cut string-match "--enable-languages.*" <>)
96 ,flags))))
97 ((#:make-flags flags)
98 (if libc
99 `(let ((libc (assoc-ref %build-inputs "libc")))
100 ;; FLAGS_FOR_TARGET are needed for the target libraries to
101 ;; receive the -Bxxx for the startfiles.
102 (cons (string-append "FLAGS_FOR_TARGET=-B" libc "/lib")
103 ,flags))
104 flags))
105 ((#:phases phases)
106 (let ((phases
107 `(alist-cons-after
108 'install 'make-cross-binutils-visible
109 (lambda* (#:key outputs inputs #:allow-other-keys)
110 (let* ((out (assoc-ref outputs "out"))
111 (libexec (string-append out "/libexec/gcc/"
112 ,target))
113 (binutils (string-append
114 (assoc-ref inputs "binutils-cross")
115 "/bin/" ,target "-")))
116 (for-each (lambda (file)
117 (symlink (string-append binutils file)
118 (string-append libexec "/"
119 file)))
120 '("as" "ld" "nm"))
121 #t))
122 ,phases)))
123 (if libc
124 `(alist-cons-before
125 'configure 'set-cross-path
126 (lambda* (#:key inputs #:allow-other-keys)
127 ;; Add the cross Linux headers to CROSS_CPATH, and remove
128 ;; them from CPATH.
129 (let ((libc (assoc-ref inputs "libc"))
130 (linux (assoc-ref inputs
131 "libc/cross-linux-headers")))
132 (define (cross? x)
133 ;; Return #t if X is a cross-libc or cross Linux.
134 (or (string-prefix? libc x)
135 (string-prefix? linux x)))
136
137 (setenv "CROSS_CPATH"
138 (string-append libc "/include:"
139 linux "/include"))
140 (setenv "CROSS_LIBRARY_PATH"
141 (string-append libc "/lib"))
142
143 (let ((cpath (search-path-as-string->list
144 (getenv "CPATH")))
145 (libpath (search-path-as-string->list
146 (getenv "LIBRARY_PATH"))))
147 (setenv "CPATH"
148 (list->search-path-as-string
149 (remove cross? cpath) ":"))
150 (setenv "LIBRARY_PATH"
151 (list->search-path-as-string
152 (remove cross? libpath) ":"))
153 #t)))
154 ,phases)
155 phases)))
156 ((#:strip-binaries? _)
157 ;; Disable stripping as this can break binaries, with object files
158 ;; of libgcc.a showing up as having an unknown architecture. See
159 ;; <http://lists.fedoraproject.org/pipermail/arm/2010-August/000663.html>
160 ;; for instance.
161 #f))))
162 (inputs
163 `(("patch/cross-env-vars"
164 ,(search-patch "gcc-cross-environment-variables.patch"))
165
166 ("binutils-cross" ,xbinutils)
167
168 ;; Call it differently so that the builder can check whether the "libc"
169 ;; input is #f.
170 ("libc-native" ,@(assoc-ref %final-inputs "libc"))
171
172 ;; Remaining inputs.
173 ,@(let ((inputs (append (package-inputs gcc-4.7)
174 (alist-delete "libc" %final-inputs))))
175 (if libc
176 `(("libc" ,libc)
177 ,@inputs)
178 inputs))))))
179
180(define* (cross-libc target
181 #:optional
182 (xgcc (cross-gcc target))
183 (xbinutils (cross-binutils target)))
184 "Return a libc cross-built for TARGET, a GNU triplet. Use XGCC and
185XBINUTILS and the cross tool chain."
186 (define xlinux-headers
187 (package (inherit linux-libre-headers)
188 (name (string-append (package-name linux-libre-headers)
189 "-cross-" target))
190 (arguments
191 (substitute-keyword-arguments (package-arguments linux-libre-headers)
192 ((#:phases phases)
193 `(alist-replace
194 'build
195 (lambda _
196 (setenv "ARCH" ,(system->linux-architecture target))
197 (format #t "`ARCH' set to `~a' (cross compiling)~%" (getenv "ARCH"))
198
199 (and (zero? (system* "make" "defconfig"))
200 (zero? (system* "make" "mrproper" "headers_check"))))
201 ,phases))))
202 (inputs `(("cross-gcc" ,xgcc)
203 ("cross-binutils" ,xbinutils)
204 ,@(package-inputs linux-libre-headers)))))
205
206 (package (inherit glibc)
207 (name (string-append "glibc-cross-" target))
208 (arguments
209 (substitute-keyword-arguments
210 `(#:strip-binaries? #f ; disable stripping (see above)
211 ,@(package-arguments glibc))
212 ((#:configure-flags flags)
213 `(cons ,(string-append "--host=" target)
214 ,flags))
215 ((#:phases phases)
216 `(alist-cons-before
217 'configure 'set-cross-linux-headers-path
218 (lambda* (#:key inputs #:allow-other-keys)
219 (let ((linux (assoc-ref inputs "cross-linux-headers")))
220 (setenv "CROSS_CPATH"
221 (string-append linux "/include"))
222 #t))
223 ,phases))))
224 (propagated-inputs `(("cross-linux-headers" ,xlinux-headers)))
225 (inputs `(("cross-gcc" ,xgcc)
226 ("cross-binutils" ,xbinutils)
227 ,@(package-inputs glibc)))))
228
229\f
230;;;
231;;; Concrete cross toolchains.
232;;;
233
234(define-public xgcc-mips64el
235 (let ((triplet "mips64el-linux-gnu"))
236 (cross-gcc triplet
237 (cross-binutils triplet)
238 (cross-libc triplet))))
239
240;; (define-public xgcc-armel
241;; (let ((triplet "armel-linux-gnueabi"))
242;; (cross-gcc triplet
243;; (cross-binutils triplet)
244;; (cross-libc triplet))))