cross-toolchain.scm 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2013, 2014, 2015, 2016 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. ;;;
  7. ;;; This file is part of GNU Guix.
  8. ;;;
  9. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  10. ;;; under the terms of the GNU General Public License as published by
  11. ;;; the Free Software Foundation; either version 3 of the License, or (at
  12. ;;; your option) any later version.
  13. ;;;
  14. ;;; GNU Guix is distributed in the hope that it will be useful, but
  15. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ;;; GNU General Public License for more details.
  18. ;;;
  19. ;;; You should have received a copy of the GNU General Public License
  20. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  21. (define-module (gnu build cross-toolchain)
  22. #:use-module (guix build utils)
  23. #:use-module (guix build gnu-build-system)
  24. #:use-module (srfi srfi-1)
  25. #:use-module (srfi srfi-26)
  26. #:use-module (ice-9 match)
  27. #:use-module (ice-9 ftw)
  28. #:export (cross-gcc-build-phases))
  29. ;;; Commentary:
  30. ;;;
  31. ;;; This module provides tools to build a cross-compiler.
  32. ;;;
  33. ;;; Code:
  34. (define %gcc-include-paths
  35. ;; Environment variables for header search paths.
  36. ;; Note: See <http://bugs.gnu.org/22186> for why not 'CPATH'.
  37. '("C_INCLUDE_PATH"
  38. "CPLUS_INCLUDE_PATH"
  39. "OBJC_INCLUDE_PATH"
  40. "OBJCPLUS_INCLUDE_PATH"))
  41. (define %gcc-cross-include-paths
  42. ;; Search path for target headers when cross-compiling.
  43. (map (cut string-append "CROSS_" <>) %gcc-include-paths))
  44. (define* (make-cross-binutils-visible #:key outputs inputs target
  45. #:allow-other-keys)
  46. "Create symlinks for 'as', 'nm', and 'ld' in the \"out\" output, under
  47. libexec/gcc, so that the cross-GCC can find them."
  48. (let* ((out (assoc-ref outputs "out"))
  49. (libexec (string-append out "/libexec/gcc/" target))
  50. (binutils (string-append (assoc-ref inputs "binutils-cross")
  51. "/bin/" target "-"))
  52. (wrapper (string-append (assoc-ref inputs "ld-wrapper-cross")
  53. "/bin/" target "-ld")))
  54. (for-each (lambda (file)
  55. (symlink (string-append binutils file)
  56. (string-append libexec "/" file)))
  57. '("as" "nm"))
  58. (symlink wrapper (string-append libexec "/ld"))
  59. #t))
  60. (define* (set-cross-path #:key inputs #:allow-other-keys)
  61. "Add the cross kernel headers to CROSS_CPATH, and remove them from
  62. C_INCLUDE_PATH et al."
  63. (match (assoc-ref inputs "libc")
  64. ((? string? libc)
  65. (let ((kernel (assoc-ref inputs "xkernel-headers")))
  66. (define (cross? x)
  67. ;; Return #t if X is a cross-libc or cross Linux.
  68. (or (string-prefix? libc x)
  69. (string-prefix? kernel x)))
  70. (let ((cpath (string-append libc "/include"
  71. ":" kernel "/include")))
  72. (for-each (cut setenv <> cpath)
  73. %gcc-cross-include-paths))
  74. (setenv "CROSS_LIBRARY_PATH"
  75. (string-append libc "/lib:" kernel "/lib")) ;for Hurd's libihash
  76. (for-each (lambda (var)
  77. (and=> (getenv var)
  78. (lambda (value)
  79. (let* ((path (search-path-as-string->list value))
  80. (native-path (list->search-path-as-string
  81. (remove cross? path) ":")))
  82. (setenv var native-path)))))
  83. (cons "LIBRARY_PATH" %gcc-include-paths))
  84. #t))
  85. (#f
  86. ;; We're building the sans-libc cross-compiler, so nothing to do.
  87. #t)))
  88. (define* (set-cross-path/mingw #:key inputs #:allow-other-keys)
  89. "Add the cross MinGW headers to CROSS_C_*_INCLUDE_PATH, and remove them from
  90. C_*INCLUDE_PATH."
  91. (let ((libc (assoc-ref inputs "libc"))
  92. (gcc (assoc-ref inputs "gcc")))
  93. (define (cross? x)
  94. (and libc (string-prefix? libc x)))
  95. (define (unpacked-mingw-dir)
  96. (match (scandir "." (lambda (name)
  97. (string-contains name "mingw-w64")))
  98. ((mingw-dir)
  99. (string-append
  100. (getcwd) "/" mingw-dir "/mingw-w64-headers"))))
  101. (if libc
  102. (let ((cpath (string-append libc "/include"
  103. ":" libc "/i686-w64-mingw32/include")))
  104. (for-each (cut setenv <> cpath)
  105. %gcc-cross-include-paths))
  106. ;; libc is false, so we are building xgcc-sans-libc.
  107. ;; Add essential headers from mingw-w64.
  108. (let ((mingw-source (assoc-ref inputs "mingw-source")))
  109. (invoke "tar" "xvf" mingw-source)
  110. (let ((mingw-headers (unpacked-mingw-dir)))
  111. ;; We need _mingw.h which will gets built from _mingw.h.in by
  112. ;; mingw-w64's configure. We cannot configure mingw-w64 until we
  113. ;; have xgcc-sans-libc; substitute to the rescue.
  114. (copy-file (string-append mingw-headers "/crt/_mingw.h.in")
  115. (string-append mingw-headers "/crt/_mingw.h"))
  116. (substitute* (string-append mingw-headers "/crt/_mingw.h")
  117. (("@MINGW_HAS_SECURE_API@")
  118. "#define MINGW_HAS_SECURE_API 1"))
  119. (let ((cpath (string-append mingw-headers "/include"
  120. ":" mingw-headers "/crt"
  121. ":" mingw-headers
  122. "/defaults/include")))
  123. (for-each (cut setenv <> cpath)
  124. (cons "CROSS_LIBRARY_PATH"
  125. %gcc-cross-include-paths))))))
  126. (when libc
  127. (setenv "CROSS_LIBRARY_PATH"
  128. (string-append libc "/lib"
  129. ":" libc "/i686-w64-mingw32/lib")))
  130. (setenv "CPP" (string-append gcc "/bin/cpp"))
  131. (for-each (lambda (var)
  132. (and=> (getenv var)
  133. (lambda (value)
  134. (let* ((path (search-path-as-string->list
  135. value))
  136. (native-path (list->search-path-as-string
  137. (remove cross? path) ":")))
  138. (setenv var native-path)))))
  139. (cons "LIBRARY_PATH" %gcc-include-paths))
  140. #t))
  141. (define (install-strip . _)
  142. "Install a stripped GCC."
  143. ;; Unlike our 'strip' phase, this will do the right thing for
  144. ;; cross-compilers.
  145. (invoke "make" "install-strip"))
  146. (define* (cross-gcc-build-phases target
  147. #:optional (phases %standard-phases))
  148. "Modify PHASES to include everything needed to build a cross-GCC for TARGET,
  149. a target triplet."
  150. (modify-phases phases
  151. (add-before 'configure 'set-cross-path
  152. (if (string-contains target "mingw")
  153. set-cross-path/mingw
  154. set-cross-path))
  155. (add-after 'install 'make-cross-binutils-visible
  156. (cut make-cross-binutils-visible #:target target <...>))
  157. (replace 'install install-strip)))
  158. ;;; cross-toolchain.scm ends here