gn_helper: find correct directory for //

gn finds // by searching .gn in parent directories.

To support out dir is not 2 directories from root (i.e. out/Default),
search .gn in parent directory for '//' location.

tools/licenses/licenses.py runs 'gn gen' in out/Default/$tmp.
for such case, '//' should be '../../..', or fail to import '//path'

Change-Id: I6e5ccbe93cb5e51704f31d4eb558c03560286865
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/6051636
Reviewed-by: Philipp Wollermann <philwo@google.com>
Commit-Queue: Fumitoshi Ukai <ukai@google.com>
Reviewed-by: Takuto Ikuta <tikuta@chromium.org>
changes/36/6051636/2
Fumitoshi Ukai committed by LUCI CQ
parent c0796a593a
commit fd5161870a

@ -7,6 +7,18 @@ import os
import re
def _find_root(output_dir):
curdir = output_dir
while True:
if os.path.exists(os.path.join(curdir, ".gn")):
return curdir
nextdir = os.path.join(curdir, "..")
if os.path.abspath(curdir) == os.path.abspath(nextdir):
raise Exception(
'Could not find checkout in any parent of the current path.')
curdir = nextdir
def _gn_lines(output_dir, path):
"""
Generator function that returns args.gn lines one at a time, following
@ -20,7 +32,7 @@ def _gn_lines(output_dir, path):
raw_import_path = match.groups()[0]
if raw_import_path[:2] == "//":
import_path = os.path.normpath(
os.path.join(output_dir, "..", "..",
os.path.join(_find_root(output_dir),
raw_import_path[2:]))
else:
import_path = os.path.normpath(

@ -36,6 +36,7 @@ class GnHelperTest(trial_dir.TestCase):
super().tearDown()
def test_lines(self):
write('.gn', '')
out_dir = os.path.join('out', 'dir')
# Make sure nested import directives work. This is based on the
# reclient test.
@ -52,6 +53,7 @@ class GnHelperTest(trial_dir.TestCase):
])
def test_args(self):
write('.gn', '')
out_dir = os.path.join('out', 'dir')
# Make sure nested import directives work. This is based on the
# reclient test.
@ -68,6 +70,7 @@ class GnHelperTest(trial_dir.TestCase):
])
def test_args_spaces(self):
write('.gn', '')
out_dir = os.path.join('out', 'dir')
# Make sure nested import directives work. This is based on the
# reclient test.

Loading…
Cancel
Save