| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 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 | |
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 |
|
| 39 | (define %gcc-include-paths
|
| 40 | |
| 41 | |
| 42 | '("C_INCLUDE_PATH"
|
| 43 | "CPLUS_INCLUDE_PATH"
|
| 44 | "OBJC_INCLUDE_PATH"
|
| 45 | "OBJCPLUS_INCLUDE_PATH"))
|
| 46 |
|
| 47 | (define %gcc-cross-include-paths
|
| 48 | |
| 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
|
| 53 | scripts 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
|
| 60 | libexec/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
|
| 76 | C_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 | |
| 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")) |
| 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 | |
| 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
|
| 108 | C_*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 | |
| 128 | |
| 129 | (let ((mingw-source (assoc-ref inputs "mingw-source")))
|
| 130 | (invoke "tar" "xvf" mingw-source)
|
| 131 | (let ((mingw-headers (unpacked-mingw-dir)))
|
| 132 | |
| 133 | |
| 134 | |
| 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 | |
| 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 | |
| 194 | (else #t)))
|
| 195 |
|
| 196 | (define (install-strip . _)
|
| 197 | "Install a stripped GCC."
|
| 198 | |
| 199 | |
| 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,
|
| 205 | a 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 | |
| 211 | |
| 212 | |
| 213 | |
| 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 | |