pw_presubmit: Add more auto-formatting of gn args
Change-Id: I83df673d0c6d09bc8a6625906f6664ac3c423071
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/22440
Commit-Queue: Joe Ethier <jethier@google.com>
Reviewed-by: Wyatt Hepler <hepler@google.com>
diff --git a/pw_presubmit/py/pw_presubmit/build.py b/pw_presubmit/py/pw_presubmit/build.py
index 72d2aab..ffb86ab 100644
--- a/pw_presubmit/py/pw_presubmit/build.py
+++ b/pw_presubmit/py/pw_presubmit/build.py
@@ -40,8 +40,26 @@
def gn_args(**kwargs) -> str:
- """Builds a string to use for the --args argument to gn gen."""
- return '--args=' + ' '.join(f'{arg}={val}' for arg, val in kwargs.items())
+ """Builds a string to use for the --args argument to gn gen.
+
+ Currently supports bool, int, and str values. In the case of str values,
+ quotation marks will be added automatically, unless the string already
+ contains one or more double quotation marks, or starts with a { or [
+ character, in which case it will be passed through as-is.
+ """
+ transformed_args = []
+ for arg, val in kwargs.items():
+ if isinstance(val, bool):
+ transformed_args.append(f'{arg}={str(val).lower()}')
+ continue
+ if (isinstance(val, str) and '"' not in val and not val.startswith("{")
+ and not val.startswith("[")):
+ transformed_args.append(f'{arg}="{val}"')
+ continue
+ # Fall-back case handles integers as well as strings that already
+ # contain double quotation marks, or look like scopes or lists.
+ transformed_args.append(f'{arg}={val}')
+ return '--args=' + ' '.join(transformed_args)
def gn_gen(gn_source_dir: Path,
diff --git a/pw_presubmit/py/pw_presubmit/pigweed_presubmit.py b/pw_presubmit/py/pw_presubmit/pigweed_presubmit.py
index 6638cad..2cfc8c3 100755
--- a/pw_presubmit/py/pw_presubmit/pigweed_presubmit.py
+++ b/pw_presubmit/py/pw_presubmit/pigweed_presubmit.py
@@ -122,7 +122,7 @@
def gn_host_tools(ctx: PresubmitContext):
- build.gn_gen(ctx.root, ctx.output_dir, pw_build_HOST_TOOLS='true')
+ build.gn_gen(ctx.root, ctx.output_dir, pw_build_HOST_TOOLS=True)
build.ninja(ctx.output_dir)
@@ -130,8 +130,8 @@
def oss_fuzz_build(ctx: PresubmitContext):
build.gn_gen(ctx.root,
ctx.output_dir,
- pw_toolchain_OSS_FUZZ_ENABLED='true',
- pw_toolchain_SANITIZER='"address"')
+ pw_toolchain_OSS_FUZZ_ENABLED=True,
+ pw_toolchain_SANITIZER="address")
build.ninja(ctx.output_dir, "host_clang")