linux-module.scm 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2019 Danny Milosavljevic <dannym@scratchpost.org>
  3. ;;; Copyright © 2020 Mathieu Othacehe <m.othacehe@gmail.com>
  4. ;;; Copyright © 2021 Ludovic Courtès <ludo@gnu.org>
  5. ;;;
  6. ;;; This file is part of GNU Guix.
  7. ;;;
  8. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  9. ;;; under the terms of the GNU General Public License as published by
  10. ;;; the Free Software Foundation; either version 3 of the License, or (at
  11. ;;; your option) any later version.
  12. ;;;
  13. ;;; GNU Guix is distributed in the hope that it will be useful, but
  14. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;;; GNU General Public License for more details.
  17. ;;;
  18. ;;; You should have received a copy of the GNU General Public License
  19. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  20. (define-module (guix build-system linux-module)
  21. #:use-module (guix store)
  22. #:use-module (guix utils)
  23. #:use-module (guix gexp)
  24. #:use-module (guix monads)
  25. #:use-module (guix search-paths)
  26. #:use-module (guix build-system)
  27. #:use-module (guix build-system gnu)
  28. #:use-module (guix packages)
  29. #:use-module (ice-9 match)
  30. #:export (%linux-module-build-system-modules
  31. linux-module-build
  32. linux-module-build-system))
  33. ;; Commentary:
  34. ;;
  35. ;; Code:
  36. (define %linux-module-build-system-modules
  37. ;; Build-side modules imported by default.
  38. `((guix build linux-module-build-system)
  39. ,@%gnu-build-system-modules))
  40. (define (default-linux)
  41. "Return the default Linux package."
  42. ;; Do not use `@' to avoid introducing circular dependencies.
  43. (let ((module (resolve-interface '(gnu packages linux))))
  44. (module-ref module 'linux-libre)))
  45. (define (system->arch system)
  46. (let ((module (resolve-interface '(gnu packages linux))))
  47. ((module-ref module 'system->linux-architecture) system)))
  48. (define (make-linux-module-builder linux)
  49. (package
  50. (inherit linux)
  51. (name (string-append (package-name linux) "-module-builder"))
  52. (inputs
  53. `(("linux" ,linux)))
  54. (arguments
  55. (substitute-keyword-arguments (package-arguments linux)
  56. ((#:phases phases)
  57. `(modify-phases ,phases
  58. (replace 'build
  59. (lambda _
  60. (invoke "make" "modules_prepare")))
  61. (delete 'strip) ; faster.
  62. (replace 'install
  63. (lambda* (#:key inputs outputs #:allow-other-keys)
  64. (let* ((out (assoc-ref outputs "out"))
  65. (out-lib-build (string-append out "/lib/modules/build")))
  66. ;; Delete some huge items that we probably don't need.
  67. ;; TODO: Only preserve the minimum, i.e. [Kbuild], Kconfig,
  68. ;; scripts, include, ".config".
  69. (copy-recursively "." out-lib-build)
  70. (for-each (lambda (name)
  71. (when (file-exists? name)
  72. (delete-file-recursively name)))
  73. (map (lambda (name)
  74. (string-append out-lib-build "/" name))
  75. '("arch" ; 137 MB
  76. ;"tools" ; 44 MB ; Note: is built by our 'build phase.
  77. "tools/testing" ; 14 MB
  78. "tools/perf" ; 17 MB
  79. "drivers" ; 600 MB
  80. "Documentation" ; 52 MB
  81. "fs" ; 43 MB
  82. "net" ; 33 MB
  83. "samples" ; 2 MB
  84. "sound"))) ; 40 MB
  85. ;; Reinstate arch/**/dts since "scripts/dtc" depends on it.
  86. ;; Reinstate arch/**/include directories.
  87. ;; Reinstate arch/**/Makefile.
  88. ;; Reinstate arch/**/module.lds.
  89. (for-each
  90. (lambda (name)
  91. (mkdir-p (dirname (string-append out-lib-build "/" name)))
  92. (copy-recursively name
  93. (string-append out-lib-build "/" name)))
  94. (append (find-files "arch" "^(dts|include)$" #:directories? #t)
  95. (find-files "arch" "^(Makefile|module.lds)$")))
  96. (let* ((linux (assoc-ref inputs "linux")))
  97. (install-file (string-append linux "/System.map")
  98. out-lib-build)
  99. (let ((source (string-append linux "/Module.symvers")))
  100. (when (file-exists? source)
  101. (install-file source out-lib-build))))
  102. #t)))))))))
  103. (define* (lower name
  104. #:key source inputs native-inputs outputs
  105. system target
  106. (linux (default-linux))
  107. #:allow-other-keys
  108. #:rest arguments)
  109. "Return a bag for NAME."
  110. (define private-keywords
  111. `(#:target #:gcc #:kmod #:linux #:inputs #:native-inputs
  112. ,@(if target '() '(#:target))))
  113. (bag
  114. (name name)
  115. (system system) (target target)
  116. (build-inputs `(,@(if source
  117. `(("source" ,source))
  118. '())
  119. ,@native-inputs
  120. ;; TODO: Remove "gmp", "mpfr", "mpc" since they are
  121. ;; only needed to compile the gcc plugins. Maybe
  122. ;; remove "flex", "bison", "elfutils", "perl",
  123. ;; "openssl". That leaves very little ("bc", "gcc",
  124. ;; "kmod").
  125. ,@(package-native-inputs linux)
  126. ,@(if target
  127. ;; Use the standard cross inputs of
  128. ;; 'gnu-build-system'.
  129. (standard-cross-packages target 'host)
  130. '())
  131. ;; Keep the standard inputs of 'gnu-build-system'.
  132. ,@(standard-packages)))
  133. (host-inputs `(,@inputs
  134. ("linux" ,linux)
  135. ("linux-module-builder"
  136. ,(make-linux-module-builder linux))))
  137. (target-inputs (if target
  138. (standard-cross-packages target 'target)
  139. '()))
  140. (outputs outputs)
  141. (build (if target linux-module-build-cross linux-module-build))
  142. (arguments (strip-keyword-arguments private-keywords arguments))))
  143. (define* (linux-module-build name inputs
  144. #:key
  145. source target
  146. (search-paths '())
  147. (tests? #t)
  148. (phases '%standard-phases)
  149. (outputs '("out"))
  150. (make-flags ''())
  151. (system (%current-system))
  152. (guile #f)
  153. (substitutable? #t)
  154. (imported-modules
  155. %linux-module-build-system-modules)
  156. (modules '((guix build linux-module-build-system)
  157. (guix build utils))))
  158. "Build SOURCE using LINUX, and with INPUTS."
  159. (define builder
  160. (with-imported-modules imported-modules
  161. #~(begin
  162. (use-modules #$@(sexp->gexp modules))
  163. (linux-module-build #:name #$name
  164. #:source #+source
  165. #:search-paths '#$(sexp->gexp
  166. (map search-path-specification->sexp
  167. search-paths))
  168. #:phases #$phases
  169. #:system #$system
  170. #:target #$target
  171. #:arch #$(system->arch (or target system))
  172. #:tests? #$tests?
  173. #:outputs #$(outputs->gexp outputs)
  174. #:make-flags #$make-flags
  175. #:inputs #$(input-tuples->gexp inputs)))))
  176. (mlet %store-monad ((guile (package->derivation (or guile (default-guile))
  177. system #:graft? #f)))
  178. (gexp->derivation name builder
  179. #:system system
  180. #:guile-for-build guile
  181. #:substitutable? substitutable?)))
  182. (define* (linux-module-build-cross
  183. name
  184. #:key
  185. source target
  186. build-inputs target-inputs host-inputs
  187. (guile #f)
  188. (outputs '("out"))
  189. (make-flags ''())
  190. (search-paths '())
  191. (native-search-paths '())
  192. (tests? #f)
  193. (phases '%standard-phases)
  194. (system (%current-system))
  195. (substitutable? #t)
  196. (imported-modules
  197. %linux-module-build-system-modules)
  198. (modules '((guix build linux-module-build-system)
  199. (guix build utils))))
  200. (define builder
  201. (with-imported-modules imported-modules
  202. #~(begin
  203. (use-modules #$@(sexp->gexp modules))
  204. (define %build-host-inputs
  205. '#+(input-tuples->gexp build-inputs))
  206. (define %build-target-inputs
  207. (append #$(input-tuples->gexp host-inputs)
  208. #+(input-tuples->gexp target-inputs)))
  209. (linux-module-build #:name #$name
  210. #:source #+source
  211. #:system #$system
  212. #:target #$target
  213. #:arch #$(system->arch (or target system))
  214. #:outputs #$(outputs->gexp outputs)
  215. #:make-flags #$make-flags
  216. #:inputs %build-target-inputs
  217. #:native-inputs %build-host-inputs
  218. #:search-paths
  219. '#$(sexp->gexp
  220. (map search-path-specification->sexp
  221. search-paths))
  222. #:native-search-paths
  223. '#$(map
  224. search-path-specification->sexp
  225. native-search-paths)
  226. #:phases #$phases
  227. #:tests? #$tests?))))
  228. (mlet %store-monad ((guile (package->derivation (or guile (default-guile))
  229. system #:graft? #f)))
  230. (gexp->derivation name builder
  231. #:system system
  232. #:guile-for-build guile
  233. #:substitutable? substitutable?)))
  234. (define linux-module-build-system
  235. (build-system
  236. (name 'linux-module)
  237. (description "The Linux module build system")
  238. (lower lower)))
  239. ;;; linux-module.scm ends here