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: 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 <masahiroy@kernel.org>
Reviewed-by: Nicolas Schier <n.schier@avm.de>

+86 -67
+6 -14
Documentation/dev-tools/coccinelle.rst
··· 250 250 - Your directory from which spatch is called is processed next 251 251 - The directory provided with the ``--dir`` option is processed last, if used 252 252 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 - 257 253 ``make coccicheck`` also supports using M= targets. If you do not supply 258 254 any M= target, it is assumed you want to target the entire kernel. 259 255 The kernel coccicheck script has:: 260 256 261 - if [ "$KBUILD_EXTMOD" = "" ] ; then 262 - OPTIONS="--dir $srctree $COCCIINCLUDE" 263 - else 264 - OPTIONS="--dir $KBUILD_EXTMOD $COCCIINCLUDE" 265 - fi 257 + OPTIONS="--dir $srcroot $COCCIINCLUDE" 266 258 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. 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. 272 264 273 265 If not using the kernel's coccicheck target, keep the above precedence 274 266 order logic of .cocciconfig reading. If using the kernel's coccicheck target,
+14
Documentation/kbuild/makefiles.rst
··· 449 449 to prerequisites are referenced with $(src) (because they are not 450 450 generated files). 451 451 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 + 452 466 $(kecho) 453 467 echoing information to user in a rule is often a good practice 454 468 but when execution ``make -s`` one does not expect to see any output
+49 -33
Makefile
··· 180 180 KBUILD_OUTPUT := $(O) 181 181 endif 182 182 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 184 201 185 202 # Do we want to change the working directory? 186 203 ifneq ($(output),) ··· 247 230 248 231 # We process the rest of the Makefile if this is the final invocation of make 249 232 250 - ifeq ($(abs_srctree),$(CURDIR)) 251 - # building in the source tree 252 - srctree := . 253 - building_out_of_srctree := 233 + ifndef KBUILD_EXTMOD 234 + srcroot := $(abs_srctree) 235 + endif 236 + 237 + ifeq ($(srcroot),$(CURDIR)) 238 + building_out_of_srctree := 254 239 else 255 - ifeq ($(abs_srctree)/,$(dir $(CURDIR))) 256 - # building in a subdirectory of the source tree 257 - srctree := .. 258 - else 259 - srctree := $(abs_srctree) 260 - endif 261 - building_out_of_srctree := 1 240 + export building_out_of_srctree := 1 262 241 endif 263 242 264 - ifneq ($(KBUILD_ABS_SRCTREE),) 265 - srctree := $(abs_srctree) 243 + ifdef KBUILD_ABS_SRCTREE 244 + # Do nothing. Use the absolute path. 245 + else ifeq ($(srcroot),$(CURDIR)) 246 + # Building in the source. 247 + srcroot := . 248 + else ifeq ($(srcroot)/,$(dir $(CURDIR))) 249 + # Building in a subdirectory of the source. 250 + srcroot := .. 266 251 endif 267 252 268 - objtree := . 253 + export srctree := $(if $(KBUILD_EXTMOD),$(abs_srctree),$(srcroot)) 269 254 270 - VPATH := 271 - 272 - ifeq ($(KBUILD_EXTMOD),) 273 255 ifdef building_out_of_srctree 274 - VPATH := $(srctree) 256 + export VPATH := $(srcroot) 257 + else 258 + VPATH := 275 259 endif 276 - endif 277 - 278 - export building_out_of_srctree srctree objtree VPATH 279 260 280 261 # To make sure we do not include .config for any of the *config targets 281 262 # catch them early, and hand them over to scripts/kconfig/Makefile ··· 555 540 LINUXINCLUDE := \ 556 541 -I$(srctree)/arch/$(SRCARCH)/include \ 557 542 -I$(objtree)/arch/$(SRCARCH)/include/generated \ 558 - $(if $(building_out_of_srctree),-I$(srctree)/include) \ 543 + -I$(srctree)/include \ 559 544 -I$(objtree)/include \ 560 545 $(USERINCLUDE) 561 546 ··· 726 711 # in addition to whatever we do anyway. 727 712 # Just "make" or "make all" shall build modules as well 728 713 729 - ifneq ($(filter all modules nsdeps %compile_commands.json clang-%,$(MAKECMDGOALS)),) 714 + ifneq ($(filter all modules nsdeps compile_commands.json clang-%,$(MAKECMDGOALS)),) 730 715 KBUILD_MODULES := 1 731 716 endif 732 717 ··· 1122 1107 1123 1108 PHONY += prepare0 1124 1109 1125 - export extmod_prefix = $(if $(KBUILD_EXTMOD),$(KBUILD_EXTMOD)/) 1110 + export extmod_prefix = 1126 1111 export MODORDER := $(extmod_prefix)modules.order 1127 1112 export MODULES_NSDEPS := $(extmod_prefix)modules.nsdeps 1128 1113 ··· 1814 1799 KBUILD_BUILTIN := 1815 1800 KBUILD_MODULES := 1 1816 1801 1817 - build-dir := $(KBUILD_EXTMOD) 1802 + build-dir := . 1818 1803 1819 - compile_commands.json: $(extmod_prefix)compile_commands.json 1820 - PHONY += compile_commands.json 1821 - 1822 - clean-dirs := $(KBUILD_EXTMOD) 1823 - clean: private rm-files := $(KBUILD_EXTMOD)/Module.symvers $(KBUILD_EXTMOD)/modules.nsdeps \ 1824 - $(KBUILD_EXTMOD)/compile_commands.json 1804 + clean-dirs := . 1805 + clean: private rm-files := Module.symvers modules.nsdeps compile_commands.json 1825 1806 1826 1807 PHONY += prepare 1827 1808 # now expand this into a simple variable to reduce the cost of shell evaluations ··· 1959 1948 1960 1949 clean: $(clean-dirs) 1961 1950 $(call cmd,rmfiles) 1962 - @find $(or $(KBUILD_EXTMOD), .) $(RCS_FIND_IGNORE) \ 1951 + @find . $(RCS_FIND_IGNORE) \ 1963 1952 \( -name '*.[aios]' -o -name '*.rsi' -o -name '*.ko' -o -name '.*.cmd' \ 1964 1953 -o -name '*.ko.*' \ 1965 1954 -o -name '*.dtb' -o -name '*.dtbo' \ ··· 1992 1981 PHONY += rust-analyzer 1993 1982 rust-analyzer: 1994 1983 +$(Q)$(CONFIG_SHELL) $(srctree)/scripts/rust_is_available.sh 1984 + ifdef KBUILD_EXTMOD 1985 + # FIXME: external modules must not descend into a sub-directory of the kernel 1986 + $(Q)$(MAKE) $(build)=$(objtree)/rust src=$(srctree)/rust $@ 1987 + else 1995 1988 $(Q)$(MAKE) $(build)=rust $@ 1989 + endif 1996 1990 1997 1991 # Script to generate missing namespace dependencies 1998 1992 # ---------------------------------------------------------------------------
+2 -2
rust/Makefile
··· 362 362 $(Q)$(srctree)/scripts/generate_rust_analyzer.py \ 363 363 --cfgs='core=$(core-cfgs)' --cfgs='alloc=$(alloc-cfgs)' \ 364 364 $(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 367 367 368 368 redirect-intrinsics = \ 369 369 __addsf3 __eqsf2 __extendsfdf2 __gesf2 __lesf2 __ltsf2 __mulsf3 __nesf2 __truncdfsf2 __unordsf2 \
+1 -1
scripts/Makefile.build
··· 3 3 # Building 4 4 # ========================================================================== 5 5 6 - src := $(if $(VPATH),$(VPATH)/)$(obj) 6 + src := $(srcroot)/$(obj) 7 7 8 8 PHONY := $(obj)/ 9 9 $(obj)/:
+1 -1
scripts/Makefile.clean
··· 3 3 # Cleaning up 4 4 # ========================================================================== 5 5 6 - src := $(if $(VPATH),$(VPATH)/)$(obj) 6 + src := $(srcroot)/$(obj) 7 7 8 8 PHONY := __clean 9 9 __clean:
+1 -1
scripts/Makefile.compiler
··· 13 13 $(if $(shell command -v -- $(c)gcc 2>/dev/null), $(c)))) 14 14 15 15 # output directory for tests below 16 - TMPOUT = $(if $(KBUILD_EXTMOD),$(firstword $(KBUILD_EXTMOD))/).tmp_$$$$ 16 + TMPOUT = .tmp_$$$$ 17 17 18 18 # try-run 19 19 # Usage: option = $(call try-run, $(CC)...-o "$$TMP",option-ok,otherwise)
+3 -3
scripts/Makefile.modpost
··· 111 111 else 112 112 113 113 # 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) 116 116 117 117 # Include the module's Makefile to find KBUILD_EXTRA_SYMBOLS 118 118 include $(kbuild-file) 119 119 120 - output-symdump := $(KBUILD_EXTMOD)/Module.symvers 120 + output-symdump := Module.symvers 121 121 122 122 ifeq ($(wildcard $(objtree)/Module.symvers),) 123 123 missing-input := $(objtree)/Module.symvers
+1 -5
scripts/coccicheck
··· 80 80 NPROC=1 81 81 else 82 82 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" 88 84 89 85 # Use only one thread per core by default if hyperthreading is enabled 90 86 THREADS_PER_CORE=$(LANG=C lscpu | grep "Thread(s) per core: " | tr -cd "[:digit:]")
+1 -7
scripts/nsdeps
··· 19 19 exit 1 20 20 fi 21 21 22 - if [ "$KBUILD_EXTMOD" ]; then 23 - src_prefix= 24 - else 25 - src_prefix=$srctree/ 26 - fi 27 - 28 22 generate_deps_for_ns() { 29 23 $SPATCH --very-quiet --in-place --sp-file \ 30 24 $srctree/scripts/coccinelle/misc/add_namespace.cocci -D nsdeps -D ns=$1 $2 ··· 28 34 local mod=${1%.ko:} 29 35 shift 30 36 local namespaces="$*" 31 - local mod_source_files=$(sed "s|^\(.*\)\.o$|${src_prefix}\1.c|" $mod.mod) 37 + local mod_source_files=$(sed "s|^\(.*\)\.o$|${srcroot}/\1.c|" $mod.mod) 32 38 33 39 for ns in $namespaces; do 34 40 echo "Adding namespace $ns to module $mod.ko."
+7
scripts/package/install-extmod-build
··· 51 51 if [ "${CC}" != "${HOSTCC}" ]; then 52 52 echo "Rebuilding host programs with ${CC}..." 53 53 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 + 54 61 cat <<-'EOF' > "${destdir}/Kbuild" 55 62 subdir-y := scripts 56 63 EOF