rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 00/23] kbuild: support building external modules in a separate build directory
@ 2024-09-17 14:16 Masahiro Yamada
  2024-09-17 14:16 ` [PATCH 01/23] kbuild: doc: update the description about Kbuild/Makefile split Masahiro Yamada
                   ` (25 more replies)
  0 siblings, 26 replies; 69+ messages in thread
From: Masahiro Yamada @ 2024-09-17 14:16 UTC (permalink / raw)
  To: linux-kbuild
  Cc: Miguel Ojeda, rust-for-linux, linux-kernel, Nathan Chancellor,
	Nicolas Schier, 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 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(-)

-- 
2.43.0


^ permalink raw reply	[flat|nested] 69+ messages in thread

* [PATCH 01/23] kbuild: doc: update the description about Kbuild/Makefile split
  2024-09-17 14:16 [PATCH 00/23] kbuild: support building external modules in a separate build directory Masahiro Yamada
@ 2024-09-17 14:16 ` Masahiro Yamada
  2024-09-18 15:35   ` Nicolas Schier
  2024-09-17 14:16 ` [PATCH 02/23] kbuild: doc: remove description about grepping CONFIG options Masahiro Yamada
                   ` (24 subsequent siblings)
  25 siblings, 1 reply; 69+ messages in thread
From: Masahiro Yamada @ 2024-09-17 14:16 UTC (permalink / raw)
  To: linux-kbuild
  Cc: Miguel Ojeda, rust-for-linux, linux-kernel, Nathan Chancellor,
	Nicolas Schier, 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.

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.

[1]: https://git.kernel.org/pub/scm/linux/kernel/git/history/history.git/commit/?id=65e433436b5794ae056d22ddba60fe9194bba007

Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
---

 Documentation/kbuild/modules.rst | 38 +++-----------------------------
 1 file changed, 3 insertions(+), 35 deletions(-)

diff --git a/Documentation/kbuild/modules.rst b/Documentation/kbuild/modules.rst
index 131863142cbb..1014a275a7e1 100644
--- a/Documentation/kbuild/modules.rst
+++ b/Documentation/kbuild/modules.rst
@@ -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
 ----------------
 
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 69+ messages in thread

* [PATCH 02/23] kbuild: doc: remove description about grepping CONFIG options
  2024-09-17 14:16 [PATCH 00/23] kbuild: support building external modules in a separate build directory Masahiro Yamada
  2024-09-17 14:16 ` [PATCH 01/23] kbuild: doc: update the description about Kbuild/Makefile split Masahiro Yamada
@ 2024-09-17 14:16 ` Masahiro Yamada
  2024-09-18 15:35   ` Nicolas Schier
  2024-09-17 14:16 ` [PATCH 03/23] kbuild: doc: remove outdated description of the limitation on -I usage Masahiro Yamada
                   ` (23 subsequent siblings)
  25 siblings, 1 reply; 69+ messages in thread
From: Masahiro Yamada @ 2024-09-17 14:16 UTC (permalink / raw)
  To: linux-kbuild
  Cc: Miguel Ojeda, rust-for-linux, linux-kernel, Nathan Chancellor,
	Nicolas Schier, Masahiro Yamada

This description was added 20 years ago [1]. It does not convey any
useful information except for a feeling of nostalgia.

[1]: https://git.kernel.org/pub/scm/linux/kernel/git/history/history.git/commit/?id=65e433436b5794ae056d22ddba60fe9194bba007

Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
---

 Documentation/kbuild/modules.rst | 6 ------
 1 file changed, 6 deletions(-)

diff --git a/Documentation/kbuild/modules.rst b/Documentation/kbuild/modules.rst
index 1014a275a7e1..268ebbd2ab74 100644
--- a/Documentation/kbuild/modules.rst
+++ b/Documentation/kbuild/modules.rst
@@ -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.
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 69+ messages in thread

* [PATCH 03/23] kbuild: doc: remove outdated description of the limitation on -I usage
  2024-09-17 14:16 [PATCH 00/23] kbuild: support building external modules in a separate build directory Masahiro Yamada
  2024-09-17 14:16 ` [PATCH 01/23] kbuild: doc: update the description about Kbuild/Makefile split Masahiro Yamada
  2024-09-17 14:16 ` [PATCH 02/23] kbuild: doc: remove description about grepping CONFIG options Masahiro Yamada
@ 2024-09-17 14:16 ` Masahiro Yamada
  2024-09-18 15:38   ` Nicolas Schier
  2024-09-17 14:16 ` [PATCH 04/23] kbuild: doc: remove the description about shipped files Masahiro Yamada
                   ` (22 subsequent siblings)
  25 siblings, 1 reply; 69+ messages in thread
From: Masahiro Yamada @ 2024-09-17 14:16 UTC (permalink / raw)
  To: linux-kbuild
  Cc: Miguel Ojeda, rust-for-linux, linux-kernel, Nathan Chancellor,
	Nicolas Schier, Masahiro Yamada

Kbuild 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)

Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
---

 Documentation/kbuild/modules.rst | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)

diff --git a/Documentation/kbuild/modules.rst b/Documentation/kbuild/modules.rst
index 268ebbd2ab74..7eceb9a65e9c 100644
--- a/Documentation/kbuild/modules.rst
+++ b/Documentation/kbuild/modules.rst
@@ -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
 --------------------------
 
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 69+ messages in thread

* [PATCH 04/23] kbuild: doc: remove the description about shipped files
  2024-09-17 14:16 [PATCH 00/23] kbuild: support building external modules in a separate build directory Masahiro Yamada
                   ` (2 preceding siblings ...)
  2024-09-17 14:16 ` [PATCH 03/23] kbuild: doc: remove outdated description of the limitation on -I usage Masahiro Yamada
@ 2024-09-17 14:16 ` Masahiro Yamada
  2024-09-18 16:24   ` Nicolas Schier
  2024-09-17 14:16 ` [PATCH 05/23] kbuild: doc: describe the -C option precisely for external module builds Masahiro Yamada
                   ` (21 subsequent siblings)
  25 siblings, 1 reply; 69+ messages in thread
From: Masahiro Yamada @ 2024-09-17 14:16 UTC (permalink / raw)
  To: linux-kbuild
  Cc: Miguel Ojeda, rust-for-linux, linux-kernel, Nathan Chancellor,
	Nicolas Schier, 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/

Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
---

 Documentation/kbuild/modules.rst | 35 +++-----------------------------
 1 file changed, 3 insertions(+), 32 deletions(-)

diff --git a/Documentation/kbuild/modules.rst b/Documentation/kbuild/modules.rst
index 7eceb9a65e9c..1afa6b1b4090 100644
--- a/Documentation/kbuild/modules.rst
+++ b/Documentation/kbuild/modules.rst
@@ -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
 --------------------------
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 69+ messages in thread

* [PATCH 05/23] kbuild: doc: describe the -C option precisely for external module builds
  2024-09-17 14:16 [PATCH 00/23] kbuild: support building external modules in a separate build directory Masahiro Yamada
                   ` (3 preceding siblings ...)
  2024-09-17 14:16 ` [PATCH 04/23] kbuild: doc: remove the description about shipped files Masahiro Yamada
@ 2024-09-17 14:16 ` Masahiro Yamada
  2024-09-18 16:44   ` Nicolas Schier
  2024-09-18 16:47   ` Nicolas Schier
  2024-09-17 14:16 ` [PATCH 06/23] kbuild: doc: replace "gcc" in external module description Masahiro Yamada
                   ` (20 subsequent siblings)
  25 siblings, 2 replies; 69+ messages in thread
From: Masahiro Yamada @ 2024-09-17 14:16 UTC (permalink / raw)
  To: linux-kbuild
  Cc: Miguel Ojeda, rust-for-linux, linux-kernel, Nathan Chancellor,
	Nicolas Schier, 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, 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.

[1]: https://git.kernel.org/pub/scm/linux/kernel/git/history/history.git/commit/?id=e321b2ec2eb2993b3d0116e5163c78ad923e3c54

Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
---

 Documentation/kbuild/kbuild.rst  | 5 +++++
 Documentation/kbuild/modules.rst | 9 ++++++---
 2 files changed, 11 insertions(+), 3 deletions(-)

diff --git a/Documentation/kbuild/kbuild.rst b/Documentation/kbuild/kbuild.rst
index 9c8d1d046ea5..716f6fb70829 100644
--- a/Documentation/kbuild/kbuild.rst
+++ b/Documentation/kbuild/kbuild.rst
@@ -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.
diff --git a/Documentation/kbuild/modules.rst b/Documentation/kbuild/modules.rst
index 1afa6b1b4090..a80bff699e77 100644
--- a/Documentation/kbuild/modules.rst
+++ b/Documentation/kbuild/modules.rst
@@ -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.
 
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 69+ messages in thread

* [PATCH 06/23] kbuild: doc: replace "gcc" in external module description
  2024-09-17 14:16 [PATCH 00/23] kbuild: support building external modules in a separate build directory Masahiro Yamada
                   ` (4 preceding siblings ...)
  2024-09-17 14:16 ` [PATCH 05/23] kbuild: doc: describe the -C option precisely for external module builds Masahiro Yamada
@ 2024-09-17 14:16 ` Masahiro Yamada
  2024-09-19 11:22   ` Nicolas Schier
  2024-09-17 14:16 ` [PATCH 07/23] kbuild: remove unnecessary prune of rust/alloc for rustfmt Masahiro Yamada
                   ` (19 subsequent siblings)
  25 siblings, 1 reply; 69+ messages in thread
From: Masahiro Yamada @ 2024-09-17 14:16 UTC (permalink / raw)
  To: linux-kbuild
  Cc: Miguel Ojeda, rust-for-linux, linux-kernel, Nathan Chancellor,
	Nicolas Schier, Masahiro Yamada

Avoid "gcc" since it is not the only compiler supported by Kbuild.

Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
---

 Documentation/kbuild/modules.rst | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/Documentation/kbuild/modules.rst b/Documentation/kbuild/modules.rst
index a80bff699e77..3a6e7bdc0889 100644
--- a/Documentation/kbuild/modules.rst
+++ b/Documentation/kbuild/modules.rst
@@ -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
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 69+ messages in thread

* [PATCH 07/23] kbuild: remove unnecessary prune of rust/alloc for rustfmt
  2024-09-17 14:16 [PATCH 00/23] kbuild: support building external modules in a separate build directory Masahiro Yamada
                   ` (5 preceding siblings ...)
  2024-09-17 14:16 ` [PATCH 06/23] kbuild: doc: replace "gcc" in external module description Masahiro Yamada
@ 2024-09-17 14:16 ` Masahiro Yamada
  2024-09-19 11:23   ` Nicolas Schier
  2024-09-17 14:16 ` [PATCH 08/23] kbuild: simplify find command " Masahiro Yamada
                   ` (18 subsequent siblings)
  25 siblings, 1 reply; 69+ messages in thread
From: Masahiro Yamada @ 2024-09-17 14:16 UTC (permalink / raw)
  To: linux-kbuild
  Cc: Miguel Ojeda, rust-for-linux, linux-kernel, Nathan Chancellor,
	Nicolas Schier, Masahiro Yamada

Commit 9d0441bab775 ("rust: alloc: remove our fork of the `alloc`
crate") removed the rust/alloc/ directory.

Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
---

 Makefile | 2 --
 1 file changed, 2 deletions(-)

diff --git a/Makefile b/Makefile
index 35b8392d2bef..5b16e0605a77 100644
--- a/Makefile
+++ b/Makefile
@@ -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)
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 69+ messages in thread

* [PATCH 08/23] kbuild: simplify find command for rustfmt
  2024-09-17 14:16 [PATCH 00/23] kbuild: support building external modules in a separate build directory Masahiro Yamada
                   ` (6 preceding siblings ...)
  2024-09-17 14:16 ` [PATCH 07/23] kbuild: remove unnecessary prune of rust/alloc for rustfmt Masahiro Yamada
@ 2024-09-17 14:16 ` Masahiro Yamada
  2024-09-19 13:54   ` Nicolas Schier
  2024-09-30 18:37   ` Miguel Ojeda
  2024-09-17 14:16 ` [PATCH 09/23] speakup: use SPKDIR=$(src) to specify the source directory Masahiro Yamada
                   ` (17 subsequent siblings)
  25 siblings, 2 replies; 69+ messages in thread
From: Masahiro Yamada @ 2024-09-17 14:16 UTC (permalink / raw)
  To: linux-kbuild
  Cc: Miguel Ojeda, rust-for-linux, linux-kernel, Nathan Chancellor,
	Nicolas Schier, Masahiro Yamada

The 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/test

With 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.

Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
---

 Makefile | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/Makefile b/Makefile
index 5b16e0605a77..4992b2895dd5 100644
--- a/Makefile
+++ b/Makefile
@@ -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)
 
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 69+ messages in thread

* [PATCH 09/23] speakup: use SPKDIR=$(src) to specify the source directory
  2024-09-17 14:16 [PATCH 00/23] kbuild: support building external modules in a separate build directory Masahiro Yamada
                   ` (7 preceding siblings ...)
  2024-09-17 14:16 ` [PATCH 08/23] kbuild: simplify find command " Masahiro Yamada
@ 2024-09-17 14:16 ` Masahiro Yamada
  2024-10-03 18:25   ` Nicolas Schier
  2024-09-17 14:16 ` [PATCH 10/23] kbuild: refactor the check for missing config files Masahiro Yamada
                   ` (16 subsequent siblings)
  25 siblings, 1 reply; 69+ messages in thread
From: Masahiro Yamada @ 2024-09-17 14:16 UTC (permalink / raw)
  To: linux-kbuild
  Cc: Miguel Ojeda, rust-for-linux, linux-kernel, Nathan Chancellor,
	Nicolas Schier, 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 <masahiroy@kernel.org>
---

 drivers/accessibility/speakup/Makefile | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

diff --git a/drivers/accessibility/speakup/Makefile b/drivers/accessibility/speakup/Makefile
index 6f6a83565c0d..14ba1cca87f4 100644
--- a/drivers/accessibility/speakup/Makefile
+++ b/drivers/accessibility/speakup/Makefile
@@ -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)
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 69+ messages in thread

* [PATCH 10/23] kbuild: refactor the check for missing config files
  2024-09-17 14:16 [PATCH 00/23] kbuild: support building external modules in a separate build directory Masahiro Yamada
                   ` (8 preceding siblings ...)
  2024-09-17 14:16 ` [PATCH 09/23] speakup: use SPKDIR=$(src) to specify the source directory Masahiro Yamada
@ 2024-09-17 14:16 ` Masahiro Yamada
  2024-09-25 14:04   ` Nicolas Schier
  2024-09-17 14:16 ` [PATCH 11/23] kbuild: check the presence of include/generated/rustc_cfg Masahiro Yamada
                   ` (15 subsequent siblings)
  25 siblings, 1 reply; 69+ messages in thread
From: Masahiro Yamada @ 2024-09-17 14:16 UTC (permalink / raw)
  To: linux-kbuild
  Cc: Miguel Ojeda, rust-for-linux, linux-kernel, Nathan Chancellor,
	Nicolas Schier, 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.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.
  ***

Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
---

 Makefile | 21 +++++++++++++--------
 1 file changed, 13 insertions(+), 8 deletions(-)

diff --git a/Makefile b/Makefile
index 4992b2895dd5..3f5c273c2901 100644
--- a/Makefile
+++ b/Makefile
@@ -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
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 69+ messages in thread

* [PATCH 11/23] kbuild: check the presence of include/generated/rustc_cfg
  2024-09-17 14:16 [PATCH 00/23] kbuild: support building external modules in a separate build directory Masahiro Yamada
                   ` (9 preceding siblings ...)
  2024-09-17 14:16 ` [PATCH 10/23] kbuild: refactor the check for missing config files Masahiro Yamada
@ 2024-09-17 14:16 ` Masahiro Yamada
  2024-09-25 14:10   ` Nicolas Schier
  2024-09-30 14:37   ` Miguel Ojeda
  2024-09-17 14:16 ` [PATCH 12/23] scripts/nsdeps: use VPATH as src_prefix Masahiro Yamada
                   ` (14 subsequent siblings)
  25 siblings, 2 replies; 69+ messages in thread
From: Masahiro Yamada @ 2024-09-17 14:16 UTC (permalink / raw)
  To: linux-kbuild
  Cc: Miguel Ojeda, rust-for-linux, linux-kernel, Nathan Chancellor,
	Nicolas Schier, Masahiro Yamada

Since 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.

Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
---

 Makefile | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/Makefile b/Makefile
index 3f5c273c2901..5700d08c9b57 100644
--- a/Makefile
+++ b/Makefile
@@ -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
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 69+ messages in thread

* [PATCH 12/23] scripts/nsdeps: use VPATH as src_prefix
  2024-09-17 14:16 [PATCH 00/23] kbuild: support building external modules in a separate build directory Masahiro Yamada
                   ` (10 preceding siblings ...)
  2024-09-17 14:16 ` [PATCH 11/23] kbuild: check the presence of include/generated/rustc_cfg Masahiro Yamada
@ 2024-09-17 14:16 ` Masahiro Yamada
  2024-09-25 14:18   ` Nicolas Schier
  2024-09-17 14:16 ` [PATCH 13/23] kbuild: replace two $(abs_objtree) with $(CURDIR) in top Makefile Masahiro Yamada
                   ` (13 subsequent siblings)
  25 siblings, 1 reply; 69+ messages in thread
From: Masahiro Yamada @ 2024-09-17 14:16 UTC (permalink / raw)
  To: linux-kbuild
  Cc: Miguel Ojeda, rust-for-linux, linux-kernel, Nathan Chancellor,
	Nicolas Schier, 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 <masahiroy@kernel.org>
---

 scripts/nsdeps | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/scripts/nsdeps b/scripts/nsdeps
index f1718cc0d700..8b4db63838ce 100644
--- a/scripts/nsdeps
+++ b/scripts/nsdeps
@@ -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() {
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 69+ messages in thread

* [PATCH 13/23] kbuild: replace two $(abs_objtree) with $(CURDIR) in top Makefile
  2024-09-17 14:16 [PATCH 00/23] kbuild: support building external modules in a separate build directory Masahiro Yamada
                   ` (11 preceding siblings ...)
  2024-09-17 14:16 ` [PATCH 12/23] scripts/nsdeps: use VPATH as src_prefix Masahiro Yamada
@ 2024-09-17 14:16 ` Masahiro Yamada
  2024-09-25 14:27   ` Nicolas Schier
  2024-09-17 14:16 ` [PATCH 14/23] kbuild: add $(objtree)/ prefix to some in-kernel build artifacts Masahiro Yamada
                   ` (12 subsequent siblings)
  25 siblings, 1 reply; 69+ messages in thread
From: Masahiro Yamada @ 2024-09-17 14:16 UTC (permalink / raw)
  To: linux-kbuild
  Cc: Miguel Ojeda, rust-for-linux, linux-kernel, Nathan Chancellor,
	Nicolas Schier, Masahiro Yamada

Kbuild changes 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 <masahiroy@kernel.org>
---

 Makefile | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/Makefile b/Makefile
index 5700d08c9b57..ee9ad0f0960f 100644
--- a/Makefile
+++ b/Makefile
@@ -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
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 69+ messages in thread

* [PATCH 14/23] kbuild: add $(objtree)/ prefix to some in-kernel build artifacts
  2024-09-17 14:16 [PATCH 00/23] kbuild: support building external modules in a separate build directory Masahiro Yamada
                   ` (12 preceding siblings ...)
  2024-09-17 14:16 ` [PATCH 13/23] kbuild: replace two $(abs_objtree) with $(CURDIR) in top Makefile Masahiro Yamada
@ 2024-09-17 14:16 ` Masahiro Yamada
  2024-09-26  7:50   ` Nicolas Schier
  2024-09-17 14:16 ` [PATCH 15/23] kbuild: rename abs_objtree to abs_output Masahiro Yamada
                   ` (11 subsequent siblings)
  25 siblings, 1 reply; 69+ messages in thread
From: Masahiro Yamada @ 2024-09-17 14:16 UTC (permalink / raw)
  To: linux-kbuild
  Cc: Miguel Ojeda, rust-for-linux, linux-kernel, Nathan Chancellor,
	Nicolas Schier, 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.

Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
---

 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(-)

diff --git a/Makefile b/Makefile
index ee9ad0f0960f..c923bea7043b 100644
--- a/Makefile
+++ b/Makefile
@@ -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)
diff --git a/scripts/Kbuild.include b/scripts/Kbuild.include
index ed8a7493524b..8c311b997e24 100644
--- a/scripts/Kbuild.include
+++ b/scripts/Kbuild.include
@@ -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)
diff --git a/scripts/Makefile.build b/scripts/Makefile.build
index 8403eba15457..6e8954cbf53a 100644
--- a/scripts/Makefile.build
+++ b/scripts/Makefile.build
@@ -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)
diff --git a/scripts/Makefile.modfinal b/scripts/Makefile.modfinal
index 1482884ec3ca..6d8aa3059ee2 100644
--- a/scripts/Makefile.modfinal
+++ b/scripts/Makefile.modfinal
@@ -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
diff --git a/scripts/Makefile.modinst b/scripts/Makefile.modinst
index 4d81ed9af294..2d39d452c49e 100644
--- a/scripts/Makefile.modinst
+++ b/scripts/Makefile.modinst
@@ -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 :=
diff --git a/scripts/Makefile.modpost b/scripts/Makefile.modpost
index 44936ebad161..12e7c15d099c 100644
--- a/scripts/Makefile.modpost
+++ b/scripts/Makefile.modpost
@@ -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))
diff --git a/scripts/depmod.sh b/scripts/depmod.sh
index e22da27fe13e..3c34fecacbc8 100755
--- a/scripts/depmod.sh
+++ b/scripts/depmod.sh
@@ -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
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 69+ messages in thread

* [PATCH 15/23] kbuild: rename abs_objtree to abs_output
  2024-09-17 14:16 [PATCH 00/23] kbuild: support building external modules in a separate build directory Masahiro Yamada
                   ` (13 preceding siblings ...)
  2024-09-17 14:16 ` [PATCH 14/23] kbuild: add $(objtree)/ prefix to some in-kernel build artifacts Masahiro Yamada
@ 2024-09-17 14:16 ` Masahiro Yamada
  2024-09-26  7:51   ` Nicolas Schier
  2024-09-17 14:16 ` [PATCH 16/23] kbuild: use 'output' variable to create the output directory Masahiro Yamada
                   ` (10 subsequent siblings)
  25 siblings, 1 reply; 69+ messages in thread
From: Masahiro Yamada @ 2024-09-17 14:16 UTC (permalink / raw)
  To: linux-kbuild
  Cc: Miguel Ojeda, rust-for-linux, linux-kernel, Nathan Chancellor,
	Nicolas Schier, 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.

Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
---

 Makefile | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/Makefile b/Makefile
index c923bea7043b..254f05a1cc0f 100644
--- a/Makefile
+++ b/Makefile
@@ -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
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 69+ messages in thread

* [PATCH 16/23] kbuild: use 'output' variable to create the output directory
  2024-09-17 14:16 [PATCH 00/23] kbuild: support building external modules in a separate build directory Masahiro Yamada
                   ` (14 preceding siblings ...)
  2024-09-17 14:16 ` [PATCH 15/23] kbuild: rename abs_objtree to abs_output Masahiro Yamada
@ 2024-09-17 14:16 ` Masahiro Yamada
  2024-09-26  7:54   ` Nicolas Schier
  2024-09-17 14:16 ` [PATCH 17/23] kbuild: build external modules in their directory Masahiro Yamada
                   ` (9 subsequent siblings)
  25 siblings, 1 reply; 69+ messages in thread
From: Masahiro Yamada @ 2024-09-17 14:16 UTC (permalink / raw)
  To: linux-kbuild
  Cc: Miguel Ojeda, rust-for-linux, linux-kernel, Nathan Chancellor,
	Nicolas Schier, 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 <masahiroy@kernel.org>
---

 Makefile | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)

diff --git a/Makefile b/Makefile
index 254f05a1cc0f..7a76452049ea 100644
--- a/Makefile
+++ b/Makefile
@@ -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)
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 69+ messages in thread

* [PATCH 17/23] kbuild: build external modules in their directory
  2024-09-17 14:16 [PATCH 00/23] kbuild: support building external modules in a separate build directory Masahiro Yamada
                   ` (15 preceding siblings ...)
  2024-09-17 14:16 ` [PATCH 16/23] kbuild: use 'output' variable to create the output directory Masahiro Yamada
@ 2024-09-17 14:16 ` Masahiro Yamada
  2024-10-03 18:41   ` Nicolas Schier
  2024-09-17 14:16 ` [PATCH 18/23] kbuild: remove extmod_prefix, MODORDER, MODULES_NSDEPS variables Masahiro Yamada
                   ` (8 subsequent siblings)
  25 siblings, 1 reply; 69+ messages in thread
From: Masahiro Yamada @ 2024-09-17 14:16 UTC (permalink / raw)
  To: linux-kbuild
  Cc: Miguel Ojeda, rust-for-linux, linux-kernel, Nathan Chancellor,
	Nicolas Schier, Masahiro Yamada

Currently, Kbuild always operates in the output directory of the kernel,
even when building external modules. This increases the risk of external
module Makefiles attempting to write to the kernel directory.

This commit switches the working directory to the external module
directory, allowing the removal of the $(KBUILD_EXTMOD)/ prefix from
some build artifacts.

The command for building external modules maintains backward
compatibility, but Makefiles that rely on working in the kernel
directory may break. In such cases, $(objtree) and $(srctree) should
be used to refer to the output and source directories of the kernel.

The appearance of the build log will change as follows:

[Before]

  $ make -C /path/to/my/linux M=/path/to/my/externel/module
  make: Entering directory '/path/to/my/linux'
    CC [M]  /path/to/my/externel/module/helloworld.o
    MODPOST /path/to/my/externel/module/Module.symvers
    CC [M]  /path/to/my/externel/module/helloworld.mod.o
    CC [M]  /path/to/my/externel/module/.module-common.o
    LD [M]  /path/to/my/externel/module/helloworld.ko
  make: Leaving directory '/path/to/my/linux'

[After]

  $ make -C /path/to/my/linux M=/path/to/my/externel/module
  make: Entering directory '/path/to/my/linux'
  make[1]: Entering directory '/path/to/my/externel/module'
    CC [M]  helloworld.o
    MODPOST Module.symvers
    CC [M]  helloworld.mod.o
    CC [M]  .module-common.o
    LD [M]  helloworld.ko
  make[1]: Leaving directory '/path/to/my/externel/module'
  make: Leaving directory '/path/to/my/linux'

Printing "Entering directory" twice is cumbersome. This will be
addressed later.

Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
---

 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(-)

diff --git a/Documentation/dev-tools/coccinelle.rst b/Documentation/dev-tools/coccinelle.rst
index 535ce126fb4f..80c83ce0babc 100644
--- a/Documentation/dev-tools/coccinelle.rst
+++ b/Documentation/dev-tools/coccinelle.rst
@@ -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,
diff --git a/Makefile b/Makefile
index 7a76452049ea..4db22c3a8555 100644
--- a/Makefile
+++ b/Makefile
@@ -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
 # ---------------------------------------------------------------------------
diff --git a/rust/Makefile b/rust/Makefile
index 8de3ebba9551..8a686ffbd3a2 100644
--- a/rust/Makefile
+++ b/rust/Makefile
@@ -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 \
diff --git a/scripts/Makefile.compiler b/scripts/Makefile.compiler
index 92be0c9a13ee..4b80b525d386 100644
--- a/scripts/Makefile.compiler
+++ b/scripts/Makefile.compiler
@@ -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)
diff --git a/scripts/Makefile.modpost b/scripts/Makefile.modpost
index 12e7c15d099c..ccca705bc389 100644
--- a/scripts/Makefile.modpost
+++ b/scripts/Makefile.modpost
@@ -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
diff --git a/scripts/coccicheck b/scripts/coccicheck
index e52cb43fede6..6272fe4814cd 100755
--- a/scripts/coccicheck
+++ b/scripts/coccicheck
@@ -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
diff --git a/scripts/package/install-extmod-build b/scripts/package/install-extmod-build
index d2c9cacecc0c..5d7d79a23bf9 100755
--- a/scripts/package/install-extmod-build
+++ b/scripts/package/install-extmod-build
@@ -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
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 69+ messages in thread

* [PATCH 18/23] kbuild: remove extmod_prefix, MODORDER, MODULES_NSDEPS variables
  2024-09-17 14:16 [PATCH 00/23] kbuild: support building external modules in a separate build directory Masahiro Yamada
                   ` (16 preceding siblings ...)
  2024-09-17 14:16 ` [PATCH 17/23] kbuild: build external modules in their directory Masahiro Yamada
@ 2024-09-17 14:16 ` Masahiro Yamada
  2024-10-03 18:47   ` Nicolas Schier
  2024-09-17 14:16 ` [PATCH 19/23] kbuild: support building external modules in a separate build directory Masahiro Yamada
                   ` (7 subsequent siblings)
  25 siblings, 1 reply; 69+ messages in thread
From: Masahiro Yamada @ 2024-09-17 14:16 UTC (permalink / raw)
  To: linux-kbuild
  Cc: Miguel Ojeda, rust-for-linux, linux-kernel, Nathan Chancellor,
	Nicolas Schier, 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 <masahiroy@kernel.org>
---

 Makefile                  | 22 +++++++++-------------
 scripts/Makefile.modfinal |  2 +-
 scripts/Makefile.modinst  |  2 +-
 scripts/Makefile.modpost  |  6 +++---
 scripts/nsdeps            |  2 +-
 5 files changed, 15 insertions(+), 19 deletions(-)

diff --git a/Makefile b/Makefile
index 4db22c3a8555..9fbf7ef6e394 100644
--- a/Makefile
+++ b/Makefile
@@ -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:
diff --git a/scripts/Makefile.modfinal b/scripts/Makefile.modfinal
index 6d8aa3059ee2..1d787b867c6d 100644
--- a/scripts/Makefile.modfinal
+++ b/scripts/Makefile.modfinal
@@ -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)
 	@:
diff --git a/scripts/Makefile.modinst b/scripts/Makefile.modinst
index 2d39d452c49e..1ec739d138b3 100644
--- a/scripts/Makefile.modinst
+++ b/scripts/Makefile.modinst
@@ -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
diff --git a/scripts/Makefile.modpost b/scripts/Makefile.modpost
index ccca705bc389..586456c72e6d 100644
--- a/scripts/Makefile.modpost
+++ b/scripts/Makefile.modpost
@@ -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),)
diff --git a/scripts/nsdeps b/scripts/nsdeps
index 8b4db63838ce..3192a3945871 100644
--- a/scripts/nsdeps
+++ b/scripts/nsdeps
@@ -57,4 +57,4 @@ generate_deps() {
 while read line
 do
 	generate_deps $line
-done < $MODULES_NSDEPS
+done < modules.nsdeps
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 69+ messages in thread

* [PATCH 19/23] kbuild: support building external modules in a separate build directory
  2024-09-17 14:16 [PATCH 00/23] kbuild: support building external modules in a separate build directory Masahiro Yamada
                   ` (17 preceding siblings ...)
  2024-09-17 14:16 ` [PATCH 18/23] kbuild: remove extmod_prefix, MODORDER, MODULES_NSDEPS variables Masahiro Yamada
@ 2024-09-17 14:16 ` Masahiro Yamada
  2024-10-03 19:47   ` Nicolas Schier
  2024-09-17 14:16 ` [PATCH 20/23] kbuild: support -fmacro-prefix-map for external modules Masahiro Yamada
                   ` (6 subsequent siblings)
  25 siblings, 1 reply; 69+ messages in thread
From: Masahiro Yamada @ 2024-09-17 14:16 UTC (permalink / raw)
  To: linux-kbuild
  Cc: Miguel Ojeda, rust-for-linux, linux-kernel, Nathan Chancellor,
	Nicolas Schier, 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 <masahiroy@kernel.org>
---

 Documentation/kbuild/kbuild.rst  |  8 +++++-
 Documentation/kbuild/modules.rst |  5 +++-
 Makefile                         | 44 +++++++++++++++++++++++---------
 3 files changed, 43 insertions(+), 14 deletions(-)

diff --git a/Documentation/kbuild/kbuild.rst b/Documentation/kbuild/kbuild.rst
index 716f6fb70829..66a9dc44ea28 100644
--- a/Documentation/kbuild/kbuild.rst
+++ b/Documentation/kbuild/kbuild.rst
@@ -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
diff --git a/Documentation/kbuild/modules.rst b/Documentation/kbuild/modules.rst
index 3a6e7bdc0889..03347e13eeb5 100644
--- a/Documentation/kbuild/modules.rst
+++ b/Documentation/kbuild/modules.rst
@@ -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
 ===========
 
diff --git a/Makefile b/Makefile
index 9fbf7ef6e394..b654baa0763a 100644
--- a/Makefile
+++ b/Makefile
@@ -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))
 
@@ -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
 
-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))
+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.
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 69+ messages in thread

* [PATCH 20/23] kbuild: support -fmacro-prefix-map for external modules
  2024-09-17 14:16 [PATCH 00/23] kbuild: support building external modules in a separate build directory Masahiro Yamada
                   ` (18 preceding siblings ...)
  2024-09-17 14:16 ` [PATCH 19/23] kbuild: support building external modules in a separate build directory Masahiro Yamada
@ 2024-09-17 14:16 ` Masahiro Yamada
  2024-10-04 18:51   ` Nicolas Schier
  2024-09-17 14:16 ` [PATCH 21/23] kbuild: use absolute path in the generated wrapper Makefile Masahiro Yamada
                   ` (5 subsequent siblings)
  25 siblings, 1 reply; 69+ messages in thread
From: Masahiro Yamada @ 2024-09-17 14:16 UTC (permalink / raw)
  To: linux-kbuild
  Cc: Miguel Ojeda, rust-for-linux, linux-kernel, Nathan Chancellor,
	Nicolas Schier, 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").

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 <masahiroy@kernel.org>
---

 Makefile | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/Makefile b/Makefile
index b654baa0763a..ce646a6994a6 100644
--- a/Makefile
+++ b/Makefile
@@ -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
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 69+ messages in thread

* [PATCH 21/23] kbuild: use absolute path in the generated wrapper Makefile
  2024-09-17 14:16 [PATCH 00/23] kbuild: support building external modules in a separate build directory Masahiro Yamada
                   ` (19 preceding siblings ...)
  2024-09-17 14:16 ` [PATCH 20/23] kbuild: support -fmacro-prefix-map for external modules Masahiro Yamada
@ 2024-09-17 14:16 ` Masahiro Yamada
  2024-10-04 19:02   ` Nicolas Schier
  2024-11-29 23:31   ` Caleb Connolly
  2024-09-17 14:16 ` [PATCH 22/23] kbuild: make wrapper Makefile more convenient for external modules Masahiro Yamada
                   ` (4 subsequent siblings)
  25 siblings, 2 replies; 69+ messages in thread
From: Masahiro Yamada @ 2024-09-17 14:16 UTC (permalink / raw)
  To: linux-kbuild
  Cc: Miguel Ojeda, rust-for-linux, linux-kernel, Nathan Chancellor,
	Nicolas Schier, Masahiro Yamada

Keep the consistent behavior when this Makefile is invoked from another
directory.

Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
---

 Makefile | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/Makefile b/Makefile
index ce646a6994a6..9f0ba07e8f25 100644
--- a/Makefile
+++ b/Makefile
@@ -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:
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 69+ messages in thread

* [PATCH 22/23] kbuild: make wrapper Makefile more convenient for external modules
  2024-09-17 14:16 [PATCH 00/23] kbuild: support building external modules in a separate build directory Masahiro Yamada
                   ` (20 preceding siblings ...)
  2024-09-17 14:16 ` [PATCH 21/23] kbuild: use absolute path in the generated wrapper Makefile Masahiro Yamada
@ 2024-09-17 14:16 ` Masahiro Yamada
  2024-10-04 19:46   ` Nicolas Schier
  2024-09-17 14:16 ` [PATCH 23/23] kbuild: allow to start building external module in any directory Masahiro Yamada
                   ` (3 subsequent siblings)
  25 siblings, 1 reply; 69+ messages in thread
From: Masahiro Yamada @ 2024-09-17 14:16 UTC (permalink / raw)
  To: linux-kbuild
  Cc: Miguel Ojeda, rust-for-linux, linux-kernel, Nathan Chancellor,
	Nicolas Schier, Masahiro Yamada

When 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

Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
---

 Makefile | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/Makefile b/Makefile
index 9f0ba07e8f25..81603a50c757 100644
--- a/Makefile
+++ b/Makefile
@@ -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
 
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 69+ messages in thread

* [PATCH 23/23] kbuild: allow to start building external module in any directory
  2024-09-17 14:16 [PATCH 00/23] kbuild: support building external modules in a separate build directory Masahiro Yamada
                   ` (21 preceding siblings ...)
  2024-09-17 14:16 ` [PATCH 22/23] kbuild: make wrapper Makefile more convenient for external modules Masahiro Yamada
@ 2024-09-17 14:16 ` Masahiro Yamada
  2024-10-04 20:05   ` Nicolas Schier
  2024-09-20 10:39 ` [PATCH 00/23] kbuild: support building external modules in a separate build directory Nicolas Schier
                   ` (2 subsequent siblings)
  25 siblings, 1 reply; 69+ messages in thread
From: Masahiro Yamada @ 2024-09-17 14:16 UTC (permalink / raw)
  To: linux-kbuild
  Cc: Miguel Ojeda, rust-for-linux, linux-kernel, Nathan Chancellor,
	Nicolas Schier, 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/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)/Makefile

This avoids unnecessary sub-make invocations:

    $ make
      CC [M]  helloworld.o
      MODPOST Module.symvers
      CC [M]  helloworld.mod.o
      CC [M]  .module-common.o
      LD [M]  helloworld.ko

Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
---

 Documentation/kbuild/modules.rst | 21 +++++++++++++++++++++
 Makefile                         |  8 ++++++--
 2 files changed, 27 insertions(+), 2 deletions(-)

diff --git a/Documentation/kbuild/modules.rst b/Documentation/kbuild/modules.rst
index 03347e13eeb5..5610cfa426bb 100644
--- a/Documentation/kbuild/modules.rst
+++ b/Documentation/kbuild/modules.rst
@@ -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
 =============================
 
diff --git a/Makefile b/Makefile
index 81603a50c757..88c160ac7b3c 100644
--- a/Makefile
+++ b/Makefile
@@ -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))
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 69+ messages in thread

* Re: [PATCH 01/23] kbuild: doc: update the description about Kbuild/Makefile split
  2024-09-17 14:16 ` [PATCH 01/23] kbuild: doc: update the description about Kbuild/Makefile split Masahiro Yamada
@ 2024-09-18 15:35   ` Nicolas Schier
  0 siblings, 0 replies; 69+ messages in thread
From: Nicolas Schier @ 2024-09-18 15:35 UTC (permalink / raw)
  To: Masahiro Yamada
  Cc: linux-kbuild, Miguel Ojeda, rust-for-linux, linux-kernel,
	Nathan Chancellor

On Tue, Sep 17, 2024 at 11:16:29PM +0900, Masahiro Yamada wrote:
> 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.
> 
> [1]: https://git.kernel.org/pub/scm/linux/kernel/git/history/history.git/commit/?id=65e433436b5794ae056d22ddba60fe9194bba007
> 
> Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
> ---

Reviewed-by: Nicolas Schier <n.schier@avm.de>

^ permalink raw reply	[flat|nested] 69+ messages in thread

* Re: [PATCH 02/23] kbuild: doc: remove description about grepping CONFIG options
  2024-09-17 14:16 ` [PATCH 02/23] kbuild: doc: remove description about grepping CONFIG options Masahiro Yamada
@ 2024-09-18 15:35   ` Nicolas Schier
  0 siblings, 0 replies; 69+ messages in thread
From: Nicolas Schier @ 2024-09-18 15:35 UTC (permalink / raw)
  To: Masahiro Yamada
  Cc: linux-kbuild, Miguel Ojeda, rust-for-linux, linux-kernel,
	Nathan Chancellor

On Tue, Sep 17, 2024 at 11:16:30PM +0900, Masahiro Yamada wrote:
> This description was added 20 years ago [1]. It does not convey any
> useful information except for a feeling of nostalgia.
> 
> [1]: https://git.kernel.org/pub/scm/linux/kernel/git/history/history.git/commit/?id=65e433436b5794ae056d22ddba60fe9194bba007
> 
> Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
> ---

Reviewed-by: Nicolas Schier <n.schier@avm.de>

^ permalink raw reply	[flat|nested] 69+ messages in thread

* Re: [PATCH 03/23] kbuild: doc: remove outdated description of the limitation on -I usage
  2024-09-17 14:16 ` [PATCH 03/23] kbuild: doc: remove outdated description of the limitation on -I usage Masahiro Yamada
@ 2024-09-18 15:38   ` Nicolas Schier
  0 siblings, 0 replies; 69+ messages in thread
From: Nicolas Schier @ 2024-09-18 15:38 UTC (permalink / raw)
  To: Masahiro Yamada
  Cc: linux-kbuild, Miguel Ojeda, rust-for-linux, linux-kernel,
	Nathan Chancellor

On Tue, Sep 17, 2024 at 11:16:31PM +0900, Masahiro Yamada wrote:
> Kbuild 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)
> 
> Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
> ---

Reviewed-by: Nicolas Schier <n.schier@avm.de>

^ permalink raw reply	[flat|nested] 69+ messages in thread

* Re: [PATCH 04/23] kbuild: doc: remove the description about shipped files
  2024-09-17 14:16 ` [PATCH 04/23] kbuild: doc: remove the description about shipped files Masahiro Yamada
@ 2024-09-18 16:24   ` Nicolas Schier
  2024-09-19 16:53     ` Masahiro Yamada
  0 siblings, 1 reply; 69+ messages in thread
From: Nicolas Schier @ 2024-09-18 16:24 UTC (permalink / raw)
  To: Masahiro Yamada
  Cc: linux-kbuild, Miguel Ojeda, rust-for-linux, linux-kernel,
	Nathan Chancellor

[-- Attachment #1: Type: text/plain, Size: 2735 bytes --]

On Tue, Sep 17, 2024 at 11:16:32PM +0900, Masahiro Yamada wrote:
> 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/
> 
> Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
> ---
> 
>  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.

> 
> diff --git a/Documentation/kbuild/modules.rst b/Documentation/kbuild/modules.rst
> index 7eceb9a65e9c..1afa6b1b4090 100644
> --- a/Documentation/kbuild/modules.rst
> +++ b/Documentation/kbuild/modules.rst
[...]
> @@ -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.
> -

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,

Reviewed-by: Nicolas Schier <n.schier@avm.de>

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

^ permalink raw reply	[flat|nested] 69+ messages in thread

* Re: [PATCH 05/23] kbuild: doc: describe the -C option precisely for external module builds
  2024-09-17 14:16 ` [PATCH 05/23] kbuild: doc: describe the -C option precisely for external module builds Masahiro Yamada
@ 2024-09-18 16:44   ` Nicolas Schier
  2024-09-18 16:47   ` Nicolas Schier
  1 sibling, 0 replies; 69+ messages in thread
From: Nicolas Schier @ 2024-09-18 16:44 UTC (permalink / raw)
  To: Masahiro Yamada
  Cc: linux-kbuild, Miguel Ojeda, rust-for-linux, linux-kernel,
	Nathan Chancellor

[-- Attachment #1: Type: text/plain, Size: 1692 bytes --]

On Tue, Sep 17, 2024 at 11:16:33PM +0900, Masahiro Yamada wrote:
> 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, 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.
> 
> [1]: https://git.kernel.org/pub/scm/linux/kernel/git/history/history.git/commit/?id=e321b2ec2eb2993b3d0116e5163c78ad923e3c54
> 
> Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
> ---
> 
>  Documentation/kbuild/kbuild.rst  | 5 +++++
>  Documentation/kbuild/modules.rst | 9 ++++++---
>  2 files changed, 11 insertions(+), 3 deletions(-)

Reviewed-by: Nicolas Schier <n.schier@avm.de>

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

^ permalink raw reply	[flat|nested] 69+ messages in thread

* Re: [PATCH 05/23] kbuild: doc: describe the -C option precisely for external module builds
  2024-09-17 14:16 ` [PATCH 05/23] kbuild: doc: describe the -C option precisely for external module builds Masahiro Yamada
  2024-09-18 16:44   ` Nicolas Schier
@ 2024-09-18 16:47   ` Nicolas Schier
  1 sibling, 0 replies; 69+ messages in thread
From: Nicolas Schier @ 2024-09-18 16:47 UTC (permalink / raw)
  To: Masahiro Yamada
  Cc: linux-kbuild, Miguel Ojeda, rust-for-linux, linux-kernel,
	Nathan Chancellor

On Tue, Sep 17, 2024 at 11:16:33PM +0900, Masahiro Yamada wrote:
> 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, 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.
> 
> [1]: https://git.kernel.org/pub/scm/linux/kernel/git/history/history.git/commit/?id=e321b2ec2eb2993b3d0116e5163c78ad923e3c54
> 
> Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
> ---
> 
>  Documentation/kbuild/kbuild.rst  | 5 +++++
>  Documentation/kbuild/modules.rst | 9 ++++++---
>  2 files changed, 11 insertions(+), 3 deletions(-)

Reviewed-by: Nicolas Schier <n.schier@avm.de>

^ permalink raw reply	[flat|nested] 69+ messages in thread

* Re: [PATCH 06/23] kbuild: doc: replace "gcc" in external module description
  2024-09-17 14:16 ` [PATCH 06/23] kbuild: doc: replace "gcc" in external module description Masahiro Yamada
@ 2024-09-19 11:22   ` Nicolas Schier
  0 siblings, 0 replies; 69+ messages in thread
From: Nicolas Schier @ 2024-09-19 11:22 UTC (permalink / raw)
  To: Masahiro Yamada
  Cc: linux-kbuild, Miguel Ojeda, rust-for-linux, linux-kernel,
	Nathan Chancellor

On Tue, Sep 17, 2024 at 11:16:34PM +0900, Masahiro Yamada wrote:
> Avoid "gcc" since it is not the only compiler supported by Kbuild.
> 
> Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
> ---
> 
>  Documentation/kbuild/modules.rst | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)

Reviewed-by: Nicolas Schier <n.schier@avm.de>

^ permalink raw reply	[flat|nested] 69+ messages in thread

* Re: [PATCH 07/23] kbuild: remove unnecessary prune of rust/alloc for rustfmt
  2024-09-17 14:16 ` [PATCH 07/23] kbuild: remove unnecessary prune of rust/alloc for rustfmt Masahiro Yamada
@ 2024-09-19 11:23   ` Nicolas Schier
  2024-09-19 13:46     ` Nicolas Schier
  0 siblings, 1 reply; 69+ messages in thread
From: Nicolas Schier @ 2024-09-19 11:23 UTC (permalink / raw)
  To: Masahiro Yamada
  Cc: linux-kbuild, Miguel Ojeda, rust-for-linux, linux-kernel,
	Nathan Chancellor

On Tue, Sep 17, 2024 at 11:16:35PM +0900, Masahiro Yamada wrote:
> Commit 9d0441bab775 ("rust: alloc: remove our fork of the `alloc`
> crate") removed the rust/alloc/ directory.
> 
> Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
> ---
> 
>  Makefile | 2 --
>  1 file changed, 2 deletions(-)

Reviewed-by: Nicolas Schier <n.schier@avm.de>

^ permalink raw reply	[flat|nested] 69+ messages in thread

* Re: [PATCH 07/23] kbuild: remove unnecessary prune of rust/alloc for rustfmt
  2024-09-19 11:23   ` Nicolas Schier
@ 2024-09-19 13:46     ` Nicolas Schier
  2024-09-30 14:29       ` Miguel Ojeda
  0 siblings, 1 reply; 69+ messages in thread
From: Nicolas Schier @ 2024-09-19 13:46 UTC (permalink / raw)
  To: Masahiro Yamada
  Cc: linux-kbuild, Miguel Ojeda, rust-for-linux, linux-kernel,
	Nathan Chancellor

On Thu, Sep 19, 2024 at 01:23:01PM +0200, Nicolas Schier wrote:
> On Tue, Sep 17, 2024 at 11:16:35PM +0900, Masahiro Yamada wrote:
> > Commit 9d0441bab775 ("rust: alloc: remove our fork of the `alloc`
> > crate") removed the rust/alloc/ directory.
> > 
> > Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
> > ---
> > 
> >  Makefile | 2 --
> >  1 file changed, 2 deletions(-)
> 
> Reviewed-by: Nicolas Schier <n.schier@avm.de>

oh, there is still a comment about 'rust/alloc' in Makefile that
probably wants to be removed, too.

^ permalink raw reply	[flat|nested] 69+ messages in thread

* Re: [PATCH 08/23] kbuild: simplify find command for rustfmt
  2024-09-17 14:16 ` [PATCH 08/23] kbuild: simplify find command " Masahiro Yamada
@ 2024-09-19 13:54   ` Nicolas Schier
  2024-09-19 13:59     ` Masahiro Yamada
  2024-09-30 18:37   ` Miguel Ojeda
  1 sibling, 1 reply; 69+ messages in thread
From: Nicolas Schier @ 2024-09-19 13:54 UTC (permalink / raw)
  To: Masahiro Yamada
  Cc: linux-kbuild, Miguel Ojeda, rust-for-linux, linux-kernel,
	Nathan Chancellor

On Tue, Sep 17, 2024 at 11:16:36PM +0900, Masahiro Yamada wrote:
> The 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/test
> 
> With 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.
> 
> Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
> ---
> 
>  Makefile | 5 ++---
>  1 file changed, 2 insertions(+), 3 deletions(-)
> 
> diff --git a/Makefile b/Makefile
> index 5b16e0605a77..4992b2895dd5 100644
> --- a/Makefile
> +++ b/Makefile
> @@ -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?

Reviewed-by: Nicolas Schier <n.schier@avm.de>

^ permalink raw reply	[flat|nested] 69+ messages in thread

* Re: [PATCH 08/23] kbuild: simplify find command for rustfmt
  2024-09-19 13:54   ` Nicolas Schier
@ 2024-09-19 13:59     ` Masahiro Yamada
  0 siblings, 0 replies; 69+ messages in thread
From: Masahiro Yamada @ 2024-09-19 13:59 UTC (permalink / raw)
  To: Nicolas Schier
  Cc: linux-kbuild, Miguel Ojeda, rust-for-linux, linux-kernel,
	Nathan Chancellor

On Thu, Sep 19, 2024 at 10:54 PM Nicolas Schier <n.schier@avm.de> wrote:
>
> On Tue, Sep 17, 2024 at 11:16:36PM +0900, Masahiro Yamada wrote:
> > The 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/test
> >
> > With 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.
> >
> > Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
> > ---
> >
> >  Makefile | 5 ++---
> >  1 file changed, 2 insertions(+), 3 deletions(-)
> >
> > diff --git a/Makefile b/Makefile
> > index 5b16e0605a77..4992b2895dd5 100644
> > --- a/Makefile
> > +++ b/Makefile
> > @@ -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?


This commit answers your question:

https://github.com/Rust-for-Linux/linux/commit/73243a8a27a67




-- 
Best Regards
Masahiro Yamada

^ permalink raw reply	[flat|nested] 69+ messages in thread

* Re: [PATCH 04/23] kbuild: doc: remove the description about shipped files
  2024-09-18 16:24   ` Nicolas Schier
@ 2024-09-19 16:53     ` Masahiro Yamada
  0 siblings, 0 replies; 69+ messages in thread
From: Masahiro Yamada @ 2024-09-19 16:53 UTC (permalink / raw)
  To: Nicolas Schier
  Cc: linux-kbuild, Miguel Ojeda, rust-for-linux, linux-kernel,
	Nathan Chancellor

On Thu, Sep 19, 2024 at 1:24 AM Nicolas Schier <n.schier@avm.de> wrote:
>
> On Tue, Sep 17, 2024 at 11:16:32PM +0900, Masahiro Yamada wrote:
> > 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/
> >
> > Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
> > ---
> >
> >  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?


Yes.

Good catch.



> $ git grep -Hrne _shipped Documentation/
> Documentation/kbuild/makefiles.rst:1668:- Describe how kbuild supports shipped files with _shipped.
>
> >
> > diff --git a/Documentation/kbuild/modules.rst b/Documentation/kbuild/modules.rst
> > index 7eceb9a65e9c..1afa6b1b4090 100644
> > --- a/Documentation/kbuild/modules.rst
> > +++ b/Documentation/kbuild/modules.rst
> [...]
> > @@ -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.
> > -
>
> 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.


> Either way,
>
> Reviewed-by: Nicolas Schier <n.schier@avm.de>



-- 
Best Regards
Masahiro Yamada

^ permalink raw reply	[flat|nested] 69+ messages in thread

* Re: [PATCH 00/23] kbuild: support building external modules in a separate build directory
  2024-09-17 14:16 [PATCH 00/23] kbuild: support building external modules in a separate build directory Masahiro Yamada
                   ` (22 preceding siblings ...)
  2024-09-17 14:16 ` [PATCH 23/23] kbuild: allow to start building external module in any directory Masahiro Yamada
@ 2024-09-20 10:39 ` Nicolas Schier
  2024-09-20 12:58   ` Masahiro Yamada
  2024-09-28  6:50 ` Masahiro Yamada
  2024-10-04 20:16 ` Nicolas Schier
  25 siblings, 1 reply; 69+ messages in thread
From: Nicolas Schier @ 2024-09-20 10:39 UTC (permalink / raw)
  To: Masahiro Yamada
  Cc: linux-kbuild, Miguel Ojeda, rust-for-linux, linux-kernel,
	Nathan Chancellor, Nicolas Schier

On Tue, Sep 17, 2024 at 11:16:28PM +0900, Masahiro Yamada wrote:
> 
> 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!

> 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:

diff --git a/scripts/Makefile.host b/scripts/Makefile.host
index e01c13a588dd..056c7da2776f 100644
--- a/scripts/Makefile.host
+++ b/scripts/Makefile.host
@@ -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
 
diff --git a/scripts/Makefile.lib b/scripts/Makefile.lib
index 1bdd77f42289..428a9eb74381 100644
--- a/scripts/Makefile.lib
+++ b/scripts/Makefile.lib
@@ -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

^ permalink raw reply related	[flat|nested] 69+ messages in thread

* Re: [PATCH 00/23] kbuild: support building external modules in a separate build directory
  2024-09-20 10:39 ` [PATCH 00/23] kbuild: support building external modules in a separate build directory Nicolas Schier
@ 2024-09-20 12:58   ` Masahiro Yamada
  2024-09-20 13:41     ` Nicolas Schier
  0 siblings, 1 reply; 69+ messages in thread
From: Masahiro Yamada @ 2024-09-20 12:58 UTC (permalink / raw)
  To: Nicolas Schier
  Cc: linux-kbuild, Miguel Ojeda, rust-for-linux, linux-kernel,
	Nathan Chancellor, Nicolas Schier

On Fri, Sep 20, 2024 at 7:39 PM Nicolas Schier <n.schier@avm.de> wrote:
>
> On Tue, Sep 17, 2024 at 11:16:28PM +0900, Masahiro Yamada wrote:
> >
> > 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!
>
> > 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:
>
> diff --git a/scripts/Makefile.host b/scripts/Makefile.host
> index e01c13a588dd..056c7da2776f 100644
> --- a/scripts/Makefile.host
> +++ b/scripts/Makefile.host
> @@ -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
>
> diff --git a/scripts/Makefile.lib b/scripts/Makefile.lib
> index 1bdd77f42289..428a9eb74381 100644
> --- a/scripts/Makefile.lib
> +++ b/scripts/Makefile.lib
> @@ -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



We can fix it more simply.




diff --git a/scripts/Makefile.host b/scripts/Makefile.host
index e01c13a588dd..c1dedf646a39 100644
--- a/scripts/Makefile.host
+++ b/scripts/Makefile.host
@@ -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

 #####
diff --git a/scripts/Makefile.lib b/scripts/Makefile.lib
index 1bdd77f42289..d8ce0f59fd17 100644
--- a/scripts/Makefile.lib
+++ b/scripts/Makefile.lib
@@ -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.








-- 
Best Regards
Masahiro Yamada

^ permalink raw reply related	[flat|nested] 69+ messages in thread

* Re: [PATCH 00/23] kbuild: support building external modules in a separate build directory
  2024-09-20 12:58   ` Masahiro Yamada
@ 2024-09-20 13:41     ` Nicolas Schier
  0 siblings, 0 replies; 69+ messages in thread
From: Nicolas Schier @ 2024-09-20 13:41 UTC (permalink / raw)
  To: Masahiro Yamada
  Cc: linux-kbuild, Miguel Ojeda, rust-for-linux, linux-kernel,
	Nathan Chancellor

On Fri, Sep 20, 2024 at 09:58:47PM +0900, Masahiro Yamada wrote:
> On Fri, Sep 20, 2024 at 7:39 PM Nicolas Schier <n.schier@avm.de> wrote:
> >
> > On Tue, Sep 17, 2024 at 11:16:28PM +0900, Masahiro Yamada wrote:
> > >
> > > 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!
> >
> > > 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:
> >
> > diff --git a/scripts/Makefile.host b/scripts/Makefile.host
> > index e01c13a588dd..056c7da2776f 100644
> > --- a/scripts/Makefile.host
> > +++ b/scripts/Makefile.host
> > @@ -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
> >
> > diff --git a/scripts/Makefile.lib b/scripts/Makefile.lib
> > index 1bdd77f42289..428a9eb74381 100644
> > --- a/scripts/Makefile.lib
> > +++ b/scripts/Makefile.lib
> > @@ -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
> 
> 
> 
> We can fix it more simply.

Ah, yes.

> diff --git a/scripts/Makefile.host b/scripts/Makefile.host
> index e01c13a588dd..c1dedf646a39 100644
> --- a/scripts/Makefile.host
> +++ b/scripts/Makefile.host
> @@ -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
> 
>  #####
> diff --git a/scripts/Makefile.lib b/scripts/Makefile.lib
> index 1bdd77f42289..d8ce0f59fd17 100644
> --- a/scripts/Makefile.lib
> +++ b/scripts/Makefile.lib
> @@ -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.

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

^ permalink raw reply	[flat|nested] 69+ messages in thread

* Re: [PATCH 10/23] kbuild: refactor the check for missing config files
  2024-09-17 14:16 ` [PATCH 10/23] kbuild: refactor the check for missing config files Masahiro Yamada
@ 2024-09-25 14:04   ` Nicolas Schier
  0 siblings, 0 replies; 69+ messages in thread
From: Nicolas Schier @ 2024-09-25 14:04 UTC (permalink / raw)
  To: Masahiro Yamada
  Cc: linux-kbuild, Miguel Ojeda, rust-for-linux, linux-kernel,
	Nathan Chancellor

On Tue, Sep 17, 2024 at 11:16:38PM +0900, Masahiro Yamada wrote:
> 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.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.
>   ***
> 
> Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
> ---
> 
>  Makefile | 21 +++++++++++++--------
>  1 file changed, 13 insertions(+), 8 deletions(-)

Reviewed-by: Nicolas Schier <n.schier@avm.de>

^ permalink raw reply	[flat|nested] 69+ messages in thread

* Re: [PATCH 11/23] kbuild: check the presence of include/generated/rustc_cfg
  2024-09-17 14:16 ` [PATCH 11/23] kbuild: check the presence of include/generated/rustc_cfg Masahiro Yamada
@ 2024-09-25 14:10   ` Nicolas Schier
  2024-09-30 14:37   ` Miguel Ojeda
  1 sibling, 0 replies; 69+ messages in thread
From: Nicolas Schier @ 2024-09-25 14:10 UTC (permalink / raw)
  To: Masahiro Yamada
  Cc: linux-kbuild, Miguel Ojeda, rust-for-linux, linux-kernel,
	Nathan Chancellor

On Tue, Sep 17, 2024 at 11:16:39PM +0900, Masahiro Yamada wrote:
> Since 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.
> 
> Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
> ---
> 
>  Makefile | 5 +++--
>  1 file changed, 3 insertions(+), 2 deletions(-)
> 
> diff --git a/Makefile b/Makefile
> index 3f5c273c2901..5700d08c9b57 100644
> --- a/Makefile
> +++ b/Makefile
> @@ -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
> -- 
> 2.43.0
> 

Reviewed-by: Nicolas Schier <n.schier@avm.de>

^ permalink raw reply	[flat|nested] 69+ messages in thread

* Re: [PATCH 12/23] scripts/nsdeps: use VPATH as src_prefix
  2024-09-17 14:16 ` [PATCH 12/23] scripts/nsdeps: use VPATH as src_prefix Masahiro Yamada
@ 2024-09-25 14:18   ` Nicolas Schier
  2024-09-26  7:30     ` Nicolas Schier
  0 siblings, 1 reply; 69+ messages in thread
From: Nicolas Schier @ 2024-09-25 14:18 UTC (permalink / raw)
  To: Masahiro Yamada
  Cc: linux-kbuild, Miguel Ojeda, rust-for-linux, linux-kernel,
	Nathan Chancellor

On Tue, Sep 17, 2024 at 11:16:40PM +0900, Masahiro Yamada wrote:
> 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 <masahiroy@kernel.org>
> ---
> 
>  scripts/nsdeps | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/scripts/nsdeps b/scripts/nsdeps
> index f1718cc0d700..8b4db63838ce 100644
> --- a/scripts/nsdeps
> +++ b/scripts/nsdeps
> @@ -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?

Reviewed-by: Nicolas Schier <n.schier@avm.de>

>  fi
>  
>  generate_deps_for_ns() {
> -- 
> 2.43.0
> 

^ permalink raw reply	[flat|nested] 69+ messages in thread

* Re: [PATCH 13/23] kbuild: replace two $(abs_objtree) with $(CURDIR) in top Makefile
  2024-09-17 14:16 ` [PATCH 13/23] kbuild: replace two $(abs_objtree) with $(CURDIR) in top Makefile Masahiro Yamada
@ 2024-09-25 14:27   ` Nicolas Schier
  0 siblings, 0 replies; 69+ messages in thread
From: Nicolas Schier @ 2024-09-25 14:27 UTC (permalink / raw)
  To: Masahiro Yamada
  Cc: linux-kbuild, Miguel Ojeda, rust-for-linux, linux-kernel,
	Nathan Chancellor

On Tue, Sep 17, 2024 at 11:16:41PM +0900, Masahiro Yamada wrote:
> Kbuild changes 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 <masahiroy@kernel.org>
> ---
> 
>  Makefile | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/Makefile b/Makefile
> index 5700d08c9b57..ee9ad0f0960f 100644
> --- a/Makefile
> +++ b/Makefile
> @@ -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
> -- 
> 2.43.0
> 

Reviewed-by: Nicolas Schier <n.schier@avm.de>

^ permalink raw reply	[flat|nested] 69+ messages in thread

* Re: [PATCH 12/23] scripts/nsdeps: use VPATH as src_prefix
  2024-09-25 14:18   ` Nicolas Schier
@ 2024-09-26  7:30     ` Nicolas Schier
  0 siblings, 0 replies; 69+ messages in thread
From: Nicolas Schier @ 2024-09-26  7:30 UTC (permalink / raw)
  To: Nicolas Schier
  Cc: Masahiro Yamada, linux-kbuild, Miguel Ojeda, rust-for-linux,
	linux-kernel, Nathan Chancellor

On Wed, Sep 25, 2024 at 04:18:56PM +0200, Nicolas Schier wrote:
> On Tue, Sep 17, 2024 at 11:16:40PM +0900, Masahiro Yamada wrote:
> > 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 <masahiroy@kernel.org>
> > ---
> > 
> >  scripts/nsdeps | 4 ++--
> >  1 file changed, 2 insertions(+), 2 deletions(-)
> > 
> > diff --git a/scripts/nsdeps b/scripts/nsdeps
> > index f1718cc0d700..8b4db63838ce 100644
> > --- a/scripts/nsdeps
> > +++ b/scripts/nsdeps
> > @@ -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?

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.

^ permalink raw reply	[flat|nested] 69+ messages in thread

* Re: [PATCH 14/23] kbuild: add $(objtree)/ prefix to some in-kernel build artifacts
  2024-09-17 14:16 ` [PATCH 14/23] kbuild: add $(objtree)/ prefix to some in-kernel build artifacts Masahiro Yamada
@ 2024-09-26  7:50   ` Nicolas Schier
  0 siblings, 0 replies; 69+ messages in thread
From: Nicolas Schier @ 2024-09-26  7:50 UTC (permalink / raw)
  To: Masahiro Yamada
  Cc: linux-kbuild, Miguel Ojeda, rust-for-linux, linux-kernel,
	Nathan Chancellor

On Tue, Sep 17, 2024 at 11:16:42PM +0900, Masahiro Yamada wrote:
> $(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.
> 
> Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
> ---
> 
>  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(-)
> 
> diff --git a/Makefile b/Makefile
> index ee9ad0f0960f..c923bea7043b 100644
> --- a/Makefile
> +++ b/Makefile
> @@ -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)
> diff --git a/scripts/Kbuild.include b/scripts/Kbuild.include
> index ed8a7493524b..8c311b997e24 100644
> --- a/scripts/Kbuild.include
> +++ b/scripts/Kbuild.include
> @@ -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)
> diff --git a/scripts/Makefile.build b/scripts/Makefile.build
> index 8403eba15457..6e8954cbf53a 100644
> --- a/scripts/Makefile.build
> +++ b/scripts/Makefile.build
> @@ -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)
> diff --git a/scripts/Makefile.modfinal b/scripts/Makefile.modfinal
> index 1482884ec3ca..6d8aa3059ee2 100644
> --- a/scripts/Makefile.modfinal
> +++ b/scripts/Makefile.modfinal
> @@ -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
> diff --git a/scripts/Makefile.modinst b/scripts/Makefile.modinst
> index 4d81ed9af294..2d39d452c49e 100644
> --- a/scripts/Makefile.modinst
> +++ b/scripts/Makefile.modinst
> @@ -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 :=
> diff --git a/scripts/Makefile.modpost b/scripts/Makefile.modpost
> index 44936ebad161..12e7c15d099c 100644
> --- a/scripts/Makefile.modpost
> +++ b/scripts/Makefile.modpost
> @@ -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))
> diff --git a/scripts/depmod.sh b/scripts/depmod.sh
> index e22da27fe13e..3c34fecacbc8 100755
> --- a/scripts/depmod.sh
> +++ b/scripts/depmod.sh
> @@ -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
> -- 
> 2.43.0
> 
> 

Reviewed-by: Nicolas Schier <nicolas@fjasle.eu>


^ permalink raw reply	[flat|nested] 69+ messages in thread

* Re: [PATCH 15/23] kbuild: rename abs_objtree to abs_output
  2024-09-17 14:16 ` [PATCH 15/23] kbuild: rename abs_objtree to abs_output Masahiro Yamada
@ 2024-09-26  7:51   ` Nicolas Schier
  0 siblings, 0 replies; 69+ messages in thread
From: Nicolas Schier @ 2024-09-26  7:51 UTC (permalink / raw)
  To: Masahiro Yamada
  Cc: linux-kbuild, Miguel Ojeda, rust-for-linux, linux-kernel,
	Nathan Chancellor

On Tue, Sep 17, 2024 at 11:16:43PM +0900, Masahiro Yamada wrote:
> '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.
> 
> Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
> ---
> 
>  Makefile | 10 +++++-----
>  1 file changed, 5 insertions(+), 5 deletions(-)
> 
> diff --git a/Makefile b/Makefile
> index c923bea7043b..254f05a1cc0f 100644
> --- a/Makefile
> +++ b/Makefile
> @@ -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
> -- 
> 2.43.0
> 
> 

Reviewed-by: Nicolas Schier <nicolas@fjasle.eu>

^ permalink raw reply	[flat|nested] 69+ messages in thread

* Re: [PATCH 16/23] kbuild: use 'output' variable to create the output directory
  2024-09-17 14:16 ` [PATCH 16/23] kbuild: use 'output' variable to create the output directory Masahiro Yamada
@ 2024-09-26  7:54   ` Nicolas Schier
  0 siblings, 0 replies; 69+ messages in thread
From: Nicolas Schier @ 2024-09-26  7:54 UTC (permalink / raw)
  To: Masahiro Yamada
  Cc: linux-kbuild, Miguel Ojeda, rust-for-linux, linux-kernel,
	Nathan Chancellor

On Tue, Sep 17, 2024 at 11:16:44PM +0900, Masahiro Yamada wrote:
> $(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 <masahiroy@kernel.org>
> ---
> 
>  Makefile | 14 ++++++++------
>  1 file changed, 8 insertions(+), 6 deletions(-)
> 
> diff --git a/Makefile b/Makefile
> index 254f05a1cc0f..7a76452049ea 100644
> --- a/Makefile
> +++ b/Makefile
> @@ -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)
> -- 
> 2.43.0
> 
> 

Reviewed-by: Nicolas Schier <nicolas@fjasle.eu>

^ permalink raw reply	[flat|nested] 69+ messages in thread

* Re: [PATCH 00/23] kbuild: support building external modules in a separate build directory
  2024-09-17 14:16 [PATCH 00/23] kbuild: support building external modules in a separate build directory Masahiro Yamada
                   ` (23 preceding siblings ...)
  2024-09-20 10:39 ` [PATCH 00/23] kbuild: support building external modules in a separate build directory Nicolas Schier
@ 2024-09-28  6:50 ` Masahiro Yamada
  2024-09-30 14:28   ` Miguel Ojeda
  2024-10-04 20:16 ` Nicolas Schier
  25 siblings, 1 reply; 69+ messages in thread
From: Masahiro Yamada @ 2024-09-28  6:50 UTC (permalink / raw)
  To: linux-kbuild, Miguel Ojeda, rust-for-linux
  Cc: linux-kernel, Nathan Chancellor, Nicolas Schier

On Tue, Sep 17, 2024 at 11:17 PM Masahiro Yamada <masahiroy@kernel.org> wrote:
>
>
> 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



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







-- 
Best Regards
Masahiro Yamada

^ permalink raw reply	[flat|nested] 69+ messages in thread

* Re: [PATCH 00/23] kbuild: support building external modules in a separate build directory
  2024-09-28  6:50 ` Masahiro Yamada
@ 2024-09-30 14:28   ` Miguel Ojeda
  0 siblings, 0 replies; 69+ messages in thread
From: Miguel Ojeda @ 2024-09-30 14:28 UTC (permalink / raw)
  To: Masahiro Yamada
  Cc: linux-kbuild, Miguel Ojeda, rust-for-linux, linux-kernel,
	Nathan Chancellor, Nicolas Schier

On Sat, Sep 28, 2024 at 8:50 AM Masahiro Yamada <masahiroy@kernel.org> wrote:
>
> 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

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:

Tested-by: Miguel Ojeda <ojeda@kernel.org>

...except for arm64, where I found that I needed this bit:

diff --git a/arch/arm64/Makefile b/arch/arm64/Makefile
index b058c4803efb..4cd9a1f2ec3d 100644
--- a/arch/arm64/Makefile
+++ b/arch/arm64/Makefile
@@ -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

^ permalink raw reply related	[flat|nested] 69+ messages in thread

* Re: [PATCH 07/23] kbuild: remove unnecessary prune of rust/alloc for rustfmt
  2024-09-19 13:46     ` Nicolas Schier
@ 2024-09-30 14:29       ` Miguel Ojeda
  0 siblings, 0 replies; 69+ messages in thread
From: Miguel Ojeda @ 2024-09-30 14:29 UTC (permalink / raw)
  To: Nicolas Schier
  Cc: Masahiro Yamada, linux-kbuild, Miguel Ojeda, rust-for-linux,
	linux-kernel, Nathan Chancellor

On Thu, Sep 19, 2024 at 3:47 PM Nicolas Schier <n.schier@avm.de> wrote:
>
> oh, there is still a comment about 'rust/alloc' in Makefile that
> 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:

Acked-by: Miguel Ojeda <ojeda@kernel.org>

Cheers,
Miguel

^ permalink raw reply	[flat|nested] 69+ messages in thread

* Re: [PATCH 11/23] kbuild: check the presence of include/generated/rustc_cfg
  2024-09-17 14:16 ` [PATCH 11/23] kbuild: check the presence of include/generated/rustc_cfg Masahiro Yamada
  2024-09-25 14:10   ` Nicolas Schier
@ 2024-09-30 14:37   ` Miguel Ojeda
  1 sibling, 0 replies; 69+ messages in thread
From: Miguel Ojeda @ 2024-09-30 14:37 UTC (permalink / raw)
  To: Masahiro Yamada
  Cc: linux-kbuild, Miguel Ojeda, rust-for-linux, linux-kernel,
	Nathan Chancellor, Nicolas Schier

On Tue, Sep 17, 2024 at 4:17 PM Masahiro Yamada <masahiroy@kernel.org> wrote:
>
> Since 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.
>
> Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>

Sounds good to me:

Acked-by: Miguel Ojeda <ojeda@kernel.org>

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:

Tested-by: Miguel Ojeda <ojeda@kernel.org>

I guess this tag may apply to the previous patch too.

Cheers,
Miguel

^ permalink raw reply	[flat|nested] 69+ messages in thread

* Re: [PATCH 08/23] kbuild: simplify find command for rustfmt
  2024-09-17 14:16 ` [PATCH 08/23] kbuild: simplify find command " Masahiro Yamada
  2024-09-19 13:54   ` Nicolas Schier
@ 2024-09-30 18:37   ` Miguel Ojeda
  2024-10-01  2:18     ` Masahiro Yamada
  1 sibling, 1 reply; 69+ messages in thread
From: Miguel Ojeda @ 2024-09-30 18:37 UTC (permalink / raw)
  To: Masahiro Yamada
  Cc: linux-kbuild, Miguel Ojeda, rust-for-linux, linux-kernel,
	Nathan Chancellor, Nicolas Schier

On Tue, Sep 17, 2024 at 4:17 PM Masahiro Yamada <masahiroy@kernel.org> wrote:
>
> 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

^ permalink raw reply	[flat|nested] 69+ messages in thread

* Re: [PATCH 08/23] kbuild: simplify find command for rustfmt
  2024-09-30 18:37   ` Miguel Ojeda
@ 2024-10-01  2:18     ` Masahiro Yamada
  2024-10-01  5:45       ` Miguel Ojeda
  0 siblings, 1 reply; 69+ messages in thread
From: Masahiro Yamada @ 2024-10-01  2:18 UTC (permalink / raw)
  To: Miguel Ojeda
  Cc: linux-kbuild, Miguel Ojeda, rust-for-linux, linux-kernel,
	Nathan Chancellor, Nicolas Schier

On Tue, Oct 1, 2024 at 3:38 AM Miguel Ojeda
<miguel.ojeda.sandonis@gmail.com> wrote:
>
> On Tue, Sep 17, 2024 at 4:17 PM Masahiro Yamada <masahiroy@kernel.org> wrote:
> >
> > 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?

Correct.


> 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.




> 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.


OK, then we can remove -prune entirely.









--
Best Regards
Masahiro Yamada

^ permalink raw reply	[flat|nested] 69+ messages in thread

* Re: [PATCH 08/23] kbuild: simplify find command for rustfmt
  2024-10-01  2:18     ` Masahiro Yamada
@ 2024-10-01  5:45       ` Miguel Ojeda
  0 siblings, 0 replies; 69+ messages in thread
From: Miguel Ojeda @ 2024-10-01  5:45 UTC (permalink / raw)
  To: Masahiro Yamada
  Cc: linux-kbuild, Miguel Ojeda, rust-for-linux, linux-kernel,
	Nathan Chancellor, Nicolas Schier

On Tue, Oct 1, 2024 at 4:18 AM Masahiro Yamada <masahiroy@kernel.org> wrote:
>
> OK, then we can remove -prune entirely.

Yeah, I think that is the simplest approach.

With those changes, please feel free to add:

Acked-by: Miguel Ojeda <ojeda@kernel.org>

Thanks!

Cheers,
Miguel

^ permalink raw reply	[flat|nested] 69+ messages in thread

* Re: [PATCH 09/23] speakup: use SPKDIR=$(src) to specify the source directory
  2024-09-17 14:16 ` [PATCH 09/23] speakup: use SPKDIR=$(src) to specify the source directory Masahiro Yamada
@ 2024-10-03 18:25   ` Nicolas Schier
  0 siblings, 0 replies; 69+ messages in thread
From: Nicolas Schier @ 2024-10-03 18:25 UTC (permalink / raw)
  To: Masahiro Yamada
  Cc: linux-kbuild, Miguel Ojeda, rust-for-linux, linux-kernel,
	Nathan Chancellor

On Tue, Sep 17, 2024 at 11:16:37PM +0900, Masahiro Yamada wrote:
> 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 <masahiroy@kernel.org>
> ---
> 
>  drivers/accessibility/speakup/Makefile | 4 +---
>  1 file changed, 1 insertion(+), 3 deletions(-)
> 
> diff --git a/drivers/accessibility/speakup/Makefile b/drivers/accessibility/speakup/Makefile
> index 6f6a83565c0d..14ba1cca87f4 100644
> --- a/drivers/accessibility/speakup/Makefile
> +++ b/drivers/accessibility/speakup/Makefile
> @@ -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)
> -- 
> 2.43.0
> 

Reviewed-by: Nicolas Schier <nicolas@fjasle.eu>

^ permalink raw reply	[flat|nested] 69+ messages in thread

* Re: [PATCH 17/23] kbuild: build external modules in their directory
  2024-09-17 14:16 ` [PATCH 17/23] kbuild: build external modules in their directory Masahiro Yamada
@ 2024-10-03 18:41   ` Nicolas Schier
  0 siblings, 0 replies; 69+ messages in thread
From: Nicolas Schier @ 2024-10-03 18:41 UTC (permalink / raw)
  To: Masahiro Yamada
  Cc: linux-kbuild, Miguel Ojeda, rust-for-linux, linux-kernel,
	Nathan Chancellor

On Tue, Sep 17, 2024 at 11:16:45PM +0900, Masahiro Yamada wrote:
> Currently, Kbuild always operates in the output directory of the kernel,
> even when building external modules. This increases the risk of external
> module Makefiles attempting to write to the kernel directory.
> 
> This commit switches the working directory to the external module
> directory, allowing the removal of the $(KBUILD_EXTMOD)/ prefix from
> some build artifacts.
> 
> The command for building external modules maintains backward
> compatibility, but Makefiles that rely on working in the kernel
> directory may break. In such cases, $(objtree) and $(srctree) should
> be used to refer to the output and source directories of the kernel.
> 
> The appearance of the build log will change as follows:
> 
> [Before]
> 
>   $ make -C /path/to/my/linux M=/path/to/my/externel/module
>   make: Entering directory '/path/to/my/linux'
>     CC [M]  /path/to/my/externel/module/helloworld.o
>     MODPOST /path/to/my/externel/module/Module.symvers
>     CC [M]  /path/to/my/externel/module/helloworld.mod.o
>     CC [M]  /path/to/my/externel/module/.module-common.o
>     LD [M]  /path/to/my/externel/module/helloworld.ko
>   make: Leaving directory '/path/to/my/linux'
> 
> [After]
> 
>   $ make -C /path/to/my/linux M=/path/to/my/externel/module
>   make: Entering directory '/path/to/my/linux'
>   make[1]: Entering directory '/path/to/my/externel/module'
>     CC [M]  helloworld.o
>     MODPOST Module.symvers
>     CC [M]  helloworld.mod.o
>     CC [M]  .module-common.o
>     LD [M]  helloworld.ko
>   make[1]: Leaving directory '/path/to/my/externel/module'
>   make: Leaving directory '/path/to/my/linux'
> 
> Printing "Entering directory" twice is cumbersome. This will be
> addressed later.
> 
> Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
> ---
> 
>  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(-)
> 
> diff --git a/Documentation/dev-tools/coccinelle.rst b/Documentation/dev-tools/coccinelle.rst
> index 535ce126fb4f..80c83ce0babc 100644
> --- a/Documentation/dev-tools/coccinelle.rst
> +++ b/Documentation/dev-tools/coccinelle.rst
> @@ -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

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.

> +.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'.

>  
>  If not using the kernel's coccicheck target, keep the above precedence
>  order logic of .cocciconfig reading. If using the kernel's coccicheck target,
> diff --git a/Makefile b/Makefile
> index 7a76452049ea..4db22c3a8555 100644
> --- a/Makefile
> +++ b/Makefile
> @@ -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)),)

Nit: I think this would better match to patch "kbuild: remove
extmod_prefix, MODORDER, MODULES_NSDEPS variables", but probably nobody
else will care.

Reviewed-by: Nicolas Schier <nicolas@fjasle.eu>

^ permalink raw reply	[flat|nested] 69+ messages in thread

* Re: [PATCH 18/23] kbuild: remove extmod_prefix, MODORDER, MODULES_NSDEPS variables
  2024-09-17 14:16 ` [PATCH 18/23] kbuild: remove extmod_prefix, MODORDER, MODULES_NSDEPS variables Masahiro Yamada
@ 2024-10-03 18:47   ` Nicolas Schier
  2024-11-10  1:36     ` Masahiro Yamada
  0 siblings, 1 reply; 69+ messages in thread
From: Nicolas Schier @ 2024-10-03 18:47 UTC (permalink / raw)
  To: Masahiro Yamada
  Cc: linux-kbuild, Miguel Ojeda, rust-for-linux, linux-kernel,
	Nathan Chancellor

On Tue, Sep 17, 2024 at 11:16:46PM +0900, Masahiro Yamada wrote:
> 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 <masahiroy@kernel.org>
> ---
> 
>  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 FORCE

W/ or w/o:

Reviewed-by: Nicolas Schier <nicolas@fjasle.eu>

^ permalink raw reply	[flat|nested] 69+ messages in thread

* Re: [PATCH 19/23] kbuild: support building external modules in a separate build directory
  2024-09-17 14:16 ` [PATCH 19/23] kbuild: support building external modules in a separate build directory Masahiro Yamada
@ 2024-10-03 19:47   ` Nicolas Schier
  2024-10-04 19:45     ` Nicolas Schier
  2024-11-10  1:36     ` Masahiro Yamada
  0 siblings, 2 replies; 69+ messages in thread
From: Nicolas Schier @ 2024-10-03 19:47 UTC (permalink / raw)
  To: Masahiro Yamada
  Cc: linux-kbuild, Miguel Ojeda, rust-for-linux, linux-kernel,
	Nathan Chancellor

On Tue, Sep 17, 2024 at 11:16:47PM +0900, Masahiro Yamada wrote:
> 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 <masahiroy@kernel.org>
> ---
> 
>  Documentation/kbuild/kbuild.rst  |  8 +++++-
>  Documentation/kbuild/modules.rst |  5 +++-
>  Makefile                         | 44 +++++++++++++++++++++++---------
>  3 files changed, 43 insertions(+), 14 deletions(-)
> 
> diff --git a/Documentation/kbuild/kbuild.rst b/Documentation/kbuild/kbuild.rst
> index 716f6fb70829..66a9dc44ea28 100644
> --- a/Documentation/kbuild/kbuild.rst
> +++ b/Documentation/kbuild/kbuild.rst
> @@ -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
> diff --git a/Documentation/kbuild/modules.rst b/Documentation/kbuild/modules.rst
> index 3a6e7bdc0889..03347e13eeb5 100644
> --- a/Documentation/kbuild/modules.rst
> +++ b/Documentation/kbuild/modules.rst
> @@ -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.

s/Speficies/Specifies/

> +
>  2.3 Targets
>  ===========
>  
> diff --git a/Makefile b/Makefile
> index 9fbf7ef6e394..b654baa0763a 100644
> --- a/Makefile
> +++ b/Makefile
> @@ -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?

>  
> @@ -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
>  
> -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?

> +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.
> -- 
> 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 ...

Reviewed-by: Nicolas Schier <nicolas@fjasle.eu>

^ permalink raw reply	[flat|nested] 69+ messages in thread

* Re: [PATCH 20/23] kbuild: support -fmacro-prefix-map for external modules
  2024-09-17 14:16 ` [PATCH 20/23] kbuild: support -fmacro-prefix-map for external modules Masahiro Yamada
@ 2024-10-04 18:51   ` Nicolas Schier
  0 siblings, 0 replies; 69+ messages in thread
From: Nicolas Schier @ 2024-10-04 18:51 UTC (permalink / raw)
  To: Masahiro Yamada
  Cc: linux-kbuild, Miguel Ojeda, rust-for-linux, linux-kernel,
	Nathan Chancellor

On Tue, Sep 17, 2024 at 11:16:48PM +0900, Masahiro Yamada wrote:
> 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").
> 
> 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 <masahiroy@kernel.org>
> ---
> 
>  Makefile | 6 ++++--
>  1 file changed, 4 insertions(+), 2 deletions(-)

Reviewed-by: Nicolas Schier <nicolas@fjasle.eu>

^ permalink raw reply	[flat|nested] 69+ messages in thread

* Re: [PATCH 21/23] kbuild: use absolute path in the generated wrapper Makefile
  2024-09-17 14:16 ` [PATCH 21/23] kbuild: use absolute path in the generated wrapper Makefile Masahiro Yamada
@ 2024-10-04 19:02   ` Nicolas Schier
  2024-11-29 23:31   ` Caleb Connolly
  1 sibling, 0 replies; 69+ messages in thread
From: Nicolas Schier @ 2024-10-04 19:02 UTC (permalink / raw)
  To: Masahiro Yamada
  Cc: linux-kbuild, Miguel Ojeda, rust-for-linux, linux-kernel,
	Nathan Chancellor

On Tue, Sep 17, 2024 at 11:16:49PM +0900, Masahiro Yamada wrote:
> Keep the consistent behavior when this Makefile is invoked from another
> directory.
> 
> Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
> ---
> 
>  Makefile | 5 +++--
>  1 file changed, 3 insertions(+), 2 deletions(-)

Reviewed-by: Nicolas Schier <nicolas@fjasle.eu>

^ permalink raw reply	[flat|nested] 69+ messages in thread

* Re: [PATCH 19/23] kbuild: support building external modules in a separate build directory
  2024-10-03 19:47   ` Nicolas Schier
@ 2024-10-04 19:45     ` Nicolas Schier
  2024-11-10  1:36       ` Masahiro Yamada
  2024-11-10  1:36     ` Masahiro Yamada
  1 sibling, 1 reply; 69+ messages in thread
From: Nicolas Schier @ 2024-10-04 19:45 UTC (permalink / raw)
  To: Masahiro Yamada
  Cc: linux-kbuild, Miguel Ojeda, rust-for-linux, linux-kernel,
	Nathan Chancellor

On Thu, Oct 03, 2024 at 09:47:35PM +0200, Nicolas Schier wrote:
> On Tue, Sep 17, 2024 at 11:16:47PM +0900, Masahiro Yamada wrote:
> > 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 <masahiroy@kernel.org>
> > ---
> > 
> >  Documentation/kbuild/kbuild.rst  |  8 +++++-
> >  Documentation/kbuild/modules.rst |  5 +++-
> >  Makefile                         | 44 +++++++++++++++++++++++---------
> >  3 files changed, 43 insertions(+), 14 deletions(-)
> > 
> > diff --git a/Documentation/kbuild/kbuild.rst b/Documentation/kbuild/kbuild.rst
> > index 716f6fb70829..66a9dc44ea28 100644
> > --- a/Documentation/kbuild/kbuild.rst
> > +++ b/Documentation/kbuild/kbuild.rst
> > @@ -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
> > diff --git a/Documentation/kbuild/modules.rst b/Documentation/kbuild/modules.rst
> > index 3a6e7bdc0889..03347e13eeb5 100644
> > --- a/Documentation/kbuild/modules.rst
> > +++ b/Documentation/kbuild/modules.rst
> > @@ -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.
> 
> s/Speficies/Specifies/
> 
> > +
> >  2.3 Targets
> >  ===========
> >  
> > diff --git a/Makefile b/Makefile
> > index 9fbf7ef6e394..b654baa0763a 100644
> > --- a/Makefile
> > +++ b/Makefile
> > @@ -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?
> 
> >  
> > @@ -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
> >  
> > -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?
> 
> > +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 \

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

^ permalink raw reply	[flat|nested] 69+ messages in thread

* Re: [PATCH 22/23] kbuild: make wrapper Makefile more convenient for external modules
  2024-09-17 14:16 ` [PATCH 22/23] kbuild: make wrapper Makefile more convenient for external modules Masahiro Yamada
@ 2024-10-04 19:46   ` Nicolas Schier
  0 siblings, 0 replies; 69+ messages in thread
From: Nicolas Schier @ 2024-10-04 19:46 UTC (permalink / raw)
  To: Masahiro Yamada
  Cc: linux-kbuild, Miguel Ojeda, rust-for-linux, linux-kernel,
	Nathan Chancellor

On Tue, Sep 17, 2024 at 11:16:50PM +0900, Masahiro Yamada wrote:
> When 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
> 
> Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
> ---
> 
>  Makefile | 12 +++++++++++-
>  1 file changed, 11 insertions(+), 1 deletion(-)
> 
> diff --git a/Makefile b/Makefile
> index 9f0ba07e8f25..81603a50c757 100644
> --- a/Makefile
> +++ b/Makefile
> @@ -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
>  
> -- 
> 2.43.0

Nice idea, thanks!

Reviewed-by: Nicolas Schier <nicolas@fjasle.eu>

^ permalink raw reply	[flat|nested] 69+ messages in thread

* Re: [PATCH 23/23] kbuild: allow to start building external module in any directory
  2024-09-17 14:16 ` [PATCH 23/23] kbuild: allow to start building external module in any directory Masahiro Yamada
@ 2024-10-04 20:05   ` Nicolas Schier
  0 siblings, 0 replies; 69+ messages in thread
From: Nicolas Schier @ 2024-10-04 20:05 UTC (permalink / raw)
  To: Masahiro Yamada
  Cc: linux-kbuild, Miguel Ojeda, rust-for-linux, linux-kernel,
	Nathan Chancellor

On Tue, Sep 17, 2024 at 11:16:51PM +0900, Masahiro Yamada wrote:
> Unless an explicit O= option is provided, external module builds must
> start from the kernel directory.
> 
> This can be achieved by using the -C option:
> 
>   $ make -C /path/to/kernel M=/path/to/external/module
> 
> This commit allows starting external module builds from any directory,
> so you can also do the following:
> 
>   $ make -f /path/to/kernel/Makefile M=/path/to/external/module
> 
> The key difference is that the -C option changes the working directory
> and parses the Makefile located there, while the -f option only
> specifies the Makefile to use.
> 
> As shown in the examples in Documentation/kbuild/modules.rst, external
> modules usually have a wrapper Makefile that allows you to build them
> without specifying any make arguments. The Makefile typically contains
> a rule as follows:
> 
>     KDIR ?= /path/to/kernel
>     default:
>             $(MAKE) -C $(KDIR) M=$(CURDIR) $(MAKECMDGOALS)
> 
> The log will appear as follows:
> 
>     $ make
>     make -C /path/to/kernel M=/path/to/external/module
>     make[1]: Entering directory '/path/to/kernel'
>     make[2]: Entering directory '/path/to/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.

>     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)/Makefile
> 
> This avoids unnecessary sub-make invocations:
> 
>     $ make
>       CC [M]  helloworld.o
>       MODPOST Module.symvers
>       CC [M]  helloworld.mod.o
>       CC [M]  .module-common.o
>       LD [M]  helloworld.ko
> 
> Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
> ---
> 
>  Documentation/kbuild/modules.rst | 21 +++++++++++++++++++++
>  Makefile                         |  8 ++++++--
>  2 files changed, 27 insertions(+), 2 deletions(-)
> 
> diff --git a/Documentation/kbuild/modules.rst b/Documentation/kbuild/modules.rst
> index 03347e13eeb5..5610cfa426bb 100644
> --- a/Documentation/kbuild/modules.rst
> +++ b/Documentation/kbuild/modules.rst
> @@ -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

KDIR ?= /lib/modules/$(shell uname -r)/build

This patch is another very good idea, thanks!

Reviewed-by: Nicolas Schier <nicolas@fjasle.eu>

> +		export KBUILD_EXTMOD = $(CURDIR)
> +		include $(KDIR)/Makefile
> +
> +
>  3.4 Building Multiple Modules
>  =============================
>  
> diff --git a/Makefile b/Makefile
> index 81603a50c757..88c160ac7b3c 100644
> --- a/Makefile
> +++ b/Makefile
> @@ -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))
> -- 
> 2.43.0
> 

-- 
Nicolas

^ permalink raw reply	[flat|nested] 69+ messages in thread

* Re: [PATCH 00/23] kbuild: support building external modules in a separate build directory
  2024-09-17 14:16 [PATCH 00/23] kbuild: support building external modules in a separate build directory Masahiro Yamada
                   ` (24 preceding siblings ...)
  2024-09-28  6:50 ` Masahiro Yamada
@ 2024-10-04 20:16 ` Nicolas Schier
  25 siblings, 0 replies; 69+ messages in thread
From: Nicolas Schier @ 2024-10-04 20:16 UTC (permalink / raw)
  To: Masahiro Yamada
  Cc: linux-kbuild, Miguel Ojeda, rust-for-linux, linux-kernel,
	Nathan Chancellor

On Tue, Sep 17, 2024 at 11:16:28PM +0900, Masahiro Yamada wrote:
> 
> 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

^ permalink raw reply	[flat|nested] 69+ messages in thread

* Re: [PATCH 19/23] kbuild: support building external modules in a separate build directory
  2024-10-03 19:47   ` Nicolas Schier
  2024-10-04 19:45     ` Nicolas Schier
@ 2024-11-10  1:36     ` Masahiro Yamada
  1 sibling, 0 replies; 69+ messages in thread
From: Masahiro Yamada @ 2024-11-10  1:36 UTC (permalink / raw)
  To: Nicolas Schier
  Cc: linux-kbuild, Miguel Ojeda, rust-for-linux, linux-kernel,
	Nathan Chancellor

On Fri, Oct 4, 2024 at 4:47 AM Nicolas Schier <nicolas@fjasle.eu> wrote:

> > @@ -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.

>
> > +
> >  2.3 Targets
> >  ===========
> >
> > diff --git a/Makefile b/Makefile
> > index 9fbf7ef6e394..b654baa0763a 100644
> > --- a/Makefile
> > +++ b/Makefile
> > @@ -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?


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.






> > -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

^ permalink raw reply	[flat|nested] 69+ messages in thread

* Re: [PATCH 19/23] kbuild: support building external modules in a separate build directory
  2024-10-04 19:45     ` Nicolas Schier
@ 2024-11-10  1:36       ` Masahiro Yamada
  0 siblings, 0 replies; 69+ messages in thread
From: Masahiro Yamada @ 2024-11-10  1:36 UTC (permalink / raw)
  To: Nicolas Schier
  Cc: linux-kbuild, Miguel Ojeda, rust-for-linux, linux-kernel,
	Nathan Chancellor

On Sat, Oct 5, 2024 at 4:45 AM Nicolas Schier <nicolas@fjasle.eu> wrote:

> > > @@ -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 \
>
> 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.

Right, this check does not work correctly
when KBUILD_EXTMOD is a relative path.

In v2, I changed this part.



--
Best Regards
Masahiro Yamada

^ permalink raw reply	[flat|nested] 69+ messages in thread

* Re: [PATCH 18/23] kbuild: remove extmod_prefix, MODORDER, MODULES_NSDEPS variables
  2024-10-03 18:47   ` Nicolas Schier
@ 2024-11-10  1:36     ` Masahiro Yamada
  0 siblings, 0 replies; 69+ messages in thread
From: Masahiro Yamada @ 2024-11-10  1:36 UTC (permalink / raw)
  To: Nicolas Schier
  Cc: linux-kbuild, Miguel Ojeda, rust-for-linux, linux-kernel,
	Nathan Chancellor

On Fri, Oct 4, 2024 at 3:47 AM Nicolas Schier <nicolas@fjasle.eu> wrote:
>
> On Tue, Sep 17, 2024 at 11:16:46PM +0900, Masahiro Yamada wrote:
> > 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 <masahiroy@kernel.org>
> > ---
> >
> >  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 FORCE

Right, I will clean up these.



> W/ or w/o:
>
> Reviewed-by: Nicolas Schier <nicolas@fjasle.eu>



--
Best Regards
Masahiro Yamada

^ permalink raw reply	[flat|nested] 69+ messages in thread

* Re: [PATCH 21/23] kbuild: use absolute path in the generated wrapper Makefile
  2024-09-17 14:16 ` [PATCH 21/23] kbuild: use absolute path in the generated wrapper Makefile Masahiro Yamada
  2024-10-04 19:02   ` Nicolas Schier
@ 2024-11-29 23:31   ` Caleb Connolly
  2024-11-30  9:19     ` Masahiro Yamada
  1 sibling, 1 reply; 69+ messages in thread
From: Caleb Connolly @ 2024-11-29 23:31 UTC (permalink / raw)
  To: Masahiro Yamada, linux-kbuild
  Cc: Miguel Ojeda, rust-for-linux, linux-kernel, Nathan Chancellor,
	Nicolas Schier, Stephan Gerhold

Hi Masahiro,

On 9/17/24 16:16, Masahiro Yamada wrote:
> Keep the consistent behavior when this Makefile is 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

https://wiki.postmarketos.org/wiki/Compiling_kernels_with_envkernel.sh#Packaging_kernels_built_without_envkernel

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
> 
> Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
> ---
> 
>   Makefile | 5 +++--
>   1 file changed, 3 insertions(+), 2 deletions(-)
> 
> diff --git a/Makefile b/Makefile
> index ce646a6994a6..9f0ba07e8f25 100644
> --- a/Makefile
> +++ b/Makefile
> @@ -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:


^ permalink raw reply	[flat|nested] 69+ messages in thread

* Re: [PATCH 21/23] kbuild: use absolute path in the generated wrapper Makefile
  2024-11-29 23:31   ` Caleb Connolly
@ 2024-11-30  9:19     ` Masahiro Yamada
  0 siblings, 0 replies; 69+ messages in thread
From: Masahiro Yamada @ 2024-11-30  9:19 UTC (permalink / raw)
  To: Caleb Connolly
  Cc: linux-kbuild, Miguel Ojeda, rust-for-linux, linux-kernel,
	Nathan Chancellor, Nicolas Schier, Stephan Gerhold

On Sat, Nov 30, 2024 at 8:31 AM Caleb Connolly
<caleb.connolly@linaro.org> wrote:
>
> Hi Masahiro,
>
> On 9/17/24 16:16, Masahiro Yamada wrote:
> > Keep the consistent behavior when this Makefile is 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
>
> https://wiki.postmarketos.org/wiki/Compiling_kernels_with_envkernel.sh#Packaging_kernels_built_without_envkernel
>
> 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.


>
> 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.


> I wouldn't be surprised if other folks with other build systems get
> bitten by this too.
>
> Kind regards,
> Caleb
> >
> > Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
> > ---
> >
> >   Makefile | 5 +++--
> >   1 file changed, 3 insertions(+), 2 deletions(-)
> >
> > diff --git a/Makefile b/Makefile
> > index ce646a6994a6..9f0ba07e8f25 100644
> > --- a/Makefile
> > +++ b/Makefile
> > @@ -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:
>


-- 
Best Regards
Masahiro Yamada

^ permalink raw reply	[flat|nested] 69+ messages in thread

end of thread, other threads:[~2024-11-30  9:19 UTC | newest]

Thread overview: 69+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-09-17 14:16 [PATCH 00/23] kbuild: support building external modules in a separate build directory Masahiro Yamada
2024-09-17 14:16 ` [PATCH 01/23] kbuild: doc: update the description about Kbuild/Makefile split Masahiro Yamada
2024-09-18 15:35   ` Nicolas Schier
2024-09-17 14:16 ` [PATCH 02/23] kbuild: doc: remove description about grepping CONFIG options Masahiro Yamada
2024-09-18 15:35   ` Nicolas Schier
2024-09-17 14:16 ` [PATCH 03/23] kbuild: doc: remove outdated description of the limitation on -I usage Masahiro Yamada
2024-09-18 15:38   ` Nicolas Schier
2024-09-17 14:16 ` [PATCH 04/23] kbuild: doc: remove the description about shipped files Masahiro Yamada
2024-09-18 16:24   ` Nicolas Schier
2024-09-19 16:53     ` Masahiro Yamada
2024-09-17 14:16 ` [PATCH 05/23] kbuild: doc: describe the -C option precisely for external module builds Masahiro Yamada
2024-09-18 16:44   ` Nicolas Schier
2024-09-18 16:47   ` Nicolas Schier
2024-09-17 14:16 ` [PATCH 06/23] kbuild: doc: replace "gcc" in external module description Masahiro Yamada
2024-09-19 11:22   ` Nicolas Schier
2024-09-17 14:16 ` [PATCH 07/23] kbuild: remove unnecessary prune of rust/alloc for rustfmt Masahiro Yamada
2024-09-19 11:23   ` Nicolas Schier
2024-09-19 13:46     ` Nicolas Schier
2024-09-30 14:29       ` Miguel Ojeda
2024-09-17 14:16 ` [PATCH 08/23] kbuild: simplify find command " Masahiro Yamada
2024-09-19 13:54   ` Nicolas Schier
2024-09-19 13:59     ` Masahiro Yamada
2024-09-30 18:37   ` Miguel Ojeda
2024-10-01  2:18     ` Masahiro Yamada
2024-10-01  5:45       ` Miguel Ojeda
2024-09-17 14:16 ` [PATCH 09/23] speakup: use SPKDIR=$(src) to specify the source directory Masahiro Yamada
2024-10-03 18:25   ` Nicolas Schier
2024-09-17 14:16 ` [PATCH 10/23] kbuild: refactor the check for missing config files Masahiro Yamada
2024-09-25 14:04   ` Nicolas Schier
2024-09-17 14:16 ` [PATCH 11/23] kbuild: check the presence of include/generated/rustc_cfg Masahiro Yamada
2024-09-25 14:10   ` Nicolas Schier
2024-09-30 14:37   ` Miguel Ojeda
2024-09-17 14:16 ` [PATCH 12/23] scripts/nsdeps: use VPATH as src_prefix Masahiro Yamada
2024-09-25 14:18   ` Nicolas Schier
2024-09-26  7:30     ` Nicolas Schier
2024-09-17 14:16 ` [PATCH 13/23] kbuild: replace two $(abs_objtree) with $(CURDIR) in top Makefile Masahiro Yamada
2024-09-25 14:27   ` Nicolas Schier
2024-09-17 14:16 ` [PATCH 14/23] kbuild: add $(objtree)/ prefix to some in-kernel build artifacts Masahiro Yamada
2024-09-26  7:50   ` Nicolas Schier
2024-09-17 14:16 ` [PATCH 15/23] kbuild: rename abs_objtree to abs_output Masahiro Yamada
2024-09-26  7:51   ` Nicolas Schier
2024-09-17 14:16 ` [PATCH 16/23] kbuild: use 'output' variable to create the output directory Masahiro Yamada
2024-09-26  7:54   ` Nicolas Schier
2024-09-17 14:16 ` [PATCH 17/23] kbuild: build external modules in their directory Masahiro Yamada
2024-10-03 18:41   ` Nicolas Schier
2024-09-17 14:16 ` [PATCH 18/23] kbuild: remove extmod_prefix, MODORDER, MODULES_NSDEPS variables Masahiro Yamada
2024-10-03 18:47   ` Nicolas Schier
2024-11-10  1:36     ` Masahiro Yamada
2024-09-17 14:16 ` [PATCH 19/23] kbuild: support building external modules in a separate build directory Masahiro Yamada
2024-10-03 19:47   ` Nicolas Schier
2024-10-04 19:45     ` Nicolas Schier
2024-11-10  1:36       ` Masahiro Yamada
2024-11-10  1:36     ` Masahiro Yamada
2024-09-17 14:16 ` [PATCH 20/23] kbuild: support -fmacro-prefix-map for external modules Masahiro Yamada
2024-10-04 18:51   ` Nicolas Schier
2024-09-17 14:16 ` [PATCH 21/23] kbuild: use absolute path in the generated wrapper Makefile Masahiro Yamada
2024-10-04 19:02   ` Nicolas Schier
2024-11-29 23:31   ` Caleb Connolly
2024-11-30  9:19     ` Masahiro Yamada
2024-09-17 14:16 ` [PATCH 22/23] kbuild: make wrapper Makefile more convenient for external modules Masahiro Yamada
2024-10-04 19:46   ` Nicolas Schier
2024-09-17 14:16 ` [PATCH 23/23] kbuild: allow to start building external module in any directory Masahiro Yamada
2024-10-04 20:05   ` Nicolas Schier
2024-09-20 10:39 ` [PATCH 00/23] kbuild: support building external modules in a separate build directory Nicolas Schier
2024-09-20 12:58   ` Masahiro Yamada
2024-09-20 13:41     ` Nicolas Schier
2024-09-28  6:50 ` Masahiro Yamada
2024-09-30 14:28   ` Miguel Ojeda
2024-10-04 20:16 ` Nicolas Schier

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).