All of lore.kernel.org
 help / color / mirror / Atom feed
From: Masahiro Yamada <yamada.masahiro@socionext.com>
To: linux-kbuild@vger.kernel.org,
	Linus Torvalds <torvalds@linux-foundation.org>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	Andrew Morton <akpm@linux-foundation.org>,
	Kees Cook <keescook@chromium.org>,
	Nicolas Pitre <nicolas.pitre@linaro.org>,
	"Luis R . Rodriguez" <mcgrof@suse.com>,
	Randy Dunlap <rdunlap@infradead.org>,
	Ulf Magnusson <ulfalizer@gmail.com>,
	Sam Ravnborg <sam@ravnborg.org>,
	Michal Marek <michal.lkml@markovi.net>,
	Martin Schwidefsky <schwidefsky@de.ibm.com>,
	Pavel Machek <pavel@ucw.cz>,
	linux-s390@vger.kernel.org, Jiri Kosina <jkosina@suse.cz>,
	Masahiro Yamada <yamada.masahiro@socionext.com>,
	linux-kernel@vger.kernel.org
Subject: [RFC PATCH 4/7] kconfig: support new special property shell=
Date: Fri,  9 Feb 2018 01:19:09 +0900	[thread overview]
Message-ID: <1518106752-29228-5-git-send-email-yamada.masahiro@socionext.com> (raw)
In-Reply-To: <1518106752-29228-1-git-send-email-yamada.masahiro@socionext.com>

This works with bool, int, hex, string types.

For bool, the symbol is set to 'y' or 'n' depending on the exit value
of the command.

For int, hex, string, the symbol is set to the value to the stdout
of the command. (only the first line of the stdout)

The following shows how to write this and how it works.

--------------------(example Kconfig)------------------
config srctree
        string
        option env="srctree"

config CC
        string
        option env="CC"

config CC_HAS_STACKPROTECTOR
        bool
        option shell="$CC -Werror -fstack-protector -c -x c /dev/null"

config CC_HAS_STACKPROTECTOR_STRONG
        bool
        option shell="$CC -Werror -fstack-protector-strong -c -x c /dev/null"

config CC_VERSION
        int
        option shell="$srctree/scripts/gcc-version.sh $CC | sed 's/^0*//'"
        help
          gcc-version.sh returns 4 digits number. Unfortunately, the preceding
          zero would cause 'number is invalid'.  Cut it off.

config CC_IS_CLANG
        bool
        option shell="$CC --version | grep -q clang"

config CC_IS_GCC
        bool
        option shell="$CC --version | grep -q gcc"
-----------------------------------------------------

  $ make alldefconfig
  scripts/kconfig/conf  --alldefconfig Kconfig
  #
  # configuration written to .config
  #
  $ cat .config
  #
  # Automatically generated file; DO NOT EDIT.
  # Linux Kernel Configuration
  #
  CONFIG_CC_HAS_STACKPROTECTOR=y
  CONFIG_CC_HAS_STACKPROTECTOR_STRONG=y
  CONFIG_CC_VERSION=504
  # CONFIG_CC_IS_CLANG is not set
  CONFIG_CC_IS_GCC=y

Suggested-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Masahiro Yamada <yamada.masahiro@socionext.com>
---

 scripts/kconfig/expr.h     |  1 +
 scripts/kconfig/kconf_id.c |  1 +
 scripts/kconfig/lkc.h      |  1 +
 scripts/kconfig/menu.c     |  3 ++
 scripts/kconfig/symbol.c   | 74 ++++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 80 insertions(+)

diff --git a/scripts/kconfig/expr.h b/scripts/kconfig/expr.h
index c16e82e..83029f92 100644
--- a/scripts/kconfig/expr.h
+++ b/scripts/kconfig/expr.h
@@ -183,6 +183,7 @@ enum prop_type {
 	P_IMPLY,    /* imply BAR */
 	P_RANGE,    /* range 7..100 (for a symbol) */
 	P_ENV,      /* value from environment variable */
+	P_SHELL,    /* shell command */
 	P_SYMBOL,   /* where a symbol is defined */
 };
 
diff --git a/scripts/kconfig/kconf_id.c b/scripts/kconfig/kconf_id.c
index 3ea9c5f..0db9d1c 100644
--- a/scripts/kconfig/kconf_id.c
+++ b/scripts/kconfig/kconf_id.c
@@ -34,6 +34,7 @@ static struct kconf_id kconf_id_array[] = {
 	{ "defconfig_list",	T_OPT_DEFCONFIG_LIST,	TF_OPTION },
 	{ "env",		T_OPT_ENV,		TF_OPTION },
 	{ "allnoconfig_y",	T_OPT_ALLNOCONFIG_Y,	TF_OPTION },
+	{ "shell",		T_OPT_SHELL,		TF_OPTION },
 };
 
 #define KCONF_ID_ARRAY_SIZE (sizeof(kconf_id_array)/sizeof(struct kconf_id))
diff --git a/scripts/kconfig/lkc.h b/scripts/kconfig/lkc.h
index 4e23feb..8d05042 100644
--- a/scripts/kconfig/lkc.h
+++ b/scripts/kconfig/lkc.h
@@ -60,6 +60,7 @@ enum conf_def_mode {
 #define T_OPT_DEFCONFIG_LIST	2
 #define T_OPT_ENV		3
 #define T_OPT_ALLNOCONFIG_Y	4
+#define T_OPT_SHELL		5
 
 struct kconf_id {
 	const char *name;
diff --git a/scripts/kconfig/menu.c b/scripts/kconfig/menu.c
index 9922285..6254dfb 100644
--- a/scripts/kconfig/menu.c
+++ b/scripts/kconfig/menu.c
@@ -216,6 +216,9 @@ void menu_add_option(int token, char *arg)
 	case T_OPT_ENV:
 		prop_add_env(arg);
 		break;
+	case T_OPT_SHELL:
+		prop_add_shell(arg);
+		break;
 	case T_OPT_ALLNOCONFIG_Y:
 		current_entry->sym->flags |= SYMBOL_ALLNOCONFIG_Y;
 		break;
diff --git a/scripts/kconfig/symbol.c b/scripts/kconfig/symbol.c
index 893eae6..02ac4f4 100644
--- a/scripts/kconfig/symbol.c
+++ b/scripts/kconfig/symbol.c
@@ -4,6 +4,7 @@
  */
 
 #include <ctype.h>
+#include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <regex.h>
@@ -1370,6 +1371,8 @@ const char *prop_get_type_name(enum prop_type type)
 		return "prompt";
 	case P_ENV:
 		return "env";
+	case P_SHELL:
+		return "shell";
 	case P_COMMENT:
 		return "comment";
 	case P_MENU:
@@ -1420,3 +1423,74 @@ static void prop_add_env(const char *env)
 	else
 		menu_warn(current_entry, "environment variable %s undefined", env);
 }
+
+static void prop_add_shell(const char *cmd)
+{
+	struct symbol *sym, *sym2;
+	struct property *prop;
+	char *expanded_cmd;
+	FILE *p;
+	char stdout[256];
+	int ret, len;
+
+	sym = current_entry->sym;
+	for_all_properties(sym, prop, P_SHELL) {
+		sym2 = prop_get_symbol(prop);
+		if (strcmp(sym2->name, cmd))
+			menu_warn(current_entry, "redefining shell command symbol from %s",
+				  sym2->name);
+		return;
+	}
+
+	prop = prop_alloc(P_SHELL, sym);
+	prop->expr = expr_alloc_symbol(sym_lookup(cmd, SYMBOL_CONST));
+
+	expanded_cmd = sym_expand_string_value(cmd);
+
+	/* surround the command with ( ) in case it is piped commands */
+	len = strlen(expanded_cmd);
+	expanded_cmd = xrealloc(expanded_cmd, len + 3);
+	memmove(expanded_cmd + 1, expanded_cmd, len);
+	expanded_cmd[0] = '(';
+	expanded_cmd[len + 1] = ')';
+	expanded_cmd[len + 2] = 0;
+
+	switch (sym->type) {
+	case S_BOOLEAN:
+		/* suppress both stdout and stderr. */
+		len = strlen(expanded_cmd) + strlen(" >/dev/null 2>&1") + 1;
+		expanded_cmd = realloc(expanded_cmd, len);
+		strcat(expanded_cmd, " >/dev/null 2>&1");
+
+		ret = system(expanded_cmd);
+		sym_add_default(sym, ret == 0 ? "y" : "n");
+		break;
+	case S_INT:
+	case S_HEX:
+	case S_STRING:
+		/* suppress only stderr. we want to get stdout. */
+		len = strlen(expanded_cmd) + strlen(" 2>/dev/null") + 1;
+		expanded_cmd = realloc(expanded_cmd, len);
+		strcat(expanded_cmd, " 2>/dev/null");
+
+		p = popen(expanded_cmd, "r");
+		if (!p)
+			goto free;
+		if (fgets(stdout, sizeof(stdout), p)) {
+			stdout[sizeof(stdout) - 1] = 0;
+			len = strlen(stdout);
+			if (stdout[len - 1] == '\n')
+				stdout[len - 1] = 0;
+		} else {
+			stdout[0] = 0;
+		}
+		sym_add_default(sym, stdout);
+		break;
+	default:
+		menu_warn(current_entry, "unsupported type for shell command\n");
+		break;
+	}
+
+free:
+	free(expanded_cmd);
+}
-- 
2.7.4

  parent reply	other threads:[~2018-02-08 16:19 UTC|newest]

Thread overview: 89+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-02-08 16:19 [RFC PATCH 0/7] Kconfig: add new special property shell= to test compiler options in Kconfig Masahiro Yamada
2018-02-08 16:19 ` [RFC PATCH 1/7] kbuild: remove kbuild cache Masahiro Yamada
2018-02-08 16:19 ` [RFC PATCH 2/7] kconfig: add xrealloc() helper Masahiro Yamada
2018-02-08 16:19 ` [RFC PATCH 3/7] kconfig: remove const qualifier from sym_expand_string_value() Masahiro Yamada
2018-02-08 16:19 ` Masahiro Yamada [this message]
2018-02-09  5:30   ` [RFC PATCH 4/7] kconfig: support new special property shell= Ulf Magnusson
2018-02-09  9:19     ` Masahiro Yamada
2018-02-09 12:46       ` Ulf Magnusson
2018-02-09 20:46         ` Kees Cook
2018-02-10  5:48           ` Ulf Magnusson
2018-02-10  7:12             ` Masahiro Yamada
2018-02-10  7:49               ` Ulf Magnusson
2018-02-10  8:05                 ` Ulf Magnusson
2018-02-10  8:55                   ` Ulf Magnusson
2018-02-10  9:21                     ` Ulf Magnusson
2018-02-10 18:05                     ` Randy Dunlap
2018-02-11  2:00                       ` Kevin Easton
2018-02-10 19:23                     ` Kees Cook
2018-02-10 20:08                       ` Linus Torvalds
2018-02-11  4:13                         ` Kees Cook
2018-02-11  4:46                           ` Linus Torvalds
2018-02-11  7:28                             ` Linus Torvalds
2018-02-11 10:34                               ` Ulf Magnusson
2018-02-11 17:56                                 ` Kees Cook
2018-02-11 18:13                                   ` Linus Torvalds
2018-02-11 19:39                                     ` Kees Cook
2018-02-11 19:53                                       ` Linus Torvalds
2018-02-11 20:06                                         ` Linus Torvalds
2018-02-11 21:10                                           ` Arnd Bergmann
2018-02-11 21:19                                             ` Kees Cook
2018-02-11 21:50                                               ` Linus Torvalds
2018-02-12 10:44                                                 ` Arnd Bergmann
2018-02-12 10:44                                                   ` Arnd Bergmann
2018-02-11 22:29                                             ` Geert Uytterhoeven
2018-02-15 23:38                                           ` [RFC PATCH 4/7] kconfig: support new special property shell Palmer Dabbelt
2018-02-11 21:11                                         ` [RFC PATCH 4/7] kconfig: support new special property shell= Kees Cook
2018-02-11 19:42                                     ` Linus Torvalds
2018-02-12  8:26                                     ` Peter Zijlstra
2018-02-12 10:27                                       ` Thomas Gleixner
2018-02-12 11:52                                         ` Peter Zijlstra
2018-02-12 16:19                                       ` David Woodhouse
2018-02-12 16:19                                         ` David Woodhouse
2018-02-12 16:56                                         ` Kees Cook
2018-02-12 17:05                                           ` Peter Zijlstra
2018-02-12 17:33                                             ` Kees Cook
2018-02-12 17:36                                               ` David Woodhouse
2018-02-12 17:36                                                 ` David Woodhouse
2018-02-12 17:37                                                 ` Kees Cook
2018-02-12 17:00                                         ` Peter Zijlstra
2018-02-11 18:34                                   ` Ulf Magnusson
2018-02-11 21:05                                     ` Kees Cook
2018-02-11 21:35                                       ` Ulf Magnusson
2018-02-11 20:29                                   ` Ulf Magnusson
2018-02-11 20:42                                     ` Ulf Magnusson
2018-02-12 12:54                                       ` Ulf Magnusson
2018-02-12 14:21                                         ` Masahiro Yamada
2018-02-12 14:23                                           ` Masahiro Yamada
2018-02-12 14:32                                             ` Ulf Magnusson
2018-02-12 14:29                                           ` Ulf Magnusson
2018-02-12 14:53                                           ` Ulf Magnusson
2018-02-12 15:22                                             ` Masahiro Yamada
2018-02-12 15:35                                               ` Ulf Magnusson
2018-02-11 21:22                                   ` Ulf Magnusson
2018-02-12 14:39                                   ` Masahiro Yamada
2018-02-12 15:24                                     ` Kees Cook
2018-02-12 23:48                                       ` Randy Dunlap
2018-02-13  1:41                                         ` Masahiro Yamada
2018-02-13  1:53                                           ` Randy Dunlap
2018-02-13  8:35                                             ` Arnd Bergmann
2018-02-13  8:59                                               ` Masahiro Yamada
2018-02-12 10:44                                 ` Masahiro Yamada
2018-02-12 11:44                                   ` Ulf Magnusson
2018-02-12 11:49                                     ` Ulf Magnusson
2018-02-12 13:53                                     ` Masahiro Yamada
2018-02-12 14:13                                       ` Ulf Magnusson
2018-02-12 15:46                                   ` Kees Cook
2018-02-12 16:10                                     ` Masahiro Yamada
2018-02-13  8:55                                       ` Ulf Magnusson
2018-02-11 16:54                               ` Kees Cook
2018-02-08 16:19 ` [RFC PATCH 5/7] kconfig: invoke silentoldconfig when compiler is updated Masahiro Yamada
2018-02-08 17:19   ` Masahiro Yamada
2018-02-08 17:45     ` Linus Torvalds
2018-02-08 16:19 ` [RFC PATCH 6/7] kconfig: add basic environments to evaluate C flags in Kconfig Masahiro Yamada
2018-02-08 16:19 ` [RFC PATCH 7/7] Test stackprotector options in Kconfig to kill CC_STACKPROTECTOR_AUTO Masahiro Yamada
2018-02-08 18:30   ` Kees Cook
2018-02-09  4:13     ` Masahiro Yamada
2018-02-08 16:43 ` [RFC PATCH 0/7] Kconfig: add new special property shell= to test compiler options in Kconfig Greg Kroah-Hartman
2018-02-08 17:19 ` Linus Torvalds
2018-02-08 17:39   ` Masahiro Yamada

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1518106752-29228-5-git-send-email-yamada.masahiro@socionext.com \
    --to=yamada.masahiro@socionext.com \
    --cc=akpm@linux-foundation.org \
    --cc=gregkh@linuxfoundation.org \
    --cc=jkosina@suse.cz \
    --cc=keescook@chromium.org \
    --cc=linux-kbuild@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-s390@vger.kernel.org \
    --cc=mcgrof@suse.com \
    --cc=michal.lkml@markovi.net \
    --cc=nicolas.pitre@linaro.org \
    --cc=pavel@ucw.cz \
    --cc=rdunlap@infradead.org \
    --cc=sam@ravnborg.org \
    --cc=schwidefsky@de.ibm.com \
    --cc=torvalds@linux-foundation.org \
    --cc=ulfalizer@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.