Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp4066579ybi; Mon, 3 Jun 2019 05:15:25 -0700 (PDT) X-Google-Smtp-Source: APXvYqwUpUEeFw729zjNAmS0fgvyod5QOHknE5SeuxXh8g4L5fkokytEcX1J1OuK13fgfNV1Elgy X-Received: by 2002:a63:ed03:: with SMTP id d3mr27544574pgi.7.1559564125432; Mon, 03 Jun 2019 05:15:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1559564125; cv=none; d=google.com; s=arc-20160816; b=gvL7N0ZwJB7GX3o8c/nAPRarR526d35IXfG88w9MM2mYrzvW7zyYSCtRfX6XQlX4/E z/iskrK7eMpgyY4mmdsVnxRSGxEVe9t6ES3OeXFqPALe3I7hmbgOymjPuyrk62mxyzYB C5XTrDqy5RLGBy76Ux6jwWQ+TwOgEaaHY9KAtDaTONl6xY6Q7Ap6t2yON2bvwYH3zPgo FTonG4r/ftgvd+NrtIhbNUt8lDh1TTuMzc9g4hx1ymjEuETWqzb3tlWd4zuArnpHtLNo jUGVGFLFgSeBHrRJXh6ZpIX4ISmx1dsRHF+qc8+PaPYrxCKlLZ/4a2Bo0/X7mI7V5KGS xIsQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :content-language:accept-language:in-reply-to:references:message-id :date:thread-index:thread-topic:subject:cc:to:from; bh=zFPP8FpU+XugJy+fsOcfBzr2L5S5es7gwuWRt/RtJQE=; b=A/YEzfJb5mvKt/f+voPcu8WOZIy/p/r4xMzlRVR5BjBDcsww7/Zo+ixf5L49vBziWe 8ayuRpX04kh14W8JvtQTyq/xRuowQCiZbGgpLw6qmjEebHnXpDhcyDQq/Zr3QS8IKoGG 0ht6I1zaX5JrsSr0/D/2ZJlUCHOB2STLJIANeHBO2QKNAXYUt0kwil1exaHdYyoFNEN0 ofr+gAQYs39zX5V6TsBRWcbFsFHApVou+ai223feWrieGtMPaGY84uCHUFsignYxyUCr STnijz+tUklbbhTp6hj2WEyrZWONJokwlhfQsS356aNIWeRgD0Qp/PAjj1Jo3kIfYyuD UBiQ== ARC-Authentication-Results: i=1; mx.google.com; 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 s73si20680275pfs.15.2019.06.03.05.15.08; Mon, 03 Jun 2019 05:15:25 -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; 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 S1727967AbfFCLOJ convert rfc822-to-8bit (ORCPT + 99 others); Mon, 3 Jun 2019 07:14:09 -0400 Received: from eu-smtp-delivery-151.mimecast.com ([146.101.78.151]:36972 "EHLO eu-smtp-delivery-151.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727851AbfFCLOJ (ORCPT ); Mon, 3 Jun 2019 07:14:09 -0400 Received: from AcuMS.aculab.com (156.67.243.126 [156.67.243.126]) (Using TLS) by relay.mimecast.com with ESMTP id uk-mta-133-AcFCnqi1O-qRKgiauL-8lw-1; Mon, 03 Jun 2019 12:14:04 +0100 Received: from AcuMS.Aculab.com (fd9f:af1c:a25b::d117) by AcuMS.aculab.com (fd9f:af1c:a25b::d117) with Microsoft SMTP Server (TLS) id 15.0.1347.2; Mon, 3 Jun 2019 12:14:03 +0100 Received: from AcuMS.Aculab.com ([fe80::43c:695e:880f:8750]) by AcuMS.aculab.com ([fe80::43c:695e:880f:8750%12]) with mapi id 15.00.1347.000; Mon, 3 Jun 2019 12:14:03 +0100 From: David Laight To: 'Masahiro Yamada' , "linux-kbuild@vger.kernel.org" CC: Vineet Gupta , Alexey Brodkin , "linux-snps-arc@lists.infradead.org" , linux-stable , Michal Marek , "linux-kernel@vger.kernel.org" Subject: RE: [PATCH] kbuild: use more portable 'command -v' for cc-cross-prefix Thread-Topic: [PATCH] kbuild: use more portable 'command -v' for cc-cross-prefix Thread-Index: AQHVGfoc7Nk6FX5Ty02s910sxgLWxaaJxI+g Date: Mon, 3 Jun 2019 11:14:03 +0000 Message-ID: <863c29c5f0214c008fbcbb2aac517a5c@AcuMS.aculab.com> References: <20190603104902.23799-1-yamada.masahiro@socionext.com> In-Reply-To: <20190603104902.23799-1-yamada.masahiro@socionext.com> Accept-Language: en-GB, en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-ms-exchange-transport-fromentityheader: Hosted x-originating-ip: [10.202.205.107] MIME-Version: 1.0 X-MC-Unique: AcFCnqi1O-qRKgiauL-8lw-1 X-Mimecast-Spam-Score: 0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8BIT Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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). You could use: $(shell sh -c "command -v $(c)gcc") or maybe: $(shell command$${x:+} -v $(c)gcc) David - Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)