Skip to contentSkip to conversation

Makefile: fix generating environment file

PATCH Superseded Oleksandr Suvorov 3 participants 10 messages Apr 20, 2021 – Apr 27, 2021 Kbuild

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 Makefile: fix generating environment file

If the CONFIG_USE_DEFAULT_ENV_FILE=y and CONFIG_DEFAULT_ENV_FILE points to the empty environment file, the auto-generated file has the wrong syntax so it leads to the compilation failure:

In file included from include/env_default.h:115,

                 from env/common.c:29:
include/generated/defaultenv_autogenerated.h:1:1: error: expected expression before ?,? token
    1 | , 0x00
      | ^

make[1]: *** [scripts/Makefile.build:266: env/common.o] Error 1 make[1]: *** Waiting for unfinished jobs....
make: *** [Makefile:1744: env] Error 2

Fix this issue conditionally adding the delimiter ", ".

---

Changes in v2:
- Fix the hex-decimal class matching.

 Makefile | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
Makefile +3 -1
index e423f6de746..4e0a9b51cb7 100644
@@ -1853,7 +1853,9 @@ define filechk_defaultenv.h
 	 grep -v '^$$' | \
 	 tr '\n' '\0' | \
 	 sed -e 's/\\\x0\s*//g' | \
-	 xxd -i ; echo ", 0x00" ; )
+	 xxd -i | \
+	 sed -r 's/([0-9a-f])$$/\1, /'; \
+	 echo "0x00" ; )
 endef
 
 define filechk_dt.h
Signature · 1 line
2.31.1
#1 lore raw

PATCH v2 1/1 Makefile: fix generating environment file

Quoted 4 lines · Oleksandr Suvorov

If the CONFIG_USE_DEFAULT_ENV_FILE=y and CONFIG_DEFAULT_ENV_FILE points to the empty environment file, the auto-generated file has the wrong syntax so it leads to the compilation failure:

Glad someone is using CONFIG_USE_DEFAULT_ENV_FILE :) And thanks for reporting this.

Quoted 2 lines

Fix this issue conditionally adding the delimiter ", ".

Hm, yeah, that should work. But I wonder if it would make more sense to ensure tr always gets a final newline (which then gets translated to a nul byte, which in turn gives the trailing 0x00). Something like (untested)

define filechk_defaultenv.h

        ( { grep -v '^#' | grep -v '^$$' ; echo '' ; } | \
         tr '\n' '\0' | \
         sed -e 's/\\\x0\s*//g' | \
         xxd -i ; )

endef

Rasmus

#2 lore raw

PATCH v2 1/1 Makefile: fix generating environment file

Hi Rasmus,

Thanks for your feedback!
Yes, I noted that there were no possible situations with the trailing code != 0x00, but simply removing the additional trailing 0x00 gives us an empty array default_environment[] for the empty defaultenv file. I need to test whether this case is handled in u-boot properly and then prepare the next patch version :P

Quoted 17 lines · Rasmus Villemoes
Quoted 4 lines · Oleksandr Suvorov

If the CONFIG_USE_DEFAULT_ENV_FILE=y and CONFIG_DEFAULT_ENV_FILE points to the empty environment file, the auto-generated file has the wrong syntax so it leads to the compilation failure:

Glad someone is using CONFIG_USE_DEFAULT_ENV_FILE :) And thanks for reporting this.

Quoted 2 lines

Fix this issue conditionally adding the delimiter ", ".

Hm, yeah, that should work. But I wonder if it would make more sense to ensure tr always gets a final newline (which then gets translated to a nul byte, which in turn gives the trailing 0x00). Something like (untested)

Quoted 8 lines

define filechk_defaultenv.h

        ( { grep -v '^#' | grep -v '^$$' ; echo '' ; } | \
         tr '\n' '\0' | \
         sed -e 's/\\\x0\s*//g' | \
         xxd -i ; )

endef

Rasmus

--
Best regards
Oleksandr Suvorov

Toradex AG
Ebenaustrasse 10 | 6048 Horw | Switzerland | T: +41 41 500 48 00

#3 lore raw

PATCH v2 1/1 Makefile: fix generating environment file

Quoted 8 lines · Oleksandr Suvorov

Hi Rasmus,

Thanks for your feedback!
Yes, I noted that there were no possible situations with the trailing code != 0x00, but simply removing the additional trailing 0x00 gives us an empty array default_environment[] for the empty defaultenv file. I need to test whether this case is handled in u-boot properly and then prepare the next patch version :P

No, I'm not suggesting removing the trailing nul byte, it very much has to be there - the binary format of the environment is a sequence of nul-terminated C strings of the key=value form, concatenated back-to-back, terminated by an empty string.

What I'm suggesting is to take the input file

===
foo=bar

# Set our IP address
ip=1.2.3.4
===

do the comment- and empty-line stripping (the two first greps), and then after that add an extra empty line

===
foo=bar
ip=1.2.3.4

===

and then feed that to the 'replace \n by nul bytes' | 'delete backslash+nul+whitespace' | xxd pipe. That way there's always that trailing nul on the input to xxd, i.e. in the example above, we would feed foo=bar\0ip-1.2.3.4\0\0 into xxd, while with an initially empty file xxd would just receive that single nul byte.

It's just that I think terminating the sequence of key=value lines by an empty line more exactly matches the binary format.

Rasmus

#4 lore raw

PATCH v2 1/1 Makefile: fix generating environment file

Hi Rasmus,

Quoted 11 lines · Rasmus Villemoes
Quoted 8 lines · Oleksandr Suvorov

Hi Rasmus,

Thanks for your feedback!
Yes, I noted that there were no possible situations with the trailing code != 0x00, but simply removing the additional trailing 0x00 gives us an empty array default_environment[] for the empty defaultenv file. I need to test whether this case is handled in u-boot properly and then prepare the next patch version :P

Quoted 4 lines

No, I'm not suggesting removing the trailing nul byte, it very much has to be there - the binary format of the environment is a sequence of nul-terminated C strings of the key=value form, concatenated back-to-back, terminated by an empty string.

(/me saying: never answer at night, never answer at night, never answer at night :-D)

Quoted 19 lines

What I'm suggesting is to take the input file

===
foo=bar

# Set our IP address
ip=1.2.3.4
===

do the comment- and empty-line stripping (the two first greps), and then after that add an extra empty line

===
foo=bar
ip=1.2.3.4

===

Quoted 8 lines

and then feed that to the 'replace \n by nul bytes' | 'delete backslash+nul+whitespace' | xxd pipe. That way there's always that trailing nul on the input to xxd, i.e. in the example above, we would feed foo=bar\0ip-1.2.3.4\0\0 into xxd, while with an initially empty file xxd would just receive that single nul byte.

It's just that I think terminating the sequence of key=value lines by an empty line more exactly matches the binary format.

Sure, now I see. Your solution is more straight and clear. Unfortunately, it doesn't work :)
So if you don't mind, I'll try to make it work as it should and post the next version of the patch.

Quoted 2 lines

Rasmus

Signature · 5 lines
Best regards
Oleksandr Suvorov

Toradex AG
Ebenaustrasse 10 | 6048 Horw | Switzerland | T: +41 41 500 48 00
#5 lore raw

PATCH v2 1/1 Makefile: fix generating environment file

Quoted 48 lines · Oleksandr Suvorov

Hi Rasmus,

Quoted 11 lines · Rasmus Villemoes
Quoted 8 lines · Oleksandr Suvorov

Hi Rasmus,

Thanks for your feedback!
Yes, I noted that there were no possible situations with the trailing code != 0x00, but simply removing the additional trailing 0x00 gives us an empty array default_environment[] for the empty defaultenv file. I need to test whether this case is handled in u-boot properly and then prepare the next patch version :P

Quoted 4 lines

No, I'm not suggesting removing the trailing nul byte, it very much has to be there - the binary format of the environment is a sequence of nul-terminated C strings of the key=value form, concatenated back-to-back, terminated by an empty string.

(/me saying: never answer at night, never answer at night, never answer at night :-D)

Quoted 25 lines

What I'm suggesting is to take the input file

===
foo=bar

# Set our IP address
ip=1.2.3.4
===

do the comment- and empty-line stripping (the two first greps), and then after that add an extra empty line

===
foo=bar
ip=1.2.3.4

===

and then feed that to the 'replace \n by nul bytes' | 'delete backslash+nul+whitespace' | xxd pipe. That way there's always that trailing nul on the input to xxd, i.e. in the example above, we would feed foo=bar\0ip-1.2.3.4\0\0 into xxd, while with an initially empty file xxd would just receive that single nul byte.

Quoted 5 lines
Quoted 2 lines

It's just that I think terminating the sequence of key=value lines by an empty line more exactly matches the binary format.

Sure, now I see. Your solution is more straight and clear. Unfortunately, it doesn't work :)

Yeah, I didn't really expect it to. Ah, it's because "set -e" is in effect, so in

	( { grep -v '^#' | grep -v '^$$' ; echo '' ; } | \

the return value of the grep -v '^#' | grep -v '^$$' pipeline is that of the second grep, and when there's no input lines that match (such as, with an empty input file), that's an EXIT_FAILURE. So the whole subshell exits at that point, and nothing gets written to defaultenv_autogenerated.h.

Doing

define filechk_defaultenv.h

	( { grep -v '^#' | grep -v '^$$' || true ; echo '' ; } | \
	 tr '\n' '\0' | \
	 sed -e 's/\\\x0\s*//g' | \
	 xxd -i ; )

endef

seems to work.

Rasmus

#6 lore raw

PATCH v2 1/1 Makefile: fix generating environment file

Quoted 68 lines · Rasmus Villemoes
Quoted 48 lines · Oleksandr Suvorov

Hi Rasmus,

Quoted 11 lines · Rasmus Villemoes
Quoted 8 lines · Oleksandr Suvorov

Hi Rasmus,

Thanks for your feedback!
Yes, I noted that there were no possible situations with the trailing code != 0x00, but simply removing the additional trailing 0x00 gives us an empty array default_environment[] for the empty defaultenv file. I need to test whether this case is handled in u-boot properly and then prepare the next patch version :P

Quoted 4 lines

No, I'm not suggesting removing the trailing nul byte, it very much has to be there - the binary format of the environment is a sequence of nul-terminated C strings of the key=value form, concatenated back-to-back, terminated by an empty string.

(/me saying: never answer at night, never answer at night, never answer at night :-D)

Quoted 25 lines

What I'm suggesting is to take the input file

===
foo=bar

# Set our IP address
ip=1.2.3.4
===

do the comment- and empty-line stripping (the two first greps), and then after that add an extra empty line

===
foo=bar
ip=1.2.3.4

===

and then feed that to the 'replace \n by nul bytes' | 'delete backslash+nul+whitespace' | xxd pipe. That way there's always that trailing nul on the input to xxd, i.e. in the example above, we would feed foo=bar\0ip-1.2.3.4\0\0 into xxd, while with an initially empty file xxd would just receive that single nul byte.

Quoted 5 lines
Quoted 2 lines

It's just that I think terminating the sequence of key=value lines by an empty line more exactly matches the binary format.

Sure, now I see. Your solution is more straight and clear. Unfortunately, it doesn't work :)

Yeah, I didn't really expect it to. Ah, it's because "set -e" is in effect, so in

        ( { grep -v '^#' | grep -v '^$$' ; echo '' ; } | \

the return value of the grep -v '^#' | grep -v '^$$' pipeline is that of the second grep, and when there's no input lines that match (such as, with an empty input file), that's an EXIT_FAILURE. So the whole subshell exits at that point, and nothing gets written to defaultenv_autogenerated.h.

Doing

Quoted 8 lines

define filechk_defaultenv.h

        ( { grep -v '^#' | grep -v '^$$' || true ; echo '' ; } | \
         tr '\n' '\0' | \
         sed -e 's/\\\x0\s*//g' | \
         xxd -i ; )

endef

seems to work.

So will you post your own patch?

Quoted 1 line

Rasmus

Signature · 5 lines
Best regards
Oleksandr Suvorov

Toradex AG
Ebenaustrasse 10 | 6048 Horw | Switzerland | T: +41 41 500 48 00
#7 lore raw

PATCH 1/1 Makefile: fix generation of defaultenv.h from empty initial file

When CONFIG_USE_DEFAULT_ENV_FILE=y and the file CONFIG_DEFAULT_ENV_FILE is empty (or at least doesn't contain any non-comment, non-empty lines), we end up feeding nothing into xxd, which in turn then outputs nothing. Then blindly appending ", 0x00" means that we end up trying to compile (roughly)

const char defaultenv[] = { , 0x00 }

which is of course broken.

To fix that, change the frobbing of the text file so that we always end up printing an extra empty line (which gets turned into that extra nul byte we need) - that corresponds better to the binary format consisting of a series of key=val nul terminated strings, terminated by an empty string.

---
 Makefile | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
Makefile +2 -3
index 3fc9777b0b..b7af2b936d 100644
@@ -1854,11 +1854,10 @@ define filechk_timestamp.h
 endef
 
 define filechk_defaultenv.h
-	(grep -v '^#' | \
-	 grep -v '^$$' | \
+	( { grep -v '^#' | grep -v '^$$' || true ; echo '' ; } | \
 	 tr '\n' '\0' | \
 	 sed -e 's/\\\x0\s*//g' | \
-	 xxd -i ; echo ", 0x00" ; )
+	 xxd -i ; )
 endef
 
 define filechk_dt.h
Signature · 1 line
2.29.2
#8 lore raw

PATCH 1/1 Makefile: fix generation of defaultenv.h from empty initial file

Hi Rasmus,

Thanks for the patch, I've tested it.

Quoted 11 lines · Rasmus Villemoes

When CONFIG_USE_DEFAULT_ENV_FILE=y and the file CONFIG_DEFAULT_ENV_FILE is empty (or at least doesn't contain any non-comment, non-empty lines), we end up feeding nothing into xxd, which in turn then outputs nothing. Then blindly appending ", 0x00" means that we end up trying to compile (roughly)

const char defaultenv[] = { , 0x00 }

which is of course broken.

Quoted 8 lines

To fix that, change the frobbing of the text file so that we always end up printing an extra empty line (which gets turned into that extra nul byte we need) - that corresponds better to the binary format consisting of a series of key=val nul terminated strings, terminated by an empty string.

Quoted 25 lines
---
 Makefile | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
Makefile +2 -4
index 3fc9777b0b..b7af2b936d 100644
@@ -1854,11 +1854,10 @@ define filechk_timestamp.h
 endef
 
 define filechk_defaultenv.h
-       (grep -v '^#' | \
-        grep -v '^$$' | \
+       ( { grep -v '^#' | grep -v '^$$' || true ; echo '' ; } | \
         tr '\n' '\0' | \
         sed -e 's/\\\x0\s*//g' | \
-        xxd -i ; echo ", 0x00" ; )
+        xxd -i ; )
 endef
 
 define filechk_dt.h
--

2.29.2

Signature · 5 lines
Best regards
Oleksandr Suvorov

Toradex AG
Ebenaustrasse 10 | 6048 Horw | Switzerland | T: +41 41 500 48 00
#9 lore raw

PATCH 1/1 Makefile: fix generation of defaultenv.h from empty initial file

In reply to Rasmus Villemoes
Quoted 16 lines · Rasmus Villemoes

When CONFIG_USE_DEFAULT_ENV_FILE=y and the file CONFIG_DEFAULT_ENV_FILE is empty (or at least doesn't contain any non-comment, non-empty lines), we end up feeding nothing into xxd, which in turn then outputs nothing. Then blindly appending ", 0x00" means that we end up trying to compile (roughly)

const char defaultenv[] = { , 0x00 }

which is of course broken.

To fix that, change the frobbing of the text file so that we always end up printing an extra empty line (which gets turned into that extra nul byte we need) - that corresponds better to the binary format consisting of a series of key=val nul terminated strings, terminated by an empty string.

Quoted 3 lines

Applied to u-boot/master, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 659 bytes
Desc: not available
URL: <https://lists.denx.de/pipermail/u-boot/attachments/20210427/6a9cafe4/attachment.sig>