Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

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 <masahiroy@kernel.org>
Reviewed-by: Nicolas Schier <nicolas@fjasle.eu>

+27 -2
+21
Documentation/kbuild/modules.rst
··· 59 59 60 60 $ make -C /lib/modules/`uname -r`/build M=$PWD modules_install 61 61 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 + 62 68 Options 63 69 ------- 64 70 ··· 226 220 each file; however, some external modules use makefiles 227 221 consisting of several hundred lines, and here it really pays 228 222 off to separate the kbuild part from the rest. 223 + 224 + Linux 6.13 and later support another way. The external module Makefile 225 + can include the kernel Makefile directly, rather than invoking sub Make. 226 + 227 + Example 3:: 228 + 229 + --> filename: Kbuild 230 + obj-m := 8123.o 231 + 8123-y := 8123_if.o 8123_pci.o 232 + 233 + --> filename: Makefile 234 + KDIR ?= /lib/modules/$(shell uname -r)/build 235 + export KBUILD_EXTMOD := $(realpath $(dir $(lastword $(MAKEFILE_LIST)))) 236 + include $(KDIR)/Makefile 237 + 229 238 230 239 Building Multiple Modules 231 240 -------------------------
+6 -2
Makefile
··· 189 189 objtree := $(realpath $(KBUILD_OUTPUT)) 190 190 $(if $(objtree),,$(error specified kernel directory "$(KBUILD_OUTPUT)" does not exist)) 191 191 else 192 - objtree := $(CURDIR) 192 + objtree := $(abs_srctree) 193 193 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))) 195 199 # KBUILD_EXTMOD might be a relative path. Remember its absolute path before 196 200 # Make changes the working directory. 197 201 srcroot := $(realpath $(KBUILD_EXTMOD))