linux-module.scm 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2019 Danny Milosavljevic <dannym@scratchpost.org>
  3. ;;;
  4. ;;; This file is part of GNU Guix.
  5. ;;;
  6. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  7. ;;; under the terms of the GNU General Public License as published by
  8. ;;; the Free Software Foundation; either version 3 of the License, or (at
  9. ;;; your option) any later version.
  10. ;;;
  11. ;;; GNU Guix is distributed in the hope that it will be useful, but
  12. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;;; GNU General Public License for more details.
  15. ;;;
  16. ;;; You should have received a copy of the GNU General Public License
  17. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  18. (define-module (guix build-system linux-module)
  19. #:use-module (guix store)
  20. #:use-module (guix utils)
  21. #:use-module (guix derivations)
  22. #:use-module (guix search-paths)
  23. #:use-module (guix build-system)
  24. #:use-module (guix build-system gnu)
  25. #:use-module (guix packages)
  26. #:use-module (ice-9 match)
  27. #:export (%linux-module-build-system-modules
  28. linux-module-build
  29. linux-module-build-system))
  30. ;; Commentary:
  31. ;;
  32. ;; Code:
  33. (define %linux-module-build-system-modules
  34. ;; Build-side modules imported by default.
  35. `((guix build linux-module-build-system)
  36. ,@%gnu-build-system-modules))
  37. (define (default-linux)
  38. "Return the default Linux package."
  39. ;; Do not use `@' to avoid introducing circular dependencies.
  40. (let ((module (resolve-interface '(gnu packages linux))))
  41. (module-ref module 'linux-libre)))
  42. (define (default-kmod)
  43. "Return the default kmod package."
  44. ;; Do not use `@' to avoid introducing circular dependencies.
  45. (let ((module (resolve-interface '(gnu packages linux))))
  46. (module-ref module 'kmod)))
  47. (define (default-gcc)
  48. "Return the default gcc package."
  49. ;; Do not use `@' to avoid introducing circular dependencies.
  50. (let ((module (resolve-interface '(gnu packages gcc))))
  51. (module-ref module 'gcc-7)))
  52. (define (make-linux-module-builder linux)
  53. (package
  54. (inherit linux)
  55. (name (string-append (package-name linux) "-module-builder"))
  56. (native-inputs
  57. `(("linux" ,linux)
  58. ,@(package-native-inputs linux)))
  59. (arguments
  60. (substitute-keyword-arguments (package-arguments linux)
  61. ((#:phases phases)
  62. `(modify-phases ,phases
  63. (replace 'build
  64. (lambda _
  65. (invoke "make" "modules_prepare")))
  66. (delete 'strip) ; faster.
  67. (replace 'install
  68. (lambda* (#:key inputs outputs #:allow-other-keys)
  69. (let* ((out (assoc-ref outputs "out"))
  70. (out-lib-build (string-append out "/lib/modules/build")))
  71. ; TODO: Only preserve the minimum, i.e. [Kbuild], Kconfig, scripts, include, ".config".
  72. (copy-recursively "." out-lib-build)
  73. (let* ((linux (assoc-ref inputs "linux")))
  74. (install-file (string-append linux "/System.map")
  75. out-lib-build)
  76. (let ((source (string-append linux "/Module.symvers")))
  77. (if (file-exists? source)
  78. (install-file source out-lib-build))))
  79. #t)))))))))
  80. (define* (lower name
  81. #:key source inputs native-inputs outputs
  82. system target
  83. (linux (default-linux))
  84. #:allow-other-keys
  85. #:rest arguments)
  86. "Return a bag for NAME."
  87. (define private-keywords
  88. '(#:source #:target #:gcc #:kmod #:linux #:inputs #:native-inputs))
  89. (and (not target) ;XXX: no cross-compilation
  90. (bag
  91. (name name)
  92. (system system)
  93. (host-inputs `(,@(if source
  94. `(("source" ,source))
  95. '())
  96. ,@inputs
  97. ,@(standard-packages)))
  98. (build-inputs `(("linux" ,linux) ; for "Module.symvers".
  99. ("linux-module-builder"
  100. ,(make-linux-module-builder linux))
  101. ,@native-inputs
  102. ;; TODO: Remove "gmp", "mpfr", "mpc" since they are only needed to compile the gcc plugins. Maybe remove "flex", "bison", "elfutils", "perl", "openssl". That leaves very little ("bc", "gcc", "kmod").
  103. ,@(package-native-inputs linux)))
  104. (outputs outputs)
  105. (build linux-module-build)
  106. (arguments (strip-keyword-arguments private-keywords arguments)))))
  107. (define* (linux-module-build store name inputs
  108. #:key
  109. (search-paths '())
  110. (tests? #t)
  111. (phases '(@ (guix build linux-module-build-system)
  112. %standard-phases))
  113. (outputs '("out"))
  114. (system (%current-system))
  115. (guile #f)
  116. (substitutable? #t)
  117. (imported-modules
  118. %linux-module-build-system-modules)
  119. (modules '((guix build linux-module-build-system)
  120. (guix build utils))))
  121. "Build SOURCE using LINUX, and with INPUTS."
  122. (define builder
  123. `(begin
  124. (use-modules ,@modules)
  125. (linux-module-build #:name ,name
  126. #:source ,(match (assoc-ref inputs "source")
  127. (((? derivation? source))
  128. (derivation->output-path source))
  129. ((source)
  130. source)
  131. (source
  132. source))
  133. #:search-paths ',(map search-path-specification->sexp
  134. search-paths)
  135. #:phases ,phases
  136. #:system ,system
  137. #:tests? ,tests?
  138. #:outputs %outputs
  139. #:inputs %build-inputs)))
  140. (define guile-for-build
  141. (match guile
  142. ((? package?)
  143. (package-derivation store guile system #:graft? #f))
  144. (#f ; the default
  145. (let* ((distro (resolve-interface '(gnu packages commencement)))
  146. (guile (module-ref distro 'guile-final)))
  147. (package-derivation store guile system #:graft? #f)))))
  148. (build-expression->derivation store name builder
  149. #:system system
  150. #:inputs inputs
  151. #:modules imported-modules
  152. #:outputs outputs
  153. #:guile-for-build guile-for-build
  154. #:substitutable? substitutable?))
  155. (define linux-module-build-system
  156. (build-system
  157. (name 'linux-module)
  158. (description "The Linux module build system")
  159. (lower lower)))
  160. ;;; linux-module.scm ends here