All of lore.kernel.org
 help / color / mirror / Atom feed
From: Masahiro Yamada <yamada.masahiro@socionext.com>
To: Ulf Magnusson <ulfalizer@gmail.com>
Cc: Linux Kbuild mailing list <linux-kbuild@vger.kernel.org>,
	Sam Ravnborg <sam@ravnborg.org>,
	Linus Torvalds <torvalds@linux-foundation.org>,
	Arnd Bergmann <arnd@arndb.de>, Kees Cook <keescook@chromium.org>,
	Thomas Gleixner <tglx@linutronix.de>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	Randy Dunlap <rdunlap@infradead.org>,
	"Luis R . Rodriguez" <mcgrof@kernel.org>,
	Nicolas Pitre <nico@linaro.org>,
	Linux Kernel Mailing List <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH v2 09/21] kconfig: add 'macro' keyword to support user-defined function
Date: Fri, 13 Apr 2018 14:44:13 +0900	[thread overview]
Message-ID: <CAK7LNAQjY_yNc51pLhaYPSbW82zdB+cMM2KnOYeDM5Lce9AHDA@mail.gmail.com> (raw)
In-Reply-To: <CAFkk2KToVh7YLm0B87ypOcaZsUVFfXgQ2Q3gbDuTujEgkzAmEA@mail.gmail.com>

2018-04-01 15:05 GMT+09:00 Ulf Magnusson <ulfalizer@gmail.com>:
> On Tue, Mar 27, 2018 at 7:29 AM, Masahiro Yamada
> <yamada.masahiro@socionext.com> wrote:
>> Now, we got a basic ability to test compiler capability in Kconfig.
>>
>> config CC_HAS_STACKPROTECTOR
>>         def_bool $(shell (($CC -Werror -fstack-protector -c -x c /dev/null -o /dev/null) && echo y) || echo n)
>>
>> This works, but it is ugly to repeat this long boilerplate.
>>
>> We want to describe like this:
>>
>> config CC_HAS_STACKPROTECTOR
>>         bool
>>         default $(cc-option -fstack-protector)
>>
>> It is straight-forward to add a new function, but I do not like to
>> hard-code specialized functions like this.  Hence, here is another
>> feature to add functions from Kconfig files.
>>
>> A user-defined function is defined with a special keyword 'macro'.
>> It can be referenced in the same way as built-in functions.  This
>> feature was also inspired by Makefile where user-defined functions
>> are referenced by $(call func-name, args...), but I omitted the 'call'
>> to makes it shorter.
>>
>> The macro definition can contain $(1), $(2), ... which will be replaced
>> with arguments from the caller.  The macro works just as a textual
>> shorthand, which is also expanded in the lexer phase.
>>
>> [Example Code]
>>
>>   macro success $(shell ($(1) && echo y) || echo n)
>>
>>   config TRUE
>>           bool "true"
>>           default $(success true)
>>
>>   config FALSE
>>           bool "false"
>>           default $(success false)
>>
>> [Result]
>>
>>   $ make -s alldefconfig
>>   $ tail -n 2 .config
>>   CONFIG_TRUE=y
>>   # CONFIG_FALSE is not set
>>
>> [Example Code]
>>
>>   macro success $(shell ($(1) && echo y) || echo n)
>>
>>   macro cc-option $(success $CC -Werror $(1) -c -x c /dev/null -o /dev/null)
>>
>>   config CC_HAS_STACKPROTECTOR
>>           def_bool $(cc-option -fstack-protector)
>>
>> [Result]
>>   $ make -s alldefconfig
>>   $ tail -n 1 .config
>>   CONFIG_CC_HAS_STACKPROTECTOR=y
>>
>> Signed-off-by: Masahiro Yamada <yamada.masahiro@socionext.com>
>> ---
>>
>> Reminder for myself:
>> Update Documentation/kbuild/kconfig-language.txt
>>
>> Changes in v2:
>>   - Use 'macro' directly instead of inside the string type symbol.
>>
>>  scripts/kconfig/function.c  | 59 +++++++++++++++++++++++++++++++++++++++++++--
>>  scripts/kconfig/lkc_proto.h |  1 +
>>  scripts/kconfig/zconf.l     | 31 ++++++++++++++++++++++++
>>  3 files changed, 89 insertions(+), 2 deletions(-)
>>
>> diff --git a/scripts/kconfig/function.c b/scripts/kconfig/function.c
>> index 913685f..389bb44 100644
>> --- a/scripts/kconfig/function.c
>> +++ b/scripts/kconfig/function.c
>> @@ -15,6 +15,7 @@ static LIST_HEAD(function_list);
>>  struct function {
>>         char *name;
>>         char *(*func)(struct function *f, int argc, char *argv[]);
>> +       char *macro;
>>         struct list_head node;
>>  };
>>
>> @@ -31,7 +32,8 @@ static struct function *func_lookup(const char *name)
>>  }
>>
>>  static void func_add(const char *name,
>> -                    char *(*func)(struct function *f, int argc, char *argv[]))
>> +                    char *(*func)(struct function *f, int argc, char *argv[]),
>> +                    const char *macro)
>>  {
>>         struct function *f;
>>
>> @@ -44,6 +46,7 @@ static void func_add(const char *name,
>>         f = xmalloc(sizeof(*f));
>>         f->name = xstrdup(name);
>>         f->func = func;
>> +       f->macro = macro ? xstrdup(macro) : NULL;
>>
>>         list_add_tail(&f->node, &function_list);
>>  }
>> @@ -51,6 +54,7 @@ static void func_add(const char *name,
>>  static void func_del(struct function *f)
>>  {
>>         list_del(&f->node);
>> +       free(f->macro);
>>         free(f->name);
>>         free(f);
>>  }
>> @@ -108,6 +112,57 @@ char *func_eval_n(const char *func, size_t n)
>>         return res;
>>  }
>>
>> +/* run user-defined function */
>> +static char *do_macro(struct function *f, int argc, char *argv[])
>> +{
>> +       char *new;
>> +       char *src, *p, *res;
>> +       size_t newlen;
>> +       int n;
>> +
>> +       new = xmalloc(1);
>> +       *new = 0;
>
> new = '\0' would be consistent with the rest of the code.
>
>> +
>> +       /*
>> +        * This is a format string. $(1), $(2), ... must be replaced with
>> +        * function arguments.
>> +        */
>> +       src = f->macro;
>> +       p = src;
>> +
>> +       while ((p = strstr(p, "$("))) {
>> +               if (isdigit(p[2]) && p[3] == ')') {
>> +                       n = p[2] - '0';
>> +                       if (n < argc) {
>> +                               newlen = strlen(new) + (p - src) +
>> +                                                       strlen(argv[n]) + 1;
>> +                               new = xrealloc(new, newlen);
>> +                               strncat(new, src, p - src);
>> +                               strcat(new, argv[n]);
>> +                               src = p + 4;
>> +                       }
>
> Might be nice to warn when a macro call has missing arguments.


I made this a feature as we see in Make.

In Makefile, it is allowed to pass more or less arguments.

cc-option is often used with one flag.

   $(call cc-option,-fno-PIE)

But, it can take one more as a fall-back compiler option.

   $(call cc-option,-mtune=marvell-f,-mtune=xscale)


Of course, we can describe the first one like follows
just to pass a blank parameter as the last parameter.

   $(call cc-option,-fno-PIE,)




>> +                       p += 2;
>> +               }
>> +               p += 2;
>> +       }
>
> I had to stare at this for a while to see how it worked. What do you
> think of this tweak?
>
> while ((p = strstr(p, "$("))) {
>         if (isdigit(p[2]) && p[3] == ')') {
>                 n = p[2] - '0';
>                 if (n < argc) {
>                         newlen = strlen(new) + (p - src) +
>                                                 strlen(argv[n]) + 1;
>                         new = xrealloc(new, newlen);
>                         strncat(new, src, p - src);
>                         strcat(new, argv[n]);
>
>                         /*
>                          * Jump past macro parameter ("$(n)") and remember the
>                          * position
>                          */
>                         p += 4;
>                         src = p;
>
>                         continue;
>                 }
>         }
>
>         /* Jump past "$(" that isn't from a macro parameter */
>         p += 2;
> }


Will work, but I re-implemented the parser in v3.


>> +
>> +       newlen = strlen(new) + strlen(src) + 1;
>> +       new = xrealloc(new, newlen);
>> +       strcat(new, src);
>> +
>> +       res = expand_string_value(new);
>> +
>> +       free(new);
>> +
>> +       return res;
>> +}
>> +
>> +/* add user-defined function (macro) */
>> +void func_add_macro(const char *name, const char *macro)
>> +{
>> +       func_add(name, do_macro, macro);
>> +}
>> +
>>  /* built-in functions */
>>  static char *do_shell(struct function *f, int argc, char *argv[])
>>  {
>> @@ -157,7 +212,7 @@ static char *do_shell(struct function *f, int argc, char *argv[])
>>  void func_init(void)
>>  {
>>         /* register built-in functions */
>> -       func_add("shell", do_shell);
>> +       func_add("shell", do_shell, NULL);
>>  }
>>
>>  void func_exit(void)
>> diff --git a/scripts/kconfig/lkc_proto.h b/scripts/kconfig/lkc_proto.h
>> index 09a4f53..48699c0 100644
>> --- a/scripts/kconfig/lkc_proto.h
>> +++ b/scripts/kconfig/lkc_proto.h
>> @@ -50,6 +50,7 @@ const char * prop_get_type_name(enum prop_type type);
>>
>>  /* function.c */
>>  char *func_eval_n(const char *func, size_t n);
>> +void func_add_macro(const char *name, const char *macro);
>>  void func_init(void);
>>  void func_exit(void);
>>
>> diff --git a/scripts/kconfig/zconf.l b/scripts/kconfig/zconf.l
>> index 551ca47..6a18c68 100644
>> --- a/scripts/kconfig/zconf.l
>> +++ b/scripts/kconfig/zconf.l
>> @@ -74,6 +74,36 @@ static void warn_ignored_character(char chr)
>>                 "%s:%d:warning: ignoring unsupported character '%c'\n",
>>                 zconf_curname(), zconf_lineno(), chr);
>>  }
>> +
>> +static void handle_macro(const char *text)
>> +{
>> +       char *p, *q;
>> +
>> +       while (isspace(*text))
>> +               text++;
>> +
>> +       p = xstrdup(text);
>> +
>> +       q = p;
>> +       while (isalnum(*q) || *q == '_' || *q == '-')
>> +               q++;
>> +
>> +       if (q == p || !*q) {
>> +               yyerror("invalid\n");
>> +               goto free;
>> +       }
>> +
>> +       *q = '\0';
>> +
>> +       q++;
>> +       while (isspace(*q))
>> +               q++;
>> +
>> +       func_add_macro(p, q);
>> +free:
>> +       free(p);
>> +}
>> +
>>  %}
>>
>>  n      [A-Za-z0-9_-]
>> @@ -82,6 +112,7 @@ n    [A-Za-z0-9_-]
>>         int str = 0;
>>         int ts, i;
>>
>> +"macro"[ \t].* handle_macro(yytext + 6);
>>  [ \t]*#.*\n    |
>>  [ \t]*\n       {
>>         return T_EOL;
>> --
>> 2.7.4
>>
>
> Cheers,
> Ulf
> --
> To unsubscribe from this list: send the line "unsubscribe linux-kbuild" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html



-- 
Best Regards
Masahiro Yamada

  parent reply	other threads:[~2018-04-13  5:44 UTC|newest]

Thread overview: 78+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-03-27  5:29 [PATCH v2 00/21] kconfig: move compiler capability tests to Kconfig Masahiro Yamada
2018-03-27  5:29 ` Masahiro Yamada
2018-03-27  5:29 ` [PATCH v2 01/21] kbuild: remove kbuild cache Masahiro Yamada
2018-03-28  3:26   ` Kees Cook
2018-03-27  5:29 ` [PATCH v2 02/21] kbuild: remove CONFIG_CROSS_COMPILE support Masahiro Yamada
2018-03-28  3:28   ` Kees Cook
2018-03-27  5:29 ` [PATCH v2 03/21] kconfig: move and rename sym_expand_string_value() Masahiro Yamada
2018-03-28  3:29   ` Kees Cook
2018-03-27  5:29 ` [PATCH v2 04/21] kconfig: reference environments directly and remove 'option env=' syntax Masahiro Yamada
2018-03-28  3:33   ` Kees Cook
2018-03-29  2:19   ` Ulf Magnusson
2018-03-29  2:56     ` Ulf Magnusson
2018-03-29 17:38       ` Ulf Magnusson
2018-03-30  5:30     ` Masahiro Yamada
2018-04-01  2:27   ` Ulf Magnusson
2018-04-01  2:40     ` Ulf Magnusson
2018-04-13  6:02     ` Masahiro Yamada
2018-03-27  5:29 ` [PATCH v2 05/21] kconfig: remove string expansion in file_lookup() Masahiro Yamada
2018-03-28  3:34   ` Kees Cook
2018-04-01  2:52   ` Ulf Magnusson
2018-03-27  5:29 ` [PATCH v2 06/21] kconfig: remove string expansion for mainmenu after yyparse() Masahiro Yamada
2018-03-28  3:35   ` Kees Cook
2018-04-01  2:59   ` Ulf Magnusson
2018-03-27  5:29 ` [PATCH v2 07/21] kconfig: add function support and implement 'shell' function Masahiro Yamada
2018-03-28  3:41   ` Kees Cook
2018-04-13  5:32     ` Masahiro Yamada
2018-03-29  2:42   ` Ulf Magnusson
2018-04-01  4:19   ` Ulf Magnusson
2018-04-13  5:37     ` Masahiro Yamada
2018-03-27  5:29 ` [PATCH v2 08/21] kconfig: replace $UNAME_RELEASE with function call Masahiro Yamada
2018-03-28  3:42   ` Kees Cook
2018-04-01  4:38   ` Ulf Magnusson
2018-03-27  5:29 ` [PATCH v2 09/21] kconfig: add 'macro' keyword to support user-defined function Masahiro Yamada
2018-03-28  3:45   ` Kees Cook
2018-04-01  6:05   ` Ulf Magnusson
2018-04-01  6:49     ` Ulf Magnusson
2018-04-13  5:44     ` Masahiro Yamada [this message]
2018-03-27  5:29 ` [PATCH v2 10/21] kconfig: add 'success' and 'cc-option' macros Masahiro Yamada
2018-03-28  3:46   ` Kees Cook
2018-04-01  6:28   ` Ulf Magnusson
2018-03-27  5:29 ` [PATCH v2 11/21] stack-protector: test compiler capability in Kconfig and drop AUTO mode Masahiro Yamada
2018-03-28 11:18   ` Kees Cook
2018-04-09  8:54     ` Masahiro Yamada
2018-04-09 15:04       ` Kees Cook
2018-04-10  3:15         ` Masahiro Yamada
2018-03-27  5:29 ` [PATCH v2 12/21] kconfig: show compiler version text in the top comment Masahiro Yamada
2018-03-28  3:26   ` Kees Cook
2018-03-27  5:29 ` [PATCH v2 13/21] kconfig: add CC_IS_GCC and GCC_VERSION Masahiro Yamada
2018-03-28 11:19   ` Kees Cook
2018-03-27  5:29 ` [PATCH v2 14/21] kconfig: add CC_IS_CLANG and CLANG_VERSION Masahiro Yamada
2018-03-28 11:22   ` Kees Cook
2018-03-28 11:52     ` Masahiro Yamada
2018-03-27  5:29 ` [PATCH v2 15/21] gcov: remove CONFIG_GCOV_FORMAT_AUTODETECT Masahiro Yamada
2018-03-27  9:12   ` Peter Oberparleiter
2018-03-28 11:24   ` Kees Cook
2018-03-27  5:29 ` [PATCH v2 16/21] kcov: imply GCC_PLUGINS and GCC_PLUGIN_SANCOV instead of select'ing them Masahiro Yamada
2018-03-28 11:25   ` Kees Cook
2018-03-28 11:53   ` Kees Cook
2018-03-27  5:29 ` [PATCH v2 17/21] gcc-plugins: always build plugins with C++ Masahiro Yamada
2018-03-28 11:29   ` Kees Cook
2018-03-27  5:29 ` [PATCH v2 18/21] gcc-plugins: move GCC version check for PowerPC to Kconfig Masahiro Yamada
2018-03-28 11:30   ` Kees Cook
2018-03-27  5:29 ` [PATCH v2 19/21] gcc-plugins: test GCC plugin support in Kconfig Masahiro Yamada
2018-03-28 11:44   ` Kees Cook
2018-04-11 15:55     ` Masahiro Yamada
2018-04-11 16:09       ` Kees Cook
2018-03-27  5:29 ` [PATCH v2 20/21] gcc-plugins: enable GCC_PLUGINS for COMPILE_TEST Masahiro Yamada
2018-03-28 11:47   ` Kees Cook
2018-04-10  6:15     ` Masahiro Yamada
2018-04-10  7:00       ` Kees Cook
2018-03-27  5:29 ` [PATCH v2 21/21] arm64: move GCC version check for ARCH_SUPPORTS_INT128 to Kconfig Masahiro Yamada
2018-03-27  5:29   ` Masahiro Yamada
2018-03-27 17:28   ` Will Deacon
2018-03-27 17:28     ` Will Deacon
2018-03-28 11:55   ` Kees Cook
2018-03-28 11:55     ` Kees Cook
2018-03-27 16:39 ` [PATCH v2 00/21] kconfig: move compiler capability tests " Masahiro Yamada
2018-03-27 16: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=CAK7LNAQjY_yNc51pLhaYPSbW82zdB+cMM2KnOYeDM5Lce9AHDA@mail.gmail.com \
    --to=yamada.masahiro@socionext.com \
    --cc=arnd@arndb.de \
    --cc=gregkh@linuxfoundation.org \
    --cc=keescook@chromium.org \
    --cc=linux-kbuild@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mcgrof@kernel.org \
    --cc=nico@linaro.org \
    --cc=rdunlap@infradead.org \
    --cc=sam@ravnborg.org \
    --cc=tglx@linutronix.de \
    --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.