projects/guix·blob java-team · refs

223 lines · 9.1 KiB

1;;; GNU Guix --- Functional package management for GNU
2;;; Copyright © 2013, 2014, 2015, 2016, 2020 Ludovic Courtès <[email protected]>
3;;; Copyright © 2014, 2015, 2018 Mark H Weaver <[email protected]>
4;;; Copyright © 2016 Jan Nieuwenhuizen <[email protected]>
5;;; Copyright © 2016 Manolis Fragkiskos Ragkousis <[email protected]>
6;;; Copyright © 2019 Marius Bakke <[email protected]>
7;;; Copyright © 2019 Carl Dong <[email protected]>
8;;;
9;;; This file is part of GNU Guix.
10;;;
11;;; GNU Guix is free software; you can redistribute it and/or modify it
12;;; under the terms of the GNU General Public License as published by
13;;; the Free Software Foundation; either version 3 of the License, or (at
14;;; your option) any later version.
15;;;
16;;; GNU Guix is distributed in the hope that it will be useful, but
17;;; WITHOUT ANY WARRANTY; without even the implied warranty of
18;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19;;; GNU General Public License for more details.
20;;;
21;;; You should have received a copy of the GNU General Public License
22;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
23
24(define-module (gnu build cross-toolchain)
25 #:use-module (guix build utils)
26 #:use-module (guix build gnu-build-system)
27 #:use-module (srfi srfi-1)
28 #:use-module (srfi srfi-26)
29 #:use-module (ice-9 match)
30 #:use-module (ice-9 ftw)
31 #:export (cross-gcc-build-phases))
32
33;;; Commentary:
34;;;
35;;; This module provides tools to build a cross-compiler.
36;;;
37;;; Code:
38
39(define %gcc-include-paths
40 ;; Environment variables for header search paths.
41 ;; Note: See <http://bugs.gnu.org/22186> for why not 'CPATH'.
42 '("C_INCLUDE_PATH"
43 "CPLUS_INCLUDE_PATH"
44 "OBJC_INCLUDE_PATH"
45 "OBJCPLUS_INCLUDE_PATH"))
46
47(define %gcc-cross-include-paths
48 ;; Search path for target headers when cross-compiling.
49 (map (cut string-append "CROSS_" <>) %gcc-include-paths))
50
51(define* (patch-genmultilib-shebang #:key inputs native-inputs #:allow-other-keys)
52 "Patch-shebang in the gcc/genmultilib file doesn't work as it contains several
53scripts inside, each with a #!/bin/sh that needs patching."
54 (substitute* "gcc/genmultilib"
55 (("#!/bin/sh") (string-append "#!" (which "sh")))))
56
57(define* (make-cross-binutils-visible #:key outputs inputs target
58 #:allow-other-keys)
59 "Create symlinks for 'as', 'nm', and 'ld' in the \"out\" output, under
60libexec/gcc, so that the cross-GCC can find them."
61 (let* ((out (assoc-ref outputs "out"))
62 (libexec (string-append out "/libexec/gcc/" target))
63 (binutils (string-append (assoc-ref inputs "binutils-cross")
64 "/bin/" target "-"))
65 (wrapper (string-append (assoc-ref inputs "ld-wrapper-cross")
66 "/bin/" target "-ld")))
67 (for-each (lambda (file)
68 (symlink (string-append binutils file)
69 (string-append libexec "/" file)))
70 '("as" "nm"))
71 (symlink wrapper (string-append libexec "/ld"))
72 #t))
73
74(define* (set-cross-path #:key inputs #:allow-other-keys)
75 "Add the cross kernel headers to CROSS_CPATH, and remove them from
76C_INCLUDE_PATH et al."
77 (match (assoc-ref inputs "libc")
78 ((? string? libc)
79 (let ((kernel (assoc-ref inputs "xkernel-headers")))
80 (define (cross? x)
81 ;; Return #t if X is a cross-libc or cross Linux.
82 (or (string-prefix? libc x)
83 (string-prefix? kernel x)))
84
85 (let ((cpath (string-append libc "/include"
86 ":" kernel "/include")))
87 (for-each (cut setenv <> cpath)
88 %gcc-cross-include-paths))
89
90 (setenv "CROSS_LIBRARY_PATH"
91 (string-append libc "/lib:" kernel "/lib")) ;for Hurd's libihash
92
93 (for-each (lambda (var)
94 (and=> (getenv var)
95 (lambda (value)
96 (let* ((path (search-path-as-string->list value))
97 (native-path (list->search-path-as-string
98 (remove cross? path) ":")))
99 (setenv var native-path)))))
100 (cons "LIBRARY_PATH" %gcc-include-paths))
101 #t))
102 (#f
103 ;; We're building the sans-libc cross-compiler, so nothing to do.
104 #t)))
105
106(define* (set-cross-path/mingw #:key inputs target #:allow-other-keys)
107 "Add the cross MinGW headers to CROSS_C_*_INCLUDE_PATH, and remove them from
108C_*INCLUDE_PATH."
109 (let ((libc (assoc-ref inputs "libc"))
110 (gcc (assoc-ref inputs "gcc")))
111 (define (cross? x)
112 (and libc (string-prefix? libc x)))
113
114 (define (unpacked-mingw-dir)
115 (match (scandir "." (lambda (name)
116 (string-contains name "mingw-w64")))
117 ((mingw-dir)
118 (string-append
119 (getcwd) "/" mingw-dir "/mingw-w64-headers"))))
120
121 (if libc
122 (let ((cpath (string-append libc "/include"
123 ":" libc "/" target "/include")))
124 (for-each (cut setenv <> cpath)
125 %gcc-cross-include-paths))
126
127 ;; libc is false, so we are building xgcc-sans-libc.
128 ;; Add essential headers from mingw-w64.
129 (let ((mingw-source (assoc-ref inputs "mingw-source")))
130 (invoke "tar" "xvf" mingw-source)
131 (let ((mingw-headers (unpacked-mingw-dir)))
132 ;; We need _mingw.h which will gets built from _mingw.h.in by
133 ;; mingw-w64's configure. We cannot configure mingw-w64 until we
134 ;; have xgcc-sans-libc; substitute to the rescue.
135 (copy-file (string-append mingw-headers "/crt/_mingw.h.in")
136 (string-append mingw-headers "/crt/_mingw.h"))
137
138 (substitute* (string-append mingw-headers "/crt/_mingw.h")
139 (("@MINGW_HAS_SECURE_API@")
140 "#define MINGW_HAS_SECURE_API 1")
141 (("@DEFAULT_WIN32_WINNT@")
142 "0x502")
143 (("@DEFAULT_MSVCRT_VERSION@")
144 "0x700"))
145
146 (let ((cpath (string-append mingw-headers "/include"
147 ":" mingw-headers "/crt"
148 ":" mingw-headers
149 "/defaults/include")))
150 (for-each (cut setenv <> cpath)
151 (cons "CROSS_LIBRARY_PATH"
152 %gcc-cross-include-paths))))))
153
154 (when libc
155 (setenv "CROSS_LIBRARY_PATH"
156 (string-append libc "/lib"
157 ":" libc "/" target "/lib")))
158
159 (setenv "CPP" (string-append gcc "/bin/cpp"))
160 (for-each (lambda (var)
161 (and=> (getenv var)
162 (lambda (value)
163 (let* ((path (search-path-as-string->list
164 value))
165 (native-path (list->search-path-as-string
166 (remove cross? path) ":")))
167 (setenv var native-path)))))
168 (cons "LIBRARY_PATH" %gcc-include-paths))
169 #t))
170
171(define* (set-cross-path/avr #:key inputs #:allow-other-keys)
172 (match (assoc-ref inputs "libc")
173 ((? string? libc)
174 (define (cross? x)
175 ;; Return #t if X is a cross-libc.
176 (string-prefix? libc x))
177
178 (let ((cpath (string-append libc "/avr/include")))
179 (for-each (cut setenv <> cpath)
180 %gcc-cross-include-paths))
181
182 (setenv "CROSS_LIBRARY_PATH"
183 (string-append libc "/avr/lib"))
184
185 (for-each (lambda (var)
186 (and=> (getenv var)
187 (lambda (value)
188 (let* ((path (search-path-as-string->list value))
189 (native-path (list->search-path-as-string
190 (remove cross? path) ":")))
191 (setenv var native-path)))))
192 (cons "LIBRARY_PATH" %gcc-include-paths)))
193 ;; AVR sans-libc cross-compiler.
194 (else #t)))
195
196(define (install-strip . _)
197 "Install a stripped GCC."
198 ;; Unlike our 'strip' phase, this will do the right thing for
199 ;; cross-compilers.
200 (invoke "make" "install-strip"))
201
202(define* (cross-gcc-build-phases target
203 #:optional (phases %standard-phases))
204 "Modify PHASES to include everything needed to build a cross-GCC for TARGET,
205a target triplet."
206 (modify-phases phases
207 (add-after 'unpack 'patch-genmultilib-shebang
208 patch-genmultilib-shebang)
209 (add-before 'configure 'set-cross-path
210 ;; This mingw32 target checking logic should match that of target-mingw?
211 ;; in (guix utils), but (guix utils) is too large too copy over to the
212 ;; build side entirely and for now we have no way to select variables to
213 ;; copy over. See (gnu packages cross-base) for more details.
214 (cond
215 ((string-suffix? "-mingw32" target)
216 (cut set-cross-path/mingw #:target target <...>))
217 ((string-prefix? "avr" target) set-cross-path/avr)
218 (#t set-cross-path)))
219 (add-after 'install 'make-cross-binutils-visible
220 (cut make-cross-binutils-visible #:target target <...>))
221 (replace 'install install-strip)))
222
223;;; cross-toolchain.scm ends here