[PATCH v4 16/31] kconfig: add 'if' built-in function
From: Masahiro Yamada
Date: Thu May 17 2018 - 01:22:17 EST
The 'if' function is invoked in the form:
$(if,condition,then-part,else-part)
The behavior is slightly different from that of Make.
In Make, the condition is true if the expansion contains any characters
(even space). That is why we sometimes need $(strip ...) in the
condition part.
I thought it was a nasty part in Make, so I changed it for Kconfig;
the condition is true if the expansion contains any characters except
whitespaces.
Unlike the other functions, the parameters passed to 'if' are lazily
expanded. The then-part is expanded only when the condition true,
and the else-part when false. If the parameters were evaluated
before passed to the 'if' function, $(if,$(cond),$(error,...))
would terminate the parsing regardless of $(cond).
Signed-off-by: Masahiro Yamada <yamada.masahiro@xxxxxxxxxxxxx>
---
Changes in v4:
- Newly added
Changes in v3: None
Changes in v2: None
scripts/kconfig/preprocess.c | 27 +++++++++++++++++++++++++++
1 file changed, 27 insertions(+)
diff --git a/scripts/kconfig/preprocess.c b/scripts/kconfig/preprocess.c
index 591bcf7..88844a7 100644
--- a/scripts/kconfig/preprocess.c
+++ b/scripts/kconfig/preprocess.c
@@ -111,6 +111,32 @@ static char *do_error(int argc, char *argv[], int old_argc, char *old_argv[])
return NULL;
}
+static char *do_if(int argc, char *argv[], int old_argc, char *old_argv[])
+{
+ char *cond, *p, *res;
+ const char *sel;
+
+ cond = expand_string_with_args(argv[0], old_argc, old_argv);
+ p = cond;
+
+ /* condition is true if any character except whitespaces is contained */
+ while (*p == ' ' || *p == '\t')
+ p++;
+
+ if (*p)
+ sel = argv[1];
+ else if (argc >= 3)
+ sel = argv[2];
+ else
+ sel = "";
+
+ res = expand_string_with_args(sel, old_argc, old_argv);
+
+ free(cond);
+
+ return res;
+}
+
static char *do_info(int argc, char *argv[], int old_argc, char *old_argv[])
{
printf("%s\n", argv[0]);
@@ -168,6 +194,7 @@ static char *do_warning(int argc, char *argv[], int old_argc, char *old_argv[])
static const struct function function_table[] = {
/* Name MIN MAX EXP? Function */
{ "error", 1, 1, true, do_error },
+ { "if", 2, 3, false, do_if },
{ "info", 1, 1, true, do_info },
{ "shell", 1, 1, true, do_shell },
{ "warning", 1, 1, true, do_warning },
--
2.7.4