diff options
author | Matthias Maennich <maennich@google.com> | 2021-12-08 16:06:13 +0000 |
---|---|---|
committer | Matthias Maennich <maennich@google.com> | 2021-12-08 16:36:37 +0000 |
commit | bf0addcb425c10a3bf9fd70a494066956f0b5347 (patch) | |
tree | ffea5cd5c732332a3d18fbd04ad3207843a802c5 | |
parent | 526323b0494be90d2dbaba296284f0646a800fb5 (diff) | |
download | interceptor-bf0addcb425c10a3bf9fd70a494066956f0b5347.tar.gz |
interceptor: store relative current directories
In one way or the other, the current directories of a command are always
relative to the root_dir. Hence, just make them relative at collection
time and remove this redundant information. This reduces the command log
of a kernel build by ~10%.
Signed-off-by: Matthias Maennich <maennich@google.com>
Change-Id: I6c4edb2a67b1d37a91e21e68d603876d96a64761
-rw-r--r-- | interceptor.cc | 41 |
1 files changed, 25 insertions, 16 deletions
diff --git a/interceptor.cc b/interceptor.cc index e9efccc..79f56e5 100644 --- a/interceptor.cc +++ b/interceptor.cc @@ -124,16 +124,17 @@ static std::optional<std::string> command_getenv(const Command& command, const c return {}; } -static bool default_make_relative(Command* command) { +static std::optional<fs::path> command_get_root_directory(const Command& command) { // determine the ROOT_DIR fs::path root_directory; - if (const auto root = command_getenv(*command, kEnvRootDirectory)) { - root_directory = fs::path(*root) / ""; - } else { - // there is no ROOT_DIR that we can use to make calls relative - return false; + if (const auto root = command_getenv(command, kEnvRootDirectory)) { + return fs::path(*root) / ""; } + // there is no ROOT_DIR that we can use to make calls relative + return {}; +} +static bool default_make_relative(Command* command, const fs::path& root_directory) { // determine the relative path to ROOT_DIR from the current working dir auto relative_root = fs::relative(root_directory) / ""; if (relative_root == "./") { @@ -146,8 +147,6 @@ static bool default_make_relative(Command* command) { return false; } - command->set_current_directory(fs::relative(command->current_directory(), root_directory)); - // replacement functor const auto replace_all = [&](auto& str) { size_t start = 0; @@ -176,7 +175,7 @@ class Analyzer { virtual ~Analyzer() = default; void set_inputs_outputs(Command* command) const; - virtual bool make_relative(Command*) const { return false; } + virtual bool make_relative(Command*, const fs::path&) const { return false; } virtual bool should_recurse(Command*) const { return true; } protected: @@ -243,7 +242,9 @@ class CompileLinkerAnalyzer : public Analyzer { return result; } - bool make_relative(Command* command) const final { return default_make_relative(command); } + bool make_relative(Command* command, const fs::path& root_directory) const final { + return default_make_relative(command, root_directory); + } // do not recurse the interceptor into the subprocesses of compilers/linkers; // otherwise we will trace (and get confused by) ld.lld/cc1-plus and friends. @@ -268,7 +269,9 @@ class ArchiverAnalyzer : public Analyzer { return result; } - bool make_relative(Command* command) const final { return default_make_relative(command); } + bool make_relative(Command* command, const fs::path& root_directory) const final { + return default_make_relative(command, root_directory); + } bool should_recurse(Command*) const final { return false; } }; @@ -338,11 +341,17 @@ static std::optional<interceptor::Command> process_command(const char* filename, bool transformed = false; - // rewrite all command line arguments (including the program itself) to use - // paths relative to ROOT_DIR. This is essential for reproducible builds and - // furthermore necessary to produce cache hits in RBE. - if (command_getenv(command, kEnvMakeRelative)) { - transformed |= analyzer->make_relative(&command); + auto root_directory = command_get_root_directory(command); + + if (root_directory.has_value()) { + command.set_current_directory(fs::relative(command.current_directory(), *root_directory)); + + // rewrite all command line arguments (including the program itself) to use + // paths relative to ROOT_DIR. This is essential for reproducible builds and + // furthermore necessary to produce cache hits in RBE. + if (command_getenv(command, kEnvMakeRelative)) { + transformed |= analyzer->make_relative(&command, *root_directory); + } } analyzer->set_inputs_outputs(&command); |