| 1 | ;;; GNU Guix --- Functional package management for GNU |
| 2 | ;;; Copyright © 2013, 2014, 2015 Ludovic Courtès <ludo@gnu.org> |
| 3 | ;;; Copyright © 2014, 2015 Mark H Weaver <mhw@netris.org> |
| 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) |
| 23 | #:use-module (gnu packages gcc) |
| 24 | #:use-module (gnu packages base) |
| 25 | #:use-module (gnu packages commencement) |
| 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) |
| 34 | #:use-module (ice-9 match) |
| 35 | #:export (cross-binutils |
| 36 | cross-libc |
| 37 | cross-gcc)) |
| 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 | |
| 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 | |
| 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. |
| 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))))))) |
| 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))) |
| 82 | |
| 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.)" |
| 86 | ;; Set the current target system so that 'glibc-dynamic-linker' returns the |
| 87 | ;; right name. |
| 88 | (parameterize ((%current-target-system target)) |
| 89 | (substitute-keyword-arguments (package-arguments gcc-4.8) |
| 90 | ((#:configure-flags flags) |
| 91 | `(append (list ,(string-append "--target=" target) |
| 92 | ,@(if libc |
| 93 | '() |
| 94 | `( ;; Disable features not needed at this stage. |
| 95 | "--disable-shared" "--enable-static" |
| 96 | |
| 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" |
| 101 | |
| 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 | ))) |
| 110 | |
| 111 | ,(if libc |
| 112 | flags |
| 113 | `(remove (cut string-match "--enable-languages.*" <>) |
| 114 | ,flags)))) |
| 115 | ((#:make-flags flags) |
| 116 | (if libc |
| 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") |
| 133 | "/bin/" ,target "-")) |
| 134 | (wrapper (string-append |
| 135 | (assoc-ref inputs "ld-wrapper-cross") |
| 136 | "/bin/" ,target "-ld"))) |
| 137 | (for-each (lambda (file) |
| 138 | (symlink (string-append binutils file) |
| 139 | (string-append libexec "/" |
| 140 | file))) |
| 141 | '("as" "nm")) |
| 142 | (symlink wrapper (string-append libexec "/ld")) |
| 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))) |
| 158 | |
| 159 | (setenv "CROSS_CPATH" |
| 160 | (string-append libc "/include:" |
| 161 | linux "/include")) |
| 162 | (setenv "CROSS_LIBRARY_PATH" |
| 163 | (string-append libc "/lib")) |
| 164 | |
| 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)))) |
| 184 | |
| 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 | |
| 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." |
| 197 | (package (inherit gcc-4.8) |
| 198 | (name (string-append "gcc-cross-" |
| 199 | (if libc "" "sans-libc-") |
| 200 | target)) |
| 201 | (source (origin (inherit (package-source gcc-4.8)) |
| 202 | (patches |
| 203 | (cons (search-patch "gcc-cross-environment-variables.patch") |
| 204 | (cross-gcc-patches target))))) |
| 205 | |
| 206 | ;; For simplicity, use a single output. Otherwise libgcc_s & co. are not |
| 207 | ;; found by default, etc. |
| 208 | (outputs '("out")) |
| 209 | |
| 210 | (arguments |
| 211 | `(#:implicit-inputs? #f |
| 212 | #:modules ((guix build gnu-build-system) |
| 213 | (guix build utils) |
| 214 | (ice-9 regex) |
| 215 | (srfi srfi-1) |
| 216 | (srfi srfi-26)) |
| 217 | |
| 218 | ,@(cross-gcc-arguments target libc))) |
| 219 | |
| 220 | (native-inputs |
| 221 | `(("ld-wrapper-cross" ,(make-ld-wrapper |
| 222 | (string-append "ld-wrapper-" target) |
| 223 | #:target target |
| 224 | #:binutils xbinutils)) |
| 225 | ("binutils-cross" ,xbinutils) |
| 226 | |
| 227 | ;; Call it differently so that the builder can check whether the "libc" |
| 228 | ;; input is #f. |
| 229 | ("libc-native" ,@(assoc-ref %final-inputs "libc")) |
| 230 | |
| 231 | ;; Remaining inputs. |
| 232 | ,@(let ((inputs (append (package-inputs gcc-4.8) |
| 233 | (alist-delete "libc" %final-inputs)))) |
| 234 | (if libc |
| 235 | `(("libc" ,libc) |
| 236 | ,@inputs) |
| 237 | inputs)))) |
| 238 | |
| 239 | (inputs '()) |
| 240 | |
| 241 | ;; Only search target inputs, not host inputs. |
| 242 | (search-paths |
| 243 | (list (search-path-specification |
| 244 | (variable "CROSS_CPATH") |
| 245 | (files '("include"))) |
| 246 | (search-path-specification |
| 247 | (variable "CROSS_LIBRARY_PATH") |
| 248 | (files '("lib" "lib64"))))) |
| 249 | (native-search-paths '()))) |
| 250 | |
| 251 | (define* (cross-libc target |
| 252 | #:optional |
| 253 | (xgcc (cross-gcc target)) |
| 254 | (xbinutils (cross-binutils target))) |
| 255 | "Return a libc cross-built for TARGET, a GNU triplet. Use XGCC and |
| 256 | XBINUTILS and the cross tool chain." |
| 257 | (define xlinux-headers |
| 258 | (package (inherit linux-libre-headers) |
| 259 | (name (string-append (package-name linux-libre-headers) |
| 260 | "-cross-" target)) |
| 261 | (arguments |
| 262 | (substitute-keyword-arguments |
| 263 | `(#:implicit-cross-inputs? #f |
| 264 | ,@(package-arguments linux-libre-headers)) |
| 265 | ((#:phases phases) |
| 266 | `(alist-replace |
| 267 | 'build |
| 268 | (lambda _ |
| 269 | (setenv "ARCH" ,(system->linux-architecture target)) |
| 270 | (format #t "`ARCH' set to `~a' (cross compiling)~%" (getenv "ARCH")) |
| 271 | |
| 272 | (and (zero? (system* "make" "defconfig")) |
| 273 | (zero? (system* "make" "mrproper" "headers_check")))) |
| 274 | ,phases)))) |
| 275 | (native-inputs `(("cross-gcc" ,xgcc) |
| 276 | ("cross-binutils" ,xbinutils) |
| 277 | ,@(package-native-inputs linux-libre-headers))))) |
| 278 | |
| 279 | (package (inherit glibc) |
| 280 | (name (string-append "glibc-cross-" target)) |
| 281 | (arguments |
| 282 | (substitute-keyword-arguments |
| 283 | `(;; Disable stripping (see above.) |
| 284 | #:strip-binaries? #f |
| 285 | |
| 286 | ;; This package is used as a target input, but it should not have |
| 287 | ;; the usual cross-compilation inputs since that would include |
| 288 | ;; itself. |
| 289 | #:implicit-cross-inputs? #f |
| 290 | |
| 291 | ,@(package-arguments glibc)) |
| 292 | ((#:configure-flags flags) |
| 293 | `(cons ,(string-append "--host=" target) |
| 294 | ,flags)) |
| 295 | ((#:phases phases) |
| 296 | `(alist-cons-before |
| 297 | 'configure 'set-cross-linux-headers-path |
| 298 | (lambda* (#:key inputs #:allow-other-keys) |
| 299 | (let ((linux (assoc-ref inputs "linux-headers"))) |
| 300 | (setenv "CROSS_CPATH" |
| 301 | (string-append linux "/include")) |
| 302 | #t)) |
| 303 | ,phases)))) |
| 304 | |
| 305 | ;; Shadow the native "linux-headers" because glibc's recipe expect the |
| 306 | ;; "linux-headers" input to point to the right thing. |
| 307 | (propagated-inputs `(("linux-headers" ,xlinux-headers))) |
| 308 | |
| 309 | ;; FIXME: 'static-bash' should really be an input, not a native input, but |
| 310 | ;; to do that will require building an intermediate cross libc. |
| 311 | (inputs '()) |
| 312 | |
| 313 | (native-inputs `(("cross-gcc" ,xgcc) |
| 314 | ("cross-binutils" ,xbinutils) |
| 315 | ,@(package-inputs glibc) ;FIXME: static-bash |
| 316 | ,@(package-native-inputs glibc))))) |
| 317 | |
| 318 | \f |
| 319 | ;;; |
| 320 | ;;; Concrete cross toolchains. |
| 321 | ;;; |
| 322 | |
| 323 | (define-public xgcc-mips64el |
| 324 | (let* ((triplet "mips64el-linux-gnuabi64") ;N64 ABI |
| 325 | (xgcc (cross-gcc triplet |
| 326 | (cross-binutils triplet) |
| 327 | (cross-libc triplet)))) |
| 328 | ;; Don't attempt to build this cross-compiler on i686; |
| 329 | ;; see <http://bugs.gnu.org/19598>. |
| 330 | (package (inherit xgcc) |
| 331 | (supported-systems (fold delete |
| 332 | (package-supported-systems xgcc) |
| 333 | '("mips64el-linux" "i686-linux")))))) |
| 334 | |
| 335 | (define-public xgcc-avr |
| 336 | ;; AVR cross-compiler, used to build AVR-Libc. |
| 337 | (let ((triplet "avr")) |
| 338 | (cross-gcc triplet |
| 339 | (cross-binutils triplet)))) |
| 340 | |
| 341 | (define-public xgcc-xtensa |
| 342 | ;; Bare-bones Xtensa cross-compiler, used to build the Atheros firmware. |
| 343 | (cross-gcc "xtensa-elf")) |
| 344 | |
| 345 | (define-public xgcc-armhf |
| 346 | (let* ((triplet "arm-linux-gnueabihf") |
| 347 | (xgcc (cross-gcc triplet |
| 348 | (cross-binutils triplet) |
| 349 | (cross-libc triplet)))) |
| 350 | (package (inherit xgcc) |
| 351 | (supported-systems (delete "armhf-linux" %supported-systems))))) |
| 352 | |
| 353 | ;; (define-public xgcc-armel |
| 354 | ;; (let ((triplet "armel-linux-gnueabi")) |
| 355 | ;; (cross-gcc triplet |
| 356 | ;; (cross-binutils triplet) |
| 357 | ;; (cross-libc triplet)))) |