0

gn_helpers: support absolute path import

There is a case that args.gn imports from absolute path.

Bug: 393209409
Change-Id: I90def7633bb8a937f27fea96fc5d4629153e4f94
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6210737
Commit-Queue: Richard Wang <richardwa@google.com>
Auto-Submit: Fumitoshi Ukai <ukai@google.com>
Reviewed-by: Richard Wang <richardwa@google.com>
Cr-Commit-Position: refs/heads/main@{#1413282}
This commit is contained in:
Fumitoshi Ukai
2025-01-29 19:04:01 -08:00
committed by Chromium LUCI CQ
parent 206fb413ba
commit af31db8a23
2 changed files with 38 additions and 2 deletions

@ -32,7 +32,7 @@ _CHROMIUM_ROOT = os.path.abspath(
ARGS_GN_FILENAME = 'args.gn'
BUILD_VARS_FILENAME = 'build_vars.json'
IMPORT_RE = re.compile(r'^import\("//(\S+)"\)')
IMPORT_RE = re.compile(r'^import\("(\S+)"\)')
class GNError(Exception):
@ -287,7 +287,20 @@ class GNValueParser(object):
regex_match = IMPORT_RE.match(line)
if not regex_match:
raise GNError('Not a valid import string: %s' % line)
import_path = os.path.join(self.checkout_root, regex_match.group(1))
import_path = regex_match.group(1)
if import_path.startswith("//"):
import_path = os.path.join(self.checkout_root, import_path[2:])
elif sys.platform.startswith('win32'):
if import_path.startswith("/"):
# gn users '/C:/path/to/foo.gn', not 'C:/path/to/foo.gn' on windows
import_path = import_path[1:]
else:
raise GNError('Need /-prefix for an absolute path: %s' % import_path)
if not os.path.isabs(import_path):
raise GNError('Unable to use relative path in import path: %s' %
import_path)
with open(import_path) as f:
imported_args = f.read()
self.input = self.input.replace(line, imported_args)

@ -315,6 +315,29 @@ class UnitTest(unittest.TestCase):
textwrap.dedent('import("some/relative/args/file.gni")'))
parser.ReplaceImports()
if sys.platform.startswith('win32'):
parser = gn_helpers.GNValueParser(
textwrap.dedent("""
some_arg1 = "val1"
import("/c:/some/args/file.gni")
some_arg2 = "val2"
"""))
with mock.patch(open_fun, mock.mock_open(read_data=fake_import)):
parser.ReplaceImports()
self.assertEqual(
parser.input,
textwrap.dedent("""
some_arg1 = "val1"
some_imported_arg = "imported_val"
some_arg2 = "val2"
"""))
# A path that's not source absolute should raise an exception.
with self.assertRaises(gn_helpers.GNError):
parser = gn_helpers.GNValueParser(
textwrap.dedent('import("c:/some/args/file.gni")'))
parser.ReplaceImports()
def test_CreateBuildCommand(self):
with tempfile.TemporaryDirectory() as temp_dir:
suffix = '.bat' if sys.platform.startswith('win32') else ''