cross-toolchain.scm 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2013, 2014, 2015, 2016, 2020 Ludovic Courtès <ludo@gnu.org>
  3. ;;; Copyright © 2014, 2015, 2018 Mark H Weaver <mhw@netris.org>
  4. ;;; Copyright © 2016 Jan Nieuwenhuizen <janneke@gnu.org>
  5. ;;; Copyright © 2016 Manolis Fragkiskos Ragkousis <manolis837@gmail.com>
  6. ;;; Copyright © 2019 Marius Bakke <mbakke@fastmail.com>
  7. ;;; Copyright © 2019 Carl Dong <contact@carldong.me>
  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. (define-module (gnu build cross-toolchain)
  24. #:use-module (guix build utils)
  25. #:use-module (guix build gnu-build-system)
  26. #:use-module (srfi srfi-1)
  27. #:use-module (srfi srfi-26)
  28. #:use-module (ice-9 match)
  29. #:use-module (ice-9 ftw)
  30. #:export (cross-gcc-build-phases))
  31. ;;; Commentary:
  32. ;;;
  33. ;;; This module provides tools to build a cross-compiler.
  34. ;;;
  35. ;;; Code:
  36. (define %gcc-include-paths
  37. ;; Environment variables for header search paths.
  38. ;; Note: See <http://bugs.gnu.org/22186> for why not 'CPATH'.
  39. '("C_INCLUDE_PATH"
  40. "CPLUS_INCLUDE_PATH"
  41. "OBJC_INCLUDE_PATH"
  42. "OBJCPLUS_INCLUDE_PATH"))
  43. (define %gcc-cross-include-paths
  44. ;; Search path for target headers when cross-compiling.
  45. (map (cut string-append "CROSS_" <>) %gcc-include-paths))
  46. (define* (make-cross-binutils-visible #:key outputs inputs target
  47. #:allow-other-keys)
  48. "Create symlinks for 'as', 'nm', and 'ld' in the \"out\" output, under
  49. libexec/gcc, so that the cross-GCC can find them."
  50. (let* ((out (assoc-ref outputs "out"))
  51. (libexec (string-append out "/libexec/gcc/" target))
  52. (binutils (string-append (assoc-ref inputs "binutils-cross")
  53. "/bin/" target "-"))
  54. (wrapper (string-append (assoc-ref inputs "ld-wrapper-cross")
  55. "/bin/" target "-ld")))
  56. (for-each (lambda (file)
  57. (symlink (string-append binutils file)
  58. (string-append libexec "/" file)))
  59. '("as" "nm"))
  60. (symlink wrapper (string-append libexec "/ld"))
  61. #t))
  62. (define* (set-cross-path #:key inputs #:allow-other-keys)
  63. "Add the cross kernel headers to CROSS_CPATH, and remove them from
  64. C_INCLUDE_PATH et al."
  65. (match (assoc-ref inputs "libc")
  66. ((? string? libc)
  67. (let ((kernel (assoc-ref inputs "xkernel-headers")))
  68. (define (cross? x)
  69. ;; Return #t if X is a cross-libc or cross Linux.
  70. (or (string-prefix? libc x)
  71. (string-prefix? kernel x)))
  72. (let ((cpath (string-append libc "/include"
  73. ":" kernel "/include")))
  74. (for-each (cut setenv <> cpath)
  75. %gcc-cross-include-paths))
  76. (setenv "CROSS_LIBRARY_PATH"
  77. (string-append libc "/lib:" kernel "/lib")) ;for Hurd's libihash
  78. (for-each (lambda (var)
  79. (and=> (getenv var)
  80. (lambda (value)
  81. (let* ((path (search-path-as-string->list value))
  82. (native-path (list->search-path-as-string
  83. (remove cross? path) ":")))
  84. (setenv var native-path)))))
  85. (cons "LIBRARY_PATH" %gcc-include-paths))
  86. #t))
  87. (#f
  88. ;; We're building the sans-libc cross-compiler, so nothing to do.
  89. #t)))
  90. (define* (set-cross-path/mingw #:key inputs target #:allow-other-keys)
  91. "Add the cross MinGW headers to CROSS_C_*_INCLUDE_PATH, and remove them from
  92. C_*INCLUDE_PATH."
  93. (let ((libc (assoc-ref inputs "libc"))
  94. (gcc (assoc-ref inputs "gcc")))
  95. (define (cross? x)
  96. (and libc (string-prefix? libc x)))
  97. (define (unpacked-mingw-dir)
  98. (match (scandir "." (lambda (name)
  99. (string-contains name "mingw-w64")))
  100. ((mingw-dir)
  101. (string-append
  102. (getcwd) "/" mingw-dir "/mingw-w64-headers"))))
  103. (if libc
  104. (let ((cpath (string-append libc "/include"
  105. ":" libc "/" target "/include")))
  106. (for-each (cut setenv <> cpath)
  107. %gcc-cross-include-paths))
  108. ;; libc is false, so we are building xgcc-sans-libc.
  109. ;; Add essential headers from mingw-w64.
  110. (let ((mingw-source (assoc-ref inputs "mingw-source")))
  111. (invoke "tar" "xvf" mingw-source)
  112. (let ((mingw-headers (unpacked-mingw-dir)))
  113. ;; We need _mingw.h which will gets built from _mingw.h.in by
  114. ;; mingw-w64's configure. We cannot configure mingw-w64 until we
  115. ;; have xgcc-sans-libc; substitute to the rescue.
  116. (copy-file (string-append mingw-headers "/crt/_mingw.h.in")
  117. (string-append mingw-headers "/crt/_mingw.h"))
  118. (substitute* (string-append mingw-headers "/crt/_mingw.h")
  119. (("@MINGW_HAS_SECURE_API@")
  120. "#define MINGW_HAS_SECURE_API 1")
  121. (("@DEFAULT_WIN32_WINNT@")
  122. "0x502")
  123. (("@DEFAULT_MSVCRT_VERSION@")
  124. "0x700"))
  125. (let ((cpath (string-append mingw-headers "/include"
  126. ":" mingw-headers "/crt"
  127. ":" mingw-headers
  128. "/defaults/include")))
  129. (for-each (cut setenv <> cpath)
  130. (cons "CROSS_LIBRARY_PATH"
  131. %gcc-cross-include-paths))))))
  132. (when libc
  133. (setenv "CROSS_LIBRARY_PATH"
  134. (string-append libc "/lib"
  135. ":" libc "/" target "/lib")))
  136. (setenv "CPP" (string-append gcc "/bin/cpp"))
  137. (for-each (lambda (var)
  138. (and=> (getenv var)
  139. (lambda (value)
  140. (let* ((path (search-path-as-string->list
  141. value))
  142. (native-path (list->search-path-as-string
  143. (remove cross? path) ":")))
  144. (setenv var native-path)))))
  145. (cons "LIBRARY_PATH" %gcc-include-paths))
  146. #t))
  147. (define (install-strip . _)
  148. "Install a stripped GCC."
  149. ;; Unlike our 'strip' phase, this will do the right thing for
  150. ;; cross-compilers.
  151. (invoke "make" "install-strip"))
  152. (define* (cross-gcc-build-phases target
  153. #:optional (phases %standard-phases))
  154. "Modify PHASES to include everything needed to build a cross-GCC for TARGET,
  155. a target triplet."
  156. (modify-phases phases
  157. (add-before 'configure 'set-cross-path
  158. ;; This mingw32 target checking logic should match that of target-mingw?
  159. ;; in (guix utils), but (guix utils) is too large too copy over to the
  160. ;; build side entirely and for now we have no way to select variables to
  161. ;; copy over. See (gnu packages cross-base) for more details.
  162. (if (string-suffix? "-mingw32" target)
  163. (cut set-cross-path/mingw #:target target <...>)
  164. set-cross-path))
  165. (add-after 'install 'make-cross-binutils-visible
  166. (cut make-cross-binutils-visible #:target target <...>))
  167. (replace 'install install-strip)))
  168. ;;; cross-toolchain.scm ends here