summaryrefslogtreecommitdiff
path: root/scripts
diff options
context:
space:
mode:
authorAnup Patel <apatel@ventanamicro.com>2022-05-13 06:56:00 +0300
committerAnup Patel <anup@brainfault.org>2022-05-13 06:56:00 +0300
commit7fb474b9dde5d56e0d4d6a0c1627337694cdc2f2 (patch)
treef9f70da61cd5242633ec8dd1587bade2f088abd1 /scripts
parentf726f2dc0186964feb17296471233086bcc3d1b2 (diff)
downloadopensbi-7fb474b9dde5d56e0d4d6a0c1627337694cdc2f2.tar.xz
Makefile: Add support for generating C array at compile time
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> Reviewed-by: Atish Patra <atishp@rivosinc.com>
Diffstat (limited to 'scripts')
-rwxr-xr-xscripts/carray.sh77
1 files changed, 77 insertions, 0 deletions
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}"