Received: by 10.223.185.116 with SMTP id b49csp1045218wrg; Fri, 16 Feb 2018 11:23:52 -0800 (PST) X-Google-Smtp-Source: AH8x2260mUms8nNKYFhQNEEa+LPMOatgxPHuFWq1K1mzm+8ALURYkkEdbct+vK3FL8ZobEtzOBs4 X-Received: by 10.101.90.65 with SMTP id z1mr4449946pgs.126.1518809032536; Fri, 16 Feb 2018 11:23:52 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1518809032; cv=none; d=google.com; s=arc-20160816; b=itOxWMbbQktcpkdtambFGPgEJW8nGB0RPAOdB80aG5D4sPrgqrgKqWHU54+uh4J4KO CO5is5oNrYV13rZUUqQ0BcGLIOIsKf+Gphsd8jVx8nxMk773/Jsup8kukvJVNo8yd+aE tNteZdzVCQ4kO0x9YwD1eY8SL5cVn+cAA30C993edG2L1KmefroKWHJdGulIUWT7Spxx UJxDlhsb6fUuzdjQyfbMU/cdcfWaJztjVrKBKQBf+fNdmJzO1TSEo2EiHhs7tmhybrN+ /qsJ96iUk7ktrhPBTWr4dbpz1Xjle/3L2bo+NThJLAcCAVyXWmLBo1oV2oyf9syX434/ BI7w== 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 y13-v6si686217plr.10.2018.02.16.11.23.38; Fri, 16 Feb 2018 11:23:52 -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 S1753726AbeBPSpZ (ORCPT + 99 others); Fri, 16 Feb 2018 13:45:25 -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(+) 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) -- 2.7.4