1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2019 Danny Milosavljevic <dannym@scratchpost.org>
4 ;;; This file is part of GNU Guix.
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.
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.
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/>.
19 (define-module (guix build-system linux-module)
20 #:use-module (guix store)
21 #:use-module (guix utils)
22 #:use-module (guix derivations)
23 #:use-module (guix search-paths)
24 #:use-module (guix build-system)
25 #:use-module (guix build-system gnu)
26 #:use-module (guix packages)
27 #:use-module (ice-9 match)
28 #:export (%linux-module-build-system-modules
30 linux-module-build-system))
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))
41 (define (default-linux)
42 "Return the default Linux package."
44 ;; Do not use `@' to avoid introducing circular dependencies.
45 (let ((module (resolve-interface '(gnu packages linux))))
46 (module-ref module 'linux-libre)))
48 (define (default-kmod)
49 "Return the default kmod package."
51 ;; Do not use `@' to avoid introducing circular dependencies.
52 (let ((module (resolve-interface '(gnu packages linux))))
53 (module-ref module 'kmod)))
56 "Return the default gcc package."
58 ;; Do not use `@' to avoid introducing circular dependencies.
59 (let ((module (resolve-interface '(gnu packages gcc))))
60 (module-ref module 'gcc-7)))
62 (define (make-linux-module-builder linux)
65 (name (string-append (package-name linux) "-module-builder"))
68 ,@(package-native-inputs linux)))
70 (substitute-keyword-arguments (package-arguments linux)
72 `(modify-phases ,phases
75 (invoke "make" "modules_prepare")))
76 (delete 'strip) ; faster.
78 (lambda* (#:key inputs outputs #:allow-other-keys)
79 (let* ((out (assoc-ref outputs "out"))
80 (out-lib-build (string-append out "/lib/modules/build")))
81 ; TODO: Only preserve the minimum, i.e. [Kbuild], Kconfig, scripts, include, ".config".
82 (copy-recursively "." out-lib-build)
83 (let* ((linux (assoc-ref inputs "linux")))
84 (install-file (string-append linux "/System.map")
86 (let ((source (string-append linux "/Module.symvers")))
87 (if (file-exists? source)
88 (install-file source out-lib-build))))
92 #:key source inputs native-inputs outputs
94 (linux (default-linux))
97 "Return a bag for NAME."
98 (define private-keywords
99 '(#:source #:target #:gcc #:kmod #:linux #:inputs #:native-inputs))
101 (and (not target) ;XXX: no cross-compilation
105 (host-inputs `(,@(if source
106 `(("source" ,source))
109 ,@(standard-packages)))
110 (build-inputs `(("linux" ,linux) ; for "Module.symvers".
111 ("linux-module-builder"
112 ,(make-linux-module-builder linux))
114 ;; 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").
115 ,@(package-native-inputs linux)))
117 (build linux-module-build)
118 (arguments (strip-keyword-arguments private-keywords arguments)))))
120 (define* (linux-module-build store name inputs
124 (phases '(@ (guix build linux-module-build-system)
127 (system (%current-system))
130 %linux-module-build-system-modules)
131 (modules '((guix build linux-module-build-system)
132 (guix build utils))))
133 "Build SOURCE using LINUX, and with INPUTS."
136 (use-modules ,@modules)
137 (linux-module-build #:name ,name
138 #:source ,(match (assoc-ref inputs "source")
139 (((? derivation? source))
140 (derivation->output-path source))
145 #:search-paths ',(map search-path-specification->sexp
151 #:inputs %build-inputs)))
153 (define guile-for-build
156 (package-derivation store guile system #:graft? #f))
158 (let* ((distro (resolve-interface '(gnu packages commencement)))
159 (guile (module-ref distro 'guile-final)))
160 (package-derivation store guile system #:graft? #f)))))
162 (build-expression->derivation store name builder
165 #:modules imported-modules
167 #:guile-for-build guile-for-build))
169 (define linux-module-build-system
172 (description "The Linux module build system")
175 ;;; linux-module.scm ends here