Reland "Add a Str() function to gclient for use in DEPS files."
This relands c7eed83 with a fix to the way variables are
propagated from parent dependencies into child dependencies.
The original CL description from c7eed83 was:
> gclient's existing functionality for handling variables is
> ambiguous: the value of a variable can either be a string literal
> or an expression fragment. The implementation is required to
> parse a value as an expression, and, if it is legal, treat it
> as an expression instead of a literal. This means that
>
> gclient_gn_args_file = 'src/build/args.gni'
> gclient_gn_args = ['xcode_version']
> vars = {
> 'xcode_version': 'xcode-12'
> }
>
> would cause a problem because gclient would try to parse the
> variable as an expression, and 'xcode' would not be defined.
>
> This patch adds a workaround for this, where you can instead
> use the Str() function to explicitly tell gclient to treat the
> value as a string and not a potential expression.
>
> The above example would be changed to:
>
> gclient_gn_args_file = 'src/build/args.gni'
> gclient_gn_args = ['xcode_version']
> vars = {
> 'xcode_version': Str('xcode-12')
> }
>
> The variable may still be used in every context where it was legal
> to be used before.
>
This reverts commit 84431987dd384c79c84515004d19db67345a1c00.
Bug: 1099242
TBR=ehmaldonado@chromium.org
Change-Id: I047b871df47c367c1f34a3985e5813504e3c5c6f
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/2274152
Commit-Queue: Dirk Pranke <dpranke@google.com>
Reviewed-by: Ben Pastene <bpastene@chromium.org>
diff --git a/gclient.py b/gclient.py
index 5acbfae..193af1b 100755
--- a/gclient.py
+++ b/gclient.py
@@ -1043,7 +1043,9 @@
variables = self.get_vars()
for arg in self._gn_args:
value = variables[arg]
- if isinstance(value, basestring):
+ if isinstance(value, gclient_eval.ConstantString):
+ value = value.value
+ elif isinstance(value, basestring):
value = gclient_eval.EvaluateCondition(value, variables)
lines.append('%s = %s' % (arg, ToGNString(value)))
with open(os.path.join(self.root.root_dir, self._gn_args_file), 'wb') as f:
@@ -1265,11 +1267,11 @@
result = {}
result.update(self._vars)
if self.parent:
- parent_vars = self.parent.get_vars()
- result.update(parent_vars)
+ merge_vars(result, self.parent.get_vars())
# Provide some built-in variables.
result.update(self.get_builtin_vars())
- result.update(self.custom_vars or {})
+ merge_vars(result, self.custom_vars)
+
return result
@@ -1283,6 +1285,20 @@
}
+def merge_vars(result, new_vars):
+ for k, v in new_vars.items():
+ if k in result:
+ if isinstance(result[k], gclient_eval.ConstantString):
+ if isinstance(v, gclient_eval.ConstantString):
+ result[k] = v
+ else:
+ result[k].value = v
+ else:
+ result[k] = v
+ else:
+ result[k] = v
+
+
def _detect_host_os():
return _PLATFORM_MAPPING[sys.platform]