diff --git a/autoninja.py b/autoninja.py index a1db7875d2..3248aaed10 100755 --- a/autoninja.py +++ b/autoninja.py @@ -85,9 +85,9 @@ def _is_google_corp_machine(): return shutil.which("gcert") is not None -def _has_internal_checkout(): +def _has_internal_checkout(output_dir): """Check if internal is checked out.""" - root_dir = gclient_paths.GetPrimarySolutionPath() + root_dir = gclient_paths.GetPrimarySolutionPath(output_dir) if not root_dir: return False return os.path.exists(os.path.join(root_dir, "internal", ".git")) @@ -111,12 +111,12 @@ def _reclient_rbe_project(): return "" -def _siso_rbe_project(): +def _siso_rbe_project(output_dir): """Returns RBE project used by siso.""" siso_project = os.environ.get('SISO_PROJECT') if siso_project: return siso_project - root_dir = gclient_paths.GetPrimarySolutionPath() + root_dir = gclient_paths.GetPrimarySolutionPath(output_dir) if not root_dir: return "" sisoenv_path = os.path.join(root_dir, 'build/config/siso/.sisoenv') @@ -151,8 +151,8 @@ def _print_cmd(cmd): print(*[shell_quoter(arg) for arg in cmd], file=sys.stderr) -def _get_remoteexec_defaults(): - root_dir = gclient_paths.GetPrimarySolutionPath() +def _get_remoteexec_defaults(output_dir): + root_dir = gclient_paths.GetPrimarySolutionPath(output_dir) if not root_dir: return None default_file = os.path.join(root_dir, @@ -179,7 +179,7 @@ def _get_remoteexec_defaults(): # this logic should match with //build/toolchain/siso.gni def _get_use_siso_default(output_dir): """Returns use_siso default value.""" - root_dir = gclient_paths.GetPrimarySolutionPath() + root_dir = gclient_paths.GetPrimarySolutionPath(output_dir) if not root_dir: return False @@ -364,7 +364,7 @@ def _main_inner(input_args, build_id): use_siso = False if use_reclient is None and use_remoteexec: - if values := _get_remoteexec_defaults(): + if values := _get_remoteexec_defaults(output_dir): if use_siso: use_reclient = values["use_reclient_on_siso"] else: @@ -381,9 +381,9 @@ def _main_inner(input_args, build_id): elif use_siso and project is None: # siso runs locally if empty project is given # even if use_remoteexec=true is set. - project = _siso_rbe_project() + project = _siso_rbe_project(output_dir) - if _has_internal_checkout(): + if _has_internal_checkout(output_dir): # user may login on non-@google.com account on corp, # but need to use @google.com and rbe-chrome-untrusted # with src-internal. diff --git a/gclient_paths.py b/gclient_paths.py index 9925526546..7d05599533 100644 --- a/gclient_paths.py +++ b/gclient_paths.py @@ -95,9 +95,12 @@ def _GetPrimarySolutionPathInternal(cwd): return None -def GetPrimarySolutionPath(): +def GetPrimarySolutionPath(from_dir=None): """Returns the full path to the primary solution. (gclient_root + src)""" - return _GetPrimarySolutionPathInternal(os.getcwd()) + if not from_dir: + from_dir = os.getcwd() + from_dir = os.path.abspath(from_dir) + return _GetPrimarySolutionPathInternal(from_dir) @functools.lru_cache diff --git a/ninja.py b/ninja.py index 28f49acaa4..b706bbf5cc 100755 --- a/ninja.py +++ b/ninja.py @@ -32,7 +32,7 @@ def find_ninja_in_path(): return ninja_path -def check_out_dir(ninja_args): +def parse_args(ninja_args): out_dir = "." tool = "" for i, arg in enumerate(ninja_args): @@ -44,6 +44,10 @@ def check_out_dir(ninja_args): out_dir = ninja_args[i + 1] elif arg.startswith("-C"): out_dir = arg[2:] + return tool, out_dir + + +def check_out_dir(tool, out_dir): if tool in ["list", "commands", "inputs", "targets"]: # These tools are just inspect ninja rules and not modify out dir. # TODO: b/339320220 - implement these in siso @@ -105,9 +109,11 @@ def main(args): os.environ.pop("LIBRARY_PATH", None) os.environ.pop("SDKROOT", None) + tool, out_dir = parse_args(args[1:]) + # Get gclient root + src. - primary_solution_path = gclient_paths.GetPrimarySolutionPath() - gclient_root_path = gclient_paths.FindGclientRoot(os.getcwd()) + primary_solution_path = gclient_paths.GetPrimarySolutionPath(out_dir) + gclient_root_path = gclient_paths.FindGclientRoot(out_dir) gclient_src_root_path = None if gclient_root_path: gclient_src_root_path = os.path.join(gclient_root_path, "src") @@ -123,7 +129,7 @@ def main(args): "ninja" + gclient_paths.GetExeSuffix(), ) if os.path.isfile(ninja_path): - check_out_dir(args[1:]) + check_out_dir(tool, out_dir) return caffeinate.run([ninja_path] + args[1:]) return fallback(args[1:]) diff --git a/siso.py b/siso.py index cdfa426c6d..dd0833c5f0 100644 --- a/siso.py +++ b/siso.py @@ -16,7 +16,7 @@ import caffeinate import gclient_paths -def checkOutdir(args): +def parse_args(args): subcmd = '' out_dir = "." for i, arg in enumerate(args): @@ -27,8 +27,10 @@ def checkOutdir(args): out_dir = args[i + 1] elif arg.startswith("-C"): out_dir = arg[2:] - if subcmd != "ninja": - return + return subcmd, out_dir + + +def check_outdir(subcmd, out_dir): ninja_marker = os.path.join(out_dir, ".ninja_deps") if os.path.exists(ninja_marker): print("depot_tools/siso.py: %s contains Ninja state file.\n" @@ -89,9 +91,11 @@ def main(args): environ = os.environ.copy() + subcmd, out_dir = parse_args(args[1:]) + # Get gclient root + src. - primary_solution_path = gclient_paths.GetPrimarySolutionPath() - gclient_root_path = gclient_paths.FindGclientRoot(os.getcwd()) + primary_solution_path = gclient_paths.GetPrimarySolutionPath(out_dir) + gclient_root_path = gclient_paths.FindGclientRoot(out_dir) gclient_src_root_path = None if gclient_root_path: gclient_src_root_path = os.path.join(gclient_root_path, 'src') @@ -150,7 +154,7 @@ def main(args): ] for siso_path in siso_paths: if siso_path and os.path.isfile(siso_path): - checkOutdir(args[1:]) + check_outdir(subcmd, out_dir) return caffeinate.run([siso_path] + args[1:], env=env) print( 'depot_tools/siso.py: Could not find siso in third_party/siso ' diff --git a/tests/autoninja_test.py b/tests/autoninja_test.py index 1c27517858..f74174cc1d 100755 --- a/tests/autoninja_test.py +++ b/tests/autoninja_test.py @@ -75,18 +75,17 @@ class AutoninjaTest(trial_dir.TestCase): def test_autoninja_reclient(self): """ - Test that when specifying use_remoteexec=true, autoninja delegates to - reclient_helper. + Test that when specifying use_remoteexec=true use_reclient=true, + autoninja delegates to reclient_helper. """ with mock.patch('reclient_helper.run_ninja', return_value=0) as run_ninja: out_dir = os.path.join('out', 'dir') - write(os.path.join(out_dir, 'args.gn'), 'use_remoteexec=true') - write(os.path.join('build', 'toolchain', 'use_reclient_value.py'), - """ -def use_reclient_value(output_dir): - return True -""") + write( + os.path.join(out_dir, 'args.gn'), """ + use_remoteexec=true + use_reclient=true + """) write(os.path.join('buildtools', 'reclient_cfgs', 'reproxy.cfg'), 'RBE_v=2') write(os.path.join('buildtools', 'reclient', 'version.txt'), '0.0') @@ -136,14 +135,12 @@ def use_reclient_value(output_dir): with mock.patch('reclient_helper.build_context', reclient_helper_mock): with mock.patch('siso.main', return_value=0) as siso_main: out_dir = os.path.join('out', 'dir') - write(os.path.join(out_dir, 'args.gn'), - 'use_siso=true\nuse_remoteexec=true') write( - os.path.join('build', 'toolchain', 'use_reclient_value.py'), - """ -def use_reclient_value(output_dir): - return True -""") + os.path.join(out_dir, 'args.gn'), """ + use_remoteexec=true + use_siso=true + use_reclient=true + """) write( os.path.join('buildtools', 'reclient_cfgs', 'reproxy.cfg'), 'instance=projects/rbe-chromium-untrusted-test/'