Skip to content

Commit 11b3d51

Browse files
committed
kbuild: support building external modules in a separate build directory
There has been a long-standing request to support building external modules in a separate build directory. This commit introduces a new environment variable, KBUILD_EXTMOD_OUTPUT, and its shorthand Make variable, MO. A simple usage: $ make -C <kernel-dir> M=<module-src-dir> MO=<module-build-dir> Signed-off-by: Masahiro Yamada <[email protected]> Reviewed-by: Nicolas Schier <[email protected]>
1 parent bad6beb commit 11b3d51

File tree

5 files changed

+35
-11
lines changed

5 files changed

+35
-11
lines changed

Documentation/kbuild/kbuild.rst

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,12 +137,18 @@ Specify the output directory when building the kernel.
137137
This variable can also be used to point to the kernel output directory when
138138
building external modules against a pre-built kernel in a separate build
139139
directory. Please note that this does NOT specify the output directory for the
140-
external modules themselves.
140+
external modules themselves. (Use KBUILD_EXTMOD_OUTPUT for that purpose.)
141141

142142
The output directory can also be specified using "O=...".
143143

144144
Setting "O=..." takes precedence over KBUILD_OUTPUT.
145145

146+
KBUILD_EXTMOD_OUTPUT
147+
--------------------
148+
Specify the output directory for external modules.
149+
150+
Setting "MO=..." takes precedence over KBUILD_EXTMOD_OUTPUT.
151+
146152
KBUILD_EXTRA_WARN
147153
-----------------
148154
Specify the extra build checks. The same value can be assigned by passing

Documentation/kbuild/modules.rst

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,10 @@ Options
6666
of the kernel output directory if the kernel was built in a separate
6767
build directory.)
6868

69-
make -C $KDIR M=$PWD
69+
You can optionally pass MO= option if you want to build the modules in
70+
a separate directory.
71+
72+
make -C $KDIR M=$PWD [MO=$BUILD_DIR]
7073

7174
-C $KDIR
7275
The directory that contains the kernel and relevant build
@@ -80,6 +83,9 @@ Options
8083
directory where the external module (kbuild file) is
8184
located.
8285

86+
MO=$BUILD_DIR
87+
Specifies a separate output directory for the external module.
88+
8389
Targets
8490
-------
8591

Makefile

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,10 @@ ifeq ("$(origin M)", "command line")
134134
KBUILD_EXTMOD := $(M)
135135
endif
136136

137+
ifeq ("$(origin MO)", "command line")
138+
KBUILD_EXTMOD_OUTPUT := $(MO)
139+
endif
140+
137141
$(if $(word 2, $(KBUILD_EXTMOD)), \
138142
$(error building multiple external modules is not supported))
139143

@@ -187,7 +191,7 @@ ifdef KBUILD_EXTMOD
187191
else
188192
objtree := $(CURDIR)
189193
endif
190-
output := $(KBUILD_EXTMOD)
194+
output := $(or $(KBUILD_EXTMOD_OUTPUT),$(KBUILD_EXTMOD))
191195
# KBUILD_EXTMOD might be a relative path. Remember its absolute path before
192196
# Make changes the working directory.
193197
srcroot := $(realpath $(KBUILD_EXTMOD))
@@ -645,6 +649,7 @@ quiet_cmd_makefile = GEN Makefile
645649
} > Makefile
646650

647651
outputmakefile:
652+
ifeq ($(KBUILD_EXTMOD),)
648653
@if [ -f $(srctree)/.config -o \
649654
-d $(srctree)/include/config -o \
650655
-d $(srctree)/arch/$(SRCARCH)/include/generated ]; then \
@@ -654,7 +659,16 @@ outputmakefile:
654659
echo >&2 "***"; \
655660
false; \
656661
fi
657-
$(Q)ln -fsn $(srctree) source
662+
else
663+
@if [ -f $(srcroot)/modules.order ]; then \
664+
echo >&2 "***"; \
665+
echo >&2 "*** The external module source tree is not clean."; \
666+
echo >&2 "*** Please run 'make -C $(abs_srctree) M=$(realpath $(srcroot)) clean'"; \
667+
echo >&2 "***"; \
668+
false; \
669+
fi
670+
endif
671+
$(Q)ln -fsn $(srcroot) source
658672
$(call cmd,makefile)
659673
$(Q)test -e .gitignore || \
660674
{ echo "# this is build directory, ignore it"; echo "*"; } > .gitignore
@@ -1940,6 +1954,8 @@ KBUILD_MODULES := 1
19401954

19411955
endif
19421956

1957+
prepare: outputmakefile
1958+
19431959
# Preset locale variables to speed up the build process. Limit locale
19441960
# tweaks to this spot to avoid wrong language settings when running
19451961
# make menuconfig etc.

scripts/Makefile.host

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,10 @@ hostrust_flags = --out-dir $(dir $@) --emit=dep-info=$(depfile) \
9696
$(KBUILD_HOSTRUSTFLAGS) $(HOST_EXTRARUSTFLAGS) \
9797
$(HOSTRUSTFLAGS_$(target-stem))
9898

99-
# $(objtree)/$(obj) for including generated headers from checkin source files
100-
ifeq ($(KBUILD_EXTMOD),)
99+
# $(obj) for including generated headers from checkin source files
101100
ifdef building_out_of_srctree
102-
hostc_flags += -I $(objtree)/$(obj)
103-
hostcxx_flags += -I $(objtree)/$(obj)
104-
endif
101+
hostc_flags += -I $(obj)
102+
hostcxx_flags += -I $(obj)
105103
endif
106104

107105
#####

scripts/Makefile.lib

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,13 +213,11 @@ endif
213213

214214
# $(src) for including checkin headers from generated source files
215215
# $(obj) for including generated headers from checkin source files
216-
ifeq ($(KBUILD_EXTMOD),)
217216
ifdef building_out_of_srctree
218217
_c_flags += $(addprefix -I, $(src) $(obj))
219218
_a_flags += $(addprefix -I, $(src) $(obj))
220219
_cpp_flags += $(addprefix -I, $(src) $(obj))
221220
endif
222-
endif
223221

224222
# If $(is-kernel-object) is 'y', this object will be linked to vmlinux or modules
225223
is-kernel-object = $(or $(part-of-builtin),$(part-of-module))

0 commit comments

Comments
 (0)