diff mbox series

[v4,16/31] kconfig: add 'if' built-in function

Message ID 1526537830-22606-17-git-send-email-yamada.masahiro@socionext.com
State New
Headers show
Series kconfig: move compiler capability tests to Kconfig | expand

Commit Message

Masahiro Yamada May 17, 2018, 6:16 a.m. UTC
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@socionext.com>

---

Changes in v4:
 - Newly added

Changes in v3: None
Changes in v2: None

 scripts/kconfig/preprocess.c | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)

-- 
2.7.4
diff mbox series

Patch

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 },