Skip to content

Commit 13b2548

Browse files
committed
kbuild: change working directory to external module directory with M=
Currently, Kbuild always operates in the output directory of the kernel, even when building external modules. This increases the risk of external module Makefiles attempting to write to the kernel directory. This commit switches the working directory to the external module directory, allowing the removal of the $(KBUILD_EXTMOD)/ prefix from some build artifacts. The command for building external modules maintains backward compatibility, but Makefiles that rely on working in the kernel directory may break. In such cases, $(objtree) and $(srctree) should be used to refer to the output and source directories of the kernel. The appearance of the build log will change as follows: [Before] $ make -C /path/to/my/linux M=/path/to/my/externel/module make: Entering directory '/path/to/my/linux' CC [M] /path/to/my/externel/module/helloworld.o MODPOST /path/to/my/externel/module/Module.symvers CC [M] /path/to/my/externel/module/helloworld.mod.o CC [M] /path/to/my/externel/module/.module-common.o LD [M] /path/to/my/externel/module/helloworld.ko make: Leaving directory '/path/to/my/linux' [After] $ make -C /path/to/my/linux M=/path/to/my/externel/module make: Entering directory '/path/to/my/linux' make[1]: Entering directory '/path/to/my/externel/module' CC [M] helloworld.o MODPOST Module.symvers CC [M] helloworld.mod.o CC [M] .module-common.o LD [M] helloworld.ko make[1]: Leaving directory '/path/to/my/externel/module' make: Leaving directory '/path/to/my/linux' Printing "Entering directory" twice is cumbersome. This will be addressed later. Signed-off-by: Masahiro Yamada <[email protected]> Reviewed-by: Nicolas Schier <[email protected]>
1 parent d171136 commit 13b2548

File tree

11 files changed

+87
-68
lines changed

11 files changed

+87
-68
lines changed

Documentation/dev-tools/coccinelle.rst

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -250,25 +250,17 @@ variables for .cocciconfig is as follows:
250250
- Your directory from which spatch is called is processed next
251251
- The directory provided with the ``--dir`` option is processed last, if used
252252

253-
Since coccicheck runs through make, it naturally runs from the kernel
254-
proper dir; as such the second rule above would be implied for picking up a
255-
.cocciconfig when using ``make coccicheck``.
256-
257253
``make coccicheck`` also supports using M= targets. If you do not supply
258254
any M= target, it is assumed you want to target the entire kernel.
259255
The kernel coccicheck script has::
260256

261-
if [ "$KBUILD_EXTMOD" = "" ] ; then
262-
OPTIONS="--dir $srctree $COCCIINCLUDE"
263-
else
264-
OPTIONS="--dir $KBUILD_EXTMOD $COCCIINCLUDE"
265-
fi
266-
267-
KBUILD_EXTMOD is set when an explicit target with M= is used. For both cases
268-
the spatch ``--dir`` argument is used, as such third rule applies when whether
269-
M= is used or not, and when M= is used the target directory can have its own
270-
.cocciconfig file. When M= is not passed as an argument to coccicheck the
271-
target directory is the same as the directory from where spatch was called.
257+
OPTIONS="--dir $srcroot $COCCIINCLUDE"
258+
259+
Here, $srcroot refers to the source directory of the target: it points to the
260+
external module's source directory when M= used, and otherwise, to the kernel
261+
source directory. The third rule ensures the spatch reads the .cocciconfig from
262+
the target directory, allowing external modules to have their own .cocciconfig
263+
file.
272264

273265
If not using the kernel's coccicheck target, keep the above precedence
274266
order logic of .cocciconfig reading. If using the kernel's coccicheck target,

Documentation/kbuild/makefiles.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,20 @@ $(obj)
449449
to prerequisites are referenced with $(src) (because they are not
450450
generated files).
451451

452+
$(srcroot)
453+
$(srcroot) refers to the root of the source you are building, which can be
454+
either the kernel source or the external modules source, depending on whether
455+
KBUILD_EXTMOD is set. This can be either a relative or an absolute path, but
456+
if KBUILD_ABS_SRCTREE=1 is set, it is always an absolute path.
457+
458+
$(srctree)
459+
$(srctree) refers to the root of the kernel source tree. When building the
460+
kernel, this is the same as $(srcroot).
461+
462+
$(objtree)
463+
$(objtree) refers to the root of the kernel object tree. It is ``.`` when
464+
building the kernel, but it is different when building external modules.
465+
452466
$(kecho)
453467
echoing information to user in a rule is often a good practice
454468
but when execution ``make -s`` one does not expect to see any output

Makefile

Lines changed: 49 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,24 @@ ifeq ("$(origin O)", "command line")
180180
KBUILD_OUTPUT := $(O)
181181
endif
182182

183-
output := $(KBUILD_OUTPUT)
183+
ifdef KBUILD_EXTMOD
184+
ifdef KBUILD_OUTPUT
185+
objtree := $(realpath $(KBUILD_OUTPUT))
186+
$(if $(objtree),,$(error specified kernel directory "$(KBUILD_OUTPUT)" does not exist))
187+
else
188+
objtree := $(CURDIR)
189+
endif
190+
output := $(KBUILD_EXTMOD)
191+
# KBUILD_EXTMOD might be a relative path. Remember its absolute path before
192+
# Make changes the working directory.
193+
srcroot := $(realpath $(KBUILD_EXTMOD))
194+
$(if $(srcroot),,$(error specified external module directory "$(KBUILD_EXTMOD)" does not exist))
195+
else
196+
objtree := .
197+
output := $(KBUILD_OUTPUT)
198+
endif
199+
200+
export objtree srcroot
184201

185202
# Do we want to change the working directory?
186203
ifneq ($(output),)
@@ -230,36 +247,34 @@ else # need-sub-make
230247

231248
# We process the rest of the Makefile if this is the final invocation of make
232249

233-
ifeq ($(abs_srctree),$(CURDIR))
234-
# building in the source tree
235-
srctree := .
236-
building_out_of_srctree :=
237-
else
238-
ifeq ($(abs_srctree)/,$(dir $(CURDIR)))
239-
# building in a subdirectory of the source tree
240-
srctree := ..
241-
else
242-
srctree := $(abs_srctree)
243-
endif
244-
building_out_of_srctree := 1
250+
ifndef KBUILD_EXTMOD
251+
srcroot := $(abs_srctree)
245252
endif
246253

247-
ifneq ($(KBUILD_ABS_SRCTREE),)
248-
srctree := $(abs_srctree)
254+
ifeq ($(srcroot),$(CURDIR))
255+
building_out_of_srctree :=
256+
else
257+
export building_out_of_srctree := 1
249258
endif
250259

251-
objtree := .
260+
ifdef KBUILD_ABS_SRCTREE
261+
# Do nothing. Use the absolute path.
262+
else ifeq ($(srcroot),$(CURDIR))
263+
# Building in the source.
264+
srcroot := .
265+
else ifeq ($(srcroot)/,$(dir $(CURDIR)))
266+
# Building in a subdirectory of the source.
267+
srcroot := ..
268+
endif
252269

253-
VPATH :=
270+
export srctree := $(if $(KBUILD_EXTMOD),$(abs_srctree),$(srcroot))
254271

255-
ifeq ($(KBUILD_EXTMOD),)
256272
ifdef building_out_of_srctree
257-
VPATH := $(srctree)
258-
endif
273+
export VPATH := $(srcroot)
274+
else
275+
VPATH :=
259276
endif
260277

261-
export building_out_of_srctree srctree objtree VPATH
262-
263278
# To make sure we do not include .config for any of the *config targets
264279
# catch them early, and hand them over to scripts/kconfig/Makefile
265280
# It is allowed to specify more targets when calling make, including
@@ -540,7 +555,7 @@ USERINCLUDE := \
540555
LINUXINCLUDE := \
541556
-I$(srctree)/arch/$(SRCARCH)/include \
542557
-I$(objtree)/arch/$(SRCARCH)/include/generated \
543-
$(if $(building_out_of_srctree),-I$(srctree)/include) \
558+
-I$(srctree)/include \
544559
-I$(objtree)/include \
545560
$(USERINCLUDE)
546561

@@ -711,7 +726,7 @@ endif
711726
# in addition to whatever we do anyway.
712727
# Just "make" or "make all" shall build modules as well
713728

714-
ifneq ($(filter all modules nsdeps %compile_commands.json clang-%,$(MAKECMDGOALS)),)
729+
ifneq ($(filter all modules nsdeps compile_commands.json clang-%,$(MAKECMDGOALS)),)
715730
KBUILD_MODULES := 1
716731
endif
717732

@@ -1107,7 +1122,7 @@ export MODLIB
11071122

11081123
PHONY += prepare0
11091124

1110-
export extmod_prefix = $(if $(KBUILD_EXTMOD),$(KBUILD_EXTMOD)/)
1125+
export extmod_prefix =
11111126
export MODORDER := $(extmod_prefix)modules.order
11121127
export MODULES_NSDEPS := $(extmod_prefix)modules.nsdeps
11131128

@@ -1799,14 +1814,10 @@ filechk_kernel.release = echo $(KERNELRELEASE)
17991814
KBUILD_BUILTIN :=
18001815
KBUILD_MODULES := 1
18011816

1802-
build-dir := $(KBUILD_EXTMOD)
1817+
build-dir := .
18031818

1804-
compile_commands.json: $(extmod_prefix)compile_commands.json
1805-
PHONY += compile_commands.json
1806-
1807-
clean-dirs := $(KBUILD_EXTMOD)
1808-
clean: private rm-files := $(KBUILD_EXTMOD)/Module.symvers $(KBUILD_EXTMOD)/modules.nsdeps \
1809-
$(KBUILD_EXTMOD)/compile_commands.json
1819+
clean-dirs := .
1820+
clean: private rm-files := Module.symvers modules.nsdeps compile_commands.json
18101821

18111822
PHONY += prepare
18121823
# now expand this into a simple variable to reduce the cost of shell evaluations
@@ -1948,7 +1959,7 @@ $(clean-dirs):
19481959

19491960
clean: $(clean-dirs)
19501961
$(call cmd,rmfiles)
1951-
@find $(or $(KBUILD_EXTMOD), .) $(RCS_FIND_IGNORE) \
1962+
@find . $(RCS_FIND_IGNORE) \
19521963
\( -name '*.[aios]' -o -name '*.rsi' -o -name '*.ko' -o -name '.*.cmd' \
19531964
-o -name '*.ko.*' \
19541965
-o -name '*.dtb' -o -name '*.dtbo' \
@@ -1981,7 +1992,12 @@ tags TAGS cscope gtags: FORCE
19811992
PHONY += rust-analyzer
19821993
rust-analyzer:
19831994
+$(Q)$(CONFIG_SHELL) $(srctree)/scripts/rust_is_available.sh
1995+
ifdef KBUILD_EXTMOD
1996+
# FIXME: external modules must not descend into a sub-directory of the kernel
1997+
$(Q)$(MAKE) $(build)=$(objtree)/rust src=$(srctree)/rust $@
1998+
else
19841999
$(Q)$(MAKE) $(build)=rust $@
2000+
endif
19852001

19862002
# Script to generate missing namespace dependencies
19872003
# ---------------------------------------------------------------------------

rust/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -362,8 +362,8 @@ rust-analyzer:
362362
$(Q)$(srctree)/scripts/generate_rust_analyzer.py \
363363
--cfgs='core=$(core-cfgs)' --cfgs='alloc=$(alloc-cfgs)' \
364364
$(realpath $(srctree)) $(realpath $(objtree)) \
365-
$(rustc_sysroot) $(RUST_LIB_SRC) $(KBUILD_EXTMOD) > \
366-
$(if $(KBUILD_EXTMOD),$(extmod_prefix),$(objtree))/rust-project.json
365+
$(rustc_sysroot) $(RUST_LIB_SRC) $(if $(KBUILD_EXTMOD),$(srcroot)) \
366+
> rust-project.json
367367

368368
redirect-intrinsics = \
369369
__addsf3 __eqsf2 __extendsfdf2 __gesf2 __lesf2 __ltsf2 __mulsf3 __nesf2 __truncdfsf2 __unordsf2 \

scripts/Makefile.build

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# Building
44
# ==========================================================================
55

6-
src := $(if $(VPATH),$(VPATH)/)$(obj)
6+
src := $(srcroot)/$(obj)
77

88
PHONY := $(obj)/
99
$(obj)/:

scripts/Makefile.clean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# Cleaning up
44
# ==========================================================================
55

6-
src := $(if $(VPATH),$(VPATH)/)$(obj)
6+
src := $(srcroot)/$(obj)
77

88
PHONY := __clean
99
__clean:

scripts/Makefile.compiler

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ cc-cross-prefix = $(firstword $(foreach c, $(1), \
1313
$(if $(shell command -v -- $(c)gcc 2>/dev/null), $(c))))
1414

1515
# output directory for tests below
16-
TMPOUT = $(if $(KBUILD_EXTMOD),$(firstword $(KBUILD_EXTMOD))/).tmp_$$$$
16+
TMPOUT = .tmp_$$$$
1717

1818
# try-run
1919
# Usage: option = $(call try-run, $(CC)...-o "$$TMP",option-ok,otherwise)

scripts/Makefile.modpost

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,13 @@ endif
111111
else
112112

113113
# set src + obj - they may be used in the modules's Makefile
114-
obj := $(KBUILD_EXTMOD)
115-
src := $(if $(VPATH),$(VPATH)/)$(obj)
114+
obj := .
115+
src := $(srcroot)
116116

117117
# Include the module's Makefile to find KBUILD_EXTRA_SYMBOLS
118118
include $(kbuild-file)
119119

120-
output-symdump := $(KBUILD_EXTMOD)/Module.symvers
120+
output-symdump := Module.symvers
121121

122122
ifeq ($(wildcard $(objtree)/Module.symvers),)
123123
missing-input := $(objtree)/Module.symvers

scripts/coccicheck

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,7 @@ command results in a shift count error.'
8080
NPROC=1
8181
else
8282
ONLINE=0
83-
if [ "$KBUILD_EXTMOD" = "" ] ; then
84-
OPTIONS="--dir $srctree $COCCIINCLUDE"
85-
else
86-
OPTIONS="--dir $KBUILD_EXTMOD $COCCIINCLUDE"
87-
fi
83+
OPTIONS="--dir $srcroot $COCCIINCLUDE"
8884

8985
# Use only one thread per core by default if hyperthreading is enabled
9086
THREADS_PER_CORE=$(LANG=C lscpu | grep "Thread(s) per core: " | tr -cd "[:digit:]")

scripts/nsdeps

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,6 @@ if ! { echo "$SPATCH_REQ_VERSION"; echo "$SPATCH_VERSION"; } | sort -CV ; then
1919
exit 1
2020
fi
2121

22-
if [ "$KBUILD_EXTMOD" ]; then
23-
src_prefix=
24-
else
25-
src_prefix=$srctree/
26-
fi
27-
2822
generate_deps_for_ns() {
2923
$SPATCH --very-quiet --in-place --sp-file \
3024
$srctree/scripts/coccinelle/misc/add_namespace.cocci -D nsdeps -D ns=$1 $2
@@ -34,7 +28,7 @@ generate_deps() {
3428
local mod=${1%.ko:}
3529
shift
3630
local namespaces="$*"
37-
local mod_source_files=$(sed "s|^\(.*\)\.o$|${src_prefix}\1.c|" $mod.mod)
31+
local mod_source_files=$(sed "s|^\(.*\)\.o$|${srcroot}/\1.c|" $mod.mod)
3832

3933
for ns in $namespaces; do
4034
echo "Adding namespace $ns to module $mod.ko."

scripts/package/install-extmod-build

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,13 @@ mkdir -p "${destdir}"
5151
if [ "${CC}" != "${HOSTCC}" ]; then
5252
echo "Rebuilding host programs with ${CC}..."
5353

54+
# This leverages external module building.
55+
# - Clear sub_make_done to allow the top-level Makefile to redo sub-make.
56+
# - Filter out --no-print-directory to print "Entering directory" logs
57+
# when Make changes the working directory.
58+
unset sub_make_done
59+
MAKEFLAGS=$(echo "${MAKEFLAGS}" | sed s/--no-print-directory//)
60+
5461
cat <<-'EOF' > "${destdir}/Kbuild"
5562
subdir-y := scripts
5663
EOF

0 commit comments

Comments
 (0)