summaryrefslogtreecommitdiffstats
path: root/Makefile
diff options
context:
space:
mode:
authorMasahiro Yamada <yamada.masahiro@socionext.com>2019-03-30 08:04:14 -0400
committerMasahiro Yamada <yamada.masahiro@socionext.com>2019-04-02 10:27:15 -0400
commit25b146c5b8ceecd43c311552d325a4e403c639f2 (patch)
treee8d022671481b5add2d94347c8f6355e24178f8f /Makefile
parent6b1a9a02c72fdb6e1d67949bb01b66e07d88ace9 (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--Makefile87
1 files changed, 50 insertions, 37 deletions
diff --git a/Makefile b/Makefile
index 16c77f6cc343..4929c1f41cfa 100644
--- a/Makefile
+++ b/Makefile
@@ -96,56 +96,65 @@ endif
96 96
97export quiet Q KBUILD_VERBOSE 97export 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?
119ifeq ("$(origin O)", "command line") 120ifeq ("$(origin O)", "command line")
120 KBUILD_OUTPUT := $(O) 121 KBUILD_OUTPUT := $(O)
121endif 122endif
122 123
123ifneq ($(words $(subst :, ,$(CURDIR))), 1) 124ifneq ($(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.
127abs_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
132abs_objtree := $(realpath $(abs_objtree))
133else
134abs_objtree := $(CURDIR)
135endif # ifneq ($(KBUILD_OUTPUT),)
136
137ifeq ($(abs_objtree),$(CURDIR))
138# Suppress "Entering directory ..." unless we are changing the work directory.
139MAKEFLAGS += --no-print-directory
140else
141need-sub-make := 1
125endif 142endif
126 143
127ifneq ($(KBUILD_OUTPUT),) 144abs_srctree := $(realpath $(dir $(lastword $(MAKEFILE_LIST))))
128# check that the output directory actually exists 145
129saved-output := $(KBUILD_OUTPUT) 146ifneq ($(words $(subst :, ,$(abs_srctree))), 1)
130KBUILD_OUTPUT := $(shell mkdir -p $(KBUILD_OUTPUT) && cd $(KBUILD_OUTPUT) \ 147$(error source directory cannot contain spaces or colons)
131 && pwd) 148endif
132$(if $(KBUILD_OUTPUT),, \
133 $(error failed to create output directory "$(saved-output)"))
134 149
150ifneq ($(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. 155MAKEFLAGS += --include-dir=$(abs_srctree)
140MAKEFLAGS += --include-dir=$(CURDIR)
141
142need-sub-make := 1 156need-sub-make := 1
143else 157endif
144
145# Do not print "Entering directory ..." at all for in-tree build.
146MAKEFLAGS += --no-print-directory
147
148endif # ifneq ($(KBUILD_OUTPUT),)
149 158
150ifneq ($(filter 3.%,$(MAKE_VERSION)),) 159ifneq ($(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)): ;
156endif 165endif
157 166
167export abs_srctree abs_objtree
158export sub_make_done := 1 168export sub_make_done := 1
159 169
160ifeq ($(need-sub-make),1) 170ifeq ($(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
168sub-make: 178sub-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
173endif # need-sub-make 181endif # need-sub-make
174endif # sub_make_done 182endif # sub_make_done
@@ -213,16 +221,21 @@ ifeq ("$(origin M)", "command line")
213 KBUILD_EXTMOD := $(M) 221 KBUILD_EXTMOD := $(M)
214endif 222endif
215 223
216ifeq ($(KBUILD_SRC),) 224ifeq ($(abs_srctree),$(abs_objtree))
217 # building in the source tree 225 # building in the source tree
218 srctree := . 226 srctree := .
219else 227else
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)
226endif 239endif
227 240
228export KBUILD_CHECKSRC KBUILD_EXTMOD KBUILD_SRC 241export KBUILD_CHECKSRC KBUILD_EXTMOD KBUILD_SRC