Don't pass diagnostic switches to the regeneration command The regeneration command copied every switch of the first gn gen except a few, so --time, --tracelog and -v applied to every rerun by Ninja: each one printed timings or verbose logs, or wrote a trace. They're meant for the run they were passed to, so skip them. Which switches regeneration keeps wasn't documented, so describe it in "gn help gen". Add base::test::ScopedCommandLine from Chromium for the test, and use it in setup_unittest.cc instead of a similar class. Change-Id: If56a4e02a5095c6f039d4a7f7741822eaeb2ad35 Reviewed-on: https://gn-review.googlesource.com/c/gn/+/28640 Reviewed-by: Sylvain Defresne <sdefresne@chromium.org> Reviewed-by: Takuto Ikuta <tikuta@google.com> Commit-Queue: Sylvain Defresne <sdefresne@chromium.org>
diff --git a/build/gen.py b/build/gen.py index 237ae22..b023875 100755 --- a/build/gen.py +++ b/build/gen.py
@@ -908,6 +908,7 @@ 'gn_unittests': { 'sources': [ 'src/base/sha2_unittest.cc', + 'src/base/test/scoped_command_line.cc', 'src/gn/action_target_generator_unittest.cc', 'src/gn/analyzer_unittest.cc', 'src/gn/args_unittest.cc',
diff --git a/docs/reference.md b/docs/reference.md index dd43a0f..d81b026 100644 --- a/docs/reference.md +++ b/docs/reference.md
@@ -901,6 +901,9 @@ documentation on that mode. See "gn help switches" for the common command-line switches. + + When the build files change, Ninja reruns "gn gen" with the same switches, + except --args and the diagnostic switches --time, --tracelog and -v. ``` #### **General options**
diff --git a/src/base/test/scoped_command_line.cc b/src/base/test/scoped_command_line.cc new file mode 100644 index 0000000..3408b71 --- /dev/null +++ b/src/base/test/scoped_command_line.cc
@@ -0,0 +1,20 @@ +// Copyright 2016 The Chromium Authors +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "base/test/scoped_command_line.h" + +namespace base::test { + +ScopedCommandLine::ScopedCommandLine() + : original_command_line_(*base::CommandLine::ForCurrentProcess()) {} + +ScopedCommandLine::~ScopedCommandLine() { + *base::CommandLine::ForCurrentProcess() = original_command_line_; +} + +CommandLine* ScopedCommandLine::GetProcessCommandLine() { + return base::CommandLine::ForCurrentProcess(); +} + +} // namespace base::test
diff --git a/src/base/test/scoped_command_line.h b/src/base/test/scoped_command_line.h new file mode 100644 index 0000000..3e685d2 --- /dev/null +++ b/src/base/test/scoped_command_line.h
@@ -0,0 +1,29 @@ +// Copyright 2016 The Chromium Authors +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef BASE_TEST_SCOPED_COMMAND_LINE_H_ +#define BASE_TEST_SCOPED_COMMAND_LINE_H_ + +#include "base/command_line.h" + +namespace base::test { + +// Helper class to restore the original command line at the end of the scope. +class ScopedCommandLine final { + public: + ScopedCommandLine(); + ~ScopedCommandLine(); + + // Gets the command line for the current process. + // NOTE: Do not name this GetCommandLine as this will conflict with Windows's + // GetCommandLine and get renamed to GetCommandLineW. + CommandLine* GetProcessCommandLine(); + + private: + const CommandLine original_command_line_; +}; + +} // namespace base::test + +#endif // BASE_TEST_SCOPED_COMMAND_LINE_H_
diff --git a/src/gn/command_gen.cc b/src/gn/command_gen.cc index 15937af..33462ed 100644 --- a/src/gn/command_gen.cc +++ b/src/gn/command_gen.cc
@@ -641,6 +641,9 @@ See "gn help switches" for the common command-line switches. + When the build files change, Ninja reruns "gn gen" with the same switches, + except --args and the diagnostic switches --time, --tracelog and -v. + General options --ninja-executable=<string>
diff --git a/src/gn/ninja_build_writer.cc b/src/gn/ninja_build_writer.cc index 375cfc8..27b4cd1 100644 --- a/src/gn/ninja_build_writer.cc +++ b/src/gn/ninja_build_writer.cc
@@ -6,8 +6,11 @@ #include <stddef.h> +#include <algorithm> +#include <array> #include <map> #include <set> +#include <string_view> #include "base/command_line.h" #include "base/files/file_util.h" @@ -48,6 +51,26 @@ const Target* last_seen; }; +// Returns whether GetSelfInvocationCommandLine() shouldn't copy a switch of the +// current process. +bool SkipSwitchForSelfInvocation(std::string_view name) { + static constexpr std::array kSkippedSwitches = { + // GetSelfInvocationCommandLine() writes these itself. + switches::kQuiet, + switches::kRoot, + switches::kDotfile, + // --args are written to args.gn, which future runs read. Keeping --args + // would mean changes to the file would be ignored. + switches::kArgs, + // Diagnostic switches, which are meant for the run they were passed to, + // not for every regeneration. + switches::kTime, + switches::kTracelog, + switches::kVerbose, + }; + return std::ranges::find(kSkippedSwitches, name) != kSkippedSwitches.end(); +} + } // namespace base::CommandLine GetSelfInvocationCommandLine( @@ -102,12 +125,7 @@ const base::CommandLine::SwitchMap& switches = our_cmdline.GetSwitches(); for (base::CommandLine::SwitchMap::const_iterator i = switches.begin(); i != switches.end(); ++i) { - // Only write arguments we haven't already written. Always skip "args" - // since those will have been written to the file and will be used - // implicitly in the future. Keeping --args would mean changes to the file - // would be ignored. - if (i->first != switches::kQuiet && i->first != switches::kRoot && - i->first != switches::kDotfile && i->first != switches::kArgs) { + if (!SkipSwitchForSelfInvocation(i->first)) { std::string escaped_value = EscapeString(FilePathToUTF8(i->second), escape_shell, nullptr); cmdline.AppendSwitch(i->first, escaped_value);
diff --git a/src/gn/ninja_build_writer_unittest.cc b/src/gn/ninja_build_writer_unittest.cc index c7f1885..671832d 100644 --- a/src/gn/ninja_build_writer_unittest.cc +++ b/src/gn/ninja_build_writer_unittest.cc
@@ -9,6 +9,7 @@ #include "base/command_line.h" #include "base/files/file_util.h" +#include "base/test/scoped_command_line.h" #include "gn/filesystem_utils.h" #include "gn/pool.h" #include "gn/scheduler.h" @@ -101,6 +102,28 @@ EXPECT_EQ("./dot.gn", cmd_out.GetSwitchValueString(switches::kDotfile)); } +TEST_F(NinjaBuildWriterTest, GetSelfInvocationCommandLineSwitches) { + TestWithScope setup; + setup.build_settings()->SetRootPath( + base::MakeAbsoluteFilePath(base::FilePath(FILE_PATH_LITERAL(".")))); + + // The regeneration command copies the switches of the current process, + // except diagnostic ones. + base::test::ScopedCommandLine cmdline; + cmdline.GetProcessCommandLine()->AppendSwitch(switches::kThreads, "2"); + cmdline.GetProcessCommandLine()->AppendSwitch(switches::kTime); + cmdline.GetProcessCommandLine()->AppendSwitch(switches::kTracelog, + "trace.json"); + cmdline.GetProcessCommandLine()->AppendSwitch(switches::kVerbose); + base::CommandLine cmd_out = + GetSelfInvocationCommandLine(setup.build_settings()); + + EXPECT_EQ("2", cmd_out.GetSwitchValueString(switches::kThreads)); + EXPECT_FALSE(cmd_out.HasSwitch(switches::kTime)); + EXPECT_FALSE(cmd_out.HasSwitch(switches::kTracelog)); + EXPECT_FALSE(cmd_out.HasSwitch(switches::kVerbose)); +} + TEST_F(NinjaBuildWriterTest, TwoTargets) { TestWithScope setup; Err err;
diff --git a/src/gn/setup_unittest.cc b/src/gn/setup_unittest.cc index a125ef9..d3140bd 100644 --- a/src/gn/setup_unittest.cc +++ b/src/gn/setup_unittest.cc
@@ -10,6 +10,7 @@ #include "base/files/file_path.h" #include "base/files/file_util.h" #include "base/files/scoped_temp_dir.h" +#include "base/test/scoped_command_line.h" #include "gn/builder_record.h" #include "gn/commands.h" #include "gn/filesystem_utils.h" @@ -670,25 +671,6 @@ EXPECT_FALSE(err.has_error()); } -// Convenience class to save the current process base::CommandLine -// on construction, modify it through the get() method, then restore -// the saved version on scope exit. This is useful for tests that need -// to modify the process global singleton temporarily. -class ScopedCommandLineForTest { - public: - ScopedCommandLineForTest() - : command_line_ref_(*base::CommandLine::ForCurrentProcess()), - saved_command_line_(command_line_ref_) {} - - ~ScopedCommandLineForTest() { command_line_ref_ = saved_command_line_; } - - base::CommandLine& get() { return command_line_ref_; } - - private: - base::CommandLine& command_line_ref_; - base::CommandLine saved_command_line_; -}; - // A test that verifies that generated_file() generation doesn't crash. // See the commands in builder_record.h regarding metadata walks // requiring all items in the graph to be fully resolved to run safely. @@ -724,7 +706,7 @@ // i.e. that metadata collection was done properly, even if it was delayed. TEST(GenCommandTest, ValidationMetadataRace) { MsgLoop msg_loop; - ScopedCommandLineForTest global_cmdline; + base::test::ScopedCommandLine global_cmdline; base::ScopedTempDir in_temp_dir; base::FilePath in_path = @@ -783,10 +765,12 @@ base::ScopedTempDir build_temp_dir; ASSERT_TRUE(build_temp_dir.CreateUniqueTempDir()); - global_cmdline.get().AppendSwitchPath(switches::kRoot, in_path); - global_cmdline.get().AppendSwitch(switches::kFailOnUnusedArgs); - global_cmdline.get().AppendSwitch(switches::kQuiet); - global_cmdline.get().AppendSwitch(switches::kThreads, "4"); + global_cmdline.GetProcessCommandLine()->AppendSwitchPath(switches::kRoot, + in_path); + global_cmdline.GetProcessCommandLine()->AppendSwitch( + switches::kFailOnUnusedArgs); + global_cmdline.GetProcessCommandLine()->AppendSwitch(switches::kQuiet); + global_cmdline.GetProcessCommandLine()->AppendSwitch(switches::kThreads, "4"); std::vector<std::string> args; args.push_back(FilePathToUTF8(build_temp_dir.GetPath())); @@ -819,7 +803,7 @@ // proper checks. TEST(GenCommandTest, ValidationMetadataRaceMissingTarget) { MsgLoop msg_loop; - ScopedCommandLineForTest global_cmdline; + base::test::ScopedCommandLine global_cmdline; base::ScopedTempDir in_temp_dir; base::FilePath in_path = @@ -886,10 +870,12 @@ base::ScopedTempDir build_temp_dir; ASSERT_TRUE(build_temp_dir.CreateUniqueTempDir()); - global_cmdline.get().AppendSwitchPath(switches::kRoot, in_path); - global_cmdline.get().AppendSwitch(switches::kFailOnUnusedArgs); - global_cmdline.get().AppendSwitch(switches::kQuiet); - global_cmdline.get().AppendSwitch(switches::kThreads, "4"); + global_cmdline.GetProcessCommandLine()->AppendSwitchPath(switches::kRoot, + in_path); + global_cmdline.GetProcessCommandLine()->AppendSwitch( + switches::kFailOnUnusedArgs); + global_cmdline.GetProcessCommandLine()->AppendSwitch(switches::kQuiet); + global_cmdline.GetProcessCommandLine()->AppendSwitch(switches::kThreads, "4"); std::vector<std::string> args; args.push_back(FilePathToUTF8(build_temp_dir.GetPath()));