Skip to contentSkip to conversation

Makefile: use shell to calculate map_size

PATCH Superseded Leon M. Busch-George 3 participants 8 messages Mar 2, 2024 – Mar 10, 2024 3 messages not in the archive Kbuild

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Makefile: use shell to calculate map_size

From: "Leon M. Busch-George" <[email protected]>

The error message "bc: command not found" is easily missed since the build continues.
bc is not a part of coreutils or base-devel. POSIX sh can also do the calculation.

---
 Makefile | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
Makefile +2 -2
index 7a3209bd9e..41dc4baad2 100644
@@ -1275,9 +1275,9 @@ OBJCOPYFLAGS_u-boot-nodtb.bin := -O binary \
 binary_size_check: u-boot-nodtb.bin FORCE
 	@file_size=$(shell wc -c u-boot-nodtb.bin | awk '{print $$1}') ; \
 	map_size=$(shell cat u-boot.map | \
-		awk '/_image_copy_start/ {start = $$1} /_image_binary_end/ {end = $$1} END {if (start != "" && end != "") print "ibase=16; " toupper(end) " - " toupper(start)}' \
+		awk '/_image_copy_start/ {start = $$1} /_image_binary_end/ {end = $$1} END {if (start != "" && end != "") print "echo $$((0x" toupper(end) " - 0x" toupper(start) "))"}' \
 		| sed 's/0X//g' \
-		| bc); \
+		| sh); \
 	if [ "" != "$$map_size" ]; then \
 		if test $$map_size -ne $$file_size; then \
 			echo "u-boot.map shows a binary size of $$map_size" >&2 ; \
Signature · 1 line
2.44.0
Reviewed-by on v1 1/1
#1 lore raw

Hello Leon,

Quoted 8 lines · Leon M. Busch-George

From: "Leon M. Busch-George" <[email protected]>

The error message "bc: command not found" is easily missed since the build continues.
bc is not a part of coreutils or base-devel. POSIX sh can also do the calculation.

Please, see a nitpick below. Otherwise, the patch is looking good to me.

Quoted 18 lines
---
 Makefile | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
Makefile +0 -1
index 7a3209bd9e..41dc4baad2 100644
@@ -1275,9 +1275,9 @@ OBJCOPYFLAGS_u-boot-nodtb.bin := -O binary \
 binary_size_check: u-boot-nodtb.bin FORCE
 	@file_size=$(shell wc -c u-boot-nodtb.bin | awk '{print $$1}') ; \
 	map_size=$(shell cat u-boot.map | \
-		awk '/_image_copy_start/ {start = $$1} /_image_binary_end/ {end =

$$1} END {if (start != "" && end != "") print "ibase=16; " toupper(end) " - " toupper(start)}' \

Makefile +1 -0
+		awk '/_image_copy_start/ {start = $$1} /_image_binary_end/ {end =

$$1} END {if (start != "" && end != "") print "echo $$((0x" toupper(end) " - 0x" toupper(start) "))"}' \

Makefile OBJCOPYFLAGS_u-boot-nodtb.bin := -O binary \ · line 1280

Quoted 3 lines
Makefile +1 -1
 		| sed 's/0X//g' \
-		| bc); \
+		| sh); \

Maybe "sh -s" could be used instead, just for some additional strictness.

Quoted 3 lines
Makefile +0 -0
 	if [ "" != "$$map_size" ]; then \
 		if test $$map_size -ne $$file_size; then \
 			echo "u-boot.map shows a binary size of $$map_size" >&2 ; \

A message that is not in the archive (sent off-list or not yet received): 20240304154007.67df1822@couch-potassium

#3 lore raw

Oops!
That should have went to the list as well...

Quoted 31 lines · On Mon, 4 Mar 2024 15:40:07 +0100 Leon Busch-George <[email protected]> wrote:

Hi Dragan :-)

Thanks for your reply!

Quoted 9 lines · On Sat, 02 Mar 2024 22:13:08 +0100 Dragan Simic <[email protected]> wrote:

Makefile OBJCOPYFLAGS_u-boot-nodtb.bin := -O binary \ · line 1280

Quoted 6 lines
(quoted diff) +1 -0
+		awk '/_image_copy_start/ {start = $$1}

/_image_binary_end/ {end = $$1} END {if (start != "" && end != "") print "echo $$((0x" toupper(end) " - 0x" toupper(start) "))"}' \

Makefile +1 -1
 		| sed 's/0X//g' \
-		| bc); \
+		| sh); \

Maybe "sh -s" could be used instead, just for some additional strictness.

-s is the default already but I see no reason against adding it. Allow me to offer another idea to improve strictness (I'll send a v2):

awk '.. print end " " start ..' | sh -c 'read end start; echo
$((end - start))'

That gets rid off sed and the interface between awk and sh is much cleaner (only the two numbers on one line rather than shell code). Sadly, the sed 's/0X//g' was introduced without an explanation in 3ce7a4fefa and but, looking at it more, I'm farly confident it was only for bc.

kind regards,
Leon

#4 lore raw

PATCH v2 1/1 Makefile: use shell to calculate map_size

In reply to Leon M. Busch-George

From: "Leon M. Busch-George" <[email protected]>

The error message "bc: command not found" is easily missed since the build continues.
bc is not a part of coreutils or base-devel. POSIX sh can also do the calculation.

---
 Makefile | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
Makefile +9 -4
index a2bc9d5903..e8e794368e 100644
@@ -1275,10 +1275,15 @@ OBJCOPYFLAGS_u-boot-nodtb.bin := -O binary \
 binary_size_check: u-boot-nodtb.bin FORCE
 	@file_size=$(shell wc -c u-boot-nodtb.bin | awk '{print $$1}') ; \
 	map_size=$(shell cat u-boot.map | \
-		awk '/_image_copy_start/ {start = $$1} /_image_binary_end/ {end = $$1} END {if (start != "" && end != "") print "ibase=16; " toupper(end) " - " toupper(start)}' \
-		| sed 's/0X//g' \
-		| bc); \
-	if [ "" != "$$map_size" ]; then \
+		awk ' \
+			/_image_copy_start/ { start = $$1 } \
+			/_image_binary_end/ { end = $$1 } \
+			END { \
+				if (start != "" && end != "") \
+					print end " " start; \
+			}' \
+		| sh -c 'read end start; [ -n "$$end" ] && echo $$((end - start))'); \
+	if [ -n "$$map_size" ]; then \
 		if test $$map_size -ne $$file_size; then \
 			echo "u-boot.map shows a binary size of $$map_size" >&2 ; \
 			echo "  but u-boot-nodtb.bin shows $$file_size" >&2 ; \
Signature · 1 line
2.44.0
#5 lore raw

PATCH v3 1/1 Makefile: use shell to calculate map_size

From: "Leon M. Busch-George" <[email protected]>

The error message "bc: command not found" is easily missed since the build continues.
bc is not a part of coreutils or base-devel. POSIX sh can also do the calculation.

---
 Makefile | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
Makefile +9 -4
index a2bc9d5903..0aef07ef3b 100644
@@ -1275,10 +1275,15 @@ OBJCOPYFLAGS_u-boot-nodtb.bin := -O binary \
 binary_size_check: u-boot-nodtb.bin FORCE
 	@file_size=$(shell wc -c u-boot-nodtb.bin | awk '{print $$1}') ; \
 	map_size=$(shell cat u-boot.map | \
-		awk '/_image_copy_start/ {start = $$1} /_image_binary_end/ {end = $$1} END {if (start != "" && end != "") print "ibase=16; " toupper(end) " - " toupper(start)}' \
-		| sed 's/0X//g' \
-		| bc); \
-	if [ "" != "$$map_size" ]; then \
+		awk ' \
+			/_image_copy_start/ { start = $$1 } \
+			/_image_binary_end/ { end = $$1 } \
+			END { \
+				if (start != "" && end != "") \
+					print end " " start; \
+			}' \
+		| sh -c 'read end start && echo $$((end - start))'); \
+	if [ -n "$$map_size" ]; then \
 		if test $$map_size -ne $$file_size; then \
 			echo "u-boot.map shows a binary size of $$map_size" >&2 ; \
 			echo "  but u-boot-nodtb.bin shows $$file_size" >&2 ; \
Signature · 1 line
2.44.0

A message that is not in the archive (sent off-list or not yet received): [email protected]

#7 lore raw
Quoted 9 lines · Dragan Simic
Quoted 8 lines · Leon M. Busch-George

From: "Leon M. Busch-George" <[email protected]>

The error message "bc: command not found" is easily missed since the build continues.
bc is not a part of coreutils or base-devel. POSIX sh can also do the calculation.

Sorry, I totally missed the v3. It's still looking good to me, and my Reviewed-by tag still applies, together with the small nitpick.

Quoted 30 lines
Quoted 30 lines
---
 Makefile | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
Makefile +0 -1
index a2bc9d5903..0aef07ef3b 100644
@@ -1275,10 +1275,15 @@ OBJCOPYFLAGS_u-boot-nodtb.bin := -O binary \
 binary_size_check: u-boot-nodtb.bin FORCE
 	@file_size=$(shell wc -c u-boot-nodtb.bin | awk '{print $$1}') ; \
 	map_size=$(shell cat u-boot.map | \
-		awk '/_image_copy_start/ {start = $$1} /_image_binary_end/ {end =

$$1} END {if (start != "" && end != "") print "ibase=16; " toupper(end) " - " toupper(start)}' \

Makefile +9 -3
-		| sed 's/0X//g' \
-		| bc); \
-	if [ "" != "$$map_size" ]; then \
+		awk ' \
+			/_image_copy_start/ { start = $$1 } \
+			/_image_binary_end/ { end = $$1 } \
+			END { \
+				if (start != "" && end != "") \
+					print end " " start; \
+			}' \
+		| sh -c 'read end start && echo $$((end - start))'); \
+	if [ -n "$$map_size" ]; then \
 		if test $$map_size -ne $$file_size; then \
 			echo "u-boot.map shows a binary size of $$map_size" >&2 ; \
 			echo "  but u-boot-nodtb.bin shows $$file_size" >&2 ; \
#8 lore raw
In reply to Leon M. Busch-George

A colleague made me aware that the '[ -n "$$end" ]' is not necessary since 'read' already returns an exit code.

v3 inc

Quoted 40 lines · On Mon, 4 Mar 2024 21:38:56 +0100 "Leon M. Busch-George" <[email protected]>…

From: "Leon M. Busch-George" <[email protected]>

The error message "bc: command not found" is easily missed since the build continues.
bc is not a part of coreutils or base-devel. POSIX sh can also do the calculation.

---
 Makefile | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
Makefile +9 -4
index a2bc9d5903..e8e794368e 100644
@@ -1275,10 +1275,15 @@ OBJCOPYFLAGS_u-boot-nodtb.bin := -O binary \
 binary_size_check: u-boot-nodtb.bin FORCE
 	@file_size=$(shell wc -c u-boot-nodtb.bin | awk '{print
 $$1}') ; \ map_size=$(shell cat u-boot.map | \
-		awk '/_image_copy_start/ {start = $$1}
 /_image_binary_end/ {end = $$1} END {if (start != "" && end != "")
 print "ibase=16; " toupper(end) " - " toupper(start)}' \
-		| sed 's/0X//g' \
-		| bc); \
-	if [ "" != "$$map_size" ]; then \
+		awk ' \
+			/_image_copy_start/ { start = $$1 } \
+			/_image_binary_end/ { end = $$1 } \
+			END { \
+				if (start != "" && end != "") \
+					print end " " start; \
+			}' \
+		| sh -c 'read end start; [ -n "$$end" ] && echo
 $$((end - start))'); \
+	if [ -n "$$map_size" ]; then \
Makefile +0 -0
 		if test $$map_size -ne $$file_size; then \
 			echo "u-boot.map shows a binary size of

$$map_size" >&2 ; \ echo " but u-boot-nodtb.bin shows $$file_size"

Quoted 1 line

&2 ; \

A message that is not in the archive (sent off-list or not yet received): [email protected]

Reviewed-by
#10 lore raw

Hello Leon,

Quoted 9 lines · Dragan Simic
Quoted 8 lines · Leon M. Busch-George

From: "Leon M. Busch-George" <[email protected]>

The error message "bc: command not found" is easily missed since the build continues.
bc is not a part of coreutils or base-devel. POSIX sh can also do the calculation.

The v2 is looking good to me. I also did some testing of the awk code by hand, outside of the actual Makefile, and it worked as expected.

See also one small nitpick below, and please feel free to include:

Quoted 11 lines

Makefile OBJCOPYFLAGS_u-boot-nodtb.bin := -O binary \ · line 1276

Quoted 11 lines
---
 Makefile | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
Makefile +0 -0
index a2bc9d5903..e8e794368e 100644
@@ -1275,10 +1275,15 @@ OBJCOPYFLAGS_u-boot-nodtb.bin := -O binary \
 binary_size_check: u-boot-nodtb.bin FORCE
 	@file_size=$(shell wc -c u-boot-nodtb.bin | awk '{print $$1}') ; \

Perhaps a couple of spaces could be added around "print $$1" in the line above, to nicely round off the code cleanups.

Quoted 20 lines
Quoted 20 lines
Makefile +0 -1
 	map_size=$(shell cat u-boot.map | \
-		awk '/_image_copy_start/ {start = $$1} /_image_binary_end/ {end =

$$1} END {if (start != "" && end != "") print "ibase=16; " toupper(end) " - " toupper(start)}' \

Makefile +8 -3
-		| sed 's/0X//g' \
-		| bc); \
-	if [ "" != "$$map_size" ]; then \
+		awk ' \
+			/_image_copy_start/ { start = $$1 } \
+			/_image_binary_end/ { end = $$1 } \
+			END { \
+				if (start != "" && end != "") \
+					print end " " start; \
+			}' \
+		| sh -c 'read end start; [ -n "$$end" ] && echo $$((end - 

start))'); \

Makefile +1 -0
+	if [ -n "$$map_size" ]; then \
 		if test $$map_size -ne $$file_size; then \
 			echo "u-boot.map shows a binary size of $$map_size" >&2 ; \
 			echo "  but u-boot-nodtb.bin shows $$file_size" >&2 ; \