Skip to content

feat: Support depend mode for MSVC - #992

Merged
jrosdahl merged 2 commits into
ccache:masterfrom
orgads:msvc-depend
Oct 12, 2022
Merged

jrosdahl merged 2 commits into
ccache:masterfrom
orgads:msvc-depend

Conversation

@orgads

@orgads orgads commented Jan 11, 2022 •

Copy link
Copy Markdown
Contributor

Based on -showIncludes, which prints included files on stdout
with a certain text prefix. Otherwise pretty similar to depend mode
handling for GCC.

This makes MSVC building way faster.

@orgads

orgads commented Jan 11, 2022

Copy link
Copy Markdown
Contributor Author

Quoting from #952

  • Missing documentation.
  • Unit tests would be appreciated.
  • Since the feature doesn't use dependency files, I don't think that it makes sense to piggy back on the name "depend mode". We could consider renaming it to something more generic that doesn't lock us into implementation details, though (not sure what).
  • Also, do I understand correctly that the user is expected to add /showIncludes themselves? If so, is that really reasonable since stderr will be littered with header file lists intermixed with errors and warnings?

@orgads
orgads force-pushed the msvc-depend branch 2 times, most recently from 3fa7d88 to 2f0fcf1 Compare January 11, 2022 11:07
@llunak

llunak commented Jan 11, 2022

Copy link
Copy Markdown
Contributor
* Missing documentation.
* Unit tests would be appreciated.

I think these two apply to most of the MSVC commits, or they at least need checked. As for tests, do MSVC-based tests already work?

* Since the feature doesn't use dependency files, I don't think that it makes sense to piggy back on the name "depend mode". We could consider renaming it to something more generic that doesn't lock us into implementation details, though (not sure what).
* Also, do I understand correctly that the user is expected to add `/showIncludes` themselves? If so, is that really reasonable since  stderr will be littered with header file lists intermixed with errors and warnings?

It really is the same as the GCC-based depend mode, it just uses a different option and output format, but otherwise it's the same, including the usage. The build system will usually add the option automatically, just like it does for GCC, and it should also filter it out from MSVC's output (and commented in #952 that's the case for ninja and e.g. LibreOffice's build system works similarly). I think even the name depend mode still fits, it's a mode that uses dependencies generated by the compiler, in whatever format the compiler provides them.

@orgads

This comment was marked as off-topic.

@orgads orgads mentioned this pull request Feb 7, 2022
@jrosdahl

This comment was marked as off-topic.

@orgads

This comment was marked as off-topic.

@jrosdahl

Copy link
Copy Markdown
Member

@llunak wrote:

I think these two apply to most of the MSVC commits, or they at least need checked. As for tests, do MSVC-based tests already work?

I was referring to the unit tests, i.e. the tests in the unittest directory, not the integration tests in the test directory. The ShowIncludesParser code is a good candidate for unit testing since it does not depend on a real compiler.

It really is the same as the GCC-based depend mode, it just uses a different option and output format, but otherwise it's the same, including the usage. The build system will usually add the option automatically, just like it does for GCC, and it should also filter it out from MSVC's output (and commented in #952 that's the case for ninja and e.g. LibreOffice's build system works similarly).

OK, thanks. Since e.g. Ninja already makes use of it out of the box I agree that it makes sense to support it.

I think even the name depend mode still fits, it's a mode that uses dependencies generated by the compiler, in whatever format the compiler provides them.

OK, I can live with that name if it's documented in a good way.

@jrosdahl jrosdahl added compiler: msvc Related to Microsoft Visual C++ issue: feature New feature labels Feb 15, 2022
@justinpryzby

Copy link
Copy Markdown

Docs seem to say that cl.exe writes to stderr, not stdout.
Does it depend on the version or ??

https://docs.microsoft.com/en-us/cpp/build/reference/showincludes-list-include-files?view=msvc-170

The /showIncludes option emits to stderr, not stdout.

@orgads

orgads commented Mar 19, 2022

Copy link
Copy Markdown
Contributor Author

Docs seem to say that cl.exe writes to stderr, not stdout. Does it depend on the version or ??

https://docs.microsoft.com/en-us/cpp/build/reference/showincludes-list-include-files?view=msvc-170

The /showIncludes option emits to stderr, not stdout.

It's a lie! 😈

I just tested, and it outputs to stdout.

@orgads

orgads commented Mar 19, 2022

Copy link
Copy Markdown
Contributor Author

@justinpryzby

Copy link
Copy Markdown

Reported now. MicrosoftDocs/cpp-docs#3785

It hardly matters, since if they change it, you'd still have to assume that the output could go to either. (without assuming that every user is using the latest release of the latest version).

@orgads

orgads commented Mar 24, 2022

Copy link
Copy Markdown
Contributor Author

They won't change it, since that will break existing tools. I reported so they fix the docs.

@justinpryzby

Copy link
Copy Markdown

They won't change it, since that will break existing tools. I reported so they fix the docs.

Will you plan to make another version of the patch to check both stdout and stderr ?

@orgads

orgads commented Jun 12, 2022

Copy link
Copy Markdown
Contributor Author

They won't change it, since that will break existing tools. I reported so they fix the docs.

Will you plan to make another version of the patch to check both stdout and stderr ?

The docs were updated. stderr is no longer mentioned.

@rkitover

Copy link
Copy Markdown
Contributor

@orgads Hi, could you please rebase on master? I'm getting some compile errors due to recent changes like the bump to c++17.

@rkitover

Copy link
Copy Markdown
Contributor

@orgads Builds rebased on master for me with this patch:

diff --git a/src/ShowIncludesParser.cpp b/src/ShowIncludesParser.cpp
index 9e7365f3..e99ce817 100644
--- a/src/ShowIncludesParser.cpp
+++ b/src/ShowIncludesParser.cpp
@@ -23,8 +23,8 @@

 namespace ShowIncludesParser {

-std::vector<nonstd::string_view>
-tokenize(nonstd::string_view file_content, nonstd::string_view prefix)
+std::vector<std::string_view>
+tokenize(std::string_view file_content, std::string_view prefix)
 {
   // -showIncludes output is written to stdout together with other messages.
   // Every line of it is '<prefix> <spaces> <file>', prefix is 'Note: including
@@ -34,17 +34,17 @@ tokenize(nonstd::string_view file_content, nonstd::string_view prefix)
     prefix = "Note: including file:";
   }

-  std::vector<nonstd::string_view> result;
+  std::vector<std::string_view> result;
   // This will split at each \r or \n, but that simply means there will be empty
   // "lines".
-  for (nonstd::string_view line :
+  for (std::string_view line :
        Util::split_into_views(file_content, "\r\n")) {
     if (util::starts_with(line, prefix)) {
       size_t pos = prefix.size();
       while (pos < line.size() && isspace(line[pos])) {
         ++pos;
       }
-      nonstd::string_view include = line.substr(pos);
+      std::string_view include = line.substr(pos);
       if (!include.empty()) {
         result.push_back(include);
       }
diff --git a/src/ShowIncludesParser.hpp b/src/ShowIncludesParser.hpp
index 6a774641..89559c98 100644
--- a/src/ShowIncludesParser.hpp
+++ b/src/ShowIncludesParser.hpp
@@ -18,13 +18,12 @@

 #pragma once

-#include "third_party/nonstd/string_view.hpp"
-
+#include <string_view>
 #include <vector>

 namespace ShowIncludesParser {

-std::vector<nonstd::string_view> tokenize(nonstd::string_view file_content,
-                                          nonstd::string_view prefix);
+std::vector<std::string_view> tokenize(std::string_view file_content,
+                                       std::string_view prefix);

 } // namespace ShowIncludesParser
diff --git a/src/argprocessing.cpp b/src/argprocessing.cpp
index 18d1f7c8..44da048c 100644
--- a/src/argprocessing.cpp
+++ b/src/argprocessing.cpp
@@ -648,7 +648,7 @@ process_option_arg(const Context& ctx,
   if (args[i] == "-showIncludes") {
     args_info.generating_includes = true;
     state.dep_args.push_back(args[i]);
-    return nullopt;
+    return std::nullopt;
   }

   if (args[i] == "-fprofile-arcs") {
diff --git a/src/ccache.cpp b/src/ccache.cpp
index 5db23aac..e7db244b 100644
--- a/src/ccache.cpp
+++ b/src/ccache.cpp
@@ -678,7 +678,7 @@ result_key_from_depfile(Context& ctx, Hash& hash)

 // Extract the used includes from -showIncludes output in stdout. Note that we
 // cannot distinguish system headers from other includes here.
-static optional<Digest>
+static std::optional<Digest>
 result_key_from_includes(Context& ctx,
                          Hash& hash,
                          const std::string& stdout_path)
@@ -688,10 +688,10 @@ result_key_from_includes(Context& ctx,
     file_content = Util::read_file(stdout_path);
   } catch (const core::Error& e) {
     LOG("Cannot open stdout tmp file {}: {}", stdout_path, e.what());
-    return nullopt;
+    return std::nullopt;
   }

-  for (string_view token : ShowIncludesParser::tokenize(
+  for (std::string_view token : ShowIncludesParser::tokenize(
          file_content, ctx.config.msvc_dep_prefix())) {
     if (!ctx.has_absolute_include_headers) {
       ctx.has_absolute_include_headers = util::is_absolute_path(token);

@orgads

orgads commented Aug 14, 2022

Copy link
Copy Markdown
Contributor Author

Thank you, sorry for the delay. Done.

@rkitover

Copy link
Copy Markdown
Contributor

Awesome, thank you! Hope we can get this merged soon 👍

@orgads
orgads force-pushed the msvc-depend branch 2 times, most recently from f0d0174 to f097e69 Compare August 14, 2022 13:36
@orgads

orgads commented Aug 14, 2022

Copy link
Copy Markdown
Contributor Author

Rebased and adapted to build. @jrosdahl Please review.

@justinpryzby

justinpryzby commented Aug 16, 2022 •

Copy link
Copy Markdown

Confirmed that this makes building postgres about 2x faster when the cache is empty.
Normally that takes ~12 minutes. Since cache misses are so slow (3x-4x slower than without using ccache), that could easily mean the difference between ccache being a win or a loss for our CI.
https://cirrus-ci.com/task/5577311092211712
https://cirrus-ci.com/task/5588532281475072

Actually .. this also makes fully-cached builds slower than without depend mode (which take ~90sec under cirrus), maybe due to the enormous amount of output. Would it make sense to suppress (or allow suppressing) the output from -ShowIncludes, during cache hits ?
https://cirrus-ci.com/task/5401686842802176

@jrosdahl

jrosdahl commented Aug 28, 2022 •

Copy link
Copy Markdown
Member

@orgads: The implementation looks reasonable to me after a cursory glance. That leaves "Missing documentation" and "Unit tests would be appreciated". Are those something that you would like to address?

@orgads

orgads commented Aug 28, 2022

Copy link
Copy Markdown
Contributor Author

@llunak can you take care of these issues?

@jrosdahl

Copy link
Copy Markdown
Member

@justinpryzby wrote:

cache misses are so slow (3x-4x slower than without using ccache)

That doesn't make sense to me. What on earth makes them so much slower?

Would it make sense to suppress (or allow suppressing) the output from -ShowIncludes, during cache hits ? cirrus-ci.com/task/5401686842802176

I think that it could be a good idea for ccache to add /showIncludes by itself if depend_mode = true (and /showIncludes isn't already on the command line) and then filter out the Note: including file: lines before emitting the stdout to the user. Unless I'm missing something, that would improve the use case for build systems that aren't based on Ninja. Does this make sense?

@orgads

orgads commented Sep 12, 2022

Copy link
Copy Markdown
Contributor Author

Would it make sense to suppress (or allow suppressing) the output from -ShowIncludes, during cache hits ? cirrus-ci.com/task/5401686842802176

I think that it could be a good idea for ccache to add /showIncludes by itself if depend_mode = true (and /showIncludes isn't already on the command line) and then filter out the Note: including file: lines before emitting the stdout to the user. Unless I'm missing something, that would improve the use case for build systems that aren't based on Ninja. Does this make sense?

Sounds good to me.

@orgads

orgads commented Sep 13, 2022

Copy link
Copy Markdown
Contributor Author

@justinpryzby wrote:

cache misses are so slow (3x-4x slower than without using ccache)

That doesn't make sense to me. What on earth makes them so much slower?

I can reproduce that. When running without -showIncludes, or with depend_mode disabled, ccache runs the preprocessor, and it takes as long as (and sometimes more than) the compiler itself.

On my tests, compilation without ccache takes ~1.7s, while with ccache it can reach even 5s, average is around 3-4s.

With depend mode and -showIncludes, performance is quite close to without ccache. 1.7s -> 2s.

@orgads

orgads commented Sep 13, 2022

Copy link
Copy Markdown
Contributor Author

Auto mode pushed in #1158.

@orgads orgads closed this Oct 5, 2022
@orgads
orgads deleted the msvc-depend branch October 5, 2022 17:37
@orgads
orgads restored the msvc-depend branch October 5, 2022 17:52
@orgads orgads reopened this Oct 5, 2022
@orgads

orgads commented Oct 5, 2022

Copy link
Copy Markdown
Contributor Author

Please review the documentation. I'll try to add unit tests tomorrow.

@jrosdahl

jrosdahl commented Oct 6, 2022

Copy link
Copy Markdown
Member

Please review the documentation. I'll try to add unit tests tomorrow.

Thanks.

Regarding the manual I'm missing documentation of the msvc_dep_prefix/CCACHE_MSVC_DEP_PREFIX configuration option.

Another thing that comes to mind when I read the diff is that it would be good to use msvc_dep_prefix in rewrite_stdout_from_compiler as well:

ccache/src/ccache.cpp

Lines 936 to 941 in e9ddf68

else if (ctx.config.compiler_type() == CompilerType::msvc
&& !ctx.config.base_dir().empty()
&& util::starts_with(line, "Note: including file:")) {
std::string orig_line(line.data(), line.length());
std::string abs_inc_path =
util::replace_first(orig_line, "Note: including file:", "");

Comment thread doc/MANUAL.adoc Resolved
Comment thread doc/MANUAL.adoc Resolved
Comment thread doc/MANUAL.adoc Resolved
Based on -showIncludes, which prints included files on stdout with a
certain text prefix. Otherwise pretty similar to depend mode handling
for GCC.

This makes MSVC building way faster.
@orgads orgads changed the title make depend mode work for MSVC feat: Support depend mode for MSVC Oct 10, 2022
@orgads

orgads commented Oct 10, 2022

Copy link
Copy Markdown
Contributor Author

Done. I moved the doc and tests to a separate commit.


TEST_SUITE_BEGIN("ShowIncludesParser");

TEST_CASE("ShowIncludesParser::tokenize")

Check notice

Code scanning / CodeQL

Unused static function

Static function DOCTEST_ANON_FUNC_3 is unreachable ([DOCTEST_ANON_VAR_4](1) must be removed at the same time)
@justinpryzby

justinpryzby commented Oct 11, 2022 via email

Copy link
Copy Markdown

@jrosdahl jrosdahl added this to the 4.7 milestone Oct 12, 2022
@jrosdahl
jrosdahl merged commit b1348e5 into ccache:master Oct 12, 2022
@jrosdahl

Copy link
Copy Markdown
Member

Thanks.

@rkitover

Copy link
Copy Markdown
Contributor

@orgads congrats on getting this merged! Some of us have been pulling this PR into our local copies.

@orgads

orgads commented Oct 12, 2022

Copy link
Copy Markdown
Contributor Author

👍

@orgads
orgads deleted the msvc-depend branch October 12, 2022 18:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

compiler: msvc Related to Microsoft Visual C++ issue: feature New feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants