Skip to content

Commit

Permalink
builders: Added escape of 'env' parameter
Browse files Browse the repository at this point in the history
There is an issue with passing the "EXT_MODULES" parameter from the
configuration moulin yaml file to the build.sh context.

The 'android_kernel' builder didn't escape double quotes, while
wrapping the 'env' parameter in additional double quotes like this:

bash -c "...env..."

As a result, an incorrect syntax was generated in the build.ninja file.

To generate a functional build.ninja file, it was necessary to take
this aspect into account in the moulin.yaml file and add additional
escaping there.

This approach is ambiguous since it requires the user to know the
implementation details of each moulin builder.

To address this issue, a simple escape function was added to all
builders that support the 'env' parameter. It escapes double quotes to
ensure proper handling of the 'env' variable.

The issue has been resolved for the following builders:
- android_kernel builder
- android builder
- zephyr builder

Signed-off-by: Mykhailo Androsiuk <[email protected]>
  • Loading branch information
Mykhailo Androsiuk committed Oct 25, 2023
1 parent b65571e commit d8fe51f
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 0 deletions.
4 changes: 4 additions & 0 deletions moulin/builders/android.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from typing import List
from moulin.yaml_wrapper import YamlValue
from moulin import ninja_syntax
from moulin import utils


def get_builder(conf: YamlValue, name: str, build_dir: str, src_stamps: List[str],
Expand Down Expand Up @@ -57,6 +58,9 @@ def gen_build(self):
else:
env_values = []
env = " ".join(env_values)

env = utils.escape(env)

variables = {
"build_dir": self.build_dir,
"env": env,
Expand Down
4 changes: 4 additions & 0 deletions moulin/builders/android_kernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from typing import List
from moulin.yaml_wrapper import YamlValue
from moulin import ninja_syntax
from moulin import utils


def get_builder(conf: YamlValue, name: str, build_dir: str, src_stamps: List[str],
Expand Down Expand Up @@ -55,6 +56,9 @@ def gen_build(self):
else:
env_values = []
env = " ".join(env_values)

env = utils.escape(env)

variables = {
"build_dir": self.build_dir,
"env": env,
Expand Down
3 changes: 3 additions & 0 deletions moulin/builders/zephyr.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from moulin.yaml_wrapper import YamlValue
from moulin import ninja_syntax
from moulin.utils import construct_fetcher_dep_cmd
from moulin import utils


def get_builder(conf: YamlValue, name: str, build_dir: str, src_stamps: List[str],
Expand Down Expand Up @@ -62,6 +63,8 @@ def gen_build(self):
env_values = []
env = " ".join(env_values)

env = utils.escape(env)

shields_node = self.conf.get("shields", None)
if shields_node:
shields_vals = [x.as_str for x in shields_node]
Expand Down
21 changes: 21 additions & 0 deletions moulin/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import os.path
import sys
import re


def create_stamp_name(*args):
Expand All @@ -18,3 +19,23 @@ def construct_fetcher_dep_cmd() -> str:
this_script = os.path.abspath(sys.argv[0])
args = " ".join(sys.argv[1:])
return f"{this_script} {args} --fetcherdep $name"


def escape(val: str) -> str:
"""
Escape special characters in the input string.
This function takes an input string `val` and escapes special characters by adding escape
sequences to them. The following transformations are applied:
- Double quotes (") are escaped as \\".
- Dollar signs ($) are escaped as $$.
Args:
val (str): The input string to be escaped.
Returns:
str: The escaped string.
"""
result = val
result = result.replace(r"\"", r"\\\"")
result = result.replace("$", "$$")
result = re.sub('(([^\\\\])"|^()")', '\\2\\"', result)
return result

0 comments on commit d8fe51f

Please sign in to comment.