Skip to content

Commit

Permalink
Zephyr builder: Add escaping for the 'vars' parameter
Browse files Browse the repository at this point in the history
Fix the issue with passing the "vars" parameters from the configuration
moulin YAML file to the build.ninja context.

The 'vars' parameter of the Zephyr builder is placed into the bash -c
"... cmd ..." context. It requires an additional level of escaping to
be added to the 'vars' parameter.

But that is not enough. The 'vars' parameter is passed to the 'west
build' call. West, in turn, passes those 'vars' parameter to CMake.

On the CMake level, Zephyr splits all incoming CMake parameters into 2
parts:

1. Those, which start with the "CONFIG_" prefix
2. All the others

The first group of parameters is stored in the KConfig file. When users
pass a string to Kconfig through CMake, they are expected to pass it so
that quotes are correct seen from Kconfig, that is:

> cmake -DCONFIG_A_STRING=\"foo bar\"

In this case, to obtain the required level of escaping:

- the first group of parameters should be additionally escaped twice on
the moulin level
- the second group of parameters should be additionally escaped once on
the moulin level

As a result of this patch, the user can specify the 'vars' on the yaml
level in the following way:

        vars:
          - "CONFIG_KERNEL_BIN_NAME=\"%{KERNEL_BIN_NAME}\""
          - "MY_VARIABLE=\"1234567890\""

Signed-off-by: Mykhailo Androsiuk <[email protected]>
  • Loading branch information
Mykhailo Androsiuk committed Nov 15, 2023
1 parent b7bcd63 commit caf6220
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion moulin/builders/zephyr.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def gen_build(self):

vars_node = self.conf.get("vars", None)
if vars_node:
vars_vals = [x.as_str for x in vars_node]
vars_vals = [ZephyrBuilder.__escape_vars_vals(x.as_str) for x in vars_node]
vars_value = " ".join([f"-D{var}" for var in vars_vals])
else:
vars_value = ""
Expand Down Expand Up @@ -102,6 +102,27 @@ def gen_build(self):

return targets

@staticmethod
def __escape_vars_vals(var: str) -> str:
"""
Escape the given variable value.
Parameters:
- var (str): The variable value to be escaped.
Returns:
- str: The escaped variable value.
If the variable value starts with "CONFIG_", double quotation mark escaping is added,
following the:
https://github.com/zephyrproject-rtos/zephyr/pull/49267/commits/d77597783a4c148b70c389c8996112f8b6d1e5ed
"""
if var.startswith("CONFIG_"):
vars_vals = utils.escape(utils.escape(var))
else:
vars_vals = utils.escape(var)
return vars_vals

def get_targets(self):
"Return list of targets that are generated by this build"
return [os.path.join(self.build_dir, t.as_str) for t in self.conf["target_images"]]
Expand Down

0 comments on commit caf6220

Please sign in to comment.