Skip to content

Commit 8cd07cc

Browse files
committed
kbuild: allow to start building external modules in any directory
Unless an explicit O= option is provided, external module builds must start from the kernel directory. This can be achieved by using the -C option: $ make -C /path/to/kernel M=/path/to/external/module This commit allows starting external module builds from any directory, so you can also do the following: $ make -f /path/to/kernel/Makefile M=/path/to/external/module The key difference is that the -C option changes the working directory and parses the Makefile located there, while the -f option only specifies the Makefile to use. As shown in the examples in Documentation/kbuild/modules.rst, external modules usually have a wrapper Makefile that allows you to build them without specifying any make arguments. The Makefile typically contains a rule as follows: KDIR ?= /path/to/kernel default: $(MAKE) -C $(KDIR) M=$(CURDIR) $(MAKECMDGOALS) The log will appear as follows: $ make make -C /path/to/kernel M=/path/to/external/module make[1]: Entering directory '/path/to/kernel' make[2]: Entering directory '/path/to/external/module' CC [M] helloworld.o MODPOST Module.symvers CC [M] helloworld.mod.o CC [M] .module-common.o LD [M] helloworld.ko make[2]: Leaving directory '/path/to/external/module' make[1]: Leaving directory '/path/to/kernel' This changes the working directory twice because the -C option first switches to the kernel directory, and then Kbuild internally recurses back to the external module directory. With this commit, the wrapper Makefile can directly include the kernel Makefile: KDIR ?= /path/to/kernel export KBUILD_EXTMOD := $(realpath $(dir $(lastword $(MAKEFILE_LIST)))) include $(KDIR)/Makefile This avoids unnecessary sub-make invocations: $ make CC [M] helloworld.o MODPOST Module.symvers CC [M] helloworld.mod.o CC [M] .module-common.o LD [M] helloworld.ko Signed-off-by: Masahiro Yamada <[email protected]> Reviewed-by: Nicolas Schier <[email protected]>
1 parent a2a45eb commit 8cd07cc

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

Documentation/kbuild/modules.rst

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ Command Syntax
5959

6060
$ make -C /lib/modules/`uname -r`/build M=$PWD modules_install
6161

62+
Starting from Linux 6.13, you can use the -f option instead of -C. This
63+
will avoid unnecessary change of the working directory. The external
64+
module will be output to the directory where you invoke make.
65+
66+
$ make -f /lib/modules/`uname -r`/build/Makefile M=$PWD
67+
6268
Options
6369
-------
6470

@@ -221,6 +227,21 @@ Separate Kbuild File and Makefile
221227
consisting of several hundred lines, and here it really pays
222228
off to separate the kbuild part from the rest.
223229

230+
Linux 6.13 and later support another way. The external module Makefile
231+
can include the kernel Makefile directly, rather than invoking sub Make.
232+
233+
Example 3::
234+
235+
--> filename: Kbuild
236+
obj-m := 8123.o
237+
8123-y := 8123_if.o 8123_pci.o
238+
239+
--> filename: Makefile
240+
KDIR ?= /lib/modules/$(shell uname -r)/build
241+
export KBUILD_EXTMOD := $(realpath $(dir $(lastword $(MAKEFILE_LIST))))
242+
include $(KDIR)/Makefile
243+
244+
224245
Building Multiple Modules
225246
-------------------------
226247

Makefile

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,9 +189,13 @@ ifdef KBUILD_EXTMOD
189189
objtree := $(realpath $(KBUILD_OUTPUT))
190190
$(if $(objtree),,$(error specified kernel directory "$(KBUILD_OUTPUT)" does not exist))
191191
else
192-
objtree := $(CURDIR)
192+
objtree := $(abs_srctree)
193193
endif
194-
output := $(or $(KBUILD_EXTMOD_OUTPUT),$(KBUILD_EXTMOD))
194+
# If Make is invoked from the kernel directory (either kernel
195+
# source directory or kernel build directory), external modules
196+
# are built in $(KBUILD_EXTMOD) for backward compatibility,
197+
# otherwise, built in the current directory.
198+
output := $(or $(KBUILD_EXTMOD_OUTPUT),$(if $(filter $(CURDIR),$(objtree) $(abs_srctree)),$(KBUILD_EXTMOD)))
195199
# KBUILD_EXTMOD might be a relative path. Remember its absolute path before
196200
# Make changes the working directory.
197201
srcroot := $(realpath $(KBUILD_EXTMOD))

0 commit comments

Comments
 (0)