diff options
author | Masahiro Yamada <yamada.masahiro@socionext.com> | 2019-03-30 08:04:14 -0400 |
---|---|---|
committer | Masahiro Yamada <yamada.masahiro@socionext.com> | 2019-04-02 10:27:15 -0400 |
commit | 25b146c5b8ceecd43c311552d325a4e403c639f2 (patch) | |
tree | e8d022671481b5add2d94347c8f6355e24178f8f /Makefile | |
parent | 6b1a9a02c72fdb6e1d67949bb01b66e07d88ace9 (diff) |
kbuild: allow Kbuild to start from any directory
Kbuild always runs in the top of the output directory.
If Make starts in the source directory with O=, it relocates the
working directory to the location specified by O=.
Also, users can start build from the output directory by using the
Makefile generated by scripts/mkmakefile.
With a little more effort, Kbuild will be able to start from any
directory path.
This commit allows to specify the source directory by using
the -f option.
For example, you can do:
$ cd path/to/output/dir
$ make -f path/to/source/dir/Makefile
Or, for the equivalent behavior, you can do:
$ make O=path/to/output/dir -f path/to/source/dir/Makefile
KBUILD_SRC is now deprecated.
Signed-off-by: Masahiro Yamada <yamada.masahiro@socionext.com>
Reviewed-by: Kieran Bingham <kbingham@kernel.org>
Diffstat (limited to 'Makefile')
-rw-r--r-- | Makefile | 87 |
1 files changed, 50 insertions, 37 deletions
@@ -96,56 +96,65 @@ endif | |||
96 | 96 | ||
97 | export quiet Q KBUILD_VERBOSE | 97 | export quiet Q KBUILD_VERBOSE |
98 | 98 | ||
99 | # kbuild supports saving output files in a separate directory. | 99 | # Kbuild will save output files in the current working directory. |
100 | # To locate output files in a separate directory two syntaxes are supported. | 100 | # This does not need to match to the root of the kernel source tree. |
101 | # In both cases the working directory must be the root of the kernel src. | 101 | # |
102 | # For example, you can do this: | ||
103 | # | ||
104 | # cd /dir/to/store/output/files; make -f /dir/to/kernel/source/Makefile | ||
105 | # | ||
106 | # If you want to save output files in a different location, there are | ||
107 | # two syntaxes to specify it. | ||
108 | # | ||
102 | # 1) O= | 109 | # 1) O= |
103 | # Use "make O=dir/to/store/output/files/" | 110 | # Use "make O=dir/to/store/output/files/" |
104 | # | 111 | # |
105 | # 2) Set KBUILD_OUTPUT | 112 | # 2) Set KBUILD_OUTPUT |
106 | # Set the environment variable KBUILD_OUTPUT to point to the directory | 113 | # Set the environment variable KBUILD_OUTPUT to point to the output directory. |
107 | # where the output files shall be placed. | 114 | # export KBUILD_OUTPUT=dir/to/store/output/files/; make |
108 | # export KBUILD_OUTPUT=dir/to/store/output/files/ | ||
109 | # make | ||
110 | # | 115 | # |
111 | # The O= assignment takes precedence over the KBUILD_OUTPUT environment | 116 | # The O= assignment takes precedence over the KBUILD_OUTPUT environment |
112 | # variable. | 117 | # variable. |
113 | 118 | ||
114 | # KBUILD_SRC is not intended to be used by the regular user (for now), | 119 | # Do we want to change the working directory? |
115 | # it is set on invocation of make with KBUILD_OUTPUT or O= specified. | ||
116 | |||
117 | # OK, Make called in directory where kernel src resides | ||
118 | # Do we want to locate output files in a separate directory? | ||
119 | ifeq ("$(origin O)", "command line") | 120 | ifeq ("$(origin O)", "command line") |
120 | KBUILD_OUTPUT := $(O) | 121 | KBUILD_OUTPUT := $(O) |
121 | endif | 122 | endif |
122 | 123 | ||
123 | ifneq ($(words $(subst :, ,$(CURDIR))), 1) | 124 | ifneq ($(KBUILD_OUTPUT),) |
124 | $(error main directory cannot contain spaces nor colons) | 125 | # Make's built-in functions such as $(abspath ...), $(realpath ...) cannot |
126 | # expand a shell special character '~'. We use a somewhat tedious way here. | ||
127 | abs_objtree := $(shell mkdir -p $(KBUILD_OUTPUT) && cd $(KBUILD_OUTPUT) && pwd) | ||
128 | $(if $(abs_objtree),, \ | ||
129 | $(error failed to create output directory "$(KBUILD_OUTPUT)")) | ||
130 | |||
131 | # $(realpath ...) resolves symlinks | ||
132 | abs_objtree := $(realpath $(abs_objtree)) | ||
133 | else | ||
134 | abs_objtree := $(CURDIR) | ||
135 | endif # ifneq ($(KBUILD_OUTPUT),) | ||
136 | |||
137 | ifeq ($(abs_objtree),$(CURDIR)) | ||
138 | # Suppress "Entering directory ..." unless we are changing the work directory. | ||
139 | MAKEFLAGS += --no-print-directory | ||
140 | else | ||
141 | need-sub-make := 1 | ||
125 | endif | 142 | endif |
126 | 143 | ||
127 | ifneq ($(KBUILD_OUTPUT),) | 144 | abs_srctree := $(realpath $(dir $(lastword $(MAKEFILE_LIST)))) |
128 | # check that the output directory actually exists | 145 | |
129 | saved-output := $(KBUILD_OUTPUT) | 146 | ifneq ($(words $(subst :, ,$(abs_srctree))), 1) |
130 | KBUILD_OUTPUT := $(shell mkdir -p $(KBUILD_OUTPUT) && cd $(KBUILD_OUTPUT) \ | 147 | $(error source directory cannot contain spaces or colons) |
131 | && pwd) | 148 | endif |
132 | $(if $(KBUILD_OUTPUT),, \ | ||
133 | $(error failed to create output directory "$(saved-output)")) | ||
134 | 149 | ||
150 | ifneq ($(abs_srctree),$(abs_objtree)) | ||
135 | # Look for make include files relative to root of kernel src | 151 | # Look for make include files relative to root of kernel src |
136 | # | 152 | # |
137 | # This does not become effective immediately because MAKEFLAGS is re-parsed | 153 | # This does not become effective immediately because MAKEFLAGS is re-parsed |
138 | # once after the Makefile is read. It is OK since we are going to invoke | 154 | # once after the Makefile is read. We need to invoke sub-make. |
139 | # 'sub-make' below. | 155 | MAKEFLAGS += --include-dir=$(abs_srctree) |
140 | MAKEFLAGS += --include-dir=$(CURDIR) | ||
141 | |||
142 | need-sub-make := 1 | 156 | need-sub-make := 1 |
143 | else | 157 | endif |
144 | |||
145 | # Do not print "Entering directory ..." at all for in-tree build. | ||
146 | MAKEFLAGS += --no-print-directory | ||
147 | |||
148 | endif # ifneq ($(KBUILD_OUTPUT),) | ||
149 | 158 | ||
150 | ifneq ($(filter 3.%,$(MAKE_VERSION)),) | 159 | ifneq ($(filter 3.%,$(MAKE_VERSION)),) |
151 | # 'MAKEFLAGS += -rR' does not immediately become effective for GNU Make 3.x | 160 | # 'MAKEFLAGS += -rR' does not immediately become effective for GNU Make 3.x |
@@ -155,6 +164,7 @@ need-sub-make := 1 | |||
155 | $(lastword $(MAKEFILE_LIST)): ; | 164 | $(lastword $(MAKEFILE_LIST)): ; |
156 | endif | 165 | endif |
157 | 166 | ||
167 | export abs_srctree abs_objtree | ||
158 | export sub_make_done := 1 | 168 | export sub_make_done := 1 |
159 | 169 | ||
160 | ifeq ($(need-sub-make),1) | 170 | ifeq ($(need-sub-make),1) |
@@ -166,9 +176,7 @@ $(filter-out _all sub-make $(lastword $(MAKEFILE_LIST)), $(MAKECMDGOALS)) _all: | |||
166 | 176 | ||
167 | # Invoke a second make in the output directory, passing relevant variables | 177 | # Invoke a second make in the output directory, passing relevant variables |
168 | sub-make: | 178 | sub-make: |
169 | $(Q)$(MAKE) \ | 179 | $(Q)$(MAKE) -C $(abs_objtree) -f $(abs_srctree)/Makefile $(MAKECMDGOALS) |
170 | $(if $(KBUILD_OUTPUT),-C $(KBUILD_OUTPUT) KBUILD_SRC=$(CURDIR)) \ | ||
171 | -f $(CURDIR)/Makefile $(MAKECMDGOALS) | ||
172 | 180 | ||
173 | endif # need-sub-make | 181 | endif # need-sub-make |
174 | endif # sub_make_done | 182 | endif # sub_make_done |
@@ -213,16 +221,21 @@ ifeq ("$(origin M)", "command line") | |||
213 | KBUILD_EXTMOD := $(M) | 221 | KBUILD_EXTMOD := $(M) |
214 | endif | 222 | endif |
215 | 223 | ||
216 | ifeq ($(KBUILD_SRC),) | 224 | ifeq ($(abs_srctree),$(abs_objtree)) |
217 | # building in the source tree | 225 | # building in the source tree |
218 | srctree := . | 226 | srctree := . |
219 | else | 227 | else |
220 | ifeq ($(KBUILD_SRC)/,$(dir $(CURDIR))) | 228 | ifeq ($(abs_srctree)/,$(dir $(abs_objtree))) |
221 | # building in a subdirectory of the source tree | 229 | # building in a subdirectory of the source tree |
222 | srctree := .. | 230 | srctree := .. |
223 | else | 231 | else |
224 | srctree := $(KBUILD_SRC) | 232 | srctree := $(abs_srctree) |
225 | endif | 233 | endif |
234 | |||
235 | # TODO: | ||
236 | # KBUILD_SRC is only used to distinguish in-tree/out-of-tree build. | ||
237 | # Replace it with $(srctree) or something. | ||
238 | KBUILD_SRC := $(abs_srctree) | ||
226 | endif | 239 | endif |
227 | 240 | ||
228 | export KBUILD_CHECKSRC KBUILD_EXTMOD KBUILD_SRC | 241 | export KBUILD_CHECKSRC KBUILD_EXTMOD KBUILD_SRC |