Patchwork [U-Boot,v2,4/5] env: Allow environment files to use the C preprocessor

login
register
mail settings
Submitter Simon Glass
Date June 24, 2013, 8:46 p.m.
Message ID <1372106765-18401-5-git-send-email-sjg@chromium.org>
Download mbox | patch
Permalink /patch/253968/
State Superseded
Delegated to: Tom Rini
Headers show

Comments

Simon Glass - June 24, 2013, 8:46 p.m.
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.

Signed-off-by: Simon Glass <sjg@chromium.org>
---
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                     |  3 ++-
 README                       | 17 ++++++++++++++++-
 tools/scripts/env2string.awk |  6 ++++++
 3 files changed, 24 insertions(+), 2 deletions(-)
Stephen Warren - June 26, 2013, 8:05 p.m.
On 06/24/2013 02:46 PM, Simon Glass 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.

The addition of += seems like a separate change to enabling
pre-processing, but I guess they're both simple enough and this is a new
features, perhaps it's not a big deal.

> diff --git a/Makefile b/Makefile

>  $(obj)include/generated/environment.in: $(obj)include/generated/autoconf.mk.base \
>  		$(wildcard $(ENV_FILE))
>  	if [ -f "$(ENV_FILE)" ]; then \
> -		cat $(ENV_FILE) >$@ ; \
> +		$(CPP) -P $(CFLAGS) -x assembler-with-cpp -D__ASSEMBLY__ \
> +			-include $(obj)include/config.h $(ENV_FILE) -o $@; \

I guess -undef doesn't make sense here, since config.h could well rely
on standard pre-defined macros.

Does it make sense to -D __UBOOT_CONFIG__ rather than, or in addition
to, -D __ASSEMBLY__ here, so headers can tell what they're being
included for? The series I sent for dtc+cpp did -D __DTS__ for similar
reasons.

> diff --git a/tools/scripts/env2string.awk b/tools/scripts/env2string.awk

> +		# Deal with +=
> +		if (match(var, "(.*)[+]$", var_arr)) {
> +			var = var_arr[1]
> +			env = vars[var] env
> +		}

Does this work if you write just:

foo+=bar

rather than:

foo=
foo+=bar

It might be worth allowing the former syntax, so you don't need to
explicitly assign empty values before a sequence of ifdef'd +=
operations. If the above blows up, at least a descriptive error message
might be useful.
Simon Glass - Oct. 20, 2013, 9:09 p.m.
Hi Stephen,

On Wed, Jun 26, 2013 at 2:05 PM, Stephen Warren <swarren@wwwdotorg.org> wrote:
> On 06/24/2013 02:46 PM, Simon Glass 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.
>
> The addition of += seems like a separate change to enabling
> pre-processing, but I guess they're both simple enough and this is a new
> features, perhaps it's not a big deal.

Yes it is separate but I was concerned about not creating too many
patches for the same feature.

>
>> diff --git a/Makefile b/Makefile
>
>>  $(obj)include/generated/environment.in: $(obj)include/generated/autoconf.mk.base \
>>               $(wildcard $(ENV_FILE))
>>       if [ -f "$(ENV_FILE)" ]; then \
>> -             cat $(ENV_FILE) >$@ ; \
>> +             $(CPP) -P $(CFLAGS) -x assembler-with-cpp -D__ASSEMBLY__ \
>> +                     -include $(obj)include/config.h $(ENV_FILE) -o $@; \
>
> I guess -undef doesn't make sense here, since config.h could well rely
> on standard pre-defined macros.

In some cases yes.

>
> Does it make sense to -D __UBOOT_CONFIG__ rather than, or in addition
> to, -D __ASSEMBLY__ here, so headers can tell what they're being
> included for? The series I sent for dtc+cpp did -D __DTS__ for similar
> reasons.

Will do.

>
>> diff --git a/tools/scripts/env2string.awk b/tools/scripts/env2string.awk
>
>> +             # Deal with +=
>> +             if (match(var, "(.*)[+]$", var_arr)) {
>> +                     var = var_arr[1]
>> +                     env = vars[var] env
>> +             }
>
> Does this work if you write just:
>
> foo+=bar
>
> rather than:
>
> foo=
> foo+=bar
>
> It might be worth allowing the former syntax, so you don't need to
> explicitly assign empty values before a sequence of ifdef'd +=
> operations. If the above blows up, at least a descriptive error message
> might be useful.

Yes that works for me. With awk variable appear when used which helps here.

Regards,
Simon

Patch

diff --git a/Makefile b/Makefile
index 9dae750..89ac4c5 100644
--- a/Makefile
+++ b/Makefile
@@ -714,7 +714,8 @@  ENV_FILE := $(if $(wildcard $(ENV_FILE_BOARD)),$(ENV_FILE_BOARD),$(ENV_FILE_COMM
 $(obj)include/generated/environment.in: $(obj)include/generated/autoconf.mk.base \
 		$(wildcard $(ENV_FILE))
 	if [ -f "$(ENV_FILE)" ]; then \
-		cat $(ENV_FILE) >$@ ; \
+		$(CPP) -P $(CFLAGS) -x assembler-with-cpp -D__ASSEMBLY__ \
+			-include $(obj)include/config.h $(ENV_FILE) -o $@; \
 	else \
 		echo -n >$@ ; \
 	fi
diff --git a/README b/README
index 2c8a8c9..6711cd0 100644
--- a/README
+++ b/README
@@ -4376,12 +4376,25 @@  have a common environment for all vendor boards.
 
 This is a plain text file where you can type your environment variables in
 the form 'var=value'. Blank lines and multi-line variables are supported.
+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.
 
 For example, for snapper9260 you would create a text file called
 board/bluewater/env/snapper9260.env containing the environment text.
 
+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.
+
 >>>
+stdout=serial
+#ifdef CONFIG_LCD
+stdout+=,lcd
+#endif
 bootcmd=
+	/* U-Boot script for booting */
+
 	if [ -z ${tftpserverip} ]; then
 		echo "Use 'setenv tftpserverip a.b.c.d' to set IP address."
 	fi
@@ -4390,7 +4403,9 @@  bootcmd=
 	tftpboot ${tftpserverip}:
 	bootm
 failed=
-	echo boot failed - please check your image
+	/* Print a message when boot fails */
+	echo CONFIG_SYS_BOARD boot failed - please check your image
+	echo Load address is CONFIG_SYS_LOAD_ADDR
 <<<
 
 The resulting environment can be exported and importing using the
diff --git a/tools/scripts/env2string.awk b/tools/scripts/env2string.awk
index 2d167c0..d647cf3 100644
--- a/tools/scripts/env2string.awk
+++ b/tools/scripts/env2string.awk
@@ -23,6 +23,12 @@  BEGIN {
 		}
 		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;