kbuild: support building external modules in a separate build directory
Subsystems (8)
cover kbuild: support building external modules in a separate build directory
There has been a long-standing request to support building external modules in a separate build directory.
The first half is cleanups of documents and Makefiles.
The last part adds KBUILD_EXTMOD_OUTPUT (MO=). This is too big changes, and too late for the current MW. (I did not test kselftest at all.)
I hope people test this and may uncover some issues.
Masahiro Yamada (23):
kbuild: doc: update the description about Kbuild/Makefile split
kbuild: doc: remove description about grepping CONFIG options
kbuild: doc: remove outdated description of the limitation on -I usage
kbuild: doc: remove the description about shipped files
kbuild: doc: describe the -C option precisely for external module
builds
kbuild: doc: replace "gcc" in external module description
kbuild: remove unnecessary prune of rust/alloc for rustfmt
kbuild: simplify find command for rustfmt
speakup: use SPKDIR=$(src) to specify the source directory
kbuild: refactor the check for missing config files
kbuild: check the presence of include/generated/rustc_cfg scripts/nsdeps: use VPATH as src_prefix
kbuild: replace two $(abs_objtree) with $(CURDIR) in top Makefile
kbuild: add $(objtree)/ prefix to some in-kernel build artifacts
kbuild: rename abs_objtree to abs_output
kbuild: use 'output' variable to create the output directory
kbuild: build external modules in their directory
kbuild: remove extmod_prefix, MODORDER, MODULES_NSDEPS variables
kbuild: support building external modules in a separate build
directory
kbuild: support -fmacro-prefix-map for external modules
kbuild: use absolute path in the generated wrapper Makefile
kbuild: make wrapper Makefile more convenient for external modules
kbuild: allow to start building external module in any directory
Documentation/dev-tools/coccinelle.rst | 19 +-- Documentation/kbuild/kbuild.rst | 11 ++ Documentation/kbuild/modules.rst | 104 ++++---------- Makefile | 184 ++++++++++++++++--------- drivers/accessibility/speakup/Makefile | 4 +- rust/Makefile | 4 +- scripts/Kbuild.include | 2 +- scripts/Makefile.build | 4 +- scripts/Makefile.compiler | 2 +- scripts/Makefile.modfinal | 16 +-- scripts/Makefile.modinst | 4 +- scripts/Makefile.modpost | 24 ++-- scripts/coccicheck | 6 +- scripts/depmod.sh | 4 +- scripts/nsdeps | 6 +- scripts/package/install-extmod-build | 7 + 16 files changed, 209 insertions(+), 192 deletions(-)
Signature · 1 line
2.43.0
PATCH 1/23 kbuild: doc: update the description about Kbuild/Makefile split
The phrase "In newer versions of the kernel" was added by commit efdf02cf0651 ("Documentation/kbuild: major edit of modules.txt sections 1-4"). This feature is no longer new, so remove it and update the paragraph.
Example 3 was written 20 years ago. [1] There is no need to note about backward compatibility with such an old build system. Remove Example 3 entirely.
---
Documentation/kbuild/modules.rst | 38 +++----------------------------- 1 file changed, 3 insertions(+), 35 deletions(-)
Documentation/kbuild/modules.rst +3 -35
@@ -224,10 +224,9 @@ module 8123.ko, which is built from the following files:: 3.2 Separate Kbuild File and Makefile ------------------------------------- - In newer versions of the kernel, kbuild will first look for a - file named "Kbuild," and only if that is not found, will it - then look for a makefile. Utilizing a "Kbuild" file allows us - to split up the makefile from example 1 into two files: + Kbuild will first look for a file named "Kbuild", and if it is not + found, it will then look for "Makefile". Utilizing a "Kbuild" file + allows us to split up the "Makefile" from example 1 into two files: Example 2::
@@ -250,37 +249,6 @@ module 8123.ko, which is built from the following files:: consisting of several hundred lines, and here it really pays off to separate the kbuild part from the rest. - The next example shows a backward compatible version. - - Example 3:: - - --> filename: Kbuild - obj-m := 8123.o - 8123-y := 8123_if.o 8123_pci.o 8123_bin.o - - --> filename: Makefile - ifneq ($(KERNELRELEASE),) - # kbuild part of makefile - include Kbuild - - else - # normal makefile - KDIR ?= /lib/modules/`uname -r`/build - - default: - $(MAKE) -C $(KDIR) M=$$PWD - - # Module specific targets - genbin: - echo "X" > 8123_bin.o_shipped - - endif - - Here the "Kbuild" file is included from the makefile. This - allows an older version of kbuild, which only knows of - makefiles, to be used when the "make" and kbuild parts are - split into separate files. - 3.3 Binary Blobs ----------------
Signature · 1 line
2.43.0
PATCH 2/23 kbuild: doc: remove description about grepping CONFIG options
In reply to Masahiro YamadaThis description was added 20 years ago [1]. It does not convey any useful information except for a feeling of nostalgia.
---
Documentation/kbuild/modules.rst | 6 ------ 1 file changed, 6 deletions(-)
Documentation/kbuild/modules.rst +0 -6
@@ -521,9 +521,3 @@ build. ext2-y := balloc.o bitmap.o dir.o ext2-$(CONFIG_EXT2_FS_XATTR) += xattr.o - - External modules have traditionally used "grep" to check for - specific `CONFIG_` settings directly in .config. This usage is - broken. As introduced before, external modules should use - kbuild for building and can therefore use the same methods as - in-tree modules when testing for `CONFIG_` definitions.
Signature · 1 line
2.43.0
PATCH 3/23 kbuild: doc: remove outdated description of the limitation on -I usage
In reply to Masahiro YamadaKbuild used to manipulate header search paths, enforcing the odd limitation of "no space after -I".
Commit cdd750bfb1f7 ("kbuild: remove 'addtree' and 'flags' magic for header search paths") stopped doing that. This limitation no longer exists. Instead, you need to accurately specify the header search path. (In this case, $(src)/include)
---
Documentation/kbuild/modules.rst | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-)
Documentation/kbuild/modules.rst +1 -5
@@ -328,13 +328,9 @@ according to the following rule: --> filename: Kbuild obj-m := 8123.o - ccflags-y := -Iinclude + ccflags-y := -I $(src)/include 8123-y := 8123_if.o 8123_pci.o 8123_bin.o - Note that in the assignment there is no space between -I and - the path. This is a limitation of kbuild: there must be no - space present. - 4.3 Several Subdirectories --------------------------
Signature · 1 line
2.43.0
PATCH 4/23 kbuild: doc: remove the description about shipped files
In reply to Masahiro YamadaThe use of shipped files is discouraged in the upstream kernel these days. [1]
Downstream Makefiles have the freedom to use shipped files or other options to handle binaries, but this is not what should be advertised in the upstream document.
[1]: https://lore.kernel.org/all/CAHk-=wgSEi_ZrHdqr=20xv+d6dr5G895CbOAi8ok+7-CQUN=fQ@mail.gmail.com/
---
Documentation/kbuild/modules.rst | 35 +++----------------------------- 1 file changed, 3 insertions(+), 32 deletions(-)
Documentation/kbuild/modules.rst +3 -32
@@ -180,7 +180,6 @@ module 8123.ko, which is built from the following files:: 8123_if.c 8123_if.h 8123_pci.c - 8123_bin.o_shipped <= Binary blob 3.1 Shared Makefile -------------------
@@ -198,7 +197,7 @@ module 8123.ko, which is built from the following files:: ifneq ($(KERNELRELEASE),) # kbuild part of makefile obj-m := 8123.o - 8123-y := 8123_if.o 8123_pci.o 8123_bin.o + 8123-y := 8123_if.o 8123_pci.o else # normal makefile
@@ -207,10 +206,6 @@ module 8123.ko, which is built from the following files:: default: $(MAKE) -C $(KDIR) M=$$PWD - # Module specific targets - genbin: - echo "X" > 8123_bin.o_shipped - endif The check for KERNELRELEASE is used to separate the two parts
@@ -232,7 +227,7 @@ module 8123.ko, which is built from the following files:: --> filename: Kbuild obj-m := 8123.o - 8123-y := 8123_if.o 8123_pci.o 8123_bin.o + 8123-y := 8123_if.o 8123_pci.o --> filename: Makefile KDIR ?= /lib/modules/`uname -r`/build
@@ -240,35 +235,11 @@ module 8123.ko, which is built from the following files:: default: $(MAKE) -C $(KDIR) M=$$PWD - # Module specific targets - genbin: - echo "X" > 8123_bin.o_shipped - The split in example 2 is questionable due to the simplicity of each file; however, some external modules use makefiles consisting of several hundred lines, and here it really pays off to separate the kbuild part from the rest. -3.3 Binary Blobs ----------------- - - Some external modules need to include an object file as a blob. - kbuild has support for this, but requires the blob file to be - named <filename>_shipped. When the kbuild rules kick in, a copy - of <filename>_shipped is created with _shipped stripped off, - giving us <filename>. This shortened filename can be used in - the assignment to the module. - - Throughout this section, 8123_bin.o_shipped has been used to - build the kernel module 8123.ko; it has been included as - 8123_bin.o:: - - 8123-y := 8123_if.o 8123_pci.o 8123_bin.o - - Although there is no distinction between the ordinary source - files and the binary file, kbuild will pick up different rules - when creating the object file for the module. - 3.4 Building Multiple Modules =============================
@@ -329,7 +300,7 @@ according to the following rule: obj-m := 8123.o ccflags-y := -I $(src)/include - 8123-y := 8123_if.o 8123_pci.o 8123_bin.o + 8123-y := 8123_if.o 8123_pci.o 4.3 Several Subdirectories --------------------------
Signature · 1 line
2.43.0
PATCH 5/23 kbuild: doc: describe the -C option precisely for external module builds
In reply to Masahiro YamadaBuilding external modules is typically done using this command:
$ make -C <KERNEL_DIR> M=<EXTMOD_DIR>
Here, <KERNEL_DIR> refers to the output directory where the kernel was built, not the kernel source directory.
When the kernel is built in-tree, there is no ambiguity, as the output directory and the source directory are the same.
If the kernel was built in a separate build directory, <KERNEL_DIR> should be the kernel output directory. Otherwise, Kbuild cannot locate necessary build artifacts such as the .config file, etc. This has been the method for building external modules based on the kernel compiled in a separate directory for over 20 years. [1]
If you pass the kernel source directory to the -C option, you must also specify the kernel build directory using the O= option. This approach works as well, though it results in a slightly longer command:
$ make -C <KERNEL_SOURCE_DIR> O=<KERNEL_BUILD_DIR> M=<EXTMOD_DIR>
Some people mistakenly believe that O= should point to a separate output directory for external modules when used together with M=. This commit adds more clarification to Documentation/kbuild/kbuild.rst.
---
Documentation/kbuild/kbuild.rst | 5 +++++ Documentation/kbuild/modules.rst | 9 ++++++--- 2 files changed, 11 insertions(+), 3 deletions(-)
Documentation/kbuild/kbuild.rst +5 -0
@@ -129,6 +129,11 @@ KBUILD_OUTPUT ------------- Specify the output directory when building the kernel. +This variable can also be used to point to the kernel output directory when +building external modules using kernel build artifacts in a separate build +directory. Please note that this does NOT specify the output directory for the +external modules themselves. + The output directory can also be specified using "O=...". Setting "O=..." takes precedence over KBUILD_OUTPUT.
Documentation/kbuild/modules.rst +6 -3
@@ -74,7 +74,7 @@ executed to make module versioning work. The command to build an external module is:: - $ make -C <path_to_kernel_src> M=$PWD + $ make -C <path_to_kernel_dir> M=$PWD The kbuild system knows that an external module is being built due to the "M=<dir>" option given in the command.
@@ -91,12 +91,15 @@ executed to make module versioning work. 2.2 Options =========== - ($KDIR refers to the path of the kernel source directory.) + ($KDIR refers to the path of the kernel source directory, or the path + of the kernel output directory if the kernel was built in a separate + build directory.) make -C $KDIR M=$PWD -C $KDIR - The directory where the kernel source is located. + The directory that contains the kernel and relevant build + artifacts used for building an external module. "make" will actually change to the specified directory when executing and will change back when finished.
Signature · 1 line
2.43.0
PATCH 6/23 kbuild: doc: replace "gcc" in external module description
In reply to Masahiro YamadaAvoid "gcc" since it is not the only compiler supported by Kbuild.
---
Documentation/kbuild/modules.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
Documentation/kbuild/modules.rst +2 -2
@@ -38,7 +38,7 @@ This document describes how to build an out-of-tree kernel module. "kbuild" is the build system used by the Linux kernel. Modules must use kbuild to stay compatible with changes in the build infrastructure and -to pick up the right flags to "gcc." Functionality for building modules +to pick up the right flags to the compiler. Functionality for building modules both in-tree and out-of-tree is provided. The method for building either is similar, and all modules are initially developed and built out-of-tree.
@@ -284,7 +284,7 @@ according to the following rule: #include <linux/module.h> - kbuild will add options to "gcc" so the relevant directories + kbuild will add options to the compiler so the relevant directories are searched. 4.2 Single Subdirectory
Signature · 1 line
2.43.0
PATCH 7/23 kbuild: remove unnecessary prune of rust/alloc for rustfmt
In reply to Masahiro YamadaCommit 9d0441bab775 ("rust: alloc: remove our fork of the alloc crate") removed the rust/alloc/ directory.
---
Makefile | 2 -- 1 file changed, 2 deletions(-)
Makefile +0 -2
@@ -1741,9 +1741,7 @@ PHONY += rustfmt rustfmtcheck # We `grep` afterwards in order to remove the directory entry itself. rustfmt: $(Q)find $(abs_srctree) -type f -name '*.rs' \ - -o -path $(abs_srctree)/rust/alloc -prune \ -o -path $(abs_objtree)/rust/test -prune \ - | grep -Fv $(abs_srctree)/rust/alloc \ | grep -Fv $(abs_objtree)/rust/test \ | grep -Fv generated \ | xargs $(RUSTFMT) $(rustfmt_flags)
Signature · 1 line
2.43.0
PATCH 8/23 kbuild: simplify find command for rustfmt
In reply to Masahiro YamadaThe current 'find' command does not prune the rust/test directory itself, requiring an additional 'grep -Fv' command to exclude it. This is cumbersome.
The correct use of the -prune option can be seen in the 'make clean' rule.
[Current command]
$ find . -type f -name '*.rs' -o -path ./rust/test -prune | wc
70 70 1939
$ find . -type f -name '*.rs' -o -path ./rust/test -prune | grep rust/test
./rust/test[Improved command]
$ find . -path ./rust/test -prune -o -type f -name '*.rs' -print | wc
69 69 1927
$ find . -path ./rust/test -prune -o -type f -name '*.rs' -print | grep rust/testWith the improved 'find' command, the grep command is no longer needed.
There is also no need to use the absolute path, so $(abs_srctree) can be replaced with $(srctree).
The pruned directory rust/test must be prefixed with $(srctree) instead of $(objtree). Otherwise, 'make O=... rustfmt' would visit the stale rust/test directory remaining in the source tree.
---
Makefile | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)
Makefile +2 -3
@@ -1740,9 +1740,8 @@ PHONY += rustfmt rustfmtcheck # when matching, which is a problem when e.g. `srctree` is `..`. # We `grep` afterwards in order to remove the directory entry itself. rustfmt: - $(Q)find $(abs_srctree) -type f -name '*.rs' \ - -o -path $(abs_objtree)/rust/test -prune \ - | grep -Fv $(abs_objtree)/rust/test \ + $(Q)find $(srctree) -path $(srctree)/rust/test -prune \ + -o -type f -name '*.rs' -print \ | grep -Fv generated \ | xargs $(RUSTFMT) $(rustfmt_flags)
Signature · 1 line
2.43.0
PATCH 9/23 speakup: use SPKDIR=$(src) to specify the source directory
In reply to Masahiro YamadaSince commit b1992c3772e6 ("kbuild: use $(src) instead of $(srctree)/$(src) for source directory"), $(src) consistently points to the source directory whether this is compiled an as external module or not.
---
drivers/accessibility/speakup/Makefile | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/accessibility/speakup/Makefile +1 -3
@@ -40,9 +40,7 @@ hostprogs += makemapdata makemapdata-objs := makemapdata.o quiet_cmd_mkmap = MKMAP $@ - cmd_mkmap = TOPDIR=$(srctree) \ - SPKDIR=$(if $(KBUILD_EXTMOD),$(KBUILD_EXTMOD),$(srctree)/drivers/accessibility/speakup) \ - $(obj)/makemapdata > $@ + cmd_mkmap = TOPDIR=$(srctree) SPKDIR=$(src) $(obj)/makemapdata > $@ $(obj)/mapdata.h: $(obj)/makemapdata $(call cmd,mkmap)
Signature · 1 line
2.43.0
PATCH 10/23 kbuild: refactor the check for missing config files
In reply to Masahiro YamadaThis commit refactors the check for missing configuration files, making it easier to add more files to the list.
The format of the error message has been slightly changed, as follows:
[Before]
ERROR: Kernel configuration is invalid.
include/generated/autoconf.h or include/config/auto.conf are missing. Run 'make oldconfig && make prepare' on kernel src to fix it.
[After]
***
*** ERROR: Kernel configuration is invalid. The following files are missing:
*** - include/generated/autoconf.h
*** - include/config/auto.conf
*** Run "make oldconfig && make prepare" on kernel source to fix it.
***
---
Makefile | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-)
Makefile +13 -8
@@ -778,17 +778,22 @@ $(KCONFIG_CONFIG): else # !may-sync-config # External modules and some install targets need include/generated/autoconf.h # and include/config/auto.conf but do not care if they are up-to-date. -# Use auto.conf to trigger the test +# Use auto.conf to show the error message + +checked-configs := include/generated/autoconf.h include/config/auto.conf +missing-configs := $(filter-out $(wildcard $(checked-configs)), $(checked-configs)) + +ifdef missing-configs PHONY += include/config/auto.conf include/config/auto.conf: - @test -e include/generated/autoconf.h -a -e $@ || ( \ - echo >&2; \ - echo >&2 " ERROR: Kernel configuration is invalid."; \ - echo >&2 " include/generated/autoconf.h or $@ are missing.";\ - echo >&2 " Run 'make oldconfig && make prepare' on kernel src to fix it."; \ - echo >&2 ; \ - /bin/false) + @echo >&2 '***' + @echo >&2 '*** ERROR: Kernel configuration is invalid. The following files are missing:' + @printf >&2 '*** - %s\n' $(missing-configs) + @echo >&2 '*** Run "make oldconfig && make prepare" on kernel source to fix it.' + @echo >&2 '***' + @/bin/false +endif endif # may-sync-config endif # need-config
Signature · 1 line
2.43.0
PATCH 11/23 kbuild: check the presence of include/generated/rustc_cfg
In reply to Masahiro YamadaSince commit 2f7ab1267dc9 ("Kbuild: add Rust support"), Kconfig generates include/generated/rustc_cfg, but its presence is not checked in the top-level Makefile. It should be checked similarly to the C header counterpart, include/generated/autoconf.h.
---
Makefile | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
Makefile +3 -2
@@ -780,7 +780,7 @@ else # !may-sync-config # and include/config/auto.conf but do not care if they are up-to-date. # Use auto.conf to show the error message -checked-configs := include/generated/autoconf.h include/config/auto.conf +checked-configs := include/generated/autoconf.h include/generated/rustc_cfg include/config/auto.conf missing-configs := $(filter-out $(wildcard $(checked-configs)), $(checked-configs)) ifdef missing-configs
@@ -1187,7 +1187,8 @@ PHONY += prepare archprepare archprepare: outputmakefile archheaders archscripts scripts include/config/kernel.release \ asm-generic $(version_h) include/generated/utsrelease.h \ - include/generated/compile.h include/generated/autoconf.h remove-stale-files + include/generated/compile.h include/generated/autoconf.h \ + include/generated/rustc_cfg remove-stale-files prepare0: archprepare $(Q)$(MAKE) $(build)=scripts/mod
Signature · 1 line
2.43.0
PATCH 12/23 scripts/nsdeps: use VPATH as src_prefix
In reply to Masahiro YamadaThis change allows it to work not only for in-tree modules but also for external modules, even if they are built in a separate build directory.
---
scripts/nsdeps | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
scripts/nsdeps +2 -2
@@ -19,10 +19,10 @@ if ! { echo "$SPATCH_REQ_VERSION"; echo "$SPATCH_VERSION"; } | sort -CV ; then exit 1 fi -if [ "$KBUILD_EXTMOD" ]; then +if [ "${VPATH+set}" ]; then src_prefix= else - src_prefix=$srctree/ + src_prefix=$VPATH/ fi generate_deps_for_ns() {
Signature · 1 line
2.43.0
PATCH 13/23 kbuild: replace two $(abs_objtree) with $(CURDIR) in top Makefile
In reply to Masahiro YamadaKbuild changes the working directory until it matches $(abs_objtree).
When $(need-sub-make) is empty, $(abs_objtree) is the same as $(CURDIR).
---
Makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
Makefile +2 -2
@@ -228,12 +228,12 @@ else # need-sub-make # We process the rest of the Makefile if this is the final invocation of make -ifeq ($(abs_srctree),$(abs_objtree)) +ifeq ($(abs_srctree),$(CURDIR)) # building in the source tree srctree := . building_out_of_srctree := else - ifeq ($(abs_srctree)/,$(dir $(abs_objtree))) + ifeq ($(abs_srctree)/,$(dir $(CURDIR))) # building in a subdirectory of the source tree srctree := .. else
Signature · 1 line
2.43.0
PATCH 14/23 kbuild: add $(objtree)/ prefix to some in-kernel build artifacts
In reply to Masahiro Yamada$(objtree) refers to the top of the output directory of kernel builds.
This commit adds the explicit (objtree)/ prefix to build artifacts needed for building external modules.
This change has no immediate impact, as the top-level Makefile currently defines:
objtree := .
However, it prepares for supporting the building of external modules in a different directory.
---
Makefile | 10 +++++----- scripts/Kbuild.include | 2 +- scripts/Makefile.build | 4 ++-- scripts/Makefile.modfinal | 14 +++++++------- scripts/Makefile.modinst | 2 +- scripts/Makefile.modpost | 12 ++++++------ scripts/depmod.sh | 4 ++-- 7 files changed, 24 insertions(+), 24 deletions(-)
Makefile +5 -5
@@ -354,7 +354,7 @@ else # !mixed-build include $(srctree)/scripts/Kbuild.include # Read KERNELRELEASE from include/config/kernel.release (if it exists) -KERNELRELEASE = $(call read-file, include/config/kernel.release) +KERNELRELEASE = $(call read-file, $(objtree)/include/config/kernel.release) KERNELVERSION = $(VERSION)$(if $(PATCHLEVEL),.$(PATCHLEVEL)$(if $(SUBLEVEL),.$(SUBLEVEL)))$(EXTRAVERSION) export VERSION PATCHLEVEL SUBLEVEL KERNELRELEASE KERNELVERSION
@@ -717,7 +717,7 @@ endif export KBUILD_MODULES KBUILD_BUILTIN ifdef need-config -include include/config/auto.conf +include $(objtree)/include/config/auto.conf endif ifeq ($(KBUILD_EXTMOD),)
@@ -780,13 +780,13 @@ else # !may-sync-config # and include/config/auto.conf but do not care if they are up-to-date. # Use auto.conf to show the error message -checked-configs := include/generated/autoconf.h include/generated/rustc_cfg include/config/auto.conf +checked-configs := $(addprefix $(objtree)/, include/generated/autoconf.h include/generated/rustc_cfg include/config/auto.conf) missing-configs := $(filter-out $(wildcard $(checked-configs)), $(checked-configs)) ifdef missing-configs -PHONY += include/config/auto.conf +PHONY += $(objtree)/include/config/auto.conf -include/config/auto.conf: +$(objtree)/include/config/auto.conf: @echo >&2 '***' @echo >&2 '*** ERROR: Kernel configuration is invalid. The following files are missing:' @printf >&2 '*** - %s\n' $(missing-configs)
scripts/Kbuild.include +1 -1
@@ -205,7 +205,7 @@ if_changed_dep = $(if $(if-changed-cond),$(cmd_and_fixdep),@:) cmd_and_fixdep = \ $(cmd); \ - scripts/basic/fixdep $(depfile) $@ '$(make-cmd)' > $(dot-target).cmd;\ + $(objtree)/scripts/basic/fixdep $(depfile) $@ '$(make-cmd)' > $(dot-target).cmd;\ rm -f $(depfile) # Usage: $(call if_changed_rule,foo)
scripts/Makefile.build +2 -2
@@ -34,7 +34,7 @@ subdir-asflags-y := subdir-ccflags-y := # Read auto.conf if it exists, otherwise ignore --include include/config/auto.conf +-include $(objtree)/include/config/auto.conf include $(srctree)/scripts/Kbuild.include include $(srctree)/scripts/Makefile.compiler
@@ -107,7 +107,7 @@ cmd_cpp_i_c = $(CPP) $(c_flags) -o $@ $< $(obj)/%.i: $(obj)/%.c FORCE $(call if_changed_dep,cpp_i_c) -genksyms = scripts/genksyms/genksyms \ +genksyms = $(objtree)/scripts/genksyms/genksyms \ $(if $(1), -T $(2)) \ $(if $(KBUILD_PRESERVE), -p) \ -r $(or $(wildcard $(2:.symtypes=.symref)), /dev/null)
scripts/Makefile.modfinal +7 -7
@@ -6,7 +6,7 @@ PHONY := __modfinal __modfinal: -include include/config/auto.conf +include $(objtree)/include/config/auto.conf include $(srctree)/scripts/Kbuild.include # for c_flags
@@ -37,15 +37,15 @@ quiet_cmd_ld_ko_o = LD [M] $@ cmd_ld_ko_o = \ $(LD) -r $(KBUILD_LDFLAGS) \ $(KBUILD_LDFLAGS_MODULE) $(LDFLAGS_MODULE) \ - -T scripts/module.lds -o $@ $(filter %.o, $^) + -T $(objtree)/scripts/module.lds -o $@ $(filter %.o, $^) quiet_cmd_btf_ko = BTF [M] $@ cmd_btf_ko = \ - if [ ! -f vmlinux ]; then \ + if [ ! -f $(objtree)/vmlinux ]; then \ printf "Skipping BTF generation for %s due to unavailability of vmlinux\n" $@ 1>&2; \ else \ - LLVM_OBJCOPY="$(OBJCOPY)" $(PAHOLE) -J $(PAHOLE_FLAGS) $(MODULE_PAHOLE_FLAGS) --btf_base vmlinux $@; \ - $(RESOLVE_BTFIDS) -b vmlinux $@; \ + LLVM_OBJCOPY="$(OBJCOPY)" $(PAHOLE) -J $(PAHOLE_FLAGS) $(MODULE_PAHOLE_FLAGS) --btf_base $(objtree)/vmlinux $@; \ + $(RESOLVE_BTFIDS) -b $(objtree)/vmlinux $@; \ fi; # Same as newer-prereqs, but allows to exclude specified extra dependencies
@@ -57,8 +57,8 @@ if_changed_except = $(if $(call newer_prereqs_except,$(2))$(cmd-check), \ printf '%s\n' 'savedcmd_$@ := $(make-cmd)' > $(dot-target).cmd, @:) # Re-generate module BTFs if either module's .ko or vmlinux changed -%.ko: %.o %.mod.o $(extmod_prefix).module-common.o scripts/module.lds $(and $(CONFIG_DEBUG_INFO_BTF_MODULES),$(KBUILD_BUILTIN),vmlinux) FORCE - +$(call if_changed_except,ld_ko_o,vmlinux) +%.ko: %.o %.mod.o $(extmod_prefix).module-common.o $(objtree)/scripts/module.lds $(and $(CONFIG_DEBUG_INFO_BTF_MODULES),$(KBUILD_BUILTIN),$(objtree)/vmlinux) FORCE + +$(call if_changed_except,ld_ko_o,$(objtree)/vmlinux) ifdef CONFIG_DEBUG_INFO_BTF_MODULES +$(if $(newer-prereqs),$(call cmd,btf_ko)) endif
scripts/Makefile.modinst +1 -1
@@ -6,7 +6,7 @@ PHONY := __modinst __modinst: -include include/config/auto.conf +include $(objtree)/include/config/auto.conf include $(srctree)/scripts/Kbuild.include install-y :=
scripts/Makefile.modpost +6 -6
@@ -35,10 +35,10 @@ PHONY := __modpost __modpost: -include include/config/auto.conf +include $(objtree)/include/config/auto.conf include $(srctree)/scripts/Kbuild.include -MODPOST = scripts/mod/modpost +MODPOST = $(objtree)/scripts/mod/modpost modpost-args = \ $(if $(CONFIG_MODULES),-M) \
@@ -119,11 +119,11 @@ include $(kbuild-file) output-symdump := $(KBUILD_EXTMOD)/Module.symvers -ifeq ($(wildcard Module.symvers),) -missing-input := Module.symvers +ifeq ($(wildcard $(objtree)/Module.symvers),) +missing-input := $(objtree)/Module.symvers else -modpost-args += -i Module.symvers -modpost-deps += Module.symvers +modpost-args += -i $(objtree)/Module.symvers +modpost-deps += $(objtree)/Module.symvers endif modpost-args += -e $(addprefix -i , $(KBUILD_EXTRA_SYMBOLS))
scripts/depmod.sh +2 -2
@@ -12,7 +12,7 @@ KERNELRELEASE=$1 : ${DEPMOD:=depmod} -if ! test -r System.map ; then +if ! test -r "${objtree}/System.map" ; then echo "Warning: modules_install: missing 'System.map' file. Skipping depmod." >&2 exit 0 fi
@@ -25,7 +25,7 @@ if [ -z $(command -v $DEPMOD) ]; then exit 0 fi -set -- -ae -F System.map +set -- -ae -F "${objtree}/System.map" if test -n "$INSTALL_MOD_PATH"; then set -- "$@" -b "$INSTALL_MOD_PATH" fi
Signature · 1 line
2.43.0
PATCH 15/23 kbuild: rename abs_objtree to abs_output
In reply to Masahiro Yamada'objtree' refers to the top of the output directory of kernel builds.
Rename abs_objtree to a more generic name, to better reflect its use in the context of external module builds.
---
Makefile | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
Makefile +5 -5
@@ -40,7 +40,7 @@ __all: this-makefile := $(lastword $(MAKEFILE_LIST)) abs_srctree := $(realpath $(dir $(this-makefile))) -abs_objtree := $(CURDIR) +abs_output := $(CURDIR) ifneq ($(sub_make_done),1)
@@ -185,8 +185,8 @@ ifneq ($(KBUILD_OUTPUT),) # $(realpath ...) gets empty if the path does not exist. Run 'mkdir -p' first. $(shell mkdir -p "$(KBUILD_OUTPUT)") # $(realpath ...) resolves symlinks -abs_objtree := $(realpath $(KBUILD_OUTPUT)) -$(if $(abs_objtree),,$(error failed to create output directory "$(KBUILD_OUTPUT)")) +abs_output := $(realpath $(KBUILD_OUTPUT)) +$(if $(abs_output),,$(error failed to create output directory "$(KBUILD_OUTPUT)")) endif # ifneq ($(KBUILD_OUTPUT),) ifneq ($(words $(subst :, ,$(abs_srctree))), 1)
@@ -197,7 +197,7 @@ export sub_make_done := 1 endif # sub_make_done -ifeq ($(abs_objtree),$(CURDIR)) +ifeq ($(abs_output),$(CURDIR)) # Suppress "Entering directory ..." if we are at the final work directory. no-print-directory := --no-print-directory else
@@ -221,7 +221,7 @@ $(filter-out $(this-makefile), $(MAKECMDGOALS)) __all: __sub-make # Invoke a second make in the output directory, passing relevant variables __sub-make: - $(Q)$(MAKE) $(no-print-directory) -C $(abs_objtree) \ + $(Q)$(MAKE) $(no-print-directory) -C $(abs_output) \ -f $(abs_srctree)/Makefile $(MAKECMDGOALS) else # need-sub-make
Signature · 1 line
2.43.0
PATCH 16/23 kbuild: use 'output' variable to create the output directory
In reply to Masahiro Yamada$(KBUILD_OUTPUT) specifies the output directory of kernel builds.
Use a more generic name, 'output', to better reflect this code hunk in the context of external module builds.
---
Makefile | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-)
Makefile +8 -6
@@ -176,18 +176,20 @@ export KBUILD_EXTRA_WARN # The O= assignment takes precedence over the KBUILD_OUTPUT environment # variable. -# Do we want to change the working directory? ifeq ("$(origin O)", "command line") KBUILD_OUTPUT := $(O) endif -ifneq ($(KBUILD_OUTPUT),) +output := $(KBUILD_OUTPUT) + +# Do we want to change the working directory? +ifneq ($(output),) # $(realpath ...) gets empty if the path does not exist. Run 'mkdir -p' first. -$(shell mkdir -p "$(KBUILD_OUTPUT)") +$(shell mkdir -p "$(output)") # $(realpath ...) resolves symlinks -abs_output := $(realpath $(KBUILD_OUTPUT)) -$(if $(abs_output),,$(error failed to create output directory "$(KBUILD_OUTPUT)")) -endif # ifneq ($(KBUILD_OUTPUT),) +abs_output := $(realpath $(output)) +$(if $(abs_output),,$(error failed to create output directory "$(output)")) +endif ifneq ($(words $(subst :, ,$(abs_srctree))), 1) $(error source directory cannot contain spaces or colons)
Signature · 1 line
2.43.0
PATCH 17/23 kbuild: build external modules in their directory
In reply to Masahiro YamadaCurrently, 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.
---
Documentation/dev-tools/coccinelle.rst | 19 +++++------- Makefile | 40 +++++++++++++++++--------- rust/Makefile | 4 +-- scripts/Makefile.compiler | 2 +- scripts/Makefile.modpost | 6 ++-- scripts/coccicheck | 6 ++-- scripts/package/install-extmod-build | 7 +++++ 7 files changed, 49 insertions(+), 35 deletions(-)
Documentation/dev-tools/coccinelle.rst +7 -12
@@ -250,25 +250,20 @@ variables for .cocciconfig is as follows: - Your directory from which spatch is called is processed next - The directory provided with the ``--dir`` option is processed last, if used -Since coccicheck runs through make, it naturally runs from the kernel -proper dir; as such the second rule above would be implied for picking up a -.cocciconfig when using ``make coccicheck``. - ``make coccicheck`` also supports using M= targets. If you do not supply any M= target, it is assumed you want to target the entire kernel. The kernel coccicheck script has:: - if [ "$KBUILD_EXTMOD" = "" ] ; then - OPTIONS="--dir $srctree $COCCIINCLUDE" + if [ "$VPATH" ] ; then + OPTIONS="--dir $VPATH $COCCIINCLUDE" else - OPTIONS="--dir $KBUILD_EXTMOD $COCCIINCLUDE" + OPTIONS="--dir . $COCCIINCLUDE" fi -KBUILD_EXTMOD is set when an explicit target with M= is used. For both cases -the spatch ``--dir`` argument is used, as such third rule applies when whether -M= is used or not, and when M= is used the target directory can have its own -.cocciconfig file. When M= is not passed as an argument to coccicheck the -target directory is the same as the directory from where spatch was called. +When an explicit target is executed with a separate output directory, VPATH is +set to the target source directory. The third rule ensures the spatch reads the +.cocciconfig from the target directory. When M= is used, the external module +directory can have its own.cocciconfig file. If not using the kernel's coccicheck target, keep the above precedence order logic of .cocciconfig reading. If using the kernel's coccicheck target,
Makefile +26 -14
@@ -180,7 +180,20 @@ ifeq ("$(origin O)", "command line") KBUILD_OUTPUT := $(O) endif -output := $(KBUILD_OUTPUT) +ifdef KBUILD_EXTMOD + ifdef KBUILD_OUTPUT + objtree := $(realpath $(KBUILD_OUTPUT)) + $(if $(objtree),,$(error specified kernel directory "$(KBUILD_OUTPUT)" does not exist)) + else + objtree := $(CURDIR) + endif + output := $(KBUILD_EXTMOD) +else + objtree := . + output := $(KBUILD_OUTPUT) +endif + +export objtree # Do we want to change the working directory? ifneq ($(output),)
@@ -248,8 +261,6 @@ ifneq ($(KBUILD_ABS_SRCTREE),) srctree := $(abs_srctree) endif -objtree := . - VPATH := ifeq ($(KBUILD_EXTMOD),)
@@ -258,7 +269,7 @@ VPATH := $(srctree) endif endif -export building_out_of_srctree srctree objtree VPATH +export building_out_of_srctree srctree VPATH # To make sure we do not include .config for any of the *config targets # catch them early, and hand them over to scripts/kconfig/Makefile
@@ -708,7 +719,7 @@ endif # in addition to whatever we do anyway. # Just "make" or "make all" shall build modules as well -ifneq ($(filter all modules nsdeps %compile_commands.json clang-%,$(MAKECMDGOALS)),) +ifneq ($(filter all modules nsdeps compile_commands.json clang-%,$(MAKECMDGOALS)),) KBUILD_MODULES := 1 endif
@@ -1091,7 +1102,7 @@ export MODLIB PHONY += prepare0 -export extmod_prefix = $(if $(KBUILD_EXTMOD),$(KBUILD_EXTMOD)/) +export extmod_prefix = export MODORDER := $(extmod_prefix)modules.order export MODULES_NSDEPS := $(extmod_prefix)modules.nsdeps
@@ -1789,14 +1800,10 @@ filechk_kernel.release = echo $(KERNELRELEASE) KBUILD_BUILTIN := KBUILD_MODULES := 1 -build-dir := $(KBUILD_EXTMOD) +build-dir := . -compile_commands.json: $(extmod_prefix)compile_commands.json -PHONY += compile_commands.json - -clean-dirs := $(KBUILD_EXTMOD) -clean: private rm-files := $(KBUILD_EXTMOD)/Module.symvers $(KBUILD_EXTMOD)/modules.nsdeps \ - $(KBUILD_EXTMOD)/compile_commands.json +clean-dirs := . +clean: private rm-files := Module.symvers modules.nsdeps compile_commands.json PHONY += prepare # now expand this into a simple variable to reduce the cost of shell evaluations
@@ -1938,7 +1945,7 @@ $(clean-dirs): clean: $(clean-dirs) $(call cmd,rmfiles) - @find $(or $(KBUILD_EXTMOD), .) $(RCS_FIND_IGNORE) \ + @find . $(RCS_FIND_IGNORE) \ \( -name '*.[aios]' -o -name '*.rsi' -o -name '*.ko' -o -name '.*.cmd' \ -o -name '*.ko.*' \ -o -name '*.dtb' -o -name '*.dtbo' \
@@ -1971,7 +1978,12 @@ tags TAGS cscope gtags: FORCE PHONY += rust-analyzer rust-analyzer: +$(Q)$(CONFIG_SHELL) $(srctree)/scripts/rust_is_available.sh +ifdef KBUILD_EXTMOD +# FIXME: external modules must not descend into a sub-directory of the kernel + $(Q)$(MAKE) $(build)=$(objtree)/rust src=$(srctree)/rust $@ +else $(Q)$(MAKE) $(build)=rust $@ +endif # Script to generate missing namespace dependencies # ---------------------------------------------------------------------------
rust/Makefile +2 -2
@@ -350,8 +350,8 @@ rust-analyzer: $(Q)$(srctree)/scripts/generate_rust_analyzer.py \ --cfgs='core=$(core-cfgs)' --cfgs='alloc=$(alloc-cfgs)' \ $(realpath $(srctree)) $(realpath $(objtree)) \ - $(rustc_sysroot) $(RUST_LIB_SRC) $(KBUILD_EXTMOD) > \ - $(if $(KBUILD_EXTMOD),$(extmod_prefix),$(objtree))/rust-project.json + $(rustc_sysroot) $(RUST_LIB_SRC) $(if $(KBUILD_EXTMOD),$(realpath $(or $(VPATH),.))) \ + > rust-project.json redirect-intrinsics = \ __addsf3 __eqsf2 __extendsfdf2 __gesf2 __lesf2 __ltsf2 __mulsf3 __nesf2 __truncdfsf2 __unordsf2 \
scripts/Makefile.compiler +1 -1
@@ -13,7 +13,7 @@ cc-cross-prefix = $(firstword $(foreach c, $(1), \ $(if $(shell command -v -- $(c)gcc 2>/dev/null), $(c)))) # output directory for tests below -TMPOUT = $(if $(KBUILD_EXTMOD),$(firstword $(KBUILD_EXTMOD))/).tmp_$$$$ +TMPOUT = .tmp_$$$$ # try-run # Usage: option = $(call try-run, $(CC)...-o "$$TMP",option-ok,otherwise)
scripts/Makefile.modpost +3 -3
@@ -111,13 +111,13 @@ endif else # set src + obj - they may be used in the modules's Makefile -obj := $(KBUILD_EXTMOD) -src := $(if $(VPATH),$(VPATH)/)$(obj) +obj := . +src := $(or $(VPATH),.) # Include the module's Makefile to find KBUILD_EXTRA_SYMBOLS include $(kbuild-file) -output-symdump := $(KBUILD_EXTMOD)/Module.symvers +output-symdump := Module.symvers ifeq ($(wildcard $(objtree)/Module.symvers),) missing-input := $(objtree)/Module.symvers
scripts/coccicheck +3 -3
@@ -80,10 +80,10 @@ command results in a shift count error.' NPROC=1 else ONLINE=0 - if [ "$KBUILD_EXTMOD" = "" ] ; then - OPTIONS="--dir $srctree $COCCIINCLUDE" + if [ "${VPATH+set}" ] ; then + OPTIONS="--dir $VPATH $COCCIINCLUDE" else - OPTIONS="--dir $KBUILD_EXTMOD $COCCIINCLUDE" + OPTIONS="--dir . $COCCIINCLUDE" fi # Use only one thread per core by default if hyperthreading is enabled
scripts/package/install-extmod-build +7 -0
@@ -53,6 +53,13 @@ mkdir -p "${destdir}" if [ "${CC}" != "${HOSTCC}" ] && is_enabled CONFIG_CC_CAN_LINK; then echo "Rebuilding host programs with ${CC}..." + # This leverages external module building. + # - Clear sub_make_done to allow the top-level Makefile to redo sub-make. + # - Filter out --no-print-directory to print "Entering directory" logs + # when Make changes the working directory. + unset sub_make_done + MAKEFLAGS=$(echo "${MAKEFLAGS}" | sed s/--no-print-directory//) + cat <<-'EOF' > "${destdir}/Kbuild" subdir-y := scripts EOF
Signature · 1 line
2.43.0
PATCH 18/23 kbuild: remove extmod_prefix, MODORDER, MODULES_NSDEPS variables
In reply to Masahiro YamadaWith the previous changes, $(extmod_prefix), $(MODORDER), and $(MODULES_NSDEPS) are constant. (empty, modules.order, and modules.nsdeps, respectively).
Remove these variables and hard-code their values.
---
Makefile | 22 +++++++++------------- scripts/Makefile.modfinal | 2 +- scripts/Makefile.modinst | 2 +- scripts/Makefile.modpost | 6 +++--- scripts/nsdeps | 2 +- 5 files changed, 15 insertions(+), 19 deletions(-)
Makefile +9 -13
@@ -1102,10 +1102,6 @@ export MODLIB PHONY += prepare0 -export extmod_prefix = -export MODORDER := $(extmod_prefix)modules.order -export MODULES_NSDEPS := $(extmod_prefix)modules.nsdeps - ifeq ($(KBUILD_EXTMOD),) build-dir := .
@@ -1862,7 +1858,7 @@ endif ifdef CONFIG_MODULES -$(MODORDER): $(build-dir) +modules.order: $(build-dir) @: # KBUILD_MODPOST_NOFINAL can be set to skip the final link of modules.
@@ -1873,7 +1869,7 @@ ifneq ($(KBUILD_MODPOST_NOFINAL),1) endif PHONY += modules_check -modules_check: $(MODORDER) +modules_check: modules.order $(Q)$(CONFIG_SHELL) $(srctree)/scripts/modules-check.sh $< else # CONFIG_MODULES
@@ -1914,15 +1910,15 @@ $(single-ko): single_modules $(single-no-ko): $(build-dir) @: -# Remove MODORDER when done because it is not the real one. +# Remove modules.order when done because it is not the real one. PHONY += single_modules single_modules: $(single-no-ko) modules_prepare - $(Q){ $(foreach m, $(single-ko), echo $(extmod_prefix)$(m:%.ko=%.o);) } > $(MODORDER) + $(Q){ $(foreach m, $(single-ko), echo $(extmod_prefix)$(m:%.ko=%.o);) } > modules.order $(Q)$(MAKE) -f $(srctree)/scripts/Makefile.modpost ifneq ($(KBUILD_MODPOST_NOFINAL),1) $(Q)$(MAKE) -f $(srctree)/scripts/Makefile.modfinal endif - $(Q)rm -f $(MODORDER) + $(Q)rm -f modules.order single-goals := $(addprefix $(build-dir)/, $(single-no-ko))
@@ -1999,12 +1995,12 @@ nsdeps: modules quiet_cmd_gen_compile_commands = GEN $@ cmd_gen_compile_commands = $(PYTHON3) $< -a $(AR) -o $@ $(filter-out $<, $(real-prereqs)) -$(extmod_prefix)compile_commands.json: $(srctree)/scripts/clang-tools/gen_compile_commands.py \ +compile_commands.json: $(srctree)/scripts/clang-tools/gen_compile_commands.py \ $(if $(KBUILD_EXTMOD),, vmlinux.a $(KBUILD_VMLINUX_LIBS)) \ - $(if $(CONFIG_MODULES), $(MODORDER)) FORCE + $(if $(CONFIG_MODULES), modules.order) FORCE $(call if_changed,gen_compile_commands) -targets += $(extmod_prefix)compile_commands.json +targets += compile_commands.json PHONY += clang-tidy clang-analyzer
@@ -2012,7 +2008,7 @@ ifdef CONFIG_CC_IS_CLANG quiet_cmd_clang_tools = CHECK $< cmd_clang_tools = $(PYTHON3) $(srctree)/scripts/clang-tools/run-clang-tools.py $@ $< -clang-tidy clang-analyzer: $(extmod_prefix)compile_commands.json +clang-tidy clang-analyzer: compile_commands.json $(call cmd,clang_tools) else clang-tidy clang-analyzer:
scripts/Makefile.modfinal +1 -1
@@ -13,7 +13,7 @@ include $(srctree)/scripts/Kbuild.include include $(srctree)/scripts/Makefile.lib # find all modules listed in modules.order -modules := $(call read-file, $(MODORDER)) +modules := $(call read-file, modules.order) __modfinal: $(modules:%.o=%.ko) @:
scripts/Makefile.modinst +1 -1
@@ -40,7 +40,7 @@ $(addprefix $(MODLIB)/, modules.builtin modules.builtin.modinfo modules.builtin. endif -modules := $(call read-file, $(MODORDER)) +modules := $(call read-file, modules.order) ifeq ($(KBUILD_EXTMOD),) dst := $(MODLIB)/kernel
scripts/Makefile.modpost +3 -3
@@ -46,7 +46,7 @@ modpost-args = \ $(if $(CONFIG_MODULE_SRCVERSION_ALL),-a) \ $(if $(CONFIG_SECTION_MISMATCH_WARN_ONLY),,-E) \ $(if $(KBUILD_MODPOST_WARN),-w) \ - $(if $(KBUILD_NSDEPS),-d $(MODULES_NSDEPS)) \ + $(if $(KBUILD_NSDEPS),-d modules.nsdeps) \ $(if $(CONFIG_MODULE_ALLOW_MISSING_NAMESPACE_IMPORTS)$(KBUILD_NSDEPS),-N) \ $(if $(findstring 1, $(KBUILD_EXTRA_WARN)),-W) \ -o $@
@@ -61,8 +61,8 @@ endif # Read out modules.order to pass in modpost. # Otherwise, allmodconfig would fail with "Argument list too long". ifdef KBUILD_MODULES -modpost-args += -T $(MODORDER) -modpost-deps += $(MODORDER) +modpost-args += -T modules.order +modpost-deps += modules.order endif ifeq ($(KBUILD_EXTMOD),)
scripts/nsdeps +1 -1
@@ -57,4 +57,4 @@ generate_deps() { while read line do generate_deps $line -done < $MODULES_NSDEPS +done < modules.nsdeps
Signature · 1 line
2.43.0
PATCH 19/23 kbuild: support building external modules in a separate build directory
In reply to Masahiro YamadaThere has been a long-standing request to support building external modules in a separate build directory.
This commit introduces a new environment variable, KBUILD_EXTMOD_OUTPUT, and its shorthand Make variable, MO.
A simple usage:
$ make -C <kernel-dir> M=<module-src-dir> MO=<module-build-dir>
---
Documentation/kbuild/kbuild.rst | 8 +++++- Documentation/kbuild/modules.rst | 5 +++- Makefile | 44 +++++++++++++++++++++++--------- 3 files changed, 43 insertions(+), 14 deletions(-)
Documentation/kbuild/kbuild.rst +7 -1
@@ -132,12 +132,18 @@ Specify the output directory when building the kernel. This variable can also be used to point to the kernel output directory when building external modules using kernel build artifacts in a separate build directory. Please note that this does NOT specify the output directory for the -external modules themselves. +external modules themselves. (Use KBUILD_EXTMOD_OUTPUT for that purpose.) The output directory can also be specified using "O=...". Setting "O=..." takes precedence over KBUILD_OUTPUT. +KBUILD_EXTMOD_OUTPUT +-------------------- +Specify the output directory for external modules. + +Setting "MO=..." takes precedence over KBUILD_EXTMOD_OUTPUT. + KBUILD_EXTRA_WARN ----------------- Specify the extra build checks. The same value can be assigned by passing
Documentation/kbuild/modules.rst +4 -1
@@ -95,7 +95,7 @@ executed to make module versioning work. of the kernel output directory if the kernel was built in a separate build directory.) - make -C $KDIR M=$PWD + make -C $KDIR M=$PWD [MO=$BUILD_DIR] -C $KDIR The directory that contains the kernel and relevant build
@@ -109,6 +109,9 @@ executed to make module versioning work. directory where the external module (kbuild file) is located. + MO=$BUILD_DIR + Speficies a separate output directory for the external module. + 2.3 Targets ===========
Makefile +32 -12
100 lines not on this page · raw message
Signature · 1 line
2.43.0
PATCH 20/23 kbuild: support -fmacro-prefix-map for external modules
In reply to Masahiro YamadaThis commit makes -fmacro-prefix-map work for external modules built in a separate output directory. It improves the reproducibility of external modules and provides the benefits described in commit a73619a845d5 ("kbuild: use -fmacro-prefix-map to make __FILE__ a relative path").
When VPATH is not defined (e.g., when the kernel or external module is built in the source directory), this option is unnecessary.
---
Makefile | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
Makefile +4 -2
@@ -1027,8 +1027,10 @@ ifdef CONFIG_CC_IS_GCC KBUILD_CFLAGS += -fconserve-stack endif -# change __FILE__ to the relative path from the srctree -KBUILD_CPPFLAGS += $(call cc-option,-fmacro-prefix-map=$(srctree)/=) +# change __FILE__ to the relative path to the source directory +ifdef VPATH +KBUILD_CPPFLAGS += $(call cc-option,-fmacro-prefix-map=$(VPATH)/=) +endif # include additional Makefiles when needed include-y := scripts/Makefile.extrawarn
Signature · 1 line
2.43.0
PATCH 21/23 kbuild: use absolute path in the generated wrapper Makefile
In reply to Masahiro YamadaKeep the consistent behavior when this Makefile is invoked from another directory.
---
Makefile | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
Makefile +3 -2
@@ -643,8 +643,9 @@ ifdef building_out_of_srctree quiet_cmd_makefile = GEN Makefile cmd_makefile = { \ - echo "\# Automatically generated by $(srctree)/Makefile: don't edit"; \ - echo "include $(srctree)/Makefile"; \ + echo "\# Automatically generated by $(abs_srctree)/Makefile: don't edit"; \ + echo "export KBUILD_OUTPUT = $(CURDIR)"; \ + echo "include $(abs_srctree)/Makefile"; \ } > Makefile outputmakefile:
Signature · 1 line
2.43.0
PATCH 22/23 kbuild: make wrapper Makefile more convenient for external modules
In reply to Masahiro YamadaWhen Kbuild starts building in a separate output directory, it generates a wrapper Makefile, allowing you to invoke 'make' from the output directory.
This commit makes it more convenient, so you can invoke 'make' without M= or MO=.
First, you need to build external modules in a separate directory:
$ make M=/path/to/module/source/dir MO=/path/to/module/build/dir
Once the wrapper Makefile is generated in /path/to/module/build/dir, you can proceed as follows:
$ cd /path/to/module/build/dir $ make
---
Makefile | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-)
Makefile +11 -1
@@ -641,10 +641,20 @@ ifdef building_out_of_srctree # At the same time when output Makefile generated, generate .gitignore to # ignore whole output directory +ifdef KBUILD_EXTMOD +print_env_for_makefile = \ + echo "export KBUILD_OUTPUT = $(objtree)"; \ + echo "export KBUILD_EXTMOD = $(_vpath)" ; \ + echo "export KBUILD_EXTMOD_OUTPUT = $(CURDIR)" +else +print_env_for_makefile = \ + echo "export KBUILD_OUTPUT = $(CURDIR)" +endif + quiet_cmd_makefile = GEN Makefile cmd_makefile = { \ echo "\# Automatically generated by $(abs_srctree)/Makefile: don't edit"; \ - echo "export KBUILD_OUTPUT = $(CURDIR)"; \ + $(print_env_for_makefile); \ echo "include $(abs_srctree)/Makefile"; \ } > Makefile
Signature · 1 line
2.43.0
PATCH 23/23 kbuild: allow to start building external module in any directory
In reply to Masahiro YamadaUnless 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/exernal/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/exernal/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 = $(CURDIR)
include $(KDIR)/MakefileThis 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---
Documentation/kbuild/modules.rst | 21 +++++++++++++++++++++ Makefile | 8 ++++++-- 2 files changed, 27 insertions(+), 2 deletions(-)
Documentation/kbuild/modules.rst +21 -0
@@ -88,6 +88,12 @@ executed to make module versioning work. $ make -C /lib/modules/`uname -r`/build M=$PWD modules_install + Starting from Linux 6.13, you can use the -f option instead of -C. This + will avoid unnecessary change of the working directory. The external + module will be output to the directory where you invoke make. + + $ make -f /lib/modules/`uname -r`/build/Makefile M=$PWD + 2.2 Options ===========
@@ -246,6 +252,21 @@ module 8123.ko, which is built from the following files:: consisting of several hundred lines, and here it really pays off to separate the kbuild part from the rest. + Linux 6.13 and later support another way. The external module Makefile + can include the kernel Makefile directly, rather than invoking sub Make. + + Example 3:: + + --> filename: Kbuild + obj-m := 8123.o + 8123-y := 8123_if.o 8123_pci.o + + --> filename: Makefile + KDIR ?= /lib/modules/`uname -r`/build + export KBUILD_EXTMOD = $(CURDIR) + include $(KDIR)/Makefile + + 3.4 Building Multiple Modules =============================
Makefile +6 -2
@@ -189,9 +189,13 @@ ifdef KBUILD_EXTMOD objtree := $(realpath $(KBUILD_OUTPUT)) $(if $(objtree),,$(error specified kernel directory "$(KBUILD_OUTPUT)" does not exist)) else - objtree := $(CURDIR) + objtree := $(abs_srctree) endif - output := $(or $(KBUILD_EXTMOD_OUTPUT),$(KBUILD_EXTMOD)) + # If Make is invoked from the kernel directory (either kernel + # source directory or kernel build directory), external modules + # are built in $(KBUILD_EXTMOD) for backward compatibility, + # otherwise, built in the current directory. + output := $(or $(KBUILD_EXTMOD_OUTPUT),$(if $(filter $(CURDIR),$(objtree) $(abs_srctree)),$(KBUILD_EXTMOD))) # KBUILD_EXTMOD might be a relative path. Remember its absolute path before # Make changes the working directory. export abs_extmodtree := $(realpath $(KBUILD_EXTMOD))
Signature · 1 line
2.43.0
Re: [PATCH 02/23] kbuild: doc: remove description about grepping CONFIG options
In reply to Masahiro YamadaQuoted 7 lines · Masahiro Yamada
This description was added 20 years ago [1]. It does not convey any useful information except for a feeling of nostalgia.
Signed-off-by Masahiro Yamada <[email protected]>---
Re: [PATCH 03/23] kbuild: doc: remove outdated description of the limitation on -I usage
In reply to Masahiro YamadaQuoted 10 lines · Masahiro Yamada
Kbuildused to manipulate header search paths, enforcing the odd limitation of "no space after -I".Commit
cdd750bfb1f7("kbuild: remove 'addtree' and 'flags' magic for header search paths") stopped doing that. This limitation no longer exists. Instead, you need to accurately specify the header search path. (In this case, $(src)/include)Signed-off-by Masahiro Yamada <[email protected]>---
Re: [PATCH 01/23] kbuild: doc: update the description about Kbuild/Makefile split
In reply to Masahiro YamadaQuoted 4 lines · Masahiro Yamada
The phrase "In newer versions of the kernel" was added by commit
efdf02cf0651("Documentation/kbuild: major edit of modules.txt sections 1-4"). This feature is no longer new, so remove it and update the paragraph.
Quoted 8 lines
Example 3 was written 20 years ago. [1] There is no need to note about backward compatibility with such an old build system. Remove Example 3 entirely.
Signed-off-by Masahiro Yamada <[email protected]>---
Re: [PATCH 04/23] kbuild: doc: remove the description about shipped files
In reply to Masahiro YamadaQuoted 7 lines · Masahiro Yamada
The use of shipped files is discouraged in the upstream kernel these days. [1]
Downstream Makefiles have the freedom to use shipped files or other options to handle binaries, but this is not what should be advertised in the upstream document.
Quoted 7 lines
[1]: https://lore.kernel.org/all/CAHk-=wgSEi_ZrHdqr=20xv+d6dr5G895CbOAi8ok+7-CQUN=fQ@mail.gmail.com/
Signed-off-by Masahiro Yamada <[email protected]>---
Documentation/kbuild/modules.rst | 35 +++----------------------------- 1 file changed, 3 insertions(+), 32 deletions(-)
Do you want to remove the TODO point refering to *_shipped files from Documentation/kbuild/makefiles.rst as well?
$ git grep -Hrne _shipped Documentation/ Documentation/kbuild/makefiles.rst:1668:- Describe how kbuild supports shipped files with _shipped.
Quoted 5 lines
Documentation/kbuild/modules.rst +0
-0
[...]
Quoted 23 lines
Documentation/kbuild/modules.rst +0
-14@@ -240,35 +235,11 @@ module 8123.ko, which is built from the following files:: default: $(MAKE) -C $(KDIR) M=$$PWD - # Module specific targets - genbin: - echo "X" > 8123_bin.o_shipped - The split in example 2 is questionable due to the simplicity of each file; however, some external modules use makefiles consisting of several hundred lines, and here it really pays off to separate the kbuild part from the rest. -3.3 Binary Blobs ----------------- - - Some external modules need to include an object file as a blob. - kbuild has support for this, but requires the blob file to be - named <filename>_shipped. When the kbuild rules kick in, a copy - of <filename>_shipped is created with _shipped stripped off, - giving us <filename>. This shortened filename can be used in - the assignment to the module. -
Documentation/kbuild/modules.rst module 8123.ko, which is built from the following files:: · line 271
Quoted 10 lines
Documentation/kbuild/modules.rst +0
-10- Throughout this section, 8123_bin.o_shipped has been used to - build the kernel module 8123.ko; it has been included as - 8123_bin.o:: - - 8123-y := 8123_if.o 8123_pci.o 8123_bin.o - - Although there is no distinction between the ordinary source - files and the binary file, kbuild will pick up different rules - when creating the object file for the module. -
I think renumbering the following sections would provide a more consistent document. If you think that keeping the numbering constant: would you mind adding a note about section 3.3 being removed? (And possibly as well in the table of contents?)
Either way,
Re: [PATCH 05/23] kbuild: doc: describe the -C option precisely for external module builds
In reply to Masahiro YamadaQuoted 26 lines · Masahiro Yamada
Building external modules is typically done using this command:
$ make -C <KERNEL_DIR> M=<EXTMOD_DIR>Here, <
KERNEL_DIR> refers to the output directory where the kernel was built, not the kernel source directory.When the kernel is built in-tree, there is no ambiguity, as the output directory and the source directory are the same.
If the kernel was built in a separate build directory, <
KERNEL_DIR> should be the kernel output directory. Otherwise,Kbuildcannot locate necessary build artifacts such as the .config file, etc. This has been the method for building external modules based on the kernel compiled in a separate directory for over 20 years. [1]If you pass the kernel source directory to the -C option, you must also specify the kernel build directory using the O= option. This approach works as well, though it results in a slightly longer command:
$ make -C <KERNEL_SOURCE_DIR> O=<KERNEL_BUILD_DIR> M=<EXTMOD_DIR>Some people mistakenly believe that O= should point to a separate output directory for external modules when used together with M=. This commit adds more clarification to
Documentation/kbuild/kbuild.rst.
Quoted 8 lines
Signed-off-by Masahiro Yamada <[email protected]>---
Documentation/kbuild/kbuild.rst | 5 +++++ Documentation/kbuild/modules.rst | 9 ++++++--- 2 files changed, 11 insertions(+), 3 deletions(-)
Re: [PATCH 05/23] kbuild: doc: describe the -C option precisely for external module builds
In reply to Masahiro YamadaQuoted 26 lines · Masahiro Yamada
Building external modules is typically done using this command:
$ make -C <KERNEL_DIR> M=<EXTMOD_DIR>Here, <
KERNEL_DIR> refers to the output directory where the kernel was built, not the kernel source directory.When the kernel is built in-tree, there is no ambiguity, as the output directory and the source directory are the same.
If the kernel was built in a separate build directory, <
KERNEL_DIR> should be the kernel output directory. Otherwise,Kbuildcannot locate necessary build artifacts such as the .config file, etc. This has been the method for building external modules based on the kernel compiled in a separate directory for over 20 years. [1]If you pass the kernel source directory to the -C option, you must also specify the kernel build directory using the O= option. This approach works as well, though it results in a slightly longer command:
$ make -C <KERNEL_SOURCE_DIR> O=<KERNEL_BUILD_DIR> M=<EXTMOD_DIR>Some people mistakenly believe that O= should point to a separate output directory for external modules when used together with M=. This commit adds more clarification to
Documentation/kbuild/kbuild.rst.
Quoted 8 lines
Signed-off-by Masahiro Yamada <[email protected]>---
Documentation/kbuild/kbuild.rst | 5 +++++ Documentation/kbuild/modules.rst | 9 ++++++--- 2 files changed, 11 insertions(+), 3 deletions(-)
Re: [PATCH 06/23] kbuild: doc: replace "gcc" in external module description
In reply to Masahiro YamadaQuoted 7 lines · Masahiro Yamada
Avoid "gcc" since it is not the only compiler supported by
Kbuild.Signed-off-by Masahiro Yamada <[email protected]>---
Documentation/kbuild/modules.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
Re: [PATCH 07/23] kbuild: remove unnecessary prune of rust/alloc for rustfmt
In reply to Masahiro YamadaQuoted 8 lines · Masahiro Yamada
Commit
9d0441bab775("rust: alloc: remove our fork of thealloccrate") removed therust/alloc/directory.Signed-off-by Masahiro Yamada <[email protected]>---
Makefile | 2 -- 1 file changed, 2 deletions(-)
Re: [PATCH 07/23] kbuild: remove unnecessary prune of rust/alloc for rustfmt
Quoted 11 lines · Nicolas Schier
Quoted 8 lines · Masahiro Yamada
Commit
9d0441bab775("rust: alloc: remove our fork of thealloccrate") removed therust/alloc/directory.Signed-off-by Masahiro Yamada <[email protected]>---
Makefile | 2 -- 1 file changed, 2 deletions(-)Reviewed-by Nicolas Schier <[email protected]>
oh, there is still a comment about 'rust/alloc' in Makefile that probably wants to be removed, too.
Re: [PATCH 08/23] kbuild: simplify find command for rustfmt
In reply to Masahiro YamadaQuoted 40 lines · Masahiro Yamada
The current 'find' command does not prune the
rust/testdirectory itself, requiring an additional 'grep -Fv' command to exclude it. This is cumbersome.The correct use of the -prune option can be seen in the 'make clean' rule.
[Current command]
$ find . -type f -name '*.rs' -o -path ./rust/test -prune | wc 70 70 1939 $ find . -type f -name '*.rs' -o -path ./rust/test -prune | grep rust/test ./rust/test[Improved command]
$ find . -path ./rust/test -prune -o -type f -name '*.rs' -print | wc 69 69 1927 $ find . -path ./rust/test -prune -o -type f -name '*.rs' -print | grep rust/testWith the improved 'find' command, the grep command is no longer needed.
There is also no need to use the absolute path, so $(abs_srctree) can be replaced with $(srctree).
The pruned directory
rust/testmust be prefixed with $(srctree) instead of $(objtree). Otherwise, 'make O=... rustfmt' would visit the stalerust/testdirectory remaining in the source tree.Signed-off-by Masahiro Yamada <[email protected]>---
Makefile | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)Makefile +0
-0
Makefile PHONY += rustfmt rustfmtcheck · line 1745
Quoted 9 lines
Makefile +2
-3@@ -1740,9 +1740,8 @@ PHONY += rustfmt rustfmtcheck # when matching, which is a problem when e.g. `srctree` is `..`. # We `grep` afterwards in order to remove the directory entry itself. rustfmt: - $(Q)find $(abs_srctree) -type f -name '*.rs' \ - -o -path $(abs_objtree)/rust/test -prune \ - | grep -Fv $(abs_objtree)/rust/test \ + $(Q)find $(srctree) -path $(srctree)/rust/test -prune \ + -o -type f -name '*.rs' -print \ | grep -Fv generated \
Is there a reason for keeping the grep for generated instead of turning it also into a find prune argument?
Re: [PATCH 08/23] kbuild: simplify find command for rustfmt
Quoted 46 lines · Nicolas Schier
Quoted 44 lines · Masahiro Yamada
The current 'find' command does not prune the
rust/testdirectory itself, requiring an additional 'grep -Fv' command to exclude it. This is cumbersome.The correct use of the -prune option can be seen in the 'make clean' rule.
[Current command]
$ find . -type f -name '*.rs' -o -path ./rust/test -prune | wc 70 70 1939 $ find . -type f -name '*.rs' -o -path ./rust/test -prune | grep rust/test ./rust/test[Improved command]
$ find . -path ./rust/test -prune -o -type f -name '*.rs' -print | wc 69 69 1927 $ find . -path ./rust/test -prune -o -type f -name '*.rs' -print | grep rust/testWith the improved 'find' command, the grep command is no longer needed.
There is also no need to use the absolute path, so $(abs_srctree) can be replaced with $(srctree).
The pruned directory
rust/testmust be prefixed with $(srctree) instead of $(objtree). Otherwise, 'make O=... rustfmt' would visit the stalerust/testdirectory remaining in the source tree.Signed-off-by Masahiro Yamada <[email protected]>---
Makefile | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)Makefile +0
-1@@ -1740,9 +1740,8 @@ PHONY += rustfmt rustfmtcheck # when matching, which is a problem when e.g. `srctree` is `..`. # We `grep` afterwards in order to remove the directory entry itself. rustfmt: - $(Q)find $(abs_srctree) -type f -name '*.rs' \
Quoted 8 lines
Makefile PHONY += rustfmt rustfmtcheck · line 1745
Quoted 5 lines
Makefile +2
-2- -o -path $(abs_objtree)/rust/test -prune \ - | grep -Fv $(abs_objtree)/rust/test \ + $(Q)find $(srctree) -path $(srctree)/rust/test -prune \ + -o -type f -name '*.rs' -print \ | grep -Fv generated \Is there a reason for keeping the grep for generated instead of turning it also into a find prune argument?
This commit answers your question:
https://github.com/Rust-for-Linux/linux/commit/73243a8a27a67
Signature · 2 lines
Best Regards Masahiro Yamada
Re: [PATCH 04/23] kbuild: doc: remove the description about shipped files
In reply to Nicolas SchierQuoted 11 lines · Nicolas Schier
Quoted 9 lines · Masahiro Yamada
The use of shipped files is discouraged in the upstream kernel these days. [1]
Downstream Makefiles have the freedom to use shipped files or other options to handle binaries, but this is not what should be advertised in the upstream document.
[1]: https://lore.kernel.org/all/CAHk-=wgSEi_ZrHdqr=20xv+d6dr5G895CbOAi8ok+7-CQUN=fQ@mail.gmail.com/
Quoted 8 lines
Quoted 5 lines
Signed-off-by Masahiro Yamada <[email protected]>---
Documentation/kbuild/modules.rst | 35 +++----------------------------- 1 file changed, 3 insertions(+), 32 deletions(-)Do you want to remove the TODO point refering to
*_shippedfiles fromDocumentation/kbuild/makefiles.rstas well?
Yes.
Good catch.
Quoted 37 lines
$ git grep -Hrne _shipped Documentation/ Documentation/kbuild/makefiles.rst:1668:- Describe how kbuild supports shipped files with _shipped.Quoted 5 lines
Documentation/kbuild/modules.rst +0
-0[...]
Quoted 28 lines
Documentation/kbuild/modules.rst +0
-19@@ -240,35 +235,11 @@ module 8123.ko, which is built from the following files:: default: $(MAKE) -C $(KDIR) M=$$PWD - # Module specific targets - genbin: - echo "X" > 8123_bin.o_shipped - The split in example 2 is questionable due to the simplicity of each file; however, some external modules use makefiles consisting of several hundred lines, and here it really pays off to separate the kbuild part from the rest. -3.3 Binary Blobs ----------------- - - Some external modules need to include an object file as a blob. - kbuild has support for this, but requires the blob file to be - named <filename>_shipped. When the kbuild rules kick in, a copy - of <filename>_shipped is created with _shipped stripped off, - giving us <filename>. This shortened filename can be used in - the assignment to the module. - - Throughout this section, 8123_bin.o_shipped has been used to - build the kernel module 8123.ko; it has been included as - 8123_bin.o:: - - 8123-y := 8123_if.o 8123_pci.o 8123_bin.o
Quoted 8 lines
Documentation/kbuild/modules.rst module 8123.ko, which is built from the following files:: · line 271
Quoted 5 lines
Documentation/kbuild/modules.rst +0
-5- - Although there is no distinction between the ordinary source - files and the binary file, kbuild will pick up different rules - when creating the object file for the module. -I think renumbering the following sections would provide a more consistent document. If you think that keeping the numbering constant: would you mind adding a note about section 3.3 being removed? (And possibly as well in the table of contents?)
Rather, I'd like to remove section numbers.
Let me do that way.
Quoted 3 lines
Either way,
Reviewed-by Nicolas Schier <[email protected]>
Signature · 2 lines
Best Regards Masahiro Yamada
Quoted 3 lines · Masahiro Yamada
There has been a long-standing request to support building external modules in a separate build directory.
Thanks a lot, you are making several of my colleages very happy with your patch set!
Quoted 6 lines
The first half is cleanups of documents and Makefiles.
The last part adds
KBUILD_EXTMOD_OUTPUT(MO=). This is too big changes, and too late for the current MW. (I did not test kselftest at all.)
I hope people test this and may uncover some issues.
I'm not through all the patches in detail yet, just one observation beforehand:
$ make KBUILD_OUTPUT=build allnoconfig
$ ./scripts/config --file build/.config --enable modules --enable accessibility
$ make KBUILD_OUTPUT=build olddefconfig
$ make KBUILD_OUTPUT=build
$ make KBUILD_OUTPUT=build CONFIG_SPEAKUP=m MO=/tmp/build M=~+/drivers/accessibility/speakup modules
/home/nschier/src/kbuild-review/drivers/accessibility/speakup/genmap.c:23:10: fatal error: mapdata.h: No such file or directory
23 | #include "mapdata.h"
| ^~~~~~~~~~~
compilation terminated.
make[3]: *** [/home/nschier/src/kbuild-review/scripts/Makefile.host:133: genmap.o] Error 1
make[3]: *** Waiting for unfinished jobs....
make[2]: *** [/home/nschier/src/kbuild-review/Makefile:1971: .] Error 2
make[1]: *** [/home/nschier/src/kbuild-review/Makefile:251: __sub-make] Error 2
make: *** [Makefile:251: __sub-make] Error 2
[exit code 2]If I add "EXTRA_CFLAGS=-I${MO} and EXTRA_HOSTCFLAGS=-I${MO}" to the module build command, it works as expected.
Patching this into kbuild works for me, too, but I haven't checked whether it breaks some other scenarios:
scripts/Makefile.host +4 -1
@@ -97,10 +97,13 @@ hostrust_flags = --out-dir $(dir $@) --emit=dep-info=$(depfile) \ $(HOSTRUSTFLAGS_$(target-stem)) # $(objtree)/$(obj) for including generated headers from checkin source files -ifeq ($(KBUILD_EXTMOD),) ifdef building_out_of_srctree +ifeq ($(KBUILD_EXTMOD),) hostc_flags += -I $(objtree)/$(obj) hostcxx_flags += -I $(objtree)/$(obj) +else +hostc_flags += -I $(CURDIR) +hostcxx_flags += -I $(CURDIR) endif endif
scripts/Makefile.lib +5 -1
@@ -190,11 +190,15 @@ endif # $(src) for including checkin headers from generated source files # $(obj) for including generated headers from checkin source files -ifeq ($(KBUILD_EXTMOD),) ifdef building_out_of_srctree +ifeq ($(KBUILD_EXTMOD),) _c_flags += $(addprefix -I, $(src) $(obj)) _a_flags += $(addprefix -I, $(src) $(obj)) _cpp_flags += $(addprefix -I, $(src) $(obj)) +else +_c_flags += $(addprefix -I, $(src) $(obj) $(CURDIR)) +_a_flags += $(addprefix -I, $(src) $(obj) $(CURDIR)) +_cpp_flags += $(addprefix -I, $(src) $(obj) $(CURDIR)) endif endif
Is '-I$(MO)' in CFLAGS/HOSTCFLAGS is something we should support by default, or should this be added to the external module's Makefile by the respective developers themselves?
Kind regards,
Nicolas
Quoted 84 lines · Nicolas Schier
Quoted 3 lines · Masahiro Yamada
There has been a long-standing request to support building external modules in a separate build directory.
Thanks a lot, you are making several of my colleages very happy with your patch set!
Quoted 6 lines
The first half is cleanups of documents and Makefiles.
The last part adds
KBUILD_EXTMOD_OUTPUT(MO=). This is too big changes, and too late for the current MW. (I did not test kselftest at all.)
I hope people test this and may uncover some issues.I'm not through all the patches in detail yet, just one observation beforehand:
$ make KBUILD_OUTPUT=build allnoconfig $ ./scripts/config --file build/.config --enable modules --enable accessibility $ make KBUILD_OUTPUT=build olddefconfig $ make KBUILD_OUTPUT=build $ make KBUILD_OUTPUT=build CONFIG_SPEAKUP=m MO=/tmp/build M=~+/drivers/accessibility/speakup modules /home/nschier/src/kbuild-review/drivers/accessibility/speakup/genmap.c:23:10: fatal error: mapdata.h: No such file or directory 23 | #include "mapdata.h" | ^~~~~~~~~~~ compilation terminated. make[3]: *** [/home/nschier/src/kbuild-review/scripts/Makefile.host:133: genmap.o] Error 1 make[3]: *** Waiting for unfinished jobs.... make[2]: *** [/home/nschier/src/kbuild-review/Makefile:1971: .] Error 2 make[1]: *** [/home/nschier/src/kbuild-review/Makefile:251: __sub-make] Error 2 make: *** [Makefile:251: __sub-make] Error 2 [exit code 2]If I add "EXTRA_CFLAGS=-I${MO} and EXTRA_HOSTCFLAGS=-I${MO}" to the module build command, it works as expected.
Patching this into kbuild works for me, too, but I haven't checked whether it breaks some other scenarios:
scripts/Makefile.host +4
-1@@ -97,10 +97,13 @@ hostrust_flags = --out-dir $(dir $@) --emit=dep-info=$(depfile) \ $(HOSTRUSTFLAGS_$(target-stem)) # $(objtree)/$(obj) for including generated headers from checkin source files -ifeq ($(KBUILD_EXTMOD),) ifdef building_out_of_srctree +ifeq ($(KBUILD_EXTMOD),) hostc_flags += -I $(objtree)/$(obj) hostcxx_flags += -I $(objtree)/$(obj) +else +hostc_flags += -I $(CURDIR) +hostcxx_flags += -I $(CURDIR) endif endifscripts/Makefile.lib +5
-1@@ -190,11 +190,15 @@ endif # $(src) for including checkin headers from generated source files # $(obj) for including generated headers from checkin source files -ifeq ($(KBUILD_EXTMOD),) ifdef building_out_of_srctree +ifeq ($(KBUILD_EXTMOD),) _c_flags += $(addprefix -I, $(src) $(obj)) _a_flags += $(addprefix -I, $(src) $(obj)) _cpp_flags += $(addprefix -I, $(src) $(obj)) +else +_c_flags += $(addprefix -I, $(src) $(obj) $(CURDIR)) +_a_flags += $(addprefix -I, $(src) $(obj) $(CURDIR)) +_cpp_flags += $(addprefix -I, $(src) $(obj) $(CURDIR)) endif endifIs '-I$(MO)' in CFLAGS/HOSTCFLAGS is something we should support by default, or should this be added to the external module's
Makefileby the respective developers themselves?
Quoted 2 lines
Kind regards,
Nicolas
We can fix it more simply.
scripts/Makefile.host +3 -6
@@ -96,12 +96,10 @@ hostrust_flags = --out-dir $(dir $@) --emit=dep-info=$(depfile) \ $(KBUILD_HOSTRUSTFLAGS) $(HOST_EXTRARUSTFLAGS) \ $(HOSTRUSTFLAGS_$(target-stem)) -# $(objtree)/$(obj) for including generated headers from checkin source files -ifeq ($(KBUILD_EXTMOD),) +# $(obj) for including generated headers from checkin source files ifdef building_out_of_srctree -hostc_flags += -I $(objtree)/$(obj) -hostcxx_flags += -I $(objtree)/$(obj) -endif +hostc_flags += -I $(obj) +hostcxx_flags += -I $(obj) endif
#####
scripts/Makefile.lib +0 -2
@@ -190,13 +190,11 @@ endif # $(src) for including checkin headers from generated source files # $(obj) for including generated headers from checkin source files -ifeq ($(KBUILD_EXTMOD),) ifdef building_out_of_srctree _c_flags += $(addprefix -I, $(src) $(obj)) _a_flags += $(addprefix -I, $(src) $(obj)) _cpp_flags += $(addprefix -I, $(src) $(obj)) endif -endif # If $(is-kernel-object) is 'y', this object will be linked to
vmlinux or modules
is-kernel-object = $(or $(part-of-builtin),$(part-of-module))
However, I'd rather fix each Makefile
to add necessary include paths explicitly.
Signature · 2 lines
Best Regards Masahiro Yamada
Quoted 85 lines · Masahiro Yamada
Quoted 84 lines · Nicolas Schier
Quoted 3 lines · Masahiro Yamada
There has been a long-standing request to support building external modules in a separate build directory.
Thanks a lot, you are making several of my colleages very happy with your patch set!
Quoted 6 lines
The first half is cleanups of documents and Makefiles.
The last part adds
KBUILD_EXTMOD_OUTPUT(MO=). This is too big changes, and too late for the current MW. (I did not test kselftest at all.)
I hope people test this and may uncover some issues.I'm not through all the patches in detail yet, just one observation beforehand:
$ make KBUILD_OUTPUT=build allnoconfig $ ./scripts/config --file build/.config --enable modules --enable accessibility $ make KBUILD_OUTPUT=build olddefconfig $ make KBUILD_OUTPUT=build $ make KBUILD_OUTPUT=build CONFIG_SPEAKUP=m MO=/tmp/build M=~+/drivers/accessibility/speakup modules /home/nschier/src/kbuild-review/drivers/accessibility/speakup/genmap.c:23:10: fatal error: mapdata.h: No such file or directory 23 | #include "mapdata.h" | ^~~~~~~~~~~ compilation terminated. make[3]: *** [/home/nschier/src/kbuild-review/scripts/Makefile.host:133: genmap.o] Error 1 make[3]: *** Waiting for unfinished jobs.... make[2]: *** [/home/nschier/src/kbuild-review/Makefile:1971: .] Error 2 make[1]: *** [/home/nschier/src/kbuild-review/Makefile:251: __sub-make] Error 2 make: *** [Makefile:251: __sub-make] Error 2 [exit code 2]If I add "EXTRA_CFLAGS=-I${MO} and EXTRA_HOSTCFLAGS=-I${MO}" to the module build command, it works as expected.
Patching this into kbuild works for me, too, but I haven't checked whether it breaks some other scenarios:
scripts/Makefile.host +4
-1@@ -97,10 +97,13 @@ hostrust_flags = --out-dir $(dir $@) --emit=dep-info=$(depfile) \ $(HOSTRUSTFLAGS_$(target-stem)) # $(objtree)/$(obj) for including generated headers from checkin source files -ifeq ($(KBUILD_EXTMOD),) ifdef building_out_of_srctree +ifeq ($(KBUILD_EXTMOD),) hostc_flags += -I $(objtree)/$(obj) hostcxx_flags += -I $(objtree)/$(obj) +else +hostc_flags += -I $(CURDIR) +hostcxx_flags += -I $(CURDIR) endif endifscripts/Makefile.lib +5
-1@@ -190,11 +190,15 @@ endif # $(src) for including checkin headers from generated source files # $(obj) for including generated headers from checkin source files -ifeq ($(KBUILD_EXTMOD),) ifdef building_out_of_srctree +ifeq ($(KBUILD_EXTMOD),) _c_flags += $(addprefix -I, $(src) $(obj)) _a_flags += $(addprefix -I, $(src) $(obj)) _cpp_flags += $(addprefix -I, $(src) $(obj)) +else +_c_flags += $(addprefix -I, $(src) $(obj) $(CURDIR)) +_a_flags += $(addprefix -I, $(src) $(obj) $(CURDIR)) +_cpp_flags += $(addprefix -I, $(src) $(obj) $(CURDIR)) endif endifIs '-I$(MO)' in CFLAGS/HOSTCFLAGS is something we should support by default, or should this be added to the external module's
Makefileby the respective developers themselves?
Quoted 6 lines
Quoted 2 lines
Kind regards,
NicolasWe can fix it more simply.
Ah, yes.
Quoted 45 lines
scripts/Makefile.host +3
-6@@ -96,12 +96,10 @@ hostrust_flags = --out-dir $(dir $@) --emit=dep-info=$(depfile) \ $(KBUILD_HOSTRUSTFLAGS) $(HOST_EXTRARUSTFLAGS) \ $(HOSTRUSTFLAGS_$(target-stem)) -# $(objtree)/$(obj) for including generated headers from checkin source files -ifeq ($(KBUILD_EXTMOD),) +# $(obj) for including generated headers from checkin source files ifdef building_out_of_srctree -hostc_flags += -I $(objtree)/$(obj) -hostcxx_flags += -I $(objtree)/$(obj) -endif +hostc_flags += -I $(obj) +hostcxx_flags += -I $(obj) endif #####scripts/Makefile.lib +0
-2@@ -190,13 +190,11 @@ endif # $(src) for including checkin headers from generated source files # $(obj) for including generated headers from checkin source files -ifeq ($(KBUILD_EXTMOD),) ifdef building_out_of_srctree _c_flags += $(addprefix -I, $(src) $(obj)) _a_flags += $(addprefix -I, $(src) $(obj)) _cpp_flags += $(addprefix -I, $(src) $(obj)) endif -endif # If $(is-kernel-object) is 'y', this object will be linked tovmlinux or modules
is-kernel-object = $(or $(part-of-builtin),$(part-of-module))
Quoted 2 lines
However, I'd rather fix each
Makefile
to add necessary include paths explicitly.
Sure, clearly makes sense. Especially as the explicit include path addition is also as simple as:
HOSTCFLAGS_genmap.o += -I $(obj)
CFLAGS_main.o += -I $(obj)Kind regards,
Nicolas
Re: [PATCH 10/23] kbuild: refactor the check for missing config files
In reply to Masahiro YamadaQuoted 20 lines · Masahiro Yamada
This commit refactors the check for missing configuration files, making it easier to add more files to the list.
The format of the error message has been slightly changed, as follows:
[Before]
ERROR: Kernel configuration is invalid.
include/generated/autoconf.horinclude/config/auto.confare missing. Run 'make oldconfig && make prepare' on kernel src to fix it.[After]
***
*** ERROR: Kernel configuration is invalid. The following files are missing:
*** -include/generated/autoconf.h
*** -include/config/auto.conf
*** Run "make oldconfig && make prepare" on kernel source to fix it.
***
Quoted 5 lines
Signed-off-by Masahiro Yamada <[email protected]>---
Makefile | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-)
Re: [PATCH 11/23] kbuild: check the presence of include/generated/rustc_cfg
In reply to Masahiro YamadaQuoted 37 lines · Masahiro Yamada
Since commit
2f7ab1267dc9("Kbuild: add Rust support"),Kconfiggeneratesinclude/generated/rustc_cfg, but its presence is not checked in the top-levelMakefile. It should be checked similarly to the C header counterpart,include/generated/autoconf.h.Signed-off-by Masahiro Yamada <[email protected]>---
Makefile | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)Makefile +3
-2@@ -780,7 +780,7 @@ else # !may-sync-config # and include/config/auto.conf but do not care if they are up-to-date. # Use auto.conf to show the error message -checked-configs := include/generated/autoconf.h include/config/auto.conf +checked-configs := include/generated/autoconf.h include/generated/rustc_cfg include/config/auto.conf missing-configs := $(filter-out $(wildcard $(checked-configs)), $(checked-configs)) ifdef missing-configs@@ -1187,7 +1187,8 @@ PHONY += prepare archprepare archprepare: outputmakefile archheaders archscripts scripts include/config/kernel.release \ asm-generic $(version_h) include/generated/utsrelease.h \ - include/generated/compile.h include/generated/autoconf.h remove-stale-files + include/generated/compile.h include/generated/autoconf.h \ + include/generated/rustc_cfg remove-stale-files prepare0: archprepare $(Q)$(MAKE) $(build)=scripts/modSignature · 1 line
2.43.0
Re: [PATCH 12/23] scripts/nsdeps: use VPATH as src_prefix
In reply to Masahiro YamadaQuoted 14 lines · Masahiro Yamada
This change allows it to work not only for in-tree modules but also for external modules, even if they are built in a separate build directory.
Signed-off-by Masahiro Yamada <[email protected]>---
scripts/nsdeps | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)scripts/nsdeps +0
-0
scripts/nsdeps if ! { echo "$SPATCH_REQ_VERSION"; echo "$SPATCH_VERSION"; } | sort -CV ; then · line 25
Quoted 9 lines
scripts/nsdeps +2
-2@@ -19,10 +19,10 @@ if ! { echo "$SPATCH_REQ_VERSION"; echo "$SPATCH_VERSION"; } | sort -CV ; then exit 1 fi -if [ "$KBUILD_EXTMOD" ]; then +if [ "${VPATH+set}" ]; then src_prefix= else - src_prefix=$srctree/ + src_prefix=$VPATH/
In kbuild, we expect VPATH to be empty or hold one single directory, while make itself allows VPATH to be a list of directories. Might it make sense to mention this at some place?
Quoted 6 lines
scripts/nsdeps +0
-0fi generate_deps_for_ns() {Signature · 1 line
2.43.0
Re: [PATCH 13/23] kbuild: replace two $(abs_objtree) with $(CURDIR) in top Makefile
In reply to Masahiro YamadaQuoted 32 lines · Masahiro Yamada
Kbuildchanges the working directory until it matches $(abs_objtree).When $(need-sub-make) is empty, $(abs_objtree) is the same as $(CURDIR).
Signed-off-by Masahiro Yamada <[email protected]>---
Makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)Makefile +2
-2@@ -228,12 +228,12 @@ else # need-sub-make # We process the rest of the Makefile if this is the final invocation of make -ifeq ($(abs_srctree),$(abs_objtree)) +ifeq ($(abs_srctree),$(CURDIR)) # building in the source tree srctree := . building_out_of_srctree := else - ifeq ($(abs_srctree)/,$(dir $(abs_objtree))) + ifeq ($(abs_srctree)/,$(dir $(CURDIR))) # building in a subdirectory of the source tree srctree := .. elseSignature · 1 line
2.43.0
Re: [PATCH 12/23] scripts/nsdeps: use VPATH as src_prefix
In reply to Nicolas SchierQuoted 18 lines · Nicolas Schier
Quoted 17 lines · Masahiro Yamada
This change allows it to work not only for in-tree modules but also for external modules, even if they are built in a separate build directory.
Signed-off-by Masahiro Yamada <[email protected]>---
scripts/nsdeps | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)scripts/nsdeps +0
-0@@ -19,10 +19,10 @@ if ! { echo "$SPATCH_REQ_VERSION"; echo "$SPATCH_VERSION"; } | sort -CV ; then exit 1 fi
Quoted 10 lines
scripts/nsdeps if ! { echo "$SPATCH_REQ_VERSION"; echo "$SPATCH_VERSION"; } | sort -CV ; then · line 25
Quoted 6 lines
scripts/nsdeps +2
-2-if [ "$KBUILD_EXTMOD" ]; then +if [ "${VPATH+set}" ]; then src_prefix= else - src_prefix=$srctree/ + src_prefix=$VPATH/In kbuild, we expect VPATH to be empty or hold one single directory, while make itself allows VPATH to be a list of directories. Might it make sense to mention this at some place?
I'd like to withdraw that question. VPATH usage is an implementation detail and it's probably not helpful to put too much effort into keeping such things documentated.
Re: [PATCH 14/23] kbuild: add $(objtree)/ prefix to some in-kernel build artifacts
In reply to Masahiro YamadaQuoted 215 lines · Masahiro Yamada
$(objtree) refers to the top of the output directory of kernel builds.
This commit adds the explicit (objtree)/ prefix to build artifacts needed for building external modules.
This change has no immediate impact, as the top-level
Makefilecurrently defines:objtree := .
However, it prepares for supporting the building of external modules in a different directory.
Signed-off-by Masahiro Yamada <[email protected]>---
Makefile | 10 +++++----- scripts/Kbuild.include | 2 +- scripts/Makefile.build | 4 ++-- scripts/Makefile.modfinal | 14 +++++++------- scripts/Makefile.modinst | 2 +- scripts/Makefile.modpost | 12 ++++++------ scripts/depmod.sh | 4 ++-- 7 files changed, 24 insertions(+), 24 deletions(-)Makefile +5
-5@@ -354,7 +354,7 @@ else # !mixed-build include $(srctree)/scripts/Kbuild.include # Read KERNELRELEASE from include/config/kernel.release (if it exists) -KERNELRELEASE = $(call read-file, include/config/kernel.release) +KERNELRELEASE = $(call read-file, $(objtree)/include/config/kernel.release) KERNELVERSION = $(VERSION)$(if $(PATCHLEVEL),.$(PATCHLEVEL)$(if $(SUBLEVEL),.$(SUBLEVEL)))$(EXTRAVERSION) export VERSION PATCHLEVEL SUBLEVEL KERNELRELEASE KERNELVERSION@@ -717,7 +717,7 @@ endif export KBUILD_MODULES KBUILD_BUILTIN ifdef need-config -include include/config/auto.conf +include $(objtree)/include/config/auto.conf endif ifeq ($(KBUILD_EXTMOD),)@@ -780,13 +780,13 @@ else # !may-sync-config # and include/config/auto.conf but do not care if they are up-to-date. # Use auto.conf to show the error message -checked-configs := include/generated/autoconf.h include/generated/rustc_cfg include/config/auto.conf +checked-configs := $(addprefix $(objtree)/, include/generated/autoconf.h include/generated/rustc_cfg include/config/auto.conf) missing-configs := $(filter-out $(wildcard $(checked-configs)), $(checked-configs)) ifdef missing-configs -PHONY += include/config/auto.conf +PHONY += $(objtree)/include/config/auto.conf -include/config/auto.conf: +$(objtree)/include/config/auto.conf: @echo >&2 '***' @echo >&2 '*** ERROR: Kernel configuration is invalid. The following files are missing:' @printf >&2 '*** - %s\n' $(missing-configs)scripts/Kbuild.include +1
-1@@ -205,7 +205,7 @@ if_changed_dep = $(if $(if-changed-cond),$(cmd_and_fixdep),@:) cmd_and_fixdep = \ $(cmd); \ - scripts/basic/fixdep $(depfile) $@ '$(make-cmd)' > $(dot-target).cmd;\ + $(objtree)/scripts/basic/fixdep $(depfile) $@ '$(make-cmd)' > $(dot-target).cmd;\ rm -f $(depfile) # Usage: $(call if_changed_rule,foo)scripts/Makefile.build +2
-2@@ -34,7 +34,7 @@ subdir-asflags-y := subdir-ccflags-y := # Read auto.conf if it exists, otherwise ignore --include include/config/auto.conf +-include $(objtree)/include/config/auto.conf include $(srctree)/scripts/Kbuild.include include $(srctree)/scripts/Makefile.compiler@@ -107,7 +107,7 @@ cmd_cpp_i_c = $(CPP) $(c_flags) -o $@ $< $(obj)/%.i: $(obj)/%.c FORCE $(call if_changed_dep,cpp_i_c) -genksyms = scripts/genksyms/genksyms \ +genksyms = $(objtree)/scripts/genksyms/genksyms \ $(if $(1), -T $(2)) \ $(if $(KBUILD_PRESERVE), -p) \ -r $(or $(wildcard $(2:.symtypes=.symref)), /dev/null)scripts/Makefile.modfinal +7
-7@@ -6,7 +6,7 @@ PHONY := __modfinal __modfinal: -include include/config/auto.conf +include $(objtree)/include/config/auto.conf include $(srctree)/scripts/Kbuild.include # for c_flags@@ -37,15 +37,15 @@ quiet_cmd_ld_ko_o = LD [M] $@ cmd_ld_ko_o = \ $(LD) -r $(KBUILD_LDFLAGS) \ $(KBUILD_LDFLAGS_MODULE) $(LDFLAGS_MODULE) \ - -T scripts/module.lds -o $@ $(filter %.o, $^) + -T $(objtree)/scripts/module.lds -o $@ $(filter %.o, $^) quiet_cmd_btf_ko = BTF [M] $@ cmd_btf_ko = \ - if [ ! -f vmlinux ]; then \ + if [ ! -f $(objtree)/vmlinux ]; then \ printf "Skipping BTF generation for %s due to unavailability of vmlinux\n" $@ 1>&2; \ else \ - LLVM_OBJCOPY="$(OBJCOPY)" $(PAHOLE) -J $(PAHOLE_FLAGS) $(MODULE_PAHOLE_FLAGS) --btf_base vmlinux $@; \ - $(RESOLVE_BTFIDS) -b vmlinux $@; \ + LLVM_OBJCOPY="$(OBJCOPY)" $(PAHOLE) -J $(PAHOLE_FLAGS) $(MODULE_PAHOLE_FLAGS) --btf_base $(objtree)/vmlinux $@; \ + $(RESOLVE_BTFIDS) -b $(objtree)/vmlinux $@; \ fi; # Same as newer-prereqs, but allows to exclude specified extra dependencies@@ -57,8 +57,8 @@ if_changed_except = $(if $(call newer_prereqs_except,$(2))$(cmd-check), \ printf '%s\n' 'savedcmd_$@ := $(make-cmd)' > $(dot-target).cmd, @:) # Re-generate module BTFs if either module's .ko or vmlinux changed -%.ko: %.o %.mod.o $(extmod_prefix).module-common.o scripts/module.lds $(and $(CONFIG_DEBUG_INFO_BTF_MODULES),$(KBUILD_BUILTIN),vmlinux) FORCE - +$(call if_changed_except,ld_ko_o,vmlinux) +%.ko: %.o %.mod.o $(extmod_prefix).module-common.o $(objtree)/scripts/module.lds $(and $(CONFIG_DEBUG_INFO_BTF_MODULES),$(KBUILD_BUILTIN),$(objtree)/vmlinux) FORCE + +$(call if_changed_except,ld_ko_o,$(objtree)/vmlinux) ifdef CONFIG_DEBUG_INFO_BTF_MODULES +$(if $(newer-prereqs),$(call cmd,btf_ko)) endifscripts/Makefile.modinst +1
-1@@ -6,7 +6,7 @@ PHONY := __modinst __modinst: -include include/config/auto.conf +include $(objtree)/include/config/auto.conf include $(srctree)/scripts/Kbuild.include install-y :=scripts/Makefile.modpost +6
-6@@ -35,10 +35,10 @@ PHONY := __modpost __modpost: -include include/config/auto.conf +include $(objtree)/include/config/auto.conf include $(srctree)/scripts/Kbuild.include -MODPOST = scripts/mod/modpost +MODPOST = $(objtree)/scripts/mod/modpost modpost-args = \ $(if $(CONFIG_MODULES),-M) \@@ -119,11 +119,11 @@ include $(kbuild-file) output-symdump := $(KBUILD_EXTMOD)/Module.symvers -ifeq ($(wildcard Module.symvers),) -missing-input := Module.symvers +ifeq ($(wildcard $(objtree)/Module.symvers),) +missing-input := $(objtree)/Module.symvers else -modpost-args += -i Module.symvers -modpost-deps += Module.symvers +modpost-args += -i $(objtree)/Module.symvers +modpost-deps += $(objtree)/Module.symvers endif modpost-args += -e $(addprefix -i , $(KBUILD_EXTRA_SYMBOLS))scripts/depmod.sh +2
-2@@ -12,7 +12,7 @@ KERNELRELEASE=$1 : ${DEPMOD:=depmod} -if ! test -r System.map ; then +if ! test -r "${objtree}/System.map" ; then echo "Warning: modules_install: missing 'System.map' file. Skipping depmod." >&2 exit 0 fi@@ -25,7 +25,7 @@ if [ -z $(command -v $DEPMOD) ]; then exit 0 fi -set -- -ae -F System.map +set -- -ae -F "${objtree}/System.map" if test -n "$INSTALL_MOD_PATH"; then set -- "$@" -b "$INSTALL_MOD_PATH" fiSignature · 1 line
2.43.0
Re: [PATCH 15/23] kbuild: rename abs_objtree to abs_output
In reply to Masahiro YamadaQuoted 57 lines · Masahiro Yamada
'objtree' refers to the top of the output directory of kernel builds.
Rename
abs_objtreeto a more generic name, to better reflect its use in the context of external module builds.Signed-off-by Masahiro Yamada <[email protected]>---
Makefile | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)Makefile +5
-5@@ -40,7 +40,7 @@ __all: this-makefile := $(lastword $(MAKEFILE_LIST)) abs_srctree := $(realpath $(dir $(this-makefile))) -abs_objtree := $(CURDIR) +abs_output := $(CURDIR) ifneq ($(sub_make_done),1)@@ -185,8 +185,8 @@ ifneq ($(KBUILD_OUTPUT),) # $(realpath ...) gets empty if the path does not exist. Run 'mkdir -p' first. $(shell mkdir -p "$(KBUILD_OUTPUT)") # $(realpath ...) resolves symlinks -abs_objtree := $(realpath $(KBUILD_OUTPUT)) -$(if $(abs_objtree),,$(error failed to create output directory "$(KBUILD_OUTPUT)")) +abs_output := $(realpath $(KBUILD_OUTPUT)) +$(if $(abs_output),,$(error failed to create output directory "$(KBUILD_OUTPUT)")) endif # ifneq ($(KBUILD_OUTPUT),) ifneq ($(words $(subst :, ,$(abs_srctree))), 1)@@ -197,7 +197,7 @@ export sub_make_done := 1 endif # sub_make_done -ifeq ($(abs_objtree),$(CURDIR)) +ifeq ($(abs_output),$(CURDIR)) # Suppress "Entering directory ..." if we are at the final work directory. no-print-directory := --no-print-directory else@@ -221,7 +221,7 @@ $(filter-out $(this-makefile), $(MAKECMDGOALS)) __all: __sub-make # Invoke a second make in the output directory, passing relevant variables __sub-make: - $(Q)$(MAKE) $(no-print-directory) -C $(abs_objtree) \ + $(Q)$(MAKE) $(no-print-directory) -C $(abs_output) \ -f $(abs_srctree)/Makefile $(MAKECMDGOALS) else # need-sub-makeSignature · 1 line
2.43.0
Re: [PATCH 16/23] kbuild: use 'output' variable to create the output directory
In reply to Masahiro YamadaQuoted 46 lines · Masahiro Yamada
$(KBUILD_OUTPUT) specifies the output directory of kernel builds.
Use a more generic name, 'output', to better reflect this code hunk in the context of external module builds.
Signed-off-by Masahiro Yamada <[email protected]>---
Makefile | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-)Makefile +8
-6@@ -176,18 +176,20 @@ export KBUILD_EXTRA_WARN # The O= assignment takes precedence over the KBUILD_OUTPUT environment # variable. -# Do we want to change the working directory? ifeq ("$(origin O)", "command line") KBUILD_OUTPUT := $(O) endif -ifneq ($(KBUILD_OUTPUT),) +output := $(KBUILD_OUTPUT) + +# Do we want to change the working directory? +ifneq ($(output),) # $(realpath ...) gets empty if the path does not exist. Run 'mkdir -p' first. -$(shell mkdir -p "$(KBUILD_OUTPUT)") +$(shell mkdir -p "$(output)") # $(realpath ...) resolves symlinks -abs_output := $(realpath $(KBUILD_OUTPUT)) -$(if $(abs_output),,$(error failed to create output directory "$(KBUILD_OUTPUT)")) -endif # ifneq ($(KBUILD_OUTPUT),) +abs_output := $(realpath $(output)) +$(if $(abs_output),,$(error failed to create output directory "$(output)")) +endif ifneq ($(words $(subst :, ,$(abs_srctree))), 1) $(error source directory cannot contain spaces or colons)Signature · 1 line
2.43.0
Quoted 32 lines · Masahiro Yamada
There has been a long-standing request to support building external modules in a separate build directory.
The first half is cleanups of documents and Makefiles.
The last part adds
KBUILD_EXTMOD_OUTPUT(MO=). This is too big changes, and too late for the current MW. (I did not test kselftest at all.)
I hope people test this and may uncover some issues.Masahiro Yamada (23):
kbuild: doc: update the description about Kbuild/Makefile split
kbuild: doc: remove description about grepping CONFIG options
kbuild: doc: remove outdated description of the limitation on -I usage
kbuild: doc: remove the description about shipped files
kbuild: doc: describe the -C option precisely for external module
builds
kbuild: doc: replace "gcc" in external module description
kbuild: remove unnecessary prune ofrust/allocfor rustfmt
kbuild: simplify find command for rustfmt
speakup: use SPKDIR=$(src) to specify the source directory
kbuild: refactor the check for missing config files
kbuild: check the presence ofinclude/generated/rustc_cfgscripts/nsdeps: use VPATH assrc_prefix
kbuild: replace two $(abs_objtree) with $(CURDIR) in topMakefile
kbuild: add $(objtree)/ prefix to some in-kernel build artifacts
kbuild: renameabs_objtreetoabs_output
kbuild: use 'output' variable to create the output directory
Quoted 8 lines
kbuild: build external modules in their directory
kbuild: removeextmod_prefix, MODORDER,MODULES_NSDEPSvariables
kbuild: support building external modules in a separate build
directory
kbuild: support -fmacro-prefix-map for external modules
kbuild: use absolute path in the generated wrapperMakefile
kbuild: make wrapperMakefilemore convenient for external modules
kbuild: allow to start building external module in any directory
I CC'ed rust ML because Ack for the following patches are appreciated.
[07/23] kbuild: remove unnecessary prune of rust/alloc for rustfmt [08/23] kbuild: simplify find command for rustfmt [11/23] kbuild: check the presence of include/generated/rustc_cfg
Signature · 2 lines
Best Regards Masahiro Yamada
Miguel Ojeda
Tested-byQuoted 6 lines · Masahiro Yamada
I CC'ed rust ML because Ack for the following patches are appreciated.
[07/23] kbuild: remove unnecessary prune of
rust/allocfor rustfmt [08/23] kbuild: simplify find command for rustfmt [11/23] kbuild: check the presence ofinclude/generated/rustc_cfg
Sorry, it was in my backlog after the conferences.
I am not sure what the base of the series was, but ran my usual tests on top of v6.11 and then on top of rust-fixes after some manual adjustment in both cases, and things appear to still work fine (i.e. what I usually build, without taking advantage of the separate build directory support).
Moreover, I tested the separate build directory support (MO=), for a trivial Rust out-of-tree module, with a subdir as well as with a directory outside the out-of-tree source code.
I also tested the new approach suggested for the out-of-tree Makefile (i.e. export KBUILD_EXTMOD and include $(KDIR)/Makefile), and it all worked as expected, so:
...except for arm64, where I found that I needed this bit:
arch/arm64/Makefile +1 -1
@@ -71,7 +71,7 @@ stack_protector_prepare: prepare0 -mstack-protector-guard-reg=sp_el0 \ -mstack-protector-guard-offset=$(shell \ awk '{if ($$2 == "TSK_STACK_CANARY") print $$3;}' \ - include/generated/asm-offsets.h)) +
$(objtree)/include/generated/asm-offsets.h))
endif
ifeq ($(CONFIG_ARM64_BTI_KERNEL),y)
Cheers,
Miguel
Miguel Ojeda
Acked-byRe: [PATCH 07/23] kbuild: remove unnecessary prune of rust/alloc for rustfmt
In reply to Nicolas SchierQuoted 3 lines · Nicolas Schier
oh, there is still a comment about '
rust/alloc' inMakefilethat probably wants to be removed, too.
Indeed, good catch. Please feel free to remove it in this series, otherwise we can do it later. Either way:
Cheers,
Miguel
Re: [PATCH 11/23] kbuild: check the presence of include/generated/rustc_cfg
In reply to Masahiro YamadaQuoted 7 lines · Masahiro Yamada
Since commit
2f7ab1267dc9("Kbuild: add Rust support"),Kconfiggeneratesinclude/generated/rustc_cfg, but its presence is not checked in the top-levelMakefile. It should be checked similarly to the C header counterpart,include/generated/autoconf.h.Signed-off-by Masahiro Yamada <[email protected]>
Sounds good to me:
I also tested that it indeed prints it if removed:
***
*** ERROR: Kernel configuration is invalid. The following files
are missing:
*** - ....../linux/include/generated/rustc_cfg
*** Run "make oldconfig && make prepare" on kernel source to fix it.
***which is nice! So:
I guess this tag may apply to the previous patch too.
Cheers,
Miguel
Re: [PATCH 08/23] kbuild: simplify find command for rustfmt
In reply to Masahiro YamadaQuoted 3 lines · Masahiro Yamada
The correct use of the -prune option can be seen in the 'make clean' rule.
Yeah, this -prune should not have been like that -- sorry about that.
The comment above this recipe should be updated.
I am not sure I understand the part of the commit message about the rust/test change. Do you mean that we should use srctree in case there is a stale one in the source tree from a previous non-completely-clean in-source-tree build? I think the original intention was to skip the objtree one if it were a subdir of srctree (and that is why the use of absolute paths).
Although I think we can simplify further by just removing the logic about rust/test, since we don't generate *.rs files there anyway at the moment.
Cheers,
Miguel
Re: [PATCH 08/23] kbuild: simplify find command for rustfmt
Quoted 6 lines · Miguel Ojeda
Quoted 3 lines · Masahiro Yamada
The correct use of the -prune option can be seen in the 'make clean' rule.
Quoted 8 lines
Yeah, this
-pruneshould not have been like that -- sorry about that.The comment above this recipe should be updated.
I am not sure I understand the part of the commit message about the
rust/testchange. Do you mean that we should usesrctreein case there is a stale one in the source tree from a previous non-completely-clean in-source-tree build?
Correct.
Quoted 3 lines
I think the original
intention was to skip the objtree one if it were a subdir of srctree (and that is why the use of absolute paths).
OK, understood.
If you insist on the current logic, I will keep it as-is, but I need to replace $(abs_objtree) with $(CURDIR) because $(abs_objtree) will be removed by this series.
Quoted 3 lines
Although I think we can simplify further by just removing the logic about
rust/test, since we don't generate*.rsfiles there anyway at the moment.
OK, then we can remove -prune entirely.
--
Best Regards
Masahiro Yamada
Miguel Ojeda
Acked-byRe: [PATCH 08/23] kbuild: simplify find command for rustfmt
Quoted 2 lines · Masahiro Yamada
OK, then we can remove -prune entirely.
Yeah, I think that is the simplest approach.
With those changes, please feel free to add:
Thanks!
Cheers,
Miguel
Re: [PATCH 09/23] speakup: use SPKDIR=$(src) to specify the source directory
In reply to Masahiro YamadaQuoted 29 lines · Masahiro Yamada
Since commit
b1992c3772e6("kbuild: use $(src) instead of $(srctree)/$(src) for source directory"), $(src) consistently points to the source directory whether this is compiled an as external module or not.Signed-off-by Masahiro Yamada <[email protected]>---
drivers/accessibility/speakup/Makefile | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)drivers/accessibility/speakup/Makefile +1
-3@@ -40,9 +40,7 @@ hostprogs += makemapdata makemapdata-objs := makemapdata.o quiet_cmd_mkmap = MKMAP $@ - cmd_mkmap = TOPDIR=$(srctree) \ - SPKDIR=$(if $(KBUILD_EXTMOD),$(KBUILD_EXTMOD),$(srctree)/drivers/accessibility/speakup) \ - $(obj)/makemapdata > $@ + cmd_mkmap = TOPDIR=$(srctree) SPKDIR=$(src) $(obj)/makemapdata > $@ $(obj)/mapdata.h: $(obj)/makemapdata $(call cmd,mkmap)Signature · 1 line
2.43.0
Re: [PATCH 17/23] kbuild: build external modules in their directory
In reply to Masahiro YamadaQuoted 76 lines · Masahiro Yamada
Currently,
Kbuildalways 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]>---
Documentation/dev-tools/coccinelle.rst | 19 +++++------- Makefile | 40 +++++++++++++++++--------- rust/Makefile | 4 +-- scripts/Makefile.compiler | 2 +- scripts/Makefile.modpost | 6 ++-- scripts/coccicheck | 6 ++-- scripts/package/install-extmod-build | 7 +++++ 7 files changed, 49 insertions(+), 35 deletions(-)Documentation/dev-tools/coccinelle.rst +2
-7@@ -250,25 +250,20 @@ variables for .cocciconfig is as follows: - Your directory from which spatch is called is processed next - The directory provided with the ``--dir`` option is processed last, if used -Since coccicheck runs through make, it naturally runs from the kernel -proper dir; as such the second rule above would be implied for picking up a -.cocciconfig when using ``make coccicheck``. - ``make coccicheck`` also supports using M= targets. If you do not supply any M= target, it is assumed you want to target the entire kernel. The kernel coccicheck script has:: - if [ "$KBUILD_EXTMOD" = "" ] ; then - OPTIONS="--dir $srctree $COCCIINCLUDE" + if [ "$VPATH" ] ; then + OPTIONS="--dir $VPATH $COCCIINCLUDE" else - OPTIONS="--dir $KBUILD_EXTMOD $COCCIINCLUDE"
Documentation/dev-tools/coccinelle.rst variables for .cocciconfig is as follows: · line 264
Quoted 10 lines
Documentation/dev-tools/coccinelle.rst +3
-5+ OPTIONS="--dir . $COCCIINCLUDE" fi -KBUILD_EXTMOD is set when an explicit target with M= is used. For both cases -the spatch ``--dir`` argument is used, as such third rule applies when whether -M= is used or not, and when M= is used the target directory can have its own -.cocciconfig file. When M= is not passed as an argument to coccicheck the -target directory is the same as the directory from where spatch was called. +When an explicit target is executed with a separate output directory, VPATH is +set to the target source directory. The third rule ensures the spatch reads the
My limited English with German background likes to have "the target's source directory" here, but I am not sure if this is more correct.
Documentation/dev-tools/coccinelle.rst variables for .cocciconfig is as follows: · line 266
Quoted 2 lines
Documentation/dev-tools/coccinelle.rst +2
-0+.cocciconfig from the target directory. When M= is used, the external module +directory can have its own.cocciconfig file.
A space is missing after 'own'.
Quoted 48 lines
Documentation/dev-tools/coccinelle.rst +0
-0If not using the kernel's coccicheck target, keep the above precedence order logic of .cocciconfig reading. If using the kernel's coccicheck target,Makefile +15
-4@@ -180,7 +180,20 @@ ifeq ("$(origin O)", "command line") KBUILD_OUTPUT := $(O) endif -output := $(KBUILD_OUTPUT) +ifdef KBUILD_EXTMOD + ifdef KBUILD_OUTPUT + objtree := $(realpath $(KBUILD_OUTPUT)) + $(if $(objtree),,$(error specified kernel directory "$(KBUILD_OUTPUT)" does not exist)) + else + objtree := $(CURDIR) + endif + output := $(KBUILD_EXTMOD) +else + objtree := . + output := $(KBUILD_OUTPUT) +endif + +export objtree # Do we want to change the working directory? ifneq ($(output),)@@ -248,8 +261,6 @@ ifneq ($(KBUILD_ABS_SRCTREE),) srctree := $(abs_srctree) endif -objtree := . - VPATH := ifeq ($(KBUILD_EXTMOD),)@@ -258,7 +269,7 @@ VPATH := $(srctree) endif endif -export building_out_of_srctree srctree objtree VPATH +export building_out_of_srctree srctree VPATH # To make sure we do not include .config for any of the *config targets # catch them early, and hand them over to scripts/kconfig/Makefile
Makefile endif · line 722
Quoted 5 lines
Makefile +1
-1@@ -708,7 +719,7 @@ endif # in addition to whatever we do anyway. # Just "make" or "make all" shall build modules as well -ifneq ($(filter all modules nsdeps %compile_commands.json clang-%,$(MAKECMDGOALS)),) +ifneq ($(filter all modules nsdeps compile_commands.json clang-%,$(MAKECMDGOALS)),)
Nit: I think this would better match to patch "kbuild: remove extmod_prefix, MODORDER, MODULES_NSDEPS variables", but probably nobody else will care.
Re: [PATCH 18/23] kbuild: remove extmod_prefix, MODORDER, MODULES_NSDEPS variables
In reply to Masahiro YamadaQuoted 7 lines · Masahiro Yamada
With the previous changes, $(extmod_prefix), $(MODORDER), and $(MODULES_NSDEPS) are constant. (empty, modules.order, and modules.nsdeps, respectively).
Remove these variables and hard-code their values.
Signed-off-by Masahiro Yamada <[email protected]>
Quoted 8 lines
---
Makefile | 22 +++++++++------------- scripts/Makefile.modfinal | 2 +- scripts/Makefile.modinst | 2 +- scripts/Makefile.modpost | 6 +++--- scripts/nsdeps | 2 +- 5 files changed, 15 insertions(+), 19 deletions(-)
Do you want to remove these also? I can't think of a reason to keep these around:
$ git grep -Hrnwe extmod_prefix -e MODORDER -e MODULES_NSDEP
Makefile:1912: $(Q){ $(foreach m, $(single-ko), echo $(extmod_prefix)$(m:%.ko=%.o);) } > modules.order
scripts/Makefile.modfinal:33:$(extmod_prefix).module-common.o: $(srctree)/scripts/module-common.c FORCE
scripts/Makefile.modfinal:60:%.ko: %.o %.mod.o $(extmod_prefix).module-common.o $(objtree)/scripts/module.lds $(and $(CONFIG_DEBUG_INFO_BTF_MODULES),$(KBUILD_BUILTIN),$(objtree)/vmlinux) FORCE
scripts/Makefile.modfinal:66:targets += $(modules:%.o=%.ko) $(modules:%.o=%.mod.o) $(extmod_prefix).module-common.o
scripts/Makefile.modinst:60:modules := $(patsubst $(extmod_prefix)%.o, $(dst)/%.ko$(suffix-y), $(modules))
scripts/Makefile.modinst:120:$(dst)/%.ko: $(extmod_prefix)%.ko FORCEW/ or w/o:
Quoted 56 lines · Masahiro Yamada
There has been a long-standing request to support building external modules in a separate build directory.
This commit introduces a new environment variable,
KBUILD_EXTMOD_OUTPUT, and its shorthand Make variable, MO.A simple usage:
$ make -C <kernel-dir> M=<module-src-dir> MO=<module-build-dir>Signed-off-by Masahiro Yamada <[email protected]>---
Documentation/kbuild/kbuild.rst | 8 +++++- Documentation/kbuild/modules.rst | 5 +++- Makefile | 44 +++++++++++++++++++++++--------- 3 files changed, 43 insertions(+), 14 deletions(-)Documentation/kbuild/kbuild.rst +7
-1@@ -132,12 +132,18 @@ Specify the output directory when building the kernel. This variable can also be used to point to the kernel output directory when building external modules using kernel build artifacts in a separate build directory. Please note that this does NOT specify the output directory for the -external modules themselves. +external modules themselves. (Use KBUILD_EXTMOD_OUTPUT for that purpose.) The output directory can also be specified using "O=...". Setting "O=..." takes precedence over KBUILD_OUTPUT. +KBUILD_EXTMOD_OUTPUT +-------------------- +Specify the output directory for external modules. + +Setting "MO=..." takes precedence over KBUILD_EXTMOD_OUTPUT. + KBUILD_EXTRA_WARN ----------------- Specify the extra build checks. The same value can be assigned by passingDocumentation/kbuild/modules.rst +1
-1@@ -95,7 +95,7 @@ executed to make module versioning work. of the kernel output directory if the kernel was built in a separate build directory.) - make -C $KDIR M=$PWD + make -C $KDIR M=$PWD [MO=$BUILD_DIR] -C $KDIR The directory that contains the kernel and relevant build
Documentation/kbuild/modules.rst executed to make module versioning work. · line 113
Quoted 5 lines
Documentation/kbuild/modules.rst +2
-0@@ -109,6 +109,9 @@ executed to make module versioning work. directory where the external module (kbuild file) is located. + MO=$BUILD_DIR + Speficies a separate output directory for the external module.
s/Speficies/Specifies/
Quoted 9 lines
Documentation/kbuild/modules.rst +1
-0+ 2.3 Targets ===========Makefile +0
-0
Makefile ifeq ("$(origin M)", "command line") · line 142
Quoted 9 lines
Makefile +4
-0@@ -134,6 +134,10 @@ ifeq ("$(origin M)", "command line") KBUILD_EXTMOD := $(M) endif +ifeq ("$(origin MO)", "command line") + KBUILD_EXTMOD_OUTPUT := $(MO) +endif + $(if $(word 2, $(KBUILD_EXTMOD)), \ $(error building multiple external modules is not supported))
Should we also check against multiple output directories?
Quoted 35 lines
Makefile +6
-4@@ -187,7 +191,11 @@ ifdef KBUILD_EXTMOD else objtree := $(CURDIR) endif - output := $(KBUILD_EXTMOD) + output := $(or $(KBUILD_EXTMOD_OUTPUT),$(KBUILD_EXTMOD)) + # KBUILD_EXTMOD might be a relative path. Remember its absolute path before + # Make changes the working directory. + export abs_extmodtree := $(realpath $(KBUILD_EXTMOD)) + $(if $(abs_extmodtree),,$(error specified external module directory "$(KBUILD_EXTMOD)" does not exist)) else objtree := . output := $(KBUILD_OUTPUT)@@ -246,7 +254,6 @@ else # need-sub-make ifeq ($(abs_srctree),$(CURDIR)) # building in the source tree srctree := . - building_out_of_srctree := else ifeq ($(abs_srctree)/,$(dir $(CURDIR))) # building in a subdirectory of the source tree@@ -254,22 +261,23 @@ else else srctree := $(abs_srctree) endif - building_out_of_srctree := 1 endif ifneq ($(KBUILD_ABS_SRCTREE),) srctree := $(abs_srctree) endif -VPATH := +export srctree
Makefile else · line 274
Quoted 10 lines
Makefile +2
-6-ifeq ($(KBUILD_EXTMOD),) -ifdef building_out_of_srctree -VPATH := $(srctree) -endif -endif +_vpath = $(or $(abs_extmodtree),$(srctree)) -export building_out_of_srctree srctree VPATH +ifeq ($(realpath $(_vpath)),$(CURDIR))
Just a style consistency question: 'ifeq (,)' with a space after ',' (as a few lines above) or without as used here?
Quoted 56 lines
Makefile +20
-2+building_out_of_srctree := +VPATH := +else +export building_out_of_srctree := 1 +export VPATH := $(_vpath) +endif # To make sure we do not include .config for any of the *config targets # catch them early, and hand them over to scripts/kconfig/Makefile@@ -550,7 +558,7 @@ USERINCLUDE := \ LINUXINCLUDE := \ -I$(srctree)/arch/$(SRCARCH)/include \ -I$(objtree)/arch/$(SRCARCH)/include/generated \ - $(if $(building_out_of_srctree),-I$(srctree)/include) \ + -I$(srctree)/include \ -I$(objtree)/include \ $(USERINCLUDE)@@ -640,6 +648,7 @@ quiet_cmd_makefile = GEN Makefile } > Makefile outputmakefile: +ifeq ($(KBUILD_EXTMOD),) @if [ -f $(srctree)/.config -o \ -d $(srctree)/include/config -o \ -d $(srctree)/arch/$(SRCARCH)/include/generated ]; then \@@ -649,7 +658,16 @@ outputmakefile: echo >&2 "***"; \ false; \ fi - $(Q)ln -fsn $(srctree) source +else + @if [ -f $(KBUILD_EXTMOD)/modules.order ]; then \ + echo >&2 "***"; \ + echo >&2 "*** The external module source tree is not clean."; \ + echo >&2 "*** Please run 'make -C $(abs_srctree) M=$(realpath $(KBUILD_EXTMOD)) clean'"; \ + echo >&2 "***"; \ + false; \ + fi +endif + $(Q)ln -fsn $(_vpath) source $(call cmd,makefile) $(Q)test -e .gitignore || \ { echo "# this is build directory, ignore it"; echo "*"; } > .gitignore@@ -1926,6 +1944,8 @@ KBUILD_MODULES := 1 endif +prepare: outputmakefile + # Preset locale variables to speed up the build process. Limit locale # tweaks to this spot to avoid wrong language settings when running # make menuconfig etc.Signature · 1 line
2.43.0
Thanks, this feature is really appreciated by a lot of my colleagues, and I think you found quite a nice solution! I'm a bit surprised, that there are not some more testers ...
Re: [PATCH 20/23] kbuild: support -fmacro-prefix-map for external modules
In reply to Masahiro YamadaQuoted 5 lines · Masahiro Yamada
This commit makes -fmacro-prefix-map work for external modules built in a separate output directory. It improves the reproducibility of external modules and provides the benefits described in commit
a73619a845d5("kbuild: use -fmacro-prefix-map to make__FILE__a relative path").
Quoted 8 lines
When VPATH is not defined (e.g., when the kernel or external module is built in the source directory), this option is unnecessary.
Signed-off-by Masahiro Yamada <[email protected]>---
Makefile | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
Re: [PATCH 21/23] kbuild: use absolute path in the generated wrapper Makefile
In reply to Masahiro YamadaQuoted 8 lines · Masahiro Yamada
Keep the consistent behavior when this
Makefileis invoked from another directory.Signed-off-by Masahiro Yamada <[email protected]>---
Makefile | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
Quoted 156 lines · Nicolas Schier
Quoted 56 lines · Masahiro Yamada
There has been a long-standing request to support building external modules in a separate build directory.
This commit introduces a new environment variable,
KBUILD_EXTMOD_OUTPUT, and its shorthand Make variable, MO.A simple usage:
$ make -C <kernel-dir> M=<module-src-dir> MO=<module-build-dir>Signed-off-by Masahiro Yamada <[email protected]>---
Documentation/kbuild/kbuild.rst | 8 +++++- Documentation/kbuild/modules.rst | 5 +++- Makefile | 44 +++++++++++++++++++++++--------- 3 files changed, 43 insertions(+), 14 deletions(-)Documentation/kbuild/kbuild.rst +7
-1@@ -132,12 +132,18 @@ Specify the output directory when building the kernel. This variable can also be used to point to the kernel output directory when building external modules using kernel build artifacts in a separate build directory. Please note that this does NOT specify the output directory for the -external modules themselves. +external modules themselves. (Use KBUILD_EXTMOD_OUTPUT for that purpose.) The output directory can also be specified using "O=...". Setting "O=..." takes precedence over KBUILD_OUTPUT. +KBUILD_EXTMOD_OUTPUT +-------------------- +Specify the output directory for external modules. + +Setting "MO=..." takes precedence over KBUILD_EXTMOD_OUTPUT. + KBUILD_EXTRA_WARN ----------------- Specify the extra build checks. The same value can be assigned by passingDocumentation/kbuild/modules.rst +1
-1@@ -95,7 +95,7 @@ executed to make module versioning work. of the kernel output directory if the kernel was built in a separate build directory.) - make -C $KDIR M=$PWD + make -C $KDIR M=$PWD [MO=$BUILD_DIR] -C $KDIR The directory that contains the kernel and relevant buildDocumentation/kbuild/modules.rst executed to make module versioning work. · line 113
Quoted 5 lines
Documentation/kbuild/modules.rst +2
-0@@ -109,6 +109,9 @@ executed to make module versioning work. directory where the external module (kbuild file) is located. + MO=$BUILD_DIR + Speficies a separate output directory for the external module.s/Speficies/Specifies/
Quoted 9 lines
Documentation/kbuild/modules.rst +1
-0+ 2.3 Targets ===========Makefile +0
-0Makefile ifeq ("$(origin M)", "command line") · line 142
Quoted 9 lines
Makefile +4
-0@@ -134,6 +134,10 @@ ifeq ("$(origin M)", "command line") KBUILD_EXTMOD := $(M) endif +ifeq ("$(origin MO)", "command line") + KBUILD_EXTMOD_OUTPUT := $(MO) +endif + $(if $(word 2, $(KBUILD_EXTMOD)), \ $(error building multiple external modules is not supported))Should we also check against multiple output directories?
Quoted 35 lines
Makefile +6
-4@@ -187,7 +191,11 @@ ifdef KBUILD_EXTMOD else objtree := $(CURDIR) endif - output := $(KBUILD_EXTMOD) + output := $(or $(KBUILD_EXTMOD_OUTPUT),$(KBUILD_EXTMOD)) + # KBUILD_EXTMOD might be a relative path. Remember its absolute path before + # Make changes the working directory. + export abs_extmodtree := $(realpath $(KBUILD_EXTMOD)) + $(if $(abs_extmodtree),,$(error specified external module directory "$(KBUILD_EXTMOD)" does not exist)) else objtree := . output := $(KBUILD_OUTPUT)@@ -246,7 +254,6 @@ else # need-sub-make ifeq ($(abs_srctree),$(CURDIR)) # building in the source tree srctree := . - building_out_of_srctree := else ifeq ($(abs_srctree)/,$(dir $(CURDIR))) # building in a subdirectory of the source tree@@ -254,22 +261,23 @@ else else srctree := $(abs_srctree) endif - building_out_of_srctree := 1 endif ifneq ($(KBUILD_ABS_SRCTREE),) srctree := $(abs_srctree) endif -VPATH := +export srctreeMakefile else · line 274
Quoted 10 lines
Makefile +2
-6-ifeq ($(KBUILD_EXTMOD),) -ifdef building_out_of_srctree -VPATH := $(srctree) -endif -endif +_vpath = $(or $(abs_extmodtree),$(srctree)) -export building_out_of_srctree srctree VPATH +ifeq ($(realpath $(_vpath)),$(CURDIR))Just a style consistency question: 'ifeq (,)' with a space after ',' (as a few lines above) or without as used here?
Quoted 21 lines
Makefile +7
-1+building_out_of_srctree := +VPATH := +else +export building_out_of_srctree := 1 +export VPATH := $(_vpath) +endif # To make sure we do not include .config for any of the *config targets # catch them early, and hand them over to scripts/kconfig/Makefile@@ -550,7 +558,7 @@ USERINCLUDE := \ LINUXINCLUDE := \ -I$(srctree)/arch/$(SRCARCH)/include \ -I$(objtree)/arch/$(SRCARCH)/include/generated \ - $(if $(building_out_of_srctree),-I$(srctree)/include) \ + -I$(srctree)/include \ -I$(objtree)/include \ $(USERINCLUDE)@@ -640,6 +648,7 @@ quiet_cmd_makefile = GEN Makefile } > Makefile
Quoted 12 lines
Makefile outputmakefile: · line 662
Quoted 12 lines
Makefile +3
-1outputmakefile: +ifeq ($(KBUILD_EXTMOD),) @if [ -f $(srctree)/.config -o \ -d $(srctree)/include/config -o \ -d $(srctree)/arch/$(SRCARCH)/include/generated ]; then \@@ -649,7 +658,16 @@ outputmakefile: echo >&2 "***"; \ false; \ fi - $(Q)ln -fsn $(srctree) source +else + @if [ -f $(KBUILD_EXTMOD)/modules.order ]; then \
If I do
make O=build
touch fs/btrfs/modules.order
make O=build M=fs/btrfs/ MO=/tmp/btrfs CONFIG_BTRFS_FS=m
the 'modules.order' check does not work correctly. But a consequtive
make -C /tmp/btrfs CONFIG_BTRFS_FS=m
shows the 'The external module source tree is not clean.' message as expected.
If I replace $(KBUILD_EXTMOD) by $(abs_extmodtree), it works for me also in the first case.
Kind regards,
Nicolas
Re: [PATCH 22/23] kbuild: make wrapper Makefile more convenient for external modules
In reply to Masahiro YamadaQuoted 51 lines · Masahiro Yamada
When
Kbuildstarts building in a separate output directory, it generates a wrapperMakefile, allowing you to invoke 'make' from the output directory.This commit makes it more convenient, so you can invoke 'make' without M= or MO=.
First, you need to build external modules in a separate directory:
$ make M=/path/to/module/source/dir MO=/path/to/module/build/dirOnce the wrapper
Makefileis generated in /path/to/module/build/dir, you can proceed as follows:$ cd /path/to/module/build/dir $ makeSigned-off-by Masahiro Yamada <[email protected]>---
Makefile | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-)Makefile +11
-1@@ -641,10 +641,20 @@ ifdef building_out_of_srctree # At the same time when output Makefile generated, generate .gitignore to # ignore whole output directory +ifdef KBUILD_EXTMOD +print_env_for_makefile = \ + echo "export KBUILD_OUTPUT = $(objtree)"; \ + echo "export KBUILD_EXTMOD = $(_vpath)" ; \ + echo "export KBUILD_EXTMOD_OUTPUT = $(CURDIR)" +else +print_env_for_makefile = \ + echo "export KBUILD_OUTPUT = $(CURDIR)" +endif + quiet_cmd_makefile = GEN Makefile cmd_makefile = { \ echo "\# Automatically generated by $(abs_srctree)/Makefile: don't edit"; \ - echo "export KBUILD_OUTPUT = $(CURDIR)"; \ + $(print_env_for_makefile); \ echo "include $(abs_srctree)/Makefile"; \ } > MakefileSignature · 1 line
2.43.0
Nice idea, thanks!
Re: [PATCH 23/23] kbuild: allow to start building external module in any directory
In reply to Masahiro YamadaQuoted 27 lines · Masahiro Yamada
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/moduleThis 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/moduleThe key difference is that the -C option changes the working directory and parses the
Makefilelocated there, while the -f option only specifies theMakefileto use.As shown in the examples in
Documentation/kbuild/modules.rst, external modules usually have a wrapperMakefilethat allows you to build them without specifying any make arguments. TheMakefiletypically contains a rule as follows:KDIR ?= /path/to/kernel
default:
$(MAKE) -C $(KDIR) M=$(CURDIR) $(MAKECMDGOALS)The log will appear as follows:
Quoted 10 lines
$ make
make -C /path/to/kernel M=/path/to/external/module
make[1]: Entering directory '/path/to/kernel'
make[2]: Entering directory '/path/to/exernal/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/exernal/module's/exernal/external/, also above.
Quoted 51 lines
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
Kbuildinternally recurses back to the external module directory.With this commit, the wrapper
Makefilecan directly include the kernelMakefile:KDIR ?= /path/to/kernel export KBUILD_EXTMOD = $(CURDIR) include $(KDIR)/MakefileThis 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.koSigned-off-by Masahiro Yamada <[email protected]>---
Documentation/kbuild/modules.rst | 21 +++++++++++++++++++++ Makefile | 8 ++++++-- 2 files changed, 27 insertions(+), 2 deletions(-)Documentation/kbuild/modules.rst +7
-0@@ -88,6 +88,12 @@ executed to make module versioning work. $ make -C /lib/modules/`uname -r`/build M=$PWD modules_install + Starting from Linux 6.13, you can use the -f option instead of -C. This + will avoid unnecessary change of the working directory. The external + module will be output to the directory where you invoke make. + + $ make -f /lib/modules/`uname -r`/build/Makefile M=$PWD + 2.2 Options ===========@@ -246,6 +252,21 @@ module 8123.ko, which is built from the following files:: consisting of several hundred lines, and here it really pays off to separate the kbuild part from the rest. + Linux 6.13 and later support another way. The external module Makefile
Documentation/kbuild/modules.rst module 8123.ko, which is built from the following files:: · line 265
Quoted 10 lines
Documentation/kbuild/modules.rst +10
-0+ can include the kernel Makefile directly, rather than invoking sub Make. + + Example 3:: + + --> filename: Kbuild + obj-m := 8123.o + 8123-y := 8123_if.o 8123_pci.o + + --> filename: Makefile + KDIR ?= /lib/modules/`uname -r`/build
KDIR ?= /lib/modules/$(shell uname -r)/build
This patch is another very good idea, thanks!
Quoted 30 lines
Documentation/kbuild/modules.rst +4
-0+ export KBUILD_EXTMOD = $(CURDIR) + include $(KDIR)/Makefile + + 3.4 Building Multiple Modules =============================Makefile +6
-2@@ -189,9 +189,13 @@ ifdef KBUILD_EXTMOD objtree := $(realpath $(KBUILD_OUTPUT)) $(if $(objtree),,$(error specified kernel directory "$(KBUILD_OUTPUT)" does not exist)) else - objtree := $(CURDIR) + objtree := $(abs_srctree) endif - output := $(or $(KBUILD_EXTMOD_OUTPUT),$(KBUILD_EXTMOD)) + # If Make is invoked from the kernel directory (either kernel + # source directory or kernel build directory), external modules + # are built in $(KBUILD_EXTMOD) for backward compatibility, + # otherwise, built in the current directory. + output := $(or $(KBUILD_EXTMOD_OUTPUT),$(if $(filter $(CURDIR),$(objtree) $(abs_srctree)),$(KBUILD_EXTMOD))) # KBUILD_EXTMOD might be a relative path. Remember its absolute path before # Make changes the working directory. export abs_extmodtree := $(realpath $(KBUILD_EXTMOD))Signature · 1 line
2.43.0
Signature · 1 line
Nicolas
Quoted 10 lines · Masahiro Yamada
There has been a long-standing request to support building external modules in a separate build directory.
The first half is cleanups of documents and Makefiles.
The last part adds
KBUILD_EXTMOD_OUTPUT(MO=). This is too big changes, and too late for the current MW. (I did not test kselftest at all.)
I hope people test this and may uncover some issues.
thanks again for the whole series. I really appreciated to go through this patch set and am sorry for that it took so long. I have tested only with some in-tree kmods and with kmods' testsuite modules (but only on amd64) and could not find any major issue, only the minor things reported. I also did not test the kselftest or similar.
Kind regards,
Nicolas
Quoted 8 lines · Nicolas Schier
Documentation/kbuild/modules.rst executed to make module versioning work. · line 113
Quoted 6 lines
Documentation/kbuild/modules.rst +2
-0@@ -109,6 +109,9 @@ executed to make module versioning work. directory where the external module (kbuild file) is located. + MO=$BUILD_DIR + Speficies a separate output directory for the external module.s/Speficies/Specifies/
Thanks, I will fix.
Quoted 13 lines
Quoted 13 lines
Documentation/kbuild/modules.rst +1
-0+ 2.3 Targets ===========Makefile +0
-0@@ -134,6 +134,10 @@ ifeq ("$(origin M)", "command line") KBUILD_EXTMOD := $(M) endif
Quoted 8 lines
Makefile ifeq ("$(origin M)", "command line") · line 142
Quoted 6 lines
Makefile +4
-0+ifeq ("$(origin MO)", "command line") + KBUILD_EXTMOD_OUTPUT := $(MO) +endif + $(if $(word 2, $(KBUILD_EXTMOD)), \ $(error building multiple external modules is not supported))Should we also check against multiple output directories?
I do not think so.
When a directory path contains a space,
it should be treated as-is, in general.
However, GNU Make is not good at handling spaces in files paths.
So, this will fail eventually (with a little more obscure way).
$ make O=build M=fs/btrfs MO='/tmp/AA BB/btffs' Makefile:214: *** failed to create output directory "/tmp/AA BB/btffs". Stop.
Quoted 5 lines
Makefile else · line 274
Quoted 2 lines
Makefile +1
-1-export building_out_of_srctree srctree VPATH +ifeq ($(realpath $(_vpath)),$(CURDIR))Just a style consistency question: 'ifeq (,)' with a space after ',' (as a few lines above) or without as used here?
Maybe, we should keep consistency.
But, this line will be removed in v2.
--
Best Regards
Masahiro Yamada
Quoted 24 lines · Nicolas Schier
Quoted 15 lines
Quoted 9 lines
Makefile +1
-0@@ -640,6 +648,7 @@ quiet_cmd_makefile = GEN Makefile } > Makefile outputmakefile: +ifeq ($(KBUILD_EXTMOD),) @if [ -f $(srctree)/.config -o \ -d $(srctree)/include/config -o \ -d $(srctree)/arch/$(SRCARCH)/include/generated ]; then \Makefile outputmakefile: · line 662
Quoted 6 lines
Makefile +2
-1@@ -649,7 +658,16 @@ outputmakefile: echo >&2 "***"; \ false; \ fi - $(Q)ln -fsn $(srctree) source +else + @if [ -f $(KBUILD_EXTMOD)/modules.order ]; then \If I do
make O=build
touchfs/btrfs/modules.order
make O=build M=fs/btrfs/ MO=/tmp/btrfsCONFIG_BTRFS_FS=mthe 'modules.order' check does not work correctly. But a consequtive
Quoted 7 lines
make -C
/tmp/btrfsCONFIG_BTRFS_FS=mshows the 'The external module source tree is not clean.' message as expected.
If I replace $(KBUILD_EXTMOD) by $(abs_extmodtree), it works for me also in the first case.
Right, this check does not work correctly
when KBUILD_EXTMOD is a relative path.
In v2, I changed this part.
--
Best Regards
Masahiro Yamada
Re: [PATCH 18/23] kbuild: remove extmod_prefix, MODORDER, MODULES_NSDEPS variables
In reply to Nicolas SchierQuoted 21 lines · Nicolas Schier
Quoted 7 lines · Masahiro Yamada
With the previous changes, $(extmod_prefix), $(MODORDER), and $(MODULES_NSDEPS) are constant. (empty, modules.order, and modules.nsdeps, respectively).
Remove these variables and hard-code their values.
Signed-off-by Masahiro Yamada <[email protected]>Quoted 8 lines
---
Makefile | 22 +++++++++------------- scripts/Makefile.modfinal | 2 +- scripts/Makefile.modinst | 2 +- scripts/Makefile.modpost | 6 +++--- scripts/nsdeps | 2 +- 5 files changed, 15 insertions(+), 19 deletions(-)Do you want to remove these also? I can't think of a reason to keep these around:
Quoted 7 lines
$ git grep -Hrnwe extmod_prefix -e MODORDER -e MODULES_NSDEP
Makefile:1912: $(Q){ $(foreach m, $(single-ko), echo $(extmod_prefix)$(m:%.ko=%.o);) } > modules.order
scripts/Makefile.modfinal:33:$(extmod_prefix).module-common.o: $(srctree)/scripts/module-common.c FORCE
scripts/Makefile.modfinal:60:%.ko: %.o %.mod.o $(extmod_prefix).module-common.o $(objtree)/scripts/module.lds $(and $(CONFIG_DEBUG_INFO_BTF_MODULES),$(KBUILD_BUILTIN),$(objtree)/vmlinux) FORCE
scripts/Makefile.modfinal:66:targets += $(modules:%.o=%.ko) $(modules:%.o=%.mod.o) $(extmod_prefix).module-common.o
scripts/Makefile.modinst:60:modules := $(patsubst $(extmod_prefix)%.o, $(dst)/%.ko$(suffix-y), $(modules))
scripts/Makefile.modinst:120:$(dst)/%.ko: $(extmod_prefix)%.ko FORCERight, I will clean up these.
Quoted 3 lines
W/ or w/o:
Reviewed-by Nicolas Schier <[email protected]>
--
Best Regards
Masahiro Yamada
Re: [PATCH 21/23] kbuild: use absolute path in the generated wrapper Makefile
In reply to Masahiro YamadaHi Masahiro,
Quoted 2 lines · Masahiro Yamada
Keep the consistent behavior when this
Makefileis invoked from another directory.
This breaks building in a chroot for me. I usually compile the kernel on my host and then use some tooling to just run the "package" step of an Alpine kernel package definition. See
Since this requires mounting the source directory into a chroot, and then symlinking the output directory to the package "src" dir. It relies on the fact that make can be run from the output directory and implicitly relies on the include being relative since the absolute paths don't map inside the chroot.
I'm not sure if breakages like this justify a revert, but I noticed this issue in -next the other day and thought it was at least worth reporting.
I wouldn't be surprised if other folks with other build systems get bitten by this too.
Kind regards,
Caleb
Quoted 23 lines
Signed-off-by Masahiro Yamada <[email protected]>---
Makefile | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)Makefile +3
-2@@ -643,8 +643,9 @@ ifdef building_out_of_srctree quiet_cmd_makefile = GEN Makefile cmd_makefile = { \ - echo "\# Automatically generated by $(srctree)/Makefile: don't edit"; \ - echo "include $(srctree)/Makefile"; \ + echo "\# Automatically generated by $(abs_srctree)/Makefile: don't edit"; \ + echo "export KBUILD_OUTPUT = $(CURDIR)"; \ + echo "include $(abs_srctree)/Makefile"; \ } > Makefile outputmakefile:
Re: [PATCH 21/23] kbuild: use absolute path in the generated wrapper Makefile
Quoted 11 lines · Caleb Connolly
Hi Masahiro,
Quoted 2 lines · Masahiro Yamada
Keep the consistent behavior when this
Makefileis invoked from another directory.This breaks building in a chroot for me. I usually compile the kernel on my host and then use some tooling to just run the "package" step of an Alpine kernel package definition. See
Quoted 7 lines
Since this requires mounting the source directory into a chroot, and then symlinking the output directory to the package "src" dir. It relies on the fact that make can be run from the output directory and implicitly relies on the include being relative since the absolute paths don't map inside the chroot.
I do not understand what is happening in your workflow, but presumably you need to come up with a solution on your end (or in the pmos community).
- Kbuild never promised that $(srctree) is always a relative path. $(srctree) is relative only when building in-tree (srctree=.) or when the output directory is a sub-directory of the source tree.
(srctree=..) For other cases, $(srctree) is an absolute path.
Therefore, "include directive with an absolute path" is not something new.
- The wrapper Makefile generated in the output directory is a bonus convenience for humans. It is not even required to build the kernel from scripts.
Your report seems like a jungle of a chroot and mounts with a symlink. I guess it just happened to work, relying on something Kbuild did not support.
Quoted 3 lines
I'm not sure if breakages like this justify a revert, but I noticed this issue in -next the other day and thought it was at least worth reporting.
Thanks for the report, but I do not think this is worth a revert for the above reasons.
Quoted 29 lines
I wouldn't be surprised if other folks with other build systems get bitten by this too.
Kind regards,
CalebQuoted 23 lines
Signed-off-by Masahiro Yamada <[email protected]>---
Makefile | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)Makefile +3
-2@@ -643,8 +643,9 @@ ifdef building_out_of_srctree quiet_cmd_makefile = GEN Makefile cmd_makefile = { \ - echo "\# Automatically generated by $(srctree)/Makefile: don't edit"; \ - echo "include $(srctree)/Makefile"; \ + echo "\# Automatically generated by $(abs_srctree)/Makefile: don't edit"; \ + echo "export KBUILD_OUTPUT = $(CURDIR)"; \ + echo "include $(abs_srctree)/Makefile"; \ } > Makefile outputmakefile:
Signature · 2 lines
Best Regards Masahiro Yamada