Skip to contentSkip to conversation

updated change to build

RFC New Ben Dooks 3 participants 18 messages Dec 2, 2024 – Dec 17, 2024 Kbuild

lore
Share & tools
lore

Ben Dooks

author
#0 lore raw

Post feedback, this is now a larger series which includes some fixes for the Makefile to do things like remove any carray files which didn't create properly and send errors to stderr so they don't end up in the build output...

Also added an Ubuntu build 'container'.

If the container is useful, I can add documentation on how to use it.

author
#1 lore raw

RFC 1/5 add build container for ubunti 18.04

Add a podman/docker container for Ubuntu 18.04 for build testing on older systems which might not have the same support as a developer has.

---
 containers/ubuntu_1804/Containerfile | 4 ++++
 1 file changed, 4 insertions(+)
 create mode 100644 containers/ubuntu_1804/Containerfile
containers/ubuntu_1804/Containerfile new file +4 -0
new file mode 100644
index 0000000..c2279db
@@ -0,0 +1,4 @@
+FROM ubuntu:18.04
+
+RUN apt-get update && apt-get install build-essential gcc-riscv64-linux-gnu -y
+
Signature · 1 line
2.37.2.352.g3c44437643
#2 lore raw

RFC 1/5 add build container for ubunti 18.04

On Mon, Dec 2, 2024 at 4:38?PM Ben Dooks <[email protected]> wrote:

Typo in subject.

s/ubunti/ubuntu/

containers/ubuntu_1804/Containerfile

Quoted 16 lines

Add a podman/docker container for Ubuntu 18.04 for build testing on older systems which might not have the same support as a developer has.

---
 containers/ubuntu_1804/Containerfile | 4 ++++
 1 file changed, 4 insertions(+)
 create mode 100644 containers/ubuntu_1804/Containerfile
containers/ubuntu_1804/Containerfile new file +0 -0
new file mode 100644
index 0000000..c2279db

Need some documentation about how to use this file.

Quoted 9 lines
containers/ubuntu_1804/Containerfile new file +4 -1
@@ -0,0 +1,4 @@
+FROM ubuntu:18.04
+
+RUN apt-get update && apt-get install build-essential gcc-riscv64-linux-gnu -y
+
--

2.37.2.352.g3c44437643

Quoted 4 lines

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

Regards,
Anup

#3 lore raw

RFC 1/5 add build container for ubunti 18.04

Quoted 11 lines · Anup Patel

On Mon, Dec 2, 2024 at 4:38?PM Ben Dooks <[email protected]> wrote:

Typo in subject.

s/ubunti/ubuntu/

Quoted 4 lines

Add a podman/docker container for Ubuntu 18.04 for build testing on older systems which might not have the same support as a developer has.

It?s EOL, why bother supporting it at all?

Jess

author
#4 lore raw

RFC 1/5 add build container for ubunti 18.04

Quoted 6 lines · Jessica Clarke
Quoted 5 lines · Anup Patel

On Mon, Dec 2, 2024 at 4:38?PM Ben Dooks <[email protected]> wrote:

Typo in subject.

Quoted 8 lines
Quoted 6 lines

s/ubunti/ubuntu/

Quoted 4 lines

Add a podman/docker container for Ubuntu 18.04 for build testing on older systems which might not have the same support as a developer has.

It?s EOL, why bother supporting it at all?

I was going for something old enough to catch anything unlikely to be supported in things like awk.

Signature · 4 lines
Ben Dooks				http://www.codethink.co.uk/
Senior Engineer				Codethink - Providing Genius

https://www.codethink.co.uk/privacy.html
author
#5 lore raw

RFC 2/5 add ~ files to .gitignore

In reply to Ben Dooks

Ignore temporary files, and avoid accidentally adding them to a commit.

---
 .gitignore | 1 +
 1 file changed, 1 insertion(+)
.gitignore +1 -0
index 424302b..94caeb9 100644
@@ -19,3 +19,4 @@ install/
 # Development friendly files
 tags
 cscope*
+*~
Signature · 1 line
2.37.2.352.g3c44437643
#6 lore raw

RFC 2/5 add ~ files to .gitignore

Quoted 5 lines · On Mon, Dec 2, 2024 at 4:39?PM Ben Dooks <[email protected]> wrote:

Ignore temporary files, and avoid accidentally adding them to a commit.

LGTM.

Thanks,
Anup

Quoted 21 lines
---
 .gitignore | 1 +
 1 file changed, 1 insertion(+)
.gitignore +1 -1
index 424302b..94caeb9 100644
@@ -19,3 +19,4 @@ install/
 # Development friendly files
 tags
 cscope*
+*~
--

2.37.2.352.g3c44437643

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

Applied
#7 lore raw

RFC 2/5 add ~ files to .gitignore

In reply to Ben Dooks
Quoted 5 lines · On Mon, Dec 2, 2024 at 4:39?PM Ben Dooks <[email protected]> wrote:

Ignore temporary files, and avoid accidentally adding them to a commit.

Added "top:" prefix to patch subject at the time of merging.

Applied this patch to the riscv/opensbi repo.

Thanks,
Anup

Quoted 21 lines
---
 .gitignore | 1 +
 1 file changed, 1 insertion(+)
.gitignore +1 -1
index 424302b..94caeb9 100644
@@ -19,3 +19,4 @@ install/
 # Development friendly files
 tags
 cscope*
+*~
--

2.37.2.352.g3c44437643

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

author
#8 lore raw

RFC 3/5 remove carry output if scripts/carray.sh fails

In reply to Ben Dooks

If the script fails, we end up trying to build either an empty or damaged .c file. Just remove it and let gcc fail on non-existant file.

---
 Makefile | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Makefile +1 -1
index dae282c..a798691 100644
@@ -502,7 +502,7 @@ compile_d2c = $(CMD_PREFIX)mkdir -p `dirname $(1)`; \
 compile_carray = $(CMD_PREFIX)mkdir -p `dirname $(1)`; \
 	     echo " CARRAY    $(subst $(build_dir)/,,$(1))"; \
 	     $(eval CARRAY_VAR_LIST := $(carray-$(subst .carray.c,,$(shell basename $(1)))-y)) \
-	     $(src_dir)/scripts/carray.sh -i $(2) -l "$(CARRAY_VAR_LIST)" > $(1)
+	     $(src_dir)/scripts/carray.sh -i $(2) -l "$(CARRAY_VAR_LIST)" > $(1) || rm $(1)
 compile_gen_dep = $(CMD_PREFIX)mkdir -p `dirname $(1)`; \
 	     echo " GEN-DEP   $(subst $(build_dir)/,,$(1))"; \
 	     echo "$(1:.dep=$(2)): $(3)" >> $(1)
Signature · 1 line
2.37.2.352.g3c44437643
#9 lore raw

RFC 3/5 remove carry output if scripts/carray.sh fails

Quoted 6 lines · On Mon, Dec 2, 2024 at 4:39?PM Ben Dooks <[email protected]> wrote:

If the script fails, we end up trying to build either an empty or damaged .c file. Just remove it and let gcc fail on non-existant file.

LGTM.

Regards,
Anup

Quoted 25 lines
---
 Makefile | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Makefile +1 -2
index dae282c..a798691 100644
@@ -502,7 +502,7 @@ compile_d2c = $(CMD_PREFIX)mkdir -p `dirname $(1)`; \
 compile_carray = $(CMD_PREFIX)mkdir -p `dirname $(1)`; \
             echo " CARRAY    $(subst $(build_dir)/,,$(1))"; \
             $(eval CARRAY_VAR_LIST := $(carray-$(subst .carray.c,,$(shell basename $(1)))-y)) \
-            $(src_dir)/scripts/carray.sh -i $(2) -l "$(CARRAY_VAR_LIST)" > $(1)
+            $(src_dir)/scripts/carray.sh -i $(2) -l "$(CARRAY_VAR_LIST)" > $(1) || rm $(1)
 compile_gen_dep = $(CMD_PREFIX)mkdir -p `dirname $(1)`; \
             echo " GEN-DEP   $(subst $(build_dir)/,,$(1))"; \
             echo "$(1:.dep=$(2)): $(3)" >> $(1)
--

2.37.2.352.g3c44437643

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

Applied
#10 lore raw

RFC 3/5 remove carry output if scripts/carray.sh fails

In reply to Ben Dooks
Quoted 6 lines · On Mon, Dec 2, 2024 at 4:39?PM Ben Dooks <[email protected]> wrote:

If the script fails, we end up trying to build either an empty or damaged .c file. Just remove it and let gcc fail on non-existant file.

Added "Makefile:" prefix to patch subject at the time of merging.

Applied this patch to the riscv/opensbi repo.

Thanks,
Anup

Quoted 25 lines
---
 Makefile | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Makefile +1 -2
index dae282c..a798691 100644
@@ -502,7 +502,7 @@ compile_d2c = $(CMD_PREFIX)mkdir -p `dirname $(1)`; \
 compile_carray = $(CMD_PREFIX)mkdir -p `dirname $(1)`; \
             echo " CARRAY    $(subst $(build_dir)/,,$(1))"; \
             $(eval CARRAY_VAR_LIST := $(carray-$(subst .carray.c,,$(shell basename $(1)))-y)) \
-            $(src_dir)/scripts/carray.sh -i $(2) -l "$(CARRAY_VAR_LIST)" > $(1)
+            $(src_dir)/scripts/carray.sh -i $(2) -l "$(CARRAY_VAR_LIST)" > $(1) || rm $(1)
 compile_gen_dep = $(CMD_PREFIX)mkdir -p `dirname $(1)`; \
             echo " GEN-DEP   $(subst $(build_dir)/,,$(1))"; \
             echo "$(1:.dep=$(2)): $(3)" >> $(1)
--

2.37.2.352.g3c44437643

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

author
#11 lore raw

RFC 4/5 script/carry.sh: send error to stderr

In reply to Ben Dooks

Send the errors to stderr, otherwise they end up being written to the file the script was redirecting to in the Makefile

Also, use EOF block to make it easier to redirect

---
 scripts/carray.sh | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)
scripts/carray.sh +8 -6
index fb985c8..a2b05d3 100755
@@ -2,12 +2,14 @@
 
 function usage()
 {
-	echo "Usage:"
-	echo " $0 [options]"
-	echo "Options:"
-	echo "     -h                   Display help or usage"
-	echo "     -i <input_config>    Input config file"
-	echo "     -l <variable_list>   List of variables in the array (Optional)"
+	cat <<EOF >&2
+Usage:  $0 [options]
+
+Options:
+     -h                   Display help or usage
+     -i <input_config>    Input config file
+     -l <variable_list>   List of variables in the array (Optional)
+EOF
 	exit 1;
 }
 
Signature · 1 line
2.37.2.352.g3c44437643
#12 lore raw

RFC 4/5 script/carry.sh: send error to stderr

Quoted 8 lines · On Mon, Dec 2, 2024 at 4:38?PM Ben Dooks <[email protected]> wrote:

Send the errors to stderr, otherwise they end up being written to the file the script was redirecting to in the Makefile

Also, use EOF block to make it easier to redirect

LGTM.

Regards,
Anup

Quoted 37 lines
---
 scripts/carray.sh | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)
scripts/carray.sh +8 -7
index fb985c8..a2b05d3 100755
@@ -2,12 +2,14 @@
 
 function usage()
 {
-       echo "Usage:"
-       echo " $0 [options]"
-       echo "Options:"
-       echo "     -h                   Display help or usage"
-       echo "     -i <input_config>    Input config file"
-       echo "     -l <variable_list>   List of variables in the array (Optional)"
+       cat <<EOF >&2
+Usage:  $0 [options]
+
+Options:
+     -h                   Display help or usage
+     -i <input_config>    Input config file
+     -l <variable_list>   List of variables in the array (Optional)
+EOF
        exit 1;
 }
 
--

2.37.2.352.g3c44437643

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

Applied
#13 lore raw

RFC 4/5 script/carry.sh: send error to stderr

In reply to Ben Dooks
Quoted 8 lines · On Mon, Dec 2, 2024 at 4:38?PM Ben Dooks <[email protected]> wrote:

Send the errors to stderr, otherwise they end up being written to the file the script was redirecting to in the Makefile

Also, use EOF block to make it easier to redirect

Applied this patch to the riscv/opensbi repo.

Thanks,
Anup

Quoted 37 lines
---
 scripts/carray.sh | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)
scripts/carray.sh +8 -7
index fb985c8..a2b05d3 100755
@@ -2,12 +2,14 @@
 
 function usage()
 {
-       echo "Usage:"
-       echo " $0 [options]"
-       echo "Options:"
-       echo "     -h                   Display help or usage"
-       echo "     -i <input_config>    Input config file"
-       echo "     -l <variable_list>   List of variables in the array (Optional)"
+       cat <<EOF >&2
+Usage:  $0 [options]
+
+Options:
+     -h                   Display help or usage
+     -i <input_config>    Input config file
+     -l <variable_list>   List of variables in the array (Optional)
+EOF
        exit 1;
 }
 
--

2.37.2.352.g3c44437643

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

author
#14 lore raw

RFC 5/5 scripts/carray: change to using single awk invocation to generate .c

In reply to Ben Dooks

The shell script makes multiple call-outs to awk to get information from the configuration file. It would be easier to just write the whole thing in one .awk script and have the makefile altered to call that instead.

There should be no functional difference other than the script type and has been tested with PLATFORM=generic build. This should be both quicker and easier to understand.

---
v2:
- updated with the new bits from Samuel Holland's changes
- minor tweaks to the awk script to fix the output spacings
v3:
- fixups for not being rather reliant on gawk (not on all distros)
- send error messages to stderr, otherwise they end up in .c output
- add no-var list a warning
- add debug output on env variable
- make awk script part of .sh script (easier to deal with change)
- fix rebase issues

---
 scripts/carray.sh | 132 ++++++++++++++++++++++++++++++++++------------
 1 file changed, 99 insertions(+), 33 deletions(-)
scripts/carray.sh +99 -33
index a2b05d3..475dfec 100755
@@ -45,42 +45,108 @@ if [ ! -f "${CONFIG_FILE}" ]; then
 	usage
 fi
 
-TYPE_HEADER=$(awk '{ if ($1 == "HEADER:") { printf $2; exit 0; } }' "${CONFIG_FILE}")
-if [ -z "${TYPE_HEADER}" ]; then
-	echo "Must specify HEADER: in input config file"
-	usage
-fi
+# use single awk script to run conversion, but leave in .sh file as awk is
+# bad for getopt, and leaves the original script name alone.
 
-TYPE_NAME=$(awk '{ if ($1 == "TYPE:") { printf $2; for (i=3; i<=NF; i++) printf " %s", $i; exit 0; } }' "${CONFIG_FILE}")
-if [ -z "${TYPE_NAME}" ]; then
-	echo "Must specify TYPE: in input config file"
-	usage
-fi
+awk -v"SCRIPT_NAME=${CONFIG_FILE}" -v "SCRIPT_NAME=`basename $0`" -v"VARLIST=${VAR_LIST}" '
+function array_length@
+{
+    result = 0
 
-ARRAY_NAME=$(awk '{ if ($1 == "NAME:") { printf $2; exit 0; } }' "${CONFIG_FILE}")
-if [ -z "${ARRAY_NAME}" ]; then
-	echo "Must specify NAME: in input config file"
-	usage
-fi
+    for (i in a)
+	result++
 
-MEMBER_NAME=$(awk '{ if ($1 == "MEMBER-NAME:") { printf $2; exit 0; } }' "${CONFIG_FILE}")
-MEMBER_TYPE=$(awk '{ if ($1 == "MEMBER-TYPE:") { printf $2; for (i=3; i<=NF; i++) printf " %s", $i; exit 0; } }' "${CONFIG_FILE}")
-if [ -n "${MEMBER_NAME}" ] && [ -z "${MEMBER_TYPE}" ]; then
-	echo "Must specify MEMBER-TYPE: when using MEMBER-NAME:"
-	usage
-fi
+    return result
+}
 
-printf "// Generated with $(basename $0) from $(basename ${CONFIG_FILE})\n"
-printf "#include <%s>\n\n" "${TYPE_HEADER}"
+function get_fields(start, end)
+{
+    result = $start;
 
-for VAR in ${VAR_LIST}; do
-	printf "extern %s %s;\n" "${TYPE_NAME}" "${VAR}"
-done
-printf "\n"
+    for (nr = start+1; nr <= end; nr++) {
+	result = result " " $nr
+    }
+    return result;
+}
+
+BEGIN {
+      split(VARLIST,VAR_LIST," ")
+}
+
+# process items from the config file
+
+/^HEADER:/	{ TYPE_HEADER = $2 }
+/^TYPE:/	{ TYPE_NAME = get_fields(2, NF); }
+/^NAME:/	{ ARRAY_NAME = $2}
+/^MEMBER-NAME:/	{ MEMBER_NAME = $2 }
+/^MEMBER-TYPE:/ { MEMBER_TYPE = get_fields(2, NF); }
+
+# code to dump the generated .c file once config file is processed
+
+END {
+    # enable for debug
+    if (length(ENVIRON["CARRAY_DEBUG"]) > 0) {
+	print "ARRAY_NAME  " ARRAY_NAME > "/dev/stderr"
+	print "TYPE_HEADER " TYPE_HEADER > "/dev/stderr"
+	print "TYPE_NAME   " TYPE_NAME > "/dev/stderr"
+
+	print "VAR_LIST length is " array_length(VAR_LIST) > "/dev/stderr"
+	for (v in VAR_LIST) {
+	    print "VAR_LIST " v  " = " VAR_LIST[v] > "/dev/stderr"
+	}
+    }
+
+    if (array_length(VAR_LIST) == 0) {
+	print "Warning; no VAR list on command line"  > "/dev/stderr"
+    }
+
+    if (length(ARRAY_NAME) == 0) {
+	print "Must specify NAME: in input config file"  > "/dev/stderr"
+	exit 1
+    }
+
+    if (length(TYPE_NAME) == 0) {
+	print "Must specify TYPE: in input config file"  > "/dev/stderr"
+	exit 1
+    }
+
+    if (length(TYPE_HEADER) == 0) {
+	print "Must specify HEADER: in input config file"  > "/dev/stderr"
+	exit 1
+    }
+
+    if (length(MEMBER_NAME) > 0 && length(MEMBER_TYPE) == 0) {
+	print "Must specify MEMBER-TYPE: when using MEMBER-NAME:" > "/dev/stderr"
+	exit 1
+    }
+
+    printf "// Generated with %s from %s\n", SCRIPT_NAME, CONFIG_FILE
+    printf "// DO NOT EDIT THIS FILE DIRECTLY\n"
+    printf "\n"
+    printf("#include <%s>\n\n", TYPE_HEADER)
+
+    for (v in VAR_LIST) {
+	printf("extern %s %s;\n", TYPE_NAME, VAR_LIST[v])
+    }
+    print ""
+
+    if (length(MEMBER_TYPE) > 0) {
+	TYPE_NAME = MEMBER_TYPE;
+    }
+
+    if (length(MEMBER_NAME) > 0) {
+	VAR_SUFFIX = "." MEMBER_NAME;
+    } else {
+	VAR_SUFFIX = ""
+    }
+
+    printf("%s *const %s[] = {\n", TYPE_NAME, ARRAY_NAME)
+    for (v in VAR_LIST) {
+	printf("\t&%s%s,\n", VAR_LIST[v], VAR_SUFFIX)
+    }
+    printf("\tNULL\n");
+
+    printf("};\n");
+}
+' < ${CONFIG_FILE} || usage
 
-printf "%s *const %s[] = {\n" "${MEMBER_TYPE:-${TYPE_NAME}}" "${ARRAY_NAME}"
-for VAR in ${VAR_LIST}; do
-	printf "\t&%s,\n" "${VAR}${MEMBER_NAME:+.}${MEMBER_NAME}"
-done
-	printf "\tNULL\n"
-printf "};\n"
Signature · 1 line
2.37.2.352.g3c44437643
#15 lore raw

RFC 5/5 scripts/carray: change to using single awk invocation to generate .c

Quoted 38 lines · On Mon, Dec 2, 2024 at 4:39?PM Ben Dooks <[email protected]> wrote:

The shell script makes multiple call-outs to awk to get information from the configuration file. It would be easier to just write the whole thing in one .awk script and have the makefile altered to call that instead.

There should be no functional difference other than the script type and has been tested with PLATFORM=generic build. This should be both quicker and easier to understand.

---
v2:
- updated with the new bits from Samuel Holland's changes
- minor tweaks to the awk script to fix the output spacings
v3:
- fixups for not being rather reliant on gawk (not on all distros)
- send error messages to stderr, otherwise they end up in .c output
- add no-var list a warning
- add debug output on env variable
- make awk script part of .sh script (easier to deal with change)
- fix rebase issues

---
 scripts/carray.sh | 132 ++++++++++++++++++++++++++++++++++------------
 1 file changed, 99 insertions(+), 33 deletions(-)
scripts/carray.sh +0 -4
index a2b05d3..475dfec 100755
@@ -45,42 +45,108 @@ if [ ! -f "${CONFIG_FILE}" ]; then
        usage
 fi
 
-TYPE_HEADER=$(awk '{ if ($1 == "HEADER:") { printf $2; exit 0; } }' "${CONFIG_FILE}")
-if [ -z "${TYPE_HEADER}" ]; then
-       echo "Must specify HEADER: in input config file"
-       usage

scripts/carray.sh if [ ! -f "${CONFIG_FILE}" ]; then · line 51

Quoted 10 lines
scripts/carray.sh +3 -6
-fi
+# use single awk script to run conversion, but leave in .sh file as awk is
+# bad for getopt, and leaves the original script name alone.
 
-TYPE_NAME=$(awk '{ if ($1 == "TYPE:") { printf $2; for (i=3; i<=NF; i++) printf " %s", $i; exit 0; } }' "${CONFIG_FILE}")
-if [ -z "${TYPE_NAME}" ]; then
-       echo "Must specify TYPE: in input config file"
-       usage
-fi
+awk -v"SCRIPT_NAME=${CONFIG_FILE}" -v "SCRIPT_NAME=`basename $0`" -v"VARLIST=${VAR_LIST}" '

The first "SCRIPT_NAME=" should be "CONFIG_FILE=".

Quoted 128 lines
scripts/carray.sh +96 -24
+function array_length(a)
+{
+    result = 0
 
-ARRAY_NAME=$(awk '{ if ($1 == "NAME:") { printf $2; exit 0; } }' "${CONFIG_FILE}")
-if [ -z "${ARRAY_NAME}" ]; then
-       echo "Must specify NAME: in input config file"
-       usage
-fi
+    for (i in a)
+       result++
 
-MEMBER_NAME=$(awk '{ if ($1 == "MEMBER-NAME:") { printf $2; exit 0; } }' "${CONFIG_FILE}")
-MEMBER_TYPE=$(awk '{ if ($1 == "MEMBER-TYPE:") { printf $2; for (i=3; i<=NF; i++) printf " %s", $i; exit 0; } }' "${CONFIG_FILE}")
-if [ -n "${MEMBER_NAME}" ] && [ -z "${MEMBER_TYPE}" ]; then
-       echo "Must specify MEMBER-TYPE: when using MEMBER-NAME:"
-       usage
-fi
+    return result
+}
 
-printf "// Generated with $(basename $0) from $(basename ${CONFIG_FILE})\n"
-printf "#include <%s>\n\n" "${TYPE_HEADER}"
+function get_fields(start, end)
+{
+    result = $start;
 
-for VAR in ${VAR_LIST}; do
-       printf "extern %s %s;\n" "${TYPE_NAME}" "${VAR}"
-done
-printf "\n"
+    for (nr = start+1; nr <= end; nr++) {
+       result = result " " $nr
+    }
+    return result;
+}
+
+BEGIN {
+      split(VARLIST,VAR_LIST," ")
+}
+
+# process items from the config file
+
+/^HEADER:/     { TYPE_HEADER = $2 }
+/^TYPE:/       { TYPE_NAME = get_fields(2, NF); }
+/^NAME:/       { ARRAY_NAME = $2}
+/^MEMBER-NAME:/        { MEMBER_NAME = $2 }
+/^MEMBER-TYPE:/ { MEMBER_TYPE = get_fields(2, NF); }
+
+# code to dump the generated .c file once config file is processed
+
+END {
+    # enable for debug
+    if (length(ENVIRON["CARRAY_DEBUG"]) > 0) {
+       print "ARRAY_NAME  " ARRAY_NAME > "/dev/stderr"
+       print "TYPE_HEADER " TYPE_HEADER > "/dev/stderr"
+       print "TYPE_NAME   " TYPE_NAME > "/dev/stderr"
+
+       print "VAR_LIST length is " array_length(VAR_LIST) > "/dev/stderr"
+       for (v in VAR_LIST) {
+           print "VAR_LIST " v  " = " VAR_LIST[v] > "/dev/stderr"
+       }
+    }
+
+    if (array_length(VAR_LIST) == 0) {
+       print "Warning; no VAR list on command line"  > "/dev/stderr"
+    }
+
+    if (length(ARRAY_NAME) == 0) {
+       print "Must specify NAME: in input config file"  > "/dev/stderr"
+       exit 1
+    }
+
+    if (length(TYPE_NAME) == 0) {
+       print "Must specify TYPE: in input config file"  > "/dev/stderr"
+       exit 1
+    }
+
+    if (length(TYPE_HEADER) == 0) {
+       print "Must specify HEADER: in input config file"  > "/dev/stderr"
+       exit 1
+    }
+
+    if (length(MEMBER_NAME) > 0 && length(MEMBER_TYPE) == 0) {
+       print "Must specify MEMBER-TYPE: when using MEMBER-NAME:" > "/dev/stderr"
+       exit 1
+    }
+
+    printf "// Generated with %s from %s\n", SCRIPT_NAME, CONFIG_FILE
+    printf "// DO NOT EDIT THIS FILE DIRECTLY\n"
+    printf "\n"
+    printf("#include <%s>\n\n", TYPE_HEADER)
+
+    for (v in VAR_LIST) {
+       printf("extern %s %s;\n", TYPE_NAME, VAR_LIST[v])
+    }
+    print ""
+
+    if (length(MEMBER_TYPE) > 0) {
+       TYPE_NAME = MEMBER_TYPE;
+    }
+
+    if (length(MEMBER_NAME) > 0) {
+       VAR_SUFFIX = "." MEMBER_NAME;
+    } else {
+       VAR_SUFFIX = ""
+    }
+
+    printf("%s *const %s[] = {\n", TYPE_NAME, ARRAY_NAME)
+    for (v in VAR_LIST) {
+       printf("\t&%s%s,\n", VAR_LIST[v], VAR_SUFFIX)
+    }
+    printf("\tNULL\n");
+
+    printf("};\n");
+}
+' < ${CONFIG_FILE} || usage
 
-printf "%s *const %s[] = {\n" "${MEMBER_TYPE:-${TYPE_NAME}}" "${ARRAY_NAME}"
-for VAR in ${VAR_LIST}; do
-       printf "\t&%s,\n" "${VAR}${MEMBER_NAME:+.}${MEMBER_NAME}"
-done
-       printf "\tNULL\n"
-printf "};\n"
--

2.37.2.352.g3c44437643

Quoted 4 lines

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

Otherwise, it looks good to me.

Regards,
Anup

Applied
#16 lore raw

RFC 5/5 scripts/carray: change to using single awk invocation to generate .c

In reply to Ben Dooks
Quoted 11 lines · On Mon, Dec 2, 2024 at 4:39?PM Ben Dooks <[email protected]> wrote:

The shell script makes multiple call-outs to awk to get information from the configuration file. It would be easier to just write the whole thing in one .awk script and have the makefile altered to call that instead.

There should be no functional difference other than the script type and has been tested with PLATFORM=generic build. This should be both quicker and easier to understand.

I have taken care of my comment on this patch. I also tested it at my end.

Applied this patch to the riscv/opensbi repo.

Thanks,
Anup

Quoted 169 lines

---
v2:
- updated with the new bits from Samuel Holland's changes
- minor tweaks to the awk script to fix the output spacings
v3:
- fixups for not being rather reliant on gawk (not on all distros)
- send error messages to stderr, otherwise they end up in .c output
- add no-var list a warning
- add debug output on env variable
- make awk script part of .sh script (easier to deal with change)
- fix rebase issues

---
 scripts/carray.sh | 132 ++++++++++++++++++++++++++++++++++------------
 1 file changed, 99 insertions(+), 33 deletions(-)
scripts/carray.sh +99 -34
index a2b05d3..475dfec 100755
@@ -45,42 +45,108 @@ if [ ! -f "${CONFIG_FILE}" ]; then
        usage
 fi
 
-TYPE_HEADER=$(awk '{ if ($1 == "HEADER:") { printf $2; exit 0; } }' "${CONFIG_FILE}")
-if [ -z "${TYPE_HEADER}" ]; then
-       echo "Must specify HEADER: in input config file"
-       usage
-fi
+# use single awk script to run conversion, but leave in .sh file as awk is
+# bad for getopt, and leaves the original script name alone.
 
-TYPE_NAME=$(awk '{ if ($1 == "TYPE:") { printf $2; for (i=3; i<=NF; i++) printf " %s", $i; exit 0; } }' "${CONFIG_FILE}")
-if [ -z "${TYPE_NAME}" ]; then
-       echo "Must specify TYPE: in input config file"
-       usage
-fi
+awk -v"SCRIPT_NAME=${CONFIG_FILE}" -v "SCRIPT_NAME=`basename $0`" -v"VARLIST=${VAR_LIST}" '
+function array_length(a)
+{
+    result = 0
 
-ARRAY_NAME=$(awk '{ if ($1 == "NAME:") { printf $2; exit 0; } }' "${CONFIG_FILE}")
-if [ -z "${ARRAY_NAME}" ]; then
-       echo "Must specify NAME: in input config file"
-       usage
-fi
+    for (i in a)
+       result++
 
-MEMBER_NAME=$(awk '{ if ($1 == "MEMBER-NAME:") { printf $2; exit 0; } }' "${CONFIG_FILE}")
-MEMBER_TYPE=$(awk '{ if ($1 == "MEMBER-TYPE:") { printf $2; for (i=3; i<=NF; i++) printf " %s", $i; exit 0; } }' "${CONFIG_FILE}")
-if [ -n "${MEMBER_NAME}" ] && [ -z "${MEMBER_TYPE}" ]; then
-       echo "Must specify MEMBER-TYPE: when using MEMBER-NAME:"
-       usage
-fi
+    return result
+}
 
-printf "// Generated with $(basename $0) from $(basename ${CONFIG_FILE})\n"
-printf "#include <%s>\n\n" "${TYPE_HEADER}"
+function get_fields(start, end)
+{
+    result = $start;
 
-for VAR in ${VAR_LIST}; do
-       printf "extern %s %s;\n" "${TYPE_NAME}" "${VAR}"
-done
-printf "\n"
+    for (nr = start+1; nr <= end; nr++) {
+       result = result " " $nr
+    }
+    return result;
+}
+
+BEGIN {
+      split(VARLIST,VAR_LIST," ")
+}
+
+# process items from the config file
+
+/^HEADER:/     { TYPE_HEADER = $2 }
+/^TYPE:/       { TYPE_NAME = get_fields(2, NF); }
+/^NAME:/       { ARRAY_NAME = $2}
+/^MEMBER-NAME:/        { MEMBER_NAME = $2 }
+/^MEMBER-TYPE:/ { MEMBER_TYPE = get_fields(2, NF); }
+
+# code to dump the generated .c file once config file is processed
+
+END {
+    # enable for debug
+    if (length(ENVIRON["CARRAY_DEBUG"]) > 0) {
+       print "ARRAY_NAME  " ARRAY_NAME > "/dev/stderr"
+       print "TYPE_HEADER " TYPE_HEADER > "/dev/stderr"
+       print "TYPE_NAME   " TYPE_NAME > "/dev/stderr"
+
+       print "VAR_LIST length is " array_length(VAR_LIST) > "/dev/stderr"
+       for (v in VAR_LIST) {
+           print "VAR_LIST " v  " = " VAR_LIST[v] > "/dev/stderr"
+       }
+    }
+
+    if (array_length(VAR_LIST) == 0) {
+       print "Warning; no VAR list on command line"  > "/dev/stderr"
+    }
+
+    if (length(ARRAY_NAME) == 0) {
+       print "Must specify NAME: in input config file"  > "/dev/stderr"
+       exit 1
+    }
+
+    if (length(TYPE_NAME) == 0) {
+       print "Must specify TYPE: in input config file"  > "/dev/stderr"
+       exit 1
+    }
+
+    if (length(TYPE_HEADER) == 0) {
+       print "Must specify HEADER: in input config file"  > "/dev/stderr"
+       exit 1
+    }
+
+    if (length(MEMBER_NAME) > 0 && length(MEMBER_TYPE) == 0) {
+       print "Must specify MEMBER-TYPE: when using MEMBER-NAME:" > "/dev/stderr"
+       exit 1
+    }
+
+    printf "// Generated with %s from %s\n", SCRIPT_NAME, CONFIG_FILE
+    printf "// DO NOT EDIT THIS FILE DIRECTLY\n"
+    printf "\n"
+    printf("#include <%s>\n\n", TYPE_HEADER)
+
+    for (v in VAR_LIST) {
+       printf("extern %s %s;\n", TYPE_NAME, VAR_LIST[v])
+    }
+    print ""
+
+    if (length(MEMBER_TYPE) > 0) {
+       TYPE_NAME = MEMBER_TYPE;
+    }
+
+    if (length(MEMBER_NAME) > 0) {
+       VAR_SUFFIX = "." MEMBER_NAME;
+    } else {
+       VAR_SUFFIX = ""
+    }
+
+    printf("%s *const %s[] = {\n", TYPE_NAME, ARRAY_NAME)
+    for (v in VAR_LIST) {
+       printf("\t&%s%s,\n", VAR_LIST[v], VAR_SUFFIX)
+    }
+    printf("\tNULL\n");
+
+    printf("};\n");
+}
+' < ${CONFIG_FILE} || usage
 
-printf "%s *const %s[] = {\n" "${MEMBER_TYPE:-${TYPE_NAME}}" "${ARRAY_NAME}"
-for VAR in ${VAR_LIST}; do
-       printf "\t&%s,\n" "${VAR}${MEMBER_NAME:+.}${MEMBER_NAME}"
-done
-       printf "\tNULL\n"
-printf "};\n"
--

2.37.2.352.g3c44437643

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

author
#17 lore raw

RFC 5/5 scripts/carray: change to using single awk invocation to generate .c

Quoted 11 lines · Anup Patel
Quoted 10 lines · On Mon, Dec 2, 2024 at 4:39?PM Ben Dooks <[email protected]> wrote:

The shell script makes multiple call-outs to awk to get information from the configuration file. It would be easier to just write the whole thing in one .awk script and have the makefile altered to call that instead.

There should be no functional difference other than the script type and has been tested with PLATFORM=generic build. This should be both quicker and easier to understand.

Quoted 7 lines
Quoted 1 line

I have taken care of my comment on this patch. I also tested it at my end.

Applied this patch to the riscv/opensbi repo.

Great, thanks for sorting.

Signature · 4 lines
Ben Dooks				http://www.codethink.co.uk/
Senior Engineer				Codethink - Providing Genius

https://www.codethink.co.uk/privacy.html