Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp4909405ybi; Mon, 3 Jun 2019 20:32:50 -0700 (PDT) X-Google-Smtp-Source: APXvYqztypITenUuw/BUJSDCFca5oH0EZmpbqXmVyQJ3dSqCpFEDllNEIc919RAg3zgtyfY0FyZE X-Received: by 2002:a17:902:ac82:: with SMTP id h2mr34428600plr.303.1559619170831; Mon, 03 Jun 2019 20:32:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1559619170; cv=none; d=google.com; s=arc-20160816; b=TN5eLI4MZuOMwk2iNfgSQfD7uFMOxaaxP9zY8R8ahw8HMr8nFBQiUgU1sIr6paZ4h0 L/iyTS8I+2YAhrMYhztxnh6DsFHnw8iXA9viTt05NnMVbLgubxPMjfyq2SncCCsgAzao 5UXBKBAk5bFfXfeRSVM26yQj94FuUBl1xPBT97e3b0XvBJrrRbIL49EWa670M8cQx6QX /kkIWOeALhiSgjlOR2NozP8BxDzgZQq6yjixtUyfz4VRBzbxv6h95lGxlQhcGe3X9VJD 8x5YSYnWqlNIx0wVN4ioq9CzwbKSKeFdE+vaLzyYsbWPPS5v8XG2swZvxFXutPoU57um C0TQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature:dkim-filter; bh=p3DMRDqjvLfleLEkftYAKoqonxYhzBlfQ7q3kX1yABI=; b=OAVw675IqKdj4LS/1AhKhBapLR0L/pqxG3Zs5iWLIpoIzEymYppQF6DypqWGGv42n1 qwPCBe2jtmAywF/sWXx0e/1j59OE+2ZuvkIema2Lh5VCW7irJ+2R1YFYIYO8+qh8jsk8 qj05UgsQcLSKNPTkrsajYEhD1+mQR0mJat+O4WaqQm+lShP4/QmpwB57nVWAQ/2QAE5x UpIIfxj/rjDVwBC7CP5A+gB9jtHBY5Cc4dUoOsRA+MTJYrjX3blStmy2O/26liZaWpSG 0i/OIU8XwxETXnrxdQUL5gA5nrjZ5shBLZRTCyNxBTw1KwOlswM4/2VdIcVvmsvsUn4m 2JDQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@nifty.com header.s=dec2015msa header.b=bYi7+isC; 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 m14si20807839pls.393.2019.06.03.20.32.33; Mon, 03 Jun 2019 20:32:50 -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=bYi7+isC; 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 S1726410AbfFDDbU (ORCPT + 99 others); Mon, 3 Jun 2019 23:31:20 -0400 Received: from conssluserg-03.nifty.com ([210.131.2.82]:62181 "EHLO conssluserg-03.nifty.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726136AbfFDDbT (ORCPT ); Mon, 3 Jun 2019 23:31:19 -0400 Received: from mail-ua1-f45.google.com (mail-ua1-f45.google.com [209.85.222.45]) (authenticated) by conssluserg-03.nifty.com with ESMTP id x543VD43005850; Tue, 4 Jun 2019 12:31:14 +0900 DKIM-Filter: OpenDKIM Filter v2.10.3 conssluserg-03.nifty.com x543VD43005850 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nifty.com; s=dec2015msa; t=1559619074; bh=p3DMRDqjvLfleLEkftYAKoqonxYhzBlfQ7q3kX1yABI=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=bYi7+isCKertT00fRULzrd7n1xy2ijri8jBhF4Nuyl7kUpCw/8ZnRRUgX0tExvhTo E4HYK5tj6gUIHISOWfCqnjXxMVE9iiN86tnB6+3p6O84sZC9+z4HGKF90/d9a40mPl xAw7v7op9drM7cioGdp91l3WH5pTtCABr9eo/NhqFCPOsVVaoQIS4BX5z40hsuXr69 fxi3NAqIb9RX/P+inYWpxCoRMYE353+QoUWxmLWzYTCHHVoJck4SyoM4yIwthQ14tr 2RAtKg6dn6Invm5UosRZaD5bAeAee5YjWfpth1jb7U3ij7sy772vHBkeShMilmL0RV 6zh/xd9lrEEAQ== X-Nifty-SrcIP: [209.85.222.45] Received: by mail-ua1-f45.google.com with SMTP id r7so7289311ual.2; Mon, 03 Jun 2019 20:31:14 -0700 (PDT) X-Gm-Message-State: APjAAAX2GMMwotR8lgulcmu0BtQxamvrTQKi3iTg/1nrolfw4Fe+iG0h yfqvxGflLNL75ct+I700w5VF7MPMrt1Ejr7O7DI= X-Received: by 2002:ab0:234d:: with SMTP id h13mr6406182uao.95.1559619073065; Mon, 03 Jun 2019 20:31:13 -0700 (PDT) MIME-Version: 1.0 References: <20190603104902.23799-1-yamada.masahiro@socionext.com> <863c29c5f0214c008fbcbb2aac517a5c@AcuMS.aculab.com> <810dd6ae018b4a31b70d26fb6b29e48d@AcuMS.aculab.com> In-Reply-To: <810dd6ae018b4a31b70d26fb6b29e48d@AcuMS.aculab.com> From: Masahiro Yamada Date: Tue, 4 Jun 2019 12:30:37 +0900 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH] kbuild: use more portable 'command -v' for cc-cross-prefix To: David Laight Cc: "linux-kbuild@vger.kernel.org" , Vineet Gupta , Alexey Brodkin , "linux-snps-arc@lists.infradead.org" , linux-stable , Michal Marek , "linux-kernel@vger.kernel.org" Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Jun 3, 2019 at 10:09 PM David Laight wrote: > > From: Masahiro Yamada > > Sent: 03 June 2019 12:38 > > Hi David, > > > > On Mon, Jun 3, 2019 at 8:14 PM David Laight wrote: > > > > > > From: Masahiro Yamada > > > > Sent: 03 June 2019 11:49 > > > > > > > > To print the pathname that will be used by shell in the current > > > > environment, 'command -v' is a standardized way. [1] > > > > > > > > 'which' is also often used in scripting, but it is not portable. > > > > > > > > When I worked on commit bd55f96fa9fc ("kbuild: refactor cc-cross-prefix > > > > implementation"), I was eager to use 'command -v' but it did not work. > > > > (The reason is explained below.) > > > > > > > > I kept 'which' as before but got rid of '> /dev/null 2>&1' as I > > > > thought it was no longer needed. Sorry, I was wrong. > > > > > > > > It works well on my Ubuntu machine, but Alexey Brodkin reports annoying > > > > warnings from the 'which' on CentOS 7 when the given command is not > > > > found in the PATH environment. > > > > > > > > $ which foo > > > > which: no foo in (/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin) > > > > > > > > Given that behavior of 'which' is different on environment, I want > > > > to try 'command -v' again. > > > > > > > > The specification [1] clearly describes the behavior of 'command -v' > > > > when the given command is not found: > > > > > > > > Otherwise, no output shall be written and the exit status shall reflect > > > > that the name was not found. > > > > > > > > However, we need a little magic to use 'command -v' from Make. > > > > > > > > $(shell ...) passes the argument to a subshell for execution, and > > > > returns the standard output of the command. > > > > > > > > Here is a trick. GNU Make may optimize this by executing the command > > > > directly instead of forking a subshell, if no shell special characters > > > > are found in the command line and omitting the subshell will not > > > > change the behavior. > > > > > > > > In this case, no shell special character is used. So, Make will try > > > > to run the command directly. However, 'command' is a shell-builtin > > > > command. In fact, Make has a table of shell-builtin commands because > > > > it must spawn a subshell to execute them. > > > > > > > > Until recently, 'command' was missing in the table. > > > > > > > > This issue was fixed by the following commit: > > > > > > > > | commit 1af314465e5dfe3e8baa839a32a72e83c04f26ef > > > > | Author: Paul Smith > > > > | Date: Sun Nov 12 18:10:28 2017 -0500 > > > > | > > > > | * job.c: Add "command" as a known shell built-in. > > > > | > > > > | This is not a POSIX shell built-in but it's common in UNIX shells. > > > > | Reported by Nick Bowler . > > > > > > > > This is not included in any released versions of Make yet. > > > > (But, some distributions may have back-ported the fix-up.) > > > > > > > > To trick Make and let it fork the subshell, I added a shell special > > > > character '~'. We may be able to get rid of this workaround someday, > > > > but it is very far into the future. > > > > > > > > [1] http://pubs.opengroup.org/onlinepubs/9699919799/utilities/command.html > > > > > > > > Fixes: bd55f96fa9fc ("kbuild: refactor cc-cross-prefix implementation") > > > > Cc: linux-stable # 5.1 > > > > Reported-by: Alexey Brodkin > > > > Signed-off-by: Masahiro Yamada > > > > --- > > > > > > > > scripts/Kbuild.include | 5 ++++- > > > > 1 file changed, 4 insertions(+), 1 deletion(-) > > > > > > > > diff --git a/scripts/Kbuild.include b/scripts/Kbuild.include > > > > index 85d758233483..5a32ca80c3f6 100644 > > > > --- a/scripts/Kbuild.include > > > > +++ b/scripts/Kbuild.include > > > > @@ -74,8 +74,11 @@ endef > > > > # Usage: CROSS_COMPILE := $(call cc-cross-prefix, m68k-linux-gnu- m68k-linux-) > > > > # Return first where a gcc is found in PATH. > > > > # If no gcc found in PATH with listed prefixes return nothing > > > > +# > > > > +# Note: the special character '~' forces Make to invoke a shell. This workaround > > > > +# is needed because this issue was only fixed after GNU Make 4.2.1 release. > > > > cc-cross-prefix = $(firstword $(foreach c, $(filter-out -%, $(1)), \ > > > > - $(if $(shell which $(c)gcc), $(c)))) > > > > + $(if $(shell command -v $(c)gcc ~), $(c)))) > > > > > > I see a problem here: > > > command -v foo bar > > > could be deemed to be an error (extra argument). > > > > OK, the specification does not allow to pass arguments > > with -v. > > > > > > > You could use: > > > $(shell sh -c "command -v $(c)gcc") > > > or maybe: > > > $(shell command$${x:+} -v $(c)gcc) > > > > > > How about this? > > > > $(shell : ~; command -v $(c)gcc) > > Overcomplicated .... > > I've not looked at the list of 'special characters' in make, > but I suspect any variable expansion is enough. > Since ${x:+} always expands to the empty string (whether or > not 'x' is defined) it can't have any unfortunate side effects. Probably, my eyes are used to Makefile. ":" is a no-op command, and it is used everywhere in kernel Makefiles in the form of "@:' It depends on people which solution seems simpler. So, this argument tends to end up with bikesheding. -- Best Regards Masahiro Yamada