diff mbox series

[05/23] kbuild: doc: describe the -C option precisely for external module builds

Message ID 20240917141725.466514-6-masahiroy@kernel.org (mailing list archive)
State New
Headers show
Series kbuild: support building external modules in a separate build directory | expand

Commit Message

Masahiro Yamada Sept. 17, 2024, 2:16 p.m. UTC
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(-)

Comments

Nicolas Schier Sept. 18, 2024, 4:44 p.m. UTC | #1
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>
Nicolas Schier Sept. 18, 2024, 4:47 p.m. UTC | #2
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>
diff mbox series

Patch

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.