Skip to contentSkip to conversation

Avoid using gawk-specific strtonum()

PATCH In review Simon Glass 5 participants 8 messages Jun 18, 2014 – Jul 8, 2014 Kbuild

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Avoid using gawk-specific strtonum()

We need to subtract two hex numbers. Avoid using strtonum() by doing the subtraction in bc with a suitable input base.

---

 Makefile | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
Makefile +2 -1
index 24d9687..bb2f615 100644
@@ -788,7 +788,8 @@ OBJCOPYFLAGS_u-boot.bin := -O binary
 binary_size_check: u-boot.bin System.map FORCE
 	@file_size=`stat -c %s u-boot.bin` ; \
 	map_size=$(shell cat System.map | \
-		awk '/_image_copy_start/ {start = $$1} /_image_binary_end/ {end = $$1} END {if (start != "" && end != "") print strtonum("0x" end) - strtonum("0x" start)}'); \
+		awk '/_image_copy_start/ {start = $$1} /_image_binary_end/ {end = $$1} END {if (start != "" && end != "") print "ibase=16; " toupper(end) " - " toupper(start)}' \
+		| bc); \
 	if [ "" != "$$map_size" ]; then \
 		if test $$map_size -ne $$file_size; then \
 			echo "System.map shows a binary size of $$map_size" >&2 ; \
Signature · 1 line
2.0.0.526.g5318336
#1 lore raw

PATCH 1/1 Avoid using gawk-specific strtonum()

Dear Simon,

Quoted 15 lines · In message <[email protected]> you wrote:

We need to subtract two hex numbers. Avoid using strtonum() by doing the subtraction in bc with a suitable input base.

---

 Makefile | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
Makefile +0 -0
index 24d9687..bb2f615 100644

Makefile OBJCOPYFLAGS_u-boot.bin := -O binary · line 792

Quoted 6 lines
Makefile +2 -1
@@ -788,7 +788,8 @@ OBJCOPYFLAGS_u-boot.bin := -O binary
 binary_size_check: u-boot.bin System.map FORCE
 	@file_size=`stat -c %s u-boot.bin` ; \
 	map_size=$(shell cat System.map | \
-		awk '/_image_copy_start/ {start = $$1} /_image_binary_end/ {end = $$1} END {if (start != "" && end != "") print strtonum("0x" end) - strtonum("0x" start)}'); \
+		awk '/_image_copy_start/ {start = $$1} /_image_binary_end/ {end = $$1} END {if (start != "" && end != "") print "ibase=16; " toupper(end) " - " toupper(start)}' \
+		| bc); \

I think instead of introducing yet another tool dependency this could be rewritten to use just bash:

	awk '/_image_copy_start/ {start = $1}
	     /_image_binary_end/ {end = $1}
	     END { if (start != "" && end != "")
	     	print "printf %d $$((0x" end " - 0x" start "))"
	     }' |
	bash

?

Best regards,

Wolfgang Denk

Signature · 5 lines
DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
Q:  What's a light-year?
A:  One-third less calories than a regular year.
#2 lore raw

PATCH 1/1 Avoid using gawk-specific strtonum()

Quoted 28 lines · Wolfgang Denk

Dear Simon,

Quoted 15 lines · In message <[email protected]> you wrote:

We need to subtract two hex numbers. Avoid using strtonum() by doing the subtraction in bc with a suitable input base.

---

 Makefile | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
Makefile +0 -0
index 24d9687..bb2f615 100644

Makefile OBJCOPYFLAGS_u-boot.bin := -O binary · line 792

Quoted 6 lines
Makefile +2 -1
@@ -788,7 +788,8 @@ OBJCOPYFLAGS_u-boot.bin := -O binary
 binary_size_check: u-boot.bin System.map FORCE
 	@file_size=`stat -c %s u-boot.bin` ; \
 	map_size=$(shell cat System.map | \
-		awk '/_image_copy_start/ {start = $$1} /_image_binary_end/ {end = $$1} END {if (start != "" && end != "") print strtonum("0x" end) - strtonum("0x" start)}'); \
+		awk '/_image_copy_start/ {start = $$1} /_image_binary_end/ {end = $$1} END {if (start != "" && end != "") print "ibase=16; " toupper(end) " - " toupper(start)}' \
+		| bc); \

I think instead of introducing yet another tool dependency this could be rewritten to use just bash:

Quoted 6 lines
	awk '/_image_copy_start/ {start = $1}
	     /_image_binary_end/ {end = $1}
	     END { if (start != "" && end != "")
	     	print "printf %d $$((0x" end " - 0x" start "))"
	     }' |
	bash

But now we're forcing bash. Jeroen, since you most often run into the "but now you've introduced a Linux'ism" problems, bash or bc or something else for a portable hex to decimal conversion? Or since so much of U-Boot is hex when talking numbers, maybe we should just make the echos say "... size of 0x$foo expected 0x$bar" ?

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: Digital signature
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20140618/79543270/attachment.pgp>
#3 lore raw

PATCH 1/1 Avoid using gawk-specific strtonum()

Quoted 37 lines · Tom Rini
Quoted 28 lines · Wolfgang Denk

Dear Simon,

Quoted 15 lines · In message <[email protected]> you wrote:

We need to subtract two hex numbers. Avoid using strtonum() by doing the subtraction in bc with a suitable input base.

---

 Makefile | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
Makefile +0 -0
index 24d9687..bb2f615 100644

Makefile OBJCOPYFLAGS_u-boot.bin := -O binary · line 792

Quoted 6 lines
Makefile +2 -1
@@ -788,7 +788,8 @@ OBJCOPYFLAGS_u-boot.bin := -O binary
 binary_size_check: u-boot.bin System.map FORCE
 	@file_size=`stat -c %s u-boot.bin` ; \
 	map_size=$(shell cat System.map | \
-		awk '/_image_copy_start/ {start = $$1} /_image_binary_end/ {end = $$1} END {if (start != "" && end != "") print strtonum("0x" end) - strtonum("0x" start)}'); \
+		awk '/_image_copy_start/ {start = $$1} /_image_binary_end/ {end = $$1} END {if (start != "" && end != "") print "ibase=16; " toupper(end) " - " toupper(start)}' \
+		| bc); \

I think instead of introducing yet another tool dependency this could be rewritten to use just bash:

Quoted 6 lines
	awk '/_image_copy_start/ {start = $1}
	     /_image_binary_end/ {end = $1}
	     END { if (start != "" && end != "")
	     	print "printf %d $$((0x" end " - 0x" start "))"
	     }' |
	bash
Quoted 5 lines

But now we're forcing bash. Jeroen, since you most often run into the "but now you've introduced a Linux'ism" problems, bash or bc or something else for a portable hex to decimal conversion? Or since so much of U-Boot is hex when talking numbers, maybe we should just make the echos say "... size of 0x$foo expected 0x$bar" ?

I haven't checked but bc is part of POSIX standard, so it should be fine, social pressure demands I watch soccer ;). But anyway see:
http://pubs.opengroup.org/onlinepubs/000095399/utilities/bc.html

Regards,
Jeroen

#4 lore raw

PATCH 1/1 Avoid using gawk-specific strtonum()

In reply to Tom Rini

Hi Tom,

Quoted 37 lines · Tom Rini
Quoted 28 lines · Wolfgang Denk

Dear Simon,

Quoted 15 lines · In message <[email protected]> you wrote:

We need to subtract two hex numbers. Avoid using strtonum() by doing the subtraction in bc with a suitable input base.

---

 Makefile | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
Makefile +0 -0
index 24d9687..bb2f615 100644

Makefile OBJCOPYFLAGS_u-boot.bin := -O binary · line 792

Quoted 6 lines
Makefile +2 -1
@@ -788,7 +788,8 @@ OBJCOPYFLAGS_u-boot.bin := -O binary
 binary_size_check: u-boot.bin System.map FORCE
    @file_size=`stat -c %s u-boot.bin` ; \
    map_size=$(shell cat System.map | \
-           awk '/_image_copy_start/ {start = $$1} /_image_binary_end/ {end = $$1} END {if (start != "" && end != "") print strtonum("0x" end) - strtonum("0x" start)}'); \
+           awk '/_image_copy_start/ {start = $$1} /_image_binary_end/ {end = $$1} END {if (start != "" && end != "") print "ibase=16; " toupper(end) " - " toupper(start)}' \
+           | bc); \

I think instead of introducing yet another tool dependency this could be rewritten to use just bash:

Quoted 6 lines
      awk '/_image_copy_start/ {start = $1}
           /_image_binary_end/ {end = $1}
           END { if (start != "" && end != "")
              print "printf %d $$((0x" end " - 0x" start "))"
           }' |
      bash
Quoted 5 lines

But now we're forcing bash. Jeroen, since you most often run into the "but now you've introduced a Linux'ism" problems, bash or bc or something else for a portable hex to decimal conversion? Or since so much of U-Boot is hex when talking numbers, maybe we should just make the echos say "... size of 0x$foo expected 0x$bar" ?

That's why I didn't use bash - I recall hitting that problem before but it might have been dtc rather than U-Boot. I thought bc might be safer. But let me know and I will respin.

Tom, the problem is that I need to subtract the two values to get an answer for the size.

Regards
Simon

#5 lore raw

PATCH 1/1 Avoid using gawk-specific strtonum()

Quoted 46 lines · Simon Glass

Hi Tom,

Quoted 37 lines · Tom Rini
Quoted 28 lines · Wolfgang Denk

Dear Simon,

Quoted 15 lines · In message <[email protected]> you wrote:

We need to subtract two hex numbers. Avoid using strtonum() by doing the subtraction in bc with a suitable input base.

---

 Makefile | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
Makefile +0 -0
index 24d9687..bb2f615 100644

Makefile OBJCOPYFLAGS_u-boot.bin := -O binary · line 792

Quoted 6 lines
Makefile +2 -1
@@ -788,7 +788,8 @@ OBJCOPYFLAGS_u-boot.bin := -O binary
 binary_size_check: u-boot.bin System.map FORCE
    @file_size=`stat -c %s u-boot.bin` ; \
    map_size=$(shell cat System.map | \
-           awk '/_image_copy_start/ {start = $$1} /_image_binary_end/ {end = $$1} END {if (start != "" && end != "") print strtonum("0x" end) - strtonum("0x" start)}'); \
+           awk '/_image_copy_start/ {start = $$1} /_image_binary_end/ {end = $$1} END {if (start != "" && end != "") print "ibase=16; " toupper(end) " - " toupper(start)}' \
+           | bc); \

I think instead of introducing yet another tool dependency this could be rewritten to use just bash:

Quoted 6 lines
      awk '/_image_copy_start/ {start = $1}
           /_image_binary_end/ {end = $1}
           END { if (start != "" && end != "")
              print "printf %d $$((0x" end " - 0x" start "))"
           }' |
      bash
Quoted 5 lines

But now we're forcing bash. Jeroen, since you most often run into the "but now you've introduced a Linux'ism" problems, bash or bc or something else for a portable hex to decimal conversion? Or since so much of U-Boot is hex when talking numbers, maybe we should just make the echos say "... size of 0x$foo expected 0x$bar" ?

Quoted 3 lines

That's why I didn't use bash - I recall hitting that problem before but it might have been dtc rather than U-Boot. I thought bc might be safer. But let me know and I will respin.

Given Jeroen's answer, we'll keep to bc.

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: Digital signature
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20140618/7de395b0/attachment.pgp>
#6 lore raw

PATCH 1/1 Avoid using gawk-specific strtonum()

In reply to Tom Rini
Quoted 30 lines · Tom Rini
Quoted 28 lines · Wolfgang Denk

Dear Simon,

Quoted 15 lines · In message <[email protected]> you wrote:

We need to subtract two hex numbers. Avoid using strtonum() by doing the subtraction in bc with a suitable input base.

---

 Makefile | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
Makefile +0 -0
index 24d9687..bb2f615 100644

Makefile OBJCOPYFLAGS_u-boot.bin := -O binary · line 792

Quoted 6 lines
Makefile +2 -1
@@ -788,7 +788,8 @@ OBJCOPYFLAGS_u-boot.bin := -O binary
 binary_size_check: u-boot.bin System.map FORCE
 	@file_size=`stat -c %s u-boot.bin` ; \
 	map_size=$(shell cat System.map | \
-		awk '/_image_copy_start/ {start = $$1} /_image_binary_end/ {end = $$1} END {if (start != "" && end != "") print strtonum("0x" end) - strtonum("0x" start)}'); \
+		awk '/_image_copy_start/ {start = $$1} /_image_binary_end/ {end = $$1} END {if (start != "" && end != "") print "ibase=16; " toupper(end) " - " toupper(start)}' \
+		| bc); \

I think instead of introducing yet another tool dependency this could be rewritten to use just bash:

Quoted 8 lines
Quoted 6 lines
	awk '/_image_copy_start/ {start = $1}
	     /_image_binary_end/ {end = $1}
	     END { if (start != "" && end != "")
	     	print "printf %d $$((0x" end " - 0x" start "))"
	     }' |
	bash

But now we're forcing bash.

Arithmetic expansion including hex constants is POSIX shell:
http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_04

Signature · 2 lines
M?ns Rullg?rd
mans at mansr.com
#7 lore raw
In reply to Simon Glass
Quoted 5 lines · Simon Glass

We need to subtract two hex numbers. Avoid using strtonum() by doing the subtraction in bc with a suitable input base.

Applied to u-boot/master, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: Digital signature
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20140707/79e3c9c8/attachment.pgp>