linux-module.scm 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. ;;; Copyright © 2021 Tobias Geerinckx-Rice <me@tobias.gr>
  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 (guix build-system linux-module)
  22. #:use-module (guix store)
  23. #:use-module (guix utils)
  24. #:use-module (guix gexp)
  25. #:use-module (guix monads)
  26. #:use-module (guix search-paths)
  27. #:use-module (guix build-system)
  28. #:use-module (guix build-system gnu)
  29. #:use-module (guix packages)
  30. #:use-module (ice-9 match)
  31. #:export (%linux-module-build-system-modules
  32. linux-module-build
  33. linux-module-build-system))
  34. ;; Commentary:
  35. ;;
  36. ;; Code:
  37. (define %linux-module-build-system-modules
  38. ;; Build-side modules imported by default.
  39. `((guix build linux-module-build-system)
  40. ,@%gnu-build-system-modules))
  41. (define (default-linux)
  42. "Return the default Linux package."
  43. ;; Do not use `@' to avoid introducing circular dependencies.
  44. (let ((module (resolve-interface '(gnu packages linux))))
  45. (module-ref module 'linux-libre)))
  46. (define (system->arch system)
  47. (let ((module (resolve-interface '(gnu packages linux))))
  48. ((module-ref module 'system->linux-architecture) system)))
  49. (define (make-linux-module-builder linux)
  50. (package
  51. (inherit linux)
  52. (name (string-append (package-name linux) "-module-builder"))
  53. (inputs
  54. `(("linux" ,linux)))
  55. (arguments
  56. (substitute-keyword-arguments (package-arguments linux)
  57. ((#:phases phases)
  58. #~(modify-phases #$phases
  59. (replace 'build
  60. (lambda _
  61. (invoke "make" "modules_prepare")))
  62. (delete 'strip) ; faster
  63. (replace 'install
  64. (lambda* (#:key inputs #:allow-other-keys)
  65. (let ((out-lib-build (string-append #$output "/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 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)$"
  95. #:directories? #t)
  96. (find-files "arch" "^(Makefile|module.lds)$")))
  97. (let* ((linux #$(this-package-input "linux")))
  98. (install-file (string-append linux "/System.map")
  99. out-lib-build)
  100. (let ((source (string-append linux "/Module.symvers")))
  101. (when (file-exists? source)
  102. (install-file source out-lib-build)))))))))))))
  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. (parallel-build? #t)
  152. (system (%current-system))
  153. (source-directory ".")
  154. (guile #f)
  155. (substitutable? #t)
  156. (imported-modules
  157. %linux-module-build-system-modules)
  158. (modules '((guix build linux-module-build-system)
  159. (guix build utils))))
  160. "Build SOURCE using LINUX, and with INPUTS."
  161. (define builder
  162. (with-imported-modules imported-modules
  163. #~(begin
  164. (use-modules #$@(sexp->gexp modules))
  165. #$(with-build-variables inputs outputs
  166. #~(linux-module-build #:name #$name
  167. #:source #+source
  168. #:source-directory #$source-directory
  169. #:search-paths '#$(sexp->gexp
  170. (map search-path-specification->sexp
  171. search-paths))
  172. #:phases #$phases
  173. #:system #$system
  174. #:target #$target
  175. #:arch #$(system->arch (or target system))
  176. #:tests? #$tests?
  177. #:outputs #$(outputs->gexp outputs)
  178. #:make-flags #$make-flags
  179. #:parallel-build? #$parallel-build?
  180. #:inputs #$(input-tuples->gexp inputs))))))
  181. (mlet %store-monad ((guile (package->derivation (or guile (default-guile))
  182. system #:graft? #f)))
  183. (gexp->derivation name builder
  184. #:system system
  185. #:guile-for-build guile
  186. #:substitutable? substitutable?)))
  187. (define* (linux-module-build-cross
  188. name
  189. #:key
  190. source target
  191. build-inputs target-inputs host-inputs
  192. (guile #f)
  193. (outputs '("out"))
  194. (make-flags ''())
  195. (parallel-build? #t)
  196. (search-paths '())
  197. (native-search-paths '())
  198. (tests? #f)
  199. (phases '%standard-phases)
  200. (system (%current-system))
  201. (substitutable? #t)
  202. (imported-modules
  203. %linux-module-build-system-modules)
  204. (modules '((guix build linux-module-build-system)
  205. (guix build utils))))
  206. (define builder
  207. (with-imported-modules imported-modules
  208. #~(begin
  209. (use-modules #$@(sexp->gexp modules))
  210. (define %build-host-inputs
  211. '#+(input-tuples->gexp build-inputs))
  212. (define %build-target-inputs
  213. (append #$(input-tuples->gexp host-inputs)
  214. #+(input-tuples->gexp target-inputs)))
  215. (linux-module-build #:name #$name
  216. #:source #+source
  217. #:system #$system
  218. #:target #$target
  219. #:arch #$(system->arch (or target system))
  220. #:outputs #$(outputs->gexp outputs)
  221. #:make-flags #$make-flags
  222. #:inputs %build-target-inputs
  223. #:native-inputs %build-host-inputs
  224. #:search-paths
  225. '#$(sexp->gexp
  226. (map search-path-specification->sexp
  227. search-paths))
  228. #:native-search-paths
  229. '#$(map
  230. search-path-specification->sexp
  231. native-search-paths)
  232. #:phases #$phases
  233. #:tests? #$tests?))))
  234. (mlet %store-monad ((guile (package->derivation (or guile (default-guile))
  235. system #:graft? #f)))
  236. (gexp->derivation name builder
  237. #:system system
  238. #:guile-for-build guile
  239. #:substitutable? substitutable?)))
  240. (define linux-module-build-system
  241. (build-system
  242. (name 'linux-module)
  243. (description "The Linux module build system")
  244. (lower lower)))
  245. ;;; linux-module.scm ends here