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>
5 ;;; This file is part of GNU Guix.
7 ;;; GNU Guix is free software; you can redistribute it and/or modify it
8 ;;; under the terms of the GNU General Public License as published by
9 ;;; the Free Software Foundation; either version 3 of the License, or (at
10 ;;; your option) any later version.
12 ;;; GNU Guix is distributed in the hope that it will be useful, but
13 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;;; GNU General Public License for more details.
17 ;;; You should have received a copy of the GNU General Public License
18 ;;; 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 derivations)
24 #:use-module (guix search-paths)
25 #:use-module (guix build-system)
26 #:use-module (guix build-system gnu)
27 #:use-module (guix packages)
28 #:use-module (ice-9 match)
29 #:export (%linux-module-build-system-modules
31 linux-module-build-system))
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))
42 (define (default-linux)
43 "Return the default Linux package."
45 ;; Do not use `@' to avoid introducing circular dependencies.
46 (let ((module (resolve-interface '(gnu packages linux))))
47 (module-ref module 'linux-libre)))
49 (define (system->arch system)
50 (let ((module (resolve-interface '(gnu packages linux))))
51 ((module-ref module 'system->linux-architecture) system)))
53 (define (make-linux-module-builder linux)
56 (name (string-append (package-name linux) "-module-builder"))
60 (substitute-keyword-arguments (package-arguments linux)
62 `(modify-phases ,phases
65 (invoke "make" "modules_prepare")))
66 (delete 'strip) ; faster.
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 ;; Delete some huge items that we probably don't need.
72 ;; TODO: Only preserve the minimum, i.e. [Kbuild], Kconfig,
73 ;; scripts, include, ".config".
74 (copy-recursively "." out-lib-build)
75 (for-each (lambda (name)
76 (when (file-exists? name)
77 (delete-file-recursively name)))
79 (string-append out-lib-build "/" name))
81 ;"tools" ; 44 MB ; Note: is built by our 'build phase.
82 "tools/testing" ; 14 MB
85 "Documentation" ; 52 MB
90 ;; Reinstate arch/**/dts since "scripts/dtc" depends on it.
91 ;; Reinstate arch/**/include directories.
92 ;; Reinstate arch/**/Makefile.
93 ;; Reinstate arch/**/module.lds.
96 (mkdir-p (dirname (string-append out-lib-build "/" name)))
97 (copy-recursively name
98 (string-append out-lib-build "/" name)))
99 (append (find-files "arch" "^(dts|include)$" #:directories? #t)
100 (find-files "arch" "^(Makefile|module.lds)$")))
101 (let* ((linux (assoc-ref inputs "linux")))
102 (install-file (string-append linux "/System.map")
104 (let ((source (string-append linux "/Module.symvers")))
105 (when (file-exists? source)
106 (install-file source out-lib-build))))
110 #:key source inputs native-inputs outputs
112 (linux (default-linux))
115 "Return a bag for NAME."
116 (define private-keywords
117 `(#:source #:target #:gcc #:kmod #:linux #:inputs #:native-inputs
118 ,@(if target '() '(#:target))))
122 (system system) (target target)
123 (build-inputs `(,@(if source
124 `(("source" ,source))
127 ;; TODO: Remove "gmp", "mpfr", "mpc" since they are
128 ;; only needed to compile the gcc plugins. Maybe
129 ;; remove "flex", "bison", "elfutils", "perl",
130 ;; "openssl". That leaves very little ("bc", "gcc",
132 ,@(package-native-inputs linux)
134 ;; Use the standard cross inputs of
135 ;; 'gnu-build-system'.
136 (standard-cross-packages target 'host)
138 ;; Keep the standard inputs of 'gnu-build-system'.
139 ,@(standard-packages)))
140 (host-inputs `(,@inputs
142 ("linux-module-builder"
143 ,(make-linux-module-builder linux))))
144 (target-inputs (if target
145 (standard-cross-packages target 'target)
148 (build (if target linux-module-build-cross linux-module-build))
149 (arguments (strip-keyword-arguments private-keywords arguments))))
151 (define* (linux-module-build store name inputs
156 (phases '(@ (guix build linux-module-build-system)
160 (system (%current-system))
164 %linux-module-build-system-modules)
165 (modules '((guix build linux-module-build-system)
166 (guix build utils))))
167 "Build SOURCE using LINUX, and with INPUTS."
170 (use-modules ,@modules)
171 (linux-module-build #:name ,name
172 #:source ,(match (assoc-ref inputs "source")
173 (((? derivation? source))
174 (derivation->output-path source))
179 #:search-paths ',(map search-path-specification->sexp
184 #:arch ,(system->arch (or target system))
187 #:make-flags ,make-flags
188 #:inputs %build-inputs)))
190 (define guile-for-build
193 (package-derivation store guile system #:graft? #f))
195 (let* ((distro (resolve-interface '(gnu packages commencement)))
196 (guile (module-ref distro 'guile-final)))
197 (package-derivation store guile system #:graft? #f)))))
199 (build-expression->derivation store name builder
202 #:modules imported-modules
204 #:guile-for-build guile-for-build
205 #:substitutable? substitutable?))
207 (define* (linux-module-build-cross
210 target native-drvs target-drvs
215 (native-search-paths '())
217 (phases '(@ (guix build linux-module-build-system)
219 (system (%current-system))
222 %linux-module-build-system-modules)
223 (modules '((guix build linux-module-build-system)
224 (guix build utils))))
227 (use-modules ,@modules)
229 (define %build-host-inputs
231 ((name (? derivation? drv) sub ...)
232 `(,name . ,(apply derivation->output-path drv sub)))
237 (define %build-target-inputs
239 ((name (? derivation? drv) sub ...)
240 `(,name . ,(apply derivation->output-path drv sub)))
241 ((name (? package? pkg) sub ...)
242 (let ((drv (package-cross-derivation store pkg
244 `(,name . ,(apply derivation->output-path drv sub))))
249 (linux-module-build #:name ,name
250 #:source ,(match (assoc-ref native-drvs "source")
251 (((? derivation? source))
252 (derivation->output-path source))
259 #:arch ,(system->arch (or target system))
261 #:make-flags ,make-flags
262 #:inputs %build-target-inputs
263 #:native-inputs %build-host-inputs
265 ',(map search-path-specification->sexp
267 #:native-search-paths
269 search-path-specification->sexp
274 (define guile-for-build
277 (package-derivation store guile system #:graft? #f))
279 (let* ((distro (resolve-interface '(gnu packages commencement)))
280 (guile (module-ref distro 'guile-final)))
281 (package-derivation store guile system #:graft? #f)))))
283 (build-expression->derivation store name builder
285 #:inputs (append native-drvs target-drvs)
287 #:modules imported-modules
288 #:guile-for-build guile-for-build
289 #:substitutable? substitutable?))
291 (define linux-module-build-system
294 (description "The Linux module build system")
297 ;;; linux-module.scm ends here