Received: by 10.223.176.5 with SMTP id f5csp2096999wra; Thu, 8 Feb 2018 08:23:40 -0800 (PST) X-Google-Smtp-Source: AH8x226EY5qD96CTw6lqYwPsJh0cAPGfxax9jOwGTtK37FyTHZJolF1jIR8qaFqvjaEytjqsL+yv X-Received: by 2002:a17:902:6809:: with SMTP id h9-v6mr1162189plk.46.1518107020019; Thu, 08 Feb 2018 08:23:40 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1518107019; cv=none; d=google.com; s=arc-20160816; b=T7dmDIOXCpn4Wu/+fhxZWNCLWJSDkNq54KfFlpexbC+0GB4Md4fqwQ++I4247EnkyF JTPPdjYQgUlZBNZYTHkfqErqaln4h0QzzNPqd1qkNtJzqku44hBJjO3jcj0Z38RKMAyI 7rb6W190MKB/uNP9cPgBRbVZpFK9Mtz08D9Pb5ImN+hheXlB7l5oTrAuTlpWXadVo09Q LmVAM93DbL/mhpHSCb7QQLMYuY1gW/3s4DlwbIopzzI6nnUkK9TPhznoQ5YP2vbAzQ4A WIBUcd0Py13f9S/cxjnWKjPDo0qcog2dlsZ9zHQ0zOGzlQgTdFtWratyxQtEjZERq1Ei Qazg== 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=SjFMsbSS827T9u8KgXcclZ/8GFOfEMUu9MVypLM9Z3w=; b=mTSS/+qoOFRHs6lgGd4NYGGMNAkGdrsqyhIMRKJ8dYA0FwBSeSq4o1bOrEUazkBOI2 pwnMqKmaqJZ9T2uFHjGWJYWnRoKREV8fEklGuoRrhJ3/b5/IrUGMOkWA4/ktZ6u7Ah2F j2U26hWIkltnqEtR9VGB5vFCBPHzdNgn/2CfSvoA1UiQlPofDtCcbQXrvI17E1OVTUVf jLxJ+kH94P/i2YGN4uTyYJiG/CsbV8Ck1gOa1RwiGYPEqKOuHp3SrHnJwlkKBloVxMvK 1p0Vilm0EfBF5Aj+ZR+4WE5hSY04yKWHsJISPHWnDzpuzyh4mrbW9zz+/uOqPHfu0Gu5 A/5A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@nifty.com header.s=dec2015msa header.b=n9mJvTC8; 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 m6-v6si153692pln.711.2018.02.08.08.23.24; Thu, 08 Feb 2018 08:23:39 -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=n9mJvTC8; 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 S1752508AbeBHQVl (ORCPT + 99 others); Thu, 8 Feb 2018 11:21:41 -0500 Received: from conuserg-12.nifty.com ([210.131.2.79]:19347 "EHLO conuserg-12.nifty.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752196AbeBHQVg (ORCPT ); Thu, 8 Feb 2018 11:21:36 -0500 Received: from grover.sesame (FL1-125-199-20-195.osk.mesh.ad.jp [125.199.20.195]) (authenticated) by conuserg-12.nifty.com with ESMTP id w18GJP1G021191; Fri, 9 Feb 2018 01:19:30 +0900 DKIM-Filter: OpenDKIM Filter v2.10.3 conuserg-12.nifty.com w18GJP1G021191 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nifty.com; s=dec2015msa; t=1518106771; bh=SjFMsbSS827T9u8KgXcclZ/8GFOfEMUu9MVypLM9Z3w=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=n9mJvTC8YlattLwD2kw2JQVUdj1+N8cCZmnSL1X+nIpDPJQb5JZ2Xv6BxsUihPT4w LUxstTgw9RC4kpwOUDra/kkMJsVhQlhFV2N4a8bWiKT17G1dqhK/kmUruQeixnOmhm MKOA6xapgBh/ialtxBbs2DiWc5J7LjNLXhROjM/62VCdGy/+pNBIRQKRyxMJKX4zL0 GC6d7p+6dmLGoyPo0S9qfA4xlRobwY1Aigd/nzCrsLmnBw98kdlRXiAROXEZPQcmg0 TqN+3+V/G4CNdLJnh/KomufWLVEQC2fnkl+DiNI9s/UV91ZZOtr0cKucWwfd+2XR9v Fcn4UaHAeZn6w== X-Nifty-SrcIP: [125.199.20.195] From: Masahiro Yamada To: linux-kbuild@vger.kernel.org, Linus Torvalds Cc: Greg Kroah-Hartman , Andrew Morton , Kees Cook , Nicolas Pitre , "Luis R . Rodriguez" , Randy Dunlap , Ulf Magnusson , Sam Ravnborg , Michal Marek , Martin Schwidefsky , Pavel Machek , linux-s390@vger.kernel.org, Jiri Kosina , Masahiro Yamada , 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 Message-Id: <1518106752-29228-5-git-send-email-yamada.masahiro@socionext.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1518106752-29228-1-git-send-email-yamada.masahiro@socionext.com> References: <1518106752-29228-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 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 Signed-off-by: Masahiro Yamada --- 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 +#include #include #include #include @@ -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