From fd5161870a65fbdd2d5750606cfbd410d32653af Mon Sep 17 00:00:00 2001 From: Fumitoshi Ukai Date: Wed, 27 Nov 2024 04:20:40 +0000 Subject: [PATCH] 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 Commit-Queue: Fumitoshi Ukai Reviewed-by: Takuto Ikuta --- gn_helper.py | 14 +++++++++++++- tests/gn_helper_test.py | 3 +++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/gn_helper.py b/gn_helper.py index 29e8e49b0..36aa2dbae 100644 --- a/gn_helper.py +++ b/gn_helper.py @@ -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( diff --git a/tests/gn_helper_test.py b/tests/gn_helper_test.py index f85cce424..eb9a5e61b 100644 --- a/tests/gn_helper_test.py +++ b/tests/gn_helper_test.py @@ -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.