diff mbox series

[v5,4/5] env: Allow environment files to use the C preprocessor

Message ID 20211001183842.v5.4.Ie78bfbfca0d01d9cba501e127f446ec48e1f7afe@changeid
State Superseded
Delegated to: Tom Rini
Headers show
Series env: Allow environment in text files | expand

Commit Message

Simon Glass Oct. 2, 2021, 12:38 a.m. UTC
In many cases environment variables need access to the U-Boot CONFIG
variables to select different options. Enable this so that the environment
scripts can be as useful as the ones currently in the board config files.

Also support += to allow variables to be appended to. This is needed when
using the preprocessor.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

(no changes since v4)

Changes in v4:
- Add documentation in rST format instead of README
- Move use of += to this patch
- Explain that environment variables may not end in +

Changes in v3:
- Define __UBOOT_CONFIG__ when collecting environment files

Changes in v2:
- Add separate patch to enable C preprocessor for environment files
- Enable var+=value form to simplify composing variables in multiple steps

 Makefile                  |  7 ++++++-
 doc/usage/environment.rst | 12 ++++++++++++
 scripts/env2string.awk    |  6 ++++++
 3 files changed, 24 insertions(+), 1 deletion(-)

Comments

Wolfgang Denk Oct. 4, 2021, 12:12 p.m. UTC | #1
Dear Simon Glass,

In message <20211001183842.v5.4.Ie78bfbfca0d01d9cba501e127f446ec48e1f7afe@changeid> you wrote:
> In many cases environment variables need access to the U-Boot CONFIG
> variables to select different options. Enable this so that the environment
> scripts can be as useful as the ones currently in the board config files.
>
> Also support += to allow variables to be appended to. This is needed when
> using the preprocessor.
>
> Signed-off-by: Simon Glass <sjg@chromium.org>
> ---
>
> (no changes since v4)
>
> Changes in v4:
> - Add documentation in rST format instead of README
> - Move use of += to this patch
> - Explain that environment variables may not end in +

Sorry, I disagree here.  There was intentionally only very little
restrictions on what a environment variable name should look like -
the only exceptions were the '=' and the NUL characters.

Adding artificial restrictions now just to enable your custom
notation for appending seems not acceptable to me.  You might want
to chose a different notation or implement a proper parser instead.

Thanks.

Wolfgang Denk
diff mbox series

Patch

diff --git a/Makefile b/Makefile
index 5c3ba37398f..289a2188cec 100644
--- a/Makefile
+++ b/Makefile
@@ -1796,7 +1796,12 @@  ENV_FILE := $(if $(wildcard $(ENV_FILE_BOARD)),$(ENV_FILE_BOARD),$(ENV_FILE_COMM
 quiet_cmd_gen_envp = ENVP    $@
       cmd_gen_envp = \
 	if [ -f "$(ENV_FILE)" ]; then \
-		cat $(ENV_FILE) >$@; \
+		$(CPP) -P $(CFLAGS) -x assembler-with-cpp -D__ASSEMBLY__ \
+			-D__UBOOT_CONFIG__ \
+			-I . -I include \
+			-I $(srctree)/include -include include/config.h \
+			-I$(srctree)/arch/$(ARCH)/include \
+			$(ENV_FILE) -o $@; \
 	else \
 		echo -n >$@ ; \
 	fi
diff --git a/doc/usage/environment.rst b/doc/usage/environment.rst
index 4e0770523eb..426d8f65329 100644
--- a/doc/usage/environment.rst
+++ b/doc/usage/environment.rst
@@ -39,12 +39,24 @@  and has an equals sign immediately afterwards. Spaces before the = are not
 permitted. It is a good idea to indent your scripts so that only the 'var='
 appears at the start of a line.
 
+To add additional text to a variable you can use var+=value. This text is
+merged into the variable during the make process and made available as a
+single value to U-Boot. To support this, environment variables may not end
+in `+`.
+
+This file can include C-style comments. Blank lines and multi-line
+variables are supported, and you can use normal C preprocessor directives
+and CONFIG defines from your board config also.
+
 For example, for snapper9260 you would create a text file called
 `board/bluewater/env/snapper9260.env` containing the environment text.
 
 Example::
 
     stdout=serial
+    #ifdef CONFIG_LCD
+    stdout+=,lcd
+    #endif
     bootcmd=
         /* U-Boot script for booting */
 
diff --git a/scripts/env2string.awk b/scripts/env2string.awk
index 661defdc350..6de3e3dd87e 100644
--- a/scripts/env2string.awk
+++ b/scripts/env2string.awk
@@ -29,6 +29,12 @@  NF {
 		}
 		var = arr[1]
 		env = arr[2]
+
+		# Deal with +=
+		if (match(var, "(.*)[+]$", var_arr)) {
+			var = var_arr[1]
+			env = vars[var] env
+		}
 	} else {
 		# Change newline to \n
 		env = env "\\n" $0;