Commit 2801162f authored by Brian Sheedy's avatar Brian Sheedy Committed by Chromium LUCI CQ

Support GN arg imports in mb.py

Adds support to mb.py for cases where an args.gn file uses import(...)
to reference GN args in a .gni file, such as how board-specific CrOS
args are specified.

Change-Id: If11ad05cbc9605e89cc743e72f9178de016bdde1
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2587552
Commit-Queue: Brian Sheedy <bsheedy@chromium.org>
Commit-Queue: Ben Pastene <bpastene@chromium.org>
Reviewed-by: default avatarBen Pastene <bpastene@chromium.org>
Auto-Submit: Brian Sheedy <bsheedy@chromium.org>
Cr-Commit-Position: refs/heads/master@{#836312}
parent 33218d5f
...@@ -853,15 +853,29 @@ class MetaBuildWrapper(object): ...@@ -853,15 +853,29 @@ class MetaBuildWrapper(object):
gn_args_path = self.PathJoin(self.ToAbsPath(build_dir), 'args.gn') gn_args_path = self.PathJoin(self.ToAbsPath(build_dir), 'args.gn')
if self.Exists(gn_args_path): if self.Exists(gn_args_path):
args_contents = self.ReadFile(gn_args_path) args_contents = self.ReadFile(gn_args_path)
gn_args = []
for l in args_contents.splitlines():
l = l.split('#', 2)[0].strip()
if not l:
continue
(name, value) = l.split('=', 1)
gn_args.append('%s=%s' % (name.strip(), value.strip()))
return ' '.join(gn_args) # Handle any .gni file imports, e.g. the ones used by CrOS. This should
# be automatically handled by gn_helpers.FromGNArgs (via its call to
# gn_helpers.GNValueParser.ReplaceImports), but that currently breaks
# mb_unittest since it mocks out file reads itself instead of using
# pyfakefs. This results in gn_helpers trying to read a non-existent file.
# The implementation of ReplaceImports here can be removed once the
# unittests use pyfakefs.
def ReplaceImports(input_contents):
output_contents = ''
for l in input_contents.splitlines(True):
if not l.strip().startswith('#') and 'import(' in l:
import_file = l.split('"', 2)[1]
import_file = self.ToAbsPath(import_file)
imported_contents = self.ReadFile(import_file)
output_contents += ReplaceImports(imported_contents) + '\n'
else:
output_contents += l
return output_contents
args_contents = ReplaceImports(args_contents)
args_dict = gn_helpers.FromGNArgs(args_contents)
return ' '.join(['%s=%s' % (k, v) for (k, v) in args_dict.items()])
def Lookup(self): def Lookup(self):
self.ReadConfigFile(self.args.config_file) self.ReadConfigFile(self.args.config_file)
......
...@@ -637,13 +637,20 @@ class UnitTest(unittest.TestCase): ...@@ -637,13 +637,20 @@ class UnitTest(unittest.TestCase):
'base_unittests'], files=files, ret=0) 'base_unittests'], files=files, ret=0)
# test running isolate on an existing build_dir # test running isolate on an existing build_dir
files['/fake_src/out/Default/args.gn'] = 'is_debug = True\n' files['/fake_src/out/Default/args.gn'] = 'is_debug = true\n'
self.check(['isolate', '//out/Default', 'base_unittests'], self.check(['isolate', '//out/Default', 'base_unittests'],
files=files, ret=0) files=files, ret=0)
self.check(['isolate', '//out/Default', 'base_unittests'], self.check(['isolate', '//out/Default', 'base_unittests'],
files=files, ret=0) files=files, ret=0)
# Existing build dir that uses a .gni import.
files['/fake_src/out/Default/args.gn'] = 'import("//import/args.gni")\n'
files['/fake_src/import/args.gni'] = 'is_debug = true\n'
self.check(['isolate', '//out/Default', 'base_unittests'],
files=files,
ret=0)
def test_isolate_dir(self): def test_isolate_dir(self):
files = { files = {
'/fake_src/out/Default/toolchain.ninja': '/fake_src/out/Default/toolchain.ninja':
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment