summaryrefslogtreecommitdiff
path: root/scripts/env2string.awk
diff options
context:
space:
mode:
authorTom Rini <[email protected]>2021-11-16 20:55:12 -0500
committerTom Rini <[email protected]>2021-11-16 20:55:12 -0500
commitf299171c1dd8fb77b56b317adf80f7c60627d64f (patch)
tree9e86d3aeaf5e46cd0e23bbb8bfc0e6e5406617e0 /scripts/env2string.awk
parent9272805139a104c83dff8230e03e9626dd9bc195 (diff)
parent78398652723b6fe743751ffb19d8256b7e3e0a4e (diff)
Merge branch '2021-11-16-env-rework' into next
To quote Simon: One barrier to completing the 7-year-long Kconfig migration is that the default environment is implemented using ad-hoc CONFIG options. At present U-Boot environment variables, and thus scripts, are defined by CONFIG_EXTRA_ENV_SETTINGS. It is not really feasible to move the environment to Kconfig as it is hundreds of lines of text in some cases. Even considering the current situation, it is painful to add large amounts of text to the config-header file and dealing with quoting and newlines is harder than it should be. It would be better if we could just type the script into a text file and have it included by U-Boot. This is already supported by the CONFIG_USE_DEFAULT_ENV_FILE feature. But that does not support use of CONFIG options or comments, so is best suited for use by other build systems wanting to define the U-Boot environment. Add a feature that brings in a .env file associated with the board config, if present. To use it, create a file board/<vendor>/<board>.env or use CONFIG_ENV_SOURCE_FILE to set a filename. The environment variables should be of the form "var=value". Values can extend to multiple lines. This series converts the existing environment documentation to rST and updates it to explain how to use this.
Diffstat (limited to 'scripts/env2string.awk')
-rw-r--r--scripts/env2string.awk80
1 files changed, 80 insertions, 0 deletions
diff --git a/scripts/env2string.awk b/scripts/env2string.awk
new file mode 100644
index 00000000000..57d0fc8f3ba
--- /dev/null
+++ b/scripts/env2string.awk
@@ -0,0 +1,80 @@
+# SPDX-License-Identifier: GPL-2.0+
+#
+# Copyright 2021 Google, Inc
+#
+# SPDX-License-Identifier: GPL-2.0+
+#
+# Awk script to parse a text file containing an environment and convert it
+# to a C string which can be compiled into U-Boot.
+
+# The resulting output is:
+#
+# #define CONFIG_EXTRA_ENV_TEXT "<environment here>"
+#
+# If the input is empty, this script outputs a comment instead.
+
+BEGIN {
+ # env holds the env variable we are currently processing
+ env = "";
+ ORS = ""
+}
+
+# Skip empty lines, as these are generated by the clang preprocessor
+NF {
+ # Quote quotes
+ gsub("\"", "\\\"")
+
+ # Is this the start of a new environment variable?
+ if (match($0, "^([^ \t=][^ =]*)=(.*)$", arr)) {
+ if (length(env) != 0) {
+ # Record the value of the variable now completed
+ vars[var] = env
+ }
+ var = arr[1]
+ env = arr[2]
+
+ # Deal with += which concatenates the new string to the existing
+ # variable
+ if (length(env) != 0 && match(var, "^(.*)[+]$", var_arr))
+ {
+ # Allow var\+=val to indicate that the variable name is
+ # var+ and this is not actually a concatenation
+ if (substr(var_arr[1], length(var_arr[1])) == "\\") {
+ # Drop the backslash
+ sub(/\\[+]$/, "+", var)
+ } else {
+ var = var_arr[1]
+ env = vars[var] env
+ }
+ }
+ } else {
+ # Change newline to space
+ gsub(/^[ \t]+/, "")
+
+ # Don't keep leading spaces generated by the previous blank line
+ if (length(env) == 0) {
+ env = $0
+ } else {
+ env = env " " $0
+ }
+ }
+}
+
+END {
+ # Record the value of the variable now completed. If the variable is
+ # empty it is not set.
+ if (length(env) != 0) {
+ vars[var] = env
+ }
+
+ if (length(vars) != 0) {
+ printf("%s", "#define CONFIG_EXTRA_ENV_TEXT \"")
+
+ # Print out all the variables
+ for (var in vars) {
+ env = vars[var]
+ print var "=" vars[var] "\\0"
+ }
+ print "\"\n"
+ }
+}