From patchwork Fri Feb 16 18:38:39 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Masahiro Yamada X-Patchwork-Id: 128617 Delivered-To: patch@linaro.org Received: by 10.46.124.24 with SMTP id x24csp867509ljc; Fri, 16 Feb 2018 10:45:31 -0800 (PST) X-Google-Smtp-Source: AH8x225S1LpobXFEtFdV732Ebf5CAga2m9TboBRIsSyWafUIUulqOtZzXvjdMTkTJ7HqkdNUrzk7 X-Received: by 10.98.17.15 with SMTP id z15mr7030260pfi.116.1518806731809; Fri, 16 Feb 2018 10:45:31 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1518806731; cv=none; d=google.com; s=arc-20160816; b=C0yioGytr4a3HjLkAS92UWlVpdCPHAyWSoYuwJyvyJpLhyQ6lWZkZXuTNL3rdQeIkl D+7QDL0wT6FjypZF8tjeNNEmv5o22CzLuMueTDHHYIsnT9h3GQ61kRChBgbeKaRI/afz kSfygPcw4Fb2EhvwXzWesE8JEDqO/5QghXXBkuFmko7sKMG8UQZ7CAftVbZ6lW93hhVr wwNoW1UOeWLCL7BuZrG2jfp62gnsozAR2qw7tZjFPj5jxIKWhy9az0BfZE7G1s92xn+/ xoTxAMNolABTkqiRZ63fQzVkOO5BJzSMFpkbL9wfjLqKH5RggvWrK/tuPvjtoZW4l+F8 +skA== 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 :arc-authentication-results; bh=6/IoqUTGHai0Czf36phhQJ7Uyzpz6vT0GEH8DaNw/3g=; b=wbIXZBHF76NxUT8umv4lbZs2jdNStA8YbE8/OjiWht1m7eqBTNaA5ov33OWqg0EM4X kgnY9YkAN0z13bgnC88+nVanLG8VdMlp7jRT/a5wh5+FWSDuCtR0XxfMZ+8q+BG0u+wt z1EjP7Nw5G8XjCXjzcifpbezDhiIQrJ3whbMnkqP+tVn8B+M5ChohJFR1igUqcb4BZUt UAunLN9/nsP+XANNtJ/F39NaSTlDWZkc6YM1wcMzEZRexM+WFhN+kxAYryvngW0Xcgxo tv1nQxO8unLzIbSpWdz4FR61VkWNsD6LMX1E6Gx90otRgdL8S1BpHo+/SZ0+XVxwy5hv IEGw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@nifty.com header.s=dec2015msa header.b=QV6T0K0+; 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 y12-v6si4889566plt.1.2018.02.16.10.45.31; Fri, 16 Feb 2018 10:45:31 -0800 (PST) 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=QV6T0K0+; 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 S1753728AbeBPSp0 (ORCPT + 28 others); Fri, 16 Feb 2018 13:45:26 -0500 Received: from conuserg-07.nifty.com ([210.131.2.74]:57765 "EHLO conuserg-07.nifty.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752658AbeBPSlS (ORCPT ); Fri, 16 Feb 2018 13:41:18 -0500 Received: from grover.sesame (FL1-125-199-20-195.osk.mesh.ad.jp [125.199.20.195]) (authenticated) by conuserg-07.nifty.com with ESMTP id w1GIdBTE013418; Sat, 17 Feb 2018 03:39:23 +0900 DKIM-Filter: OpenDKIM Filter v2.10.3 conuserg-07.nifty.com w1GIdBTE013418 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nifty.com; s=dec2015msa; t=1518806363; bh=6/IoqUTGHai0Czf36phhQJ7Uyzpz6vT0GEH8DaNw/3g=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=QV6T0K0+Jzg4vHnXjGJv3bKHiP6jTezf1u5g068P9oib0rEugu3lBkSnawvMe+/SI FsfbsV0dn0vBjN9tDLWZq9wcWeSJdRlXnRxHf480HzEr1BYUYdgMLNfM9BjGlXOlVc lJIZ/thtEJIgVB7KlIyQ6BMokwuxgHoZ5WW+2RPJrWE+ngwjpzr8iyDTIgSreXmRUO Qe51ZpW9OLCKbQfuyPrMNTreW0upKK3sLr0W42Yon2BdHdQEGmFrMqbtDqZw5iUMxi VU0+/mXTrGoqP4xKrMcIlKysMEBV4PAEv4yyzw9YpL0PXQgE+xKFuoUyzp+EPIJhgO b7g29fgpDo9gA== X-Nifty-SrcIP: [125.199.20.195] From: Masahiro Yamada To: linux-kbuild@vger.kernel.org, Linus Torvalds Cc: Greg Kroah-Hartman , Arnd Bergmann , Kees Cook , Randy Dunlap , Ulf Magnusson , Sam Ravnborg , Michal Marek , Masahiro Yamada , linux-kernel@vger.kernel.org Subject: [PATCH 11/23] kconfig: add 'shell-stdout' function Date: Sat, 17 Feb 2018 03:38:39 +0900 Message-Id: <1518806331-7101-12-git-send-email-yamada.masahiro@socionext.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1518806331-7101-1-git-send-email-yamada.masahiro@socionext.com> References: <1518806331-7101-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 This is the second built-in function, which retrieves the first line of stdout from the given shell command. Example code: config CC_IS_GCC bool default $(shell $CC --version | grep -q gcc) config GCC_VERSION int default $(shell-stdout $srctree/scripts/gcc-version.sh $CC | sed 's/^0*//') if CC_IS_GCC default 0 Result: $ make -s alldefconfig && tail -n 2 .config CONFIG_CC_IS_GCC=y CONFIG_GCC_VERSION=504 $ make CC=clang -s alldefconfig && tail -n 2 .config # CONFIG_CC_IS_GCC is not set CONFIG_GCC_VERSION=0 By the way, function calls can be nested, so the following works. Example code: config FOO bool default $(shell $(shell-stdout echo $COMMAND_IN_CAPITAL | tr [A-Z] [a-z])) Result: $ make -s COMMAND=TRUE alldefconfig && tail -n 1 .config CONFIG_FOO=y $ make -s COMMAND=FALSE alldefconfig && tail -n 1 .config # CONFIG_FOO is not set Signed-off-by: Masahiro Yamada --- scripts/kconfig/function.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) -- 2.7.4 diff --git a/scripts/kconfig/function.c b/scripts/kconfig/function.c index f7f154d..266f4ec 100644 --- a/scripts/kconfig/function.c +++ b/scripts/kconfig/function.c @@ -189,10 +189,56 @@ static char *do_shell(struct function *f, int argc, char *argv[]) return xstrdup(ret == 0 ? "y" : "n"); } +static char *do_shell_stdout(struct function *f, int argc, char *argv[]) +{ + static const char *pre = "("; + static const char *post = ") 2>/dev/null"; + FILE *p; + char buf[256]; + char *cmd; + int ret; + + if (argc != 2) + return NULL; + + /* + * Surround the command with ( ) in case it is piped commands. + * Also, redirect stderr to /dev/null. + */ + cmd = xmalloc(strlen(pre) + strlen(argv[1]) + strlen(post) + 1); + strcpy(cmd, pre); + strcat(cmd, argv[1]); + strcat(cmd, post); + + p = popen(cmd, "r"); + if (!p) { + perror(cmd); + goto free; + } + if (fgets(buf, sizeof(buf), p)) { + size_t len = strlen(buf); + + if (buf[len - 1] == '\n') + buf[len - 1] = '\0'; + } else { + buf[0] = '\0'; + } + + ret = pclose(p); + if (ret == -1) + perror(cmd); + +free: + free(cmd); + + return xstrdup(buf); +} + void func_init(void) { /* register built-in functions */ func_add("shell", do_shell, NULL); + func_add("shell-stdout", do_shell_stdout, NULL); } void func_exit(void)