Improve GN templates use of "visibility" and "testonly"
Many spots using forward_variables_from(invoker, "*") were not
allowing these two variables to be set in outer scopes.
* Introduce a helper variable "EXPLICIT_FORWARDS" for use with the
recommended pattern of:
forward_variables_from(invoker, "*", [ "testonly", "visibility" ])
forward_variables_from(invoker, [ "testonly", "visibility" ])
* Ensures this pattern is used in android templates, test.gni,
and BUILDCONFIG.gn
* Documents this pattern in writing_gn_templates.md
* Adds a PRESUBMIT.py for it
* Fixes visibility of a few blink targets now that test()
respects it.
Bug: 862232
Change-Id: Ib71dbf34be76131fc749c721aea856e1146bc69a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2454427
Commit-Queue: Andrew Grieve <[email protected]>
Reviewed-by: Dirk Pranke <[email protected]>
Cr-Commit-Position: refs/heads/master@{#830678}
diff --git a/PRESUBMIT.py b/PRESUBMIT.py
index 218a5ca..1e3fa54 100644
--- a/PRESUBMIT.py
+++ b/PRESUBMIT.py
@@ -3995,6 +3995,35 @@
return []
+def CheckGnGlobForward(input_api, output_api):
+ """Checks that forward_variables_from(invoker, "*") follows best practices.
+
+ As documented at //build/docs/writing_gn_templates.md
+ """
+ def gn_files(f):
+ return input_api.FilterSourceFile(f, files_to_check=(r'.+\.gni', ))
+
+ problems = []
+ for f in input_api.AffectedSourceFiles(gn_files):
+ for line_num, line in f.ChangedContents():
+ if 'forward_variables_from(invoker, "*")' in line:
+ problems.append(
+ 'Bare forward_variables_from(invoker, "*") in %s:%d' % (
+ f.LocalPath(), line_num))
+
+ if problems:
+ return [output_api.PresubmitPromptWarning(
+ 'forward_variables_from("*") without exclusions',
+ items=sorted(problems),
+ long_text=('The variables "visibilty" and "test_only" should be '
+ 'explicitly listed in forward_variables_from(). For more '
+ 'details, see:\n'
+ 'https://chromium.googlesource.com/chromium/src/+/HEAD/'
+ 'build/docs/writing_gn_templates.md'
+ '#Using-forward_variables_from'))]
+ return []
+
+
def CheckNewHeaderWithoutGnChangeOnUpload(input_api, output_api):
"""Checks that newly added header files have corresponding GN changes.
Note that this is only a heuristic. To be precise, run script: