From: Anup Patel <apatel@ventanamicro.com>
To: opensbi@lists.infradead.org
Subject: [PATCH 02/11] Makefile: Add support for generating C array at compile time
Date: Tue, 3 May 2022 09:07:50 +0530 [thread overview]
Message-ID: <20220503033759.544156-3-apatel@ventanamicro.com> (raw)
In-Reply-To: <20220503033759.544156-1-apatel@ventanamicro.com>
Generating C array at compile time based on details provided by
objects.mk is a very useful feature which will help us compile
only a subset of drivers or modules.
We add a bash script (carray.sh) which takes array details and
object/variable list from command-line to generate a C source
containing array of object/variable pointers. We also extend
top-level makefile to use carray.sh whenever specified through
objects.mk.
Signed-off-by: Anup Patel <apatel@ventanamicro.com>
---
Makefile | 11 +++++++
scripts/carray.sh | 77 +++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 88 insertions(+)
create mode 100755 scripts/carray.sh
diff --git a/Makefile b/Makefile
index 30f7c46..f619ef7 100644
--- a/Makefile
+++ b/Makefile
@@ -404,6 +404,10 @@ compile_d2c = $(CMD_PREFIX)mkdir -p `dirname $(1)`; \
$(if $($(2)-varprefix-$(3)),$(eval D2C_NAME_PREFIX := $($(2)-varprefix-$(3))),$(eval D2C_NAME_PREFIX := $(5))) \
$(if $($(2)-padding-$(3)),$(eval D2C_PADDING_BYTES := $($(2)-padding-$(3))),$(eval D2C_PADDING_BYTES := 0)) \
$(src_dir)/scripts/d2c.sh -i $(6) -a $(D2C_ALIGN_BYTES) -p $(D2C_NAME_PREFIX) -t $(D2C_PADDING_BYTES) > $(1)
+compile_carray = $(CMD_PREFIX)mkdir -p `dirname $(1)`; \
+ echo " CARRAY $(subst $(build_dir)/,,$(1))"; \
+ $(eval CARRAY_VAR_LIST := $(carray-$(subst .c,,$(shell basename $(1)))-y)) \
+ $(src_dir)/scripts/carray.sh -i $(2) -l "$(CARRAY_VAR_LIST)" > $(1)
compile_gen_dep = $(CMD_PREFIX)mkdir -p `dirname $(1)`; \
echo " GEN-DEP $(subst $(build_dir)/,,$(1))"; \
echo "$(1:.dep=$(2)): $(3)" >> $(1)
@@ -451,6 +455,13 @@ $(build_dir)/%.dep: $(src_dir)/%.S
$(build_dir)/%.o: $(src_dir)/%.S
$(call compile_as,$@,$<)
+$(build_dir)/%.dep: $(src_dir)/%.carray
+ $(call compile_gen_dep,$@,.c,$<)
+ $(call compile_gen_dep,$@,.o,$(@:.dep=.c))
+
+$(build_dir)/%.c: $(src_dir)/%.carray
+ $(call compile_carray,$@,$<)
+
$(platform_build_dir)/%.bin: $(platform_build_dir)/%.elf
$(call compile_objcopy,$@,$<)
diff --git a/scripts/carray.sh b/scripts/carray.sh
new file mode 100755
index 0000000..0c52bd6
--- /dev/null
+++ b/scripts/carray.sh
@@ -0,0 +1,77 @@
+#!/bin/bash
+
+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)"
+ exit 1;
+}
+
+# Command line options
+CONFIG_FILE=""
+VAR_LIST=""
+
+while getopts "hi:l:" o; do
+ case "${o}" in
+ h)
+ usage
+ ;;
+ i)
+ CONFIG_FILE=${OPTARG}
+ ;;
+ l)
+ VAR_LIST=${OPTARG}
+ ;;
+ *)
+ usage
+ ;;
+ esac
+done
+shift $((OPTIND-1))
+
+if [ -z "${CONFIG_FILE}" ]; then
+ echo "Must specify input config file"
+ usage
+fi
+
+if [ ! -f "${CONFIG_FILE}" ]; then
+ echo "The input config file should be a present"
+ usage
+fi
+
+TYPE_HEADER=`cat ${CONFIG_FILE} | awk '{ if ($1 == "HEADER:") { printf $2; exit 0; } }'`
+if [ -z "${TYPE_HEADER}" ]; then
+ echo "Must specify HEADER: in input config file"
+ usage
+fi
+
+TYPE_NAME=`cat ${CONFIG_FILE} | awk '{ if ($1 == "TYPE:") { printf $2; for (i=3; i<=NF; i++) printf " %s", $i; exit 0; } }'`
+if [ -z "${TYPE_NAME}" ]; then
+ echo "Must specify TYPE: in input config file"
+ usage
+fi
+
+ARRAY_NAME=`cat ${CONFIG_FILE} | awk '{ if ($1 == "NAME:") { printf $2; exit 0; } }'`
+if [ -z "${ARRAY_NAME}" ]; then
+ echo "Must specify NAME: in input config file"
+ usage
+fi
+
+printf "#include <%s>\n\n" "${TYPE_HEADER}"
+
+for VAR in ${VAR_LIST}; do
+ printf "extern %s %s;\n" "${TYPE_NAME}" "${VAR}"
+done
+printf "\n"
+
+printf "%s *%s[] = {\n" "${TYPE_NAME}" "${ARRAY_NAME}"
+for VAR in ${VAR_LIST}; do
+ printf "\t&%s,\n" "${VAR}"
+done
+printf "};\n\n"
+
+printf "unsigned long %s_size = sizeof(%s) / sizeof(%s *);\n" "${ARRAY_NAME}" "${ARRAY_NAME}" "${TYPE_NAME}"
--
2.34.1
next prev parent reply other threads:[~2022-05-03 3:37 UTC|newest]
Thread overview: 29+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-05-03 3:37 [PATCH 00/11] OpenSBI compile-time C arrays Anup Patel
2022-05-03 3:37 ` [PATCH 01/11] Makefile: Allow generated C source to be anywhere in build directory Anup Patel
2022-05-09 17:24 ` Atish Patra
2022-05-03 3:37 ` Anup Patel [this message]
2022-05-09 17:26 ` [PATCH 02/11] Makefile: Add support for generating C array at compile time Atish Patra
2022-05-03 3:37 ` [PATCH 03/11] lib: utils/reset: Generate FDT reset driver list at compile-time Anup Patel
2022-05-03 3:37 ` [PATCH 04/11] lib: utils/serial: Generate FDT serial " Anup Patel
2022-05-03 3:37 ` [PATCH 05/11] lib: utils/timer: Generate FDT timer " Anup Patel
2022-05-03 3:37 ` [PATCH 06/11] lib: utils/irqchip: Generate FDT irqchip " Anup Patel
2022-05-03 3:37 ` [PATCH 07/11] lib: utils/ipi: Generate FDT ipi " Anup Patel
2022-05-03 3:37 ` [PATCH 08/11] lib: utils/i2c: Generate FDT i2c adapter " Anup Patel
2022-05-03 3:37 ` [PATCH 09/11] lib: utils/gpio: Generate FDT gpio " Anup Patel
2022-05-03 3:37 ` [PATCH 10/11] platform: generic: Generate platform override module " Anup Patel
2022-05-03 3:37 ` [PATCH 11/11] platform: generic: Move Sifive platform overrides into own directory Anup Patel
2022-05-09 17:28 ` Atish Patra
2022-05-03 3:42 ` [PATCH 00/11] OpenSBI compile-time C arrays Jessica Clarke
2022-05-03 3:52 ` Anup Patel
2022-05-03 3:54 ` Jessica Clarke
2022-05-03 4:04 ` Anup Patel
2022-05-03 4:13 ` Jessica Clarke
2022-05-03 4:17 ` Anup Patel
2022-05-03 4:23 ` Jessica Clarke
2022-05-03 4:30 ` Anup Patel
2022-05-03 4:45 ` Jessica Clarke
2022-05-03 4:47 ` Anup Patel
2022-05-03 15:35 ` Xiang W
2022-05-03 16:24 ` Anup Patel
2022-05-09 17:29 ` Atish Patra
2022-05-13 4:03 ` Anup Patel
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20220503033759.544156-3-apatel@ventanamicro.com \
--to=apatel@ventanamicro.com \
--cc=opensbi@lists.infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox