From patchwork Sat Mar 30 12:04:14 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Masahiro Yamada X-Patchwork-Id: 161495 Delivered-To: patch@linaro.org Received: by 2002:a02:c6d8:0:0:0:0:0 with SMTP id r24csp3168309jan; Sat, 30 Mar 2019 05:04:34 -0700 (PDT) X-Google-Smtp-Source: APXvYqyyVG+h6UAqHcl45zsVGniJnGdjmrRBuuNtXtRlUglbQM+fx1IHtpQqF0kC2QwsSUJpY/c+ X-Received: by 2002:a17:902:2888:: with SMTP id f8mr54994537plb.244.1553947474291; Sat, 30 Mar 2019 05:04:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1553947474; cv=none; d=google.com; s=arc-20160816; b=LOgZuvIUGKaSwt9xchqhJyhCanfVDxlPcI06Vi7PuJzqk3nV5zzn1aliQ2ZW7/LAvI rsdmQcf7ZQrtLZexG3UCsG3Aox+aboHFDj1PHLX118iEeaPS9D44PjXSOujWPy4jx2vy 4ArkgIN3CWoT9qPyi9Of0Mv4PqJhLUnFLyrudXr4IY9fJrZdnwWG1qcN8XKHIxugDEBu 9SpSf35x5blxp3Nn1vpIyVm5Qvo0H8MLcToWOMqIGhgB2cI6Xeh2TxngI6XLkAkQH1n9 UhuBVMii20RaRnzm60ENOMb7kG4hsYJeJk79QAtKokCy1VdQ1/3hxSvZ0OALKvpYkaZ8 d9pw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:dkim-filter; bh=9yEvR1x1Ke1YeqsC6yVRM/WecwsH6o1PtvvtPG1nSUQ=; b=fF+E/PNANFMurAGJXRkwwDDr7x7owZ0ipVyKbKNUeRtaQnKozKV6Nplt5WifW074CZ P0daMBzEeKqEu8/I6OnJBEj1Hzfnx86LbLoXXlSUQv5X/148BnzC7dF68od7559T6Xbl k/G1fhbteEfJfvjpf5ZKsT+dnlVsCsm9/1BsF15WLHqMNIzBpa7H/YeQ+80lf2eMM+NR LLXXTWhyWt2QJU4NGA3bK8a1x2atf8syhqfurMrTxJlDlK7cp9M34Xzt39K2bNLHGrmW JGPoT8O8axavoIdK2jVlvbZBwinCmwsqS0oMW1upvUs3zPtd9HlI6gf02FefO/uo2CkU SqdA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@nifty.com header.s=dec2015msa header.b=be+xqpt4; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id u10si4193960pgr.112.2019.03.30.05.04.33; Sat, 30 Mar 2019 05:04:34 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@nifty.com header.s=dec2015msa header.b=be+xqpt4; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730794AbfC3MEc (ORCPT + 31 others); Sat, 30 Mar 2019 08:04:32 -0400 Received: from conuserg-10.nifty.com ([210.131.2.77]:36039 "EHLO conuserg-10.nifty.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730384AbfC3ME3 (ORCPT ); Sat, 30 Mar 2019 08:04:29 -0400 Received: from grover.tkatk1.zaq.ne.jp (zaqdadce369.zaq.ne.jp [218.220.227.105]) (authenticated) by conuserg-10.nifty.com with ESMTP id x2UC4MsO021262; Sat, 30 Mar 2019 21:04:24 +0900 DKIM-Filter: OpenDKIM Filter v2.10.3 conuserg-10.nifty.com x2UC4MsO021262 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nifty.com; s=dec2015msa; t=1553947464; bh=9yEvR1x1Ke1YeqsC6yVRM/WecwsH6o1PtvvtPG1nSUQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=be+xqpt4jaJ3qrr97pGw11Fpjmu10JPmv/0RBeDuakDS0goO3ig+QYLlkOGa+AS0I k+2kXOuJoG17veDZq5bLFoNGpq2Y2706trYiROYCzmEZX42o+jemjLcf49ngm6RpCQ 6vFVGYcewA9wvF2dJHWH65btor1MiSTH2UELE4bcpBtpaKQra0hT8lNCO4dM4zh+za nyVR7h3pAsRj5uTiqcvFYpomO1fpU2vcFC9uTYMb+92ZD1OIDm+MOYqYVb6TSCHTgz 453WLgGQz1vJ5nzp/Cp0yX/4LBxQShaa3VUrI8MKcU4y8izKa3qdye2jVch20Qcb4I q3ZcBiLZVVgzA== X-Nifty-SrcIP: [218.220.227.105] From: Masahiro Yamada To: linux-kbuild@vger.kernel.org Cc: Masahiro Yamada , Michal Marek , linux-kernel@vger.kernel.org Subject: [PATCH 2/6] kbuild: allow Kbuild to start from any directory Date: Sat, 30 Mar 2019 21:04:14 +0900 Message-Id: <1553947458-8311-2-git-send-email-yamada.masahiro@socionext.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1553947458-8311-1-git-send-email-yamada.masahiro@socionext.com> References: <1553947458-8311-1-git-send-email-yamada.masahiro@socionext.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 --- Makefile | 87 +++++++++++++++++++++++++++++++++++++--------------------------- 1 file changed, 50 insertions(+), 37 deletions(-) -- 2.7.4 Reviewed-by: Kieran Bingham diff --git a/Makefile b/Makefile index 9cbd367..1b2a70e 100644 --- a/Makefile +++ b/Makefile @@ -96,56 +96,65 @@ endif export quiet Q KBUILD_VERBOSE -# kbuild supports saving output files in a separate directory. -# To locate output files in a separate directory two syntaxes are supported. -# In both cases the working directory must be the root of the kernel src. +# Kbuild will save output files in the current working directory. +# This does not need to match to the root of the kernel source tree. +# +# For example, you can do this: +# +# cd /dir/to/store/output/files; make -f /dir/to/kernel/source/Makefile +# +# If you want to save output files in a different location, there are +# two syntaxes to specify it. +# # 1) O= # Use "make O=dir/to/store/output/files/" # # 2) Set KBUILD_OUTPUT -# Set the environment variable KBUILD_OUTPUT to point to the directory -# where the output files shall be placed. -# export KBUILD_OUTPUT=dir/to/store/output/files/ -# make +# Set the environment variable KBUILD_OUTPUT to point to the output directory. +# export KBUILD_OUTPUT=dir/to/store/output/files/; make # # The O= assignment takes precedence over the KBUILD_OUTPUT environment # variable. -# KBUILD_SRC is not intended to be used by the regular user (for now), -# it is set on invocation of make with KBUILD_OUTPUT or O= specified. - -# OK, Make called in directory where kernel src resides -# Do we want to locate output files in a separate directory? +# Do we want to change the working directory? ifeq ("$(origin O)", "command line") KBUILD_OUTPUT := $(O) endif -ifneq ($(words $(subst :, ,$(CURDIR))), 1) - $(error main directory cannot contain spaces nor colons) +ifneq ($(KBUILD_OUTPUT),) +# Make's built-in functions such as $(abspath ...), $(realpath ...) cannot +# expand a shell special character '~'. We use a bit tredious way to handle it. +abs_objtree := $(shell mkdir -p $(KBUILD_OUTPUT) && cd $(KBUILD_OUTPUT) && pwd) +$(if $(abs_objtree),, \ + $(error failed to create output directory "$(KBUILD_OUTPUT)")) + +# $(realpath ...) resolves symlinks +abs_objtree := $(realpath $(abs_objtree)) +else +abs_objtree := $(CURDIR) +endif # ifneq ($(KBUILD_OUTPUT),) + +ifeq ($(abs_objtree),$(CURDIR)) +# Suppress "Entering directory ..." unless we are changing the work directory. +MAKEFLAGS += --no-print-directory +else +need-sub-make := 1 endif -ifneq ($(KBUILD_OUTPUT),) -# check that the output directory actually exists -saved-output := $(KBUILD_OUTPUT) -KBUILD_OUTPUT := $(shell mkdir -p $(KBUILD_OUTPUT) && cd $(KBUILD_OUTPUT) \ - && pwd) -$(if $(KBUILD_OUTPUT),, \ - $(error failed to create output directory "$(saved-output)")) +abs_srctree := $(realpath $(dir $(lastword $(MAKEFILE_LIST)))) + +ifneq ($(words $(subst :, ,$(abs_srctree))), 1) +$(error source directory cannot contain spaces or colons) +endif +ifneq ($(abs_srctree),$(abs_objtree)) # Look for make include files relative to root of kernel src # # This does not become effective immediately because MAKEFLAGS is re-parsed -# once after the Makefile is read. It is OK since we are going to invoke -# 'sub-make' below. -MAKEFLAGS += --include-dir=$(CURDIR) - +# once after the Makefile is read. We need to invoke sub-make. +MAKEFLAGS += --include-dir=$(abs_srctree) need-sub-make := 1 -else - -# Do not print "Entering directory ..." at all for in-tree build. -MAKEFLAGS += --no-print-directory - -endif # ifneq ($(KBUILD_OUTPUT),) +endif ifneq ($(filter 3.%,$(MAKE_VERSION)),) # 'MAKEFLAGS += -rR' does not immediately become effective for GNU Make 3.x @@ -155,6 +164,7 @@ need-sub-make := 1 $(lastword $(MAKEFILE_LIST)): ; endif +export abs_srctree abs_objtree export sub_make_done := 1 ifeq ($(need-sub-make),1) @@ -166,9 +176,7 @@ $(filter-out _all sub-make $(lastword $(MAKEFILE_LIST)), $(MAKECMDGOALS)) _all: # Invoke a second make in the output directory, passing relevant variables sub-make: - $(Q)$(MAKE) \ - $(if $(KBUILD_OUTPUT),-C $(KBUILD_OUTPUT) KBUILD_SRC=$(CURDIR)) \ - -f $(CURDIR)/Makefile $(MAKECMDGOALS) + $(Q)$(MAKE) -C $(abs_objtree) -f $(abs_srctree)/Makefile $(MAKECMDGOALS) endif # need-sub-make endif # sub_make_done @@ -213,16 +221,21 @@ ifeq ("$(origin M)", "command line") KBUILD_EXTMOD := $(M) endif -ifeq ($(KBUILD_SRC),) +ifeq ($(abs_srctree),$(abs_objtree)) # building in the source tree srctree := . else - ifeq ($(KBUILD_SRC)/,$(dir $(CURDIR))) + ifeq ($(abs_srctree)/,$(dir $(abs_objtree))) # building in a subdirectory of the source tree srctree := .. else - srctree := $(KBUILD_SRC) + srctree := $(abs_srctree) endif + + # TODO: + # KBUILD_SRC is only used to distinguish in-tree/out-of-tree build. + # Replace it with $(srctree) or something. + KBUILD_SRC := $(abs_srctree) endif export KBUILD_CHECKSRC KBUILD_EXTMOD KBUILD_SRC