From patchwork Tue Aug 15 01:29:40 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matthias Kaehlcke X-Patchwork-Id: 9900579 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 5EA40603FB for ; Tue, 15 Aug 2017 01:30:36 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 50B8628797 for ; Tue, 15 Aug 2017 01:30:36 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 456312879C; Tue, 15 Aug 2017 01:30:36 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9DE02287A3 for ; Tue, 15 Aug 2017 01:30:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753786AbdHOBa1 (ORCPT ); Mon, 14 Aug 2017 21:30:27 -0400 Received: from mail-pg0-f45.google.com ([74.125.83.45]:34662 "EHLO mail-pg0-f45.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753113AbdHOBaY (ORCPT ); Mon, 14 Aug 2017 21:30:24 -0400 Received: by mail-pg0-f45.google.com with SMTP id u185so57739485pgb.1 for ; Mon, 14 Aug 2017 18:30:23 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=k4lItgAfvdqFviy2Q900vl2nBeyyrCuapkg36Phrrz8=; b=OcsPPas8+I4s/Z5CU5a+mFy0ou+XGhYhasqpzV0v8CTALdcTr6xvme8lyfPERSoGV+ aggv9ksRRbldZegwcRPcLYUT5VTA51JLSeTIkik8jjbcpzAodcr2Y/BJ5eBmFPtCYYTN GOlb42A+5xNFiRE0KzTVIQXd/DiUtRcaRlT0RUrzTN/ZNn2bvxOaLQRimAcBPMnkfgKo v68vVwYD2l6e+niA6MMY1leQSN9BaOeHBZjt2p93NWohbDB4wRdrgJcWi9YB8fKAUbmF 4ugv7pJVuzNToeM5X2kQuaXrZyLJOcliTQ5Gdsvrmc/4QDdqPTZKlaZLcHx2vEbgIkxo uJng== X-Gm-Message-State: AHYfb5jtYxr4Mzz01uKDVtyKCAYdZvrH9t9mIm0apMynxxLeC6ycI1pP oVw2u5bd2FQK+Fya X-Received: by 10.98.204.22 with SMTP id a22mr26274105pfg.59.1502760623184; Mon, 14 Aug 2017 18:30:23 -0700 (PDT) Received: from mka.mtv.corp.google.com ([172.22.112.49]) by smtp.gmail.com with ESMTPSA id a86sm16681762pfe.181.2017.08.14.18.30.21 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 14 Aug 2017 18:30:22 -0700 (PDT) From: Matthias Kaehlcke To: Thomas Gleixner , Ingo Molnar , "H . Peter Anvin" , Masahiro Yamada , Michal Marek Cc: x86@kernel.org, linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, dianders@chromium.org, Michael Davidson , Greg Hackmann , Nick Desaulniers , Stephen Hines , Kees Cook , Arnd Bergmann , Bernhard.Rosenkranzer@linaro.org, Matthias Kaehlcke Subject: [PATCH v2 1/2] kbuild: Also evaluate alternative option passed to cc-option, etc Date: Mon, 14 Aug 2017 18:29:40 -0700 Message-Id: <20170815012941.75151-1-mka@chromium.org> X-Mailer: git-send-email 2.14.0.434.g98096fd7a8-goog Sender: linux-kbuild-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kbuild@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP The macro cc-option receives two parameters (the second may be empty). It returns the first parameter if it is a valid compiler option, otherwise the second one. It is not evaluated if the second parameter is a valid compiler option. This seems to be fine in virtually all cases, however there are scenarios where the second parameter needs to be evaluated too, and an empty value (or a third option) should be returned if it is not valid. The new macro try-run-opt receives a 'base command' and two options as parameters. It runs 'base command' + option 1 and returns option 1 upon success. In case of failure 'base command' + option 2 is executed, in case of success option 2 is returned, otherwise an empty string. Rework [__]cc-option, ld-option, and cc-ldoption to use try-run-opt instead of try-run to make sure the alternative option is evaluated. Signed-off-by: Matthias Kaehlcke --- Changes in v2: - add try-run-opt instead of cc-option-3 - use try-run-opt for [__]cc-option, ld-option and ccld-option - updated subject (was 'kbuild: Add macros cc-option-3 and __cc-option-3') and commit message scripts/Kbuild.include | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/scripts/Kbuild.include b/scripts/Kbuild.include index dd8e2dde0b34..48a6d0e9b073 100644 --- a/scripts/Kbuild.include +++ b/scripts/Kbuild.include @@ -85,8 +85,8 @@ TMPOUT := $(if $(KBUILD_EXTMOD),$(firstword $(KBUILD_EXTMOD))/) # try-run # Usage: option = $(call try-run, $(CC)...-o "$$TMP",option-ok,otherwise) -# Exit code chooses option. "$$TMP" is can be used as temporary file and -# is automatically cleaned up. +# Runs the command $1, exit code chooses option. "$$TMP" is used as temporary +# file and is automatically cleaned up. try-run = $(shell set -e; \ TMP="$(TMPOUT).$$$$.tmp"; \ TMPO="$(TMPOUT).$$$$.o"; \ @@ -96,6 +96,23 @@ try-run = $(shell set -e; \ fi; \ rm -f "$$TMP" "$$TMPO") +# try-run-opt +# Usage: option = $(call try-run-opt, $(CC)...-o "$$TMP",option-ok,otherwise) +# Runs the command '$1 $2' and outputs $2 if the execution was successful. +# Otherwise runs '$1 $3' (if $3 is not empty) and outputs $3 upon success. No +# output if both commands fail. "$$TMP" is used as temporary file and is +# automatically cleaned up. +try-run-opt = $(shell set -e; \ + TMP="$(TMPOUT).$$$$.tmp"; \ + TMPO="$(TMPOUT).$$$$.o"; \ + if ($(1) $(2)) >/dev/null 2>&1; \ + then echo "$(2)"; \ + elif [ -n "$(3)" ] && ($(1) $(3)) >/dev/null 2>&1; \ + then echo "$(3)"; \ + else echo ""; \ + fi; \ + rm -f "$$TMP" "$$TMPO") + # as-option # Usage: cflags-y += $(call as-option,-Wa$(comma)-isa=foo,) @@ -110,8 +127,8 @@ as-instr = $(call try-run,\ # __cc-option # Usage: MY_CFLAGS += $(call __cc-option,$(CC),$(MY_CFLAGS),-march=winchip-c6,-march=i586) -__cc-option = $(call try-run,\ - $(1) -Werror $(2) $(3) -c -x c /dev/null -o "$$TMP",$(3),$(4)) +__cc-option = $(call try-run-opt,\ + $(1) -Werror $(2) -c -x c /dev/null -o "$$TMP",$(3),$(4)) # Do not attempt to build with gcc plugins during cc-option tests. # (And this uses delayed resolution so the flags will be up to date.) @@ -159,13 +176,13 @@ cc-if-fullversion = $(shell [ $(cc-fullversion) $(1) $(2) ] && echo $(3) || echo # cc-ldoption # Usage: ldflags += $(call cc-ldoption, -Wl$(comma)--hash-style=both) -cc-ldoption = $(call try-run,\ - $(CC) $(1) -nostdlib -x c /dev/null -o "$$TMP",$(1),$(2)) +cc-ldoption = $(call try-run-opt,\ + $(CC) -nostdlib -x c /dev/null -o "$$TMP",$(1),$(2)) # ld-option # Usage: LDFLAGS += $(call ld-option, -X) -ld-option = $(call try-run,\ - $(CC) -x c /dev/null -c -o "$$TMPO" ; $(LD) $(1) "$$TMPO" -o "$$TMP",$(1),$(2)) +ld-option = $(call try-run-opt,\ + $(CC) -x c /dev/null -c -o "$$TMPO" ; $(LD) "$$TMPO" -o "$$TMP",$(1),$(2)) # ar-option # Usage: KBUILD_ARFLAGS := $(call ar-option,D)