Skip to content

feat: Support auto depend mode for MSVC without /showIncludes - #1158

Merged
jrosdahl merged 2 commits into
ccache:masterfrom
orgads:msvc-autodepend
Oct 5, 2022
Merged

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

Conversation

@orgads

@orgads orgads commented Sep 13, 2022

Copy link
Copy Markdown
Contributor

If MSVC is executed without /showIncludes, and ccache is configured with depend mode, add /showIncludes, and strip the extra output from stdout.

@orgads

orgads commented Sep 13, 2022

Copy link
Copy Markdown
Contributor Author

Depends on #992.

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

Copy link
Copy Markdown

It seems like the "Including file" output is being elided during the initial compilation, as intended.
But then it's being output when using the cached compilation.

@orgads

orgads commented Sep 13, 2022

Copy link
Copy Markdown
Contributor Author

Good catch, thanks!

Should be Ok now, not sure if the code is well positioned though. What do you say?

@justinpryzby

justinpryzby commented Sep 13, 2022 via email

Copy link
Copy Markdown

@vvainola

Copy link
Copy Markdown
Contributor

I still get "Note: including file:" if using ccache from within VS. On cache hits or when compiling from command line the includes are suppressed.

@justinpryzby

justinpryzby commented Sep 15, 2022 via email

Copy link
Copy Markdown

@vvainola

Copy link
Copy Markdown
Contributor

No, /showIncludes is not in compile flags so includes are not printed with CCACHE_DISABLE=1

@orgads

orgads commented Sep 16, 2022

Copy link
Copy Markdown
Contributor Author

@vvainola How do you use it in VS?

@vvainola

vvainola commented Sep 16, 2022 •

Copy link
Copy Markdown
Contributor

I made small VS solution for reproducing that you can try. In the zip there is a path in Directory.Build.Props file to cl.exe which should be a symlink to ccache.exe. If CCACHE_DEPEND=1, includes are printed on cache miss and if CCACHE_DEPEND is not set they are never printed.
ccache_demo.zip

I narrowed the issue down to the actual execute call

int status = execute(ctx,
so it seems to have nothing to do with changes in this PR except that with depend mode the showIncludes is added automatically. For some reason the output "leaks" if compiling from Visual Studio but not if compiling with MSBuild from command line. I tried adding std::abort() right before the execute call and then there is no output in Visual Studio but if I add the std::abort() right after then the includes are printed.

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 Sep 30, 2022

Copy link
Copy Markdown
Contributor Author

I narrowed the issue down to the actual execute call

int status = execute(ctx,

so it seems to have nothing to do with changes in this PR except that with depend mode the showIncludes is added automatically. For some reason the output "leaks" if compiling from Visual Studio but not if compiling with MSBuild from command line. I tried adding std::abort() right before the execute call and then there is no output in Visual Studio but if I add the std::abort() right after then the includes are printed.

I can reproduce. It is really strange. Looks like MSVC has a side-channel to capture the output. I also noticed that although capture_output is set, nothing is captured in the temp file.

Any idea how to proceed with it?

If MSVC is executed *without* /showIncludes, and ccache is configured with
depend mode, add /showIncludes, and strip the extra output from stdout.
@justinpryzby

justinpryzby commented Sep 30, 2022 via email

Copy link
Copy Markdown

@orgads

orgads commented Sep 30, 2022

Copy link
Copy Markdown
Contributor Author

I don't have an MSVC to test with. Did you check if this is related to the confusion about stdout vs stderr? They updated the documentation to match the apparent behavior, but is it possible that CL writes its includes to a different stream when run from MSVC ?

Nah, both files (stdout and stderr) are empty. The output goes to stdout when running from command-line.

@justinpryzby

justinpryzby commented Sep 30, 2022 via email

Copy link
Copy Markdown

@jrosdahl

jrosdahl commented Oct 1, 2022

Copy link
Copy Markdown
Member

@vvainola

vvainola commented Oct 1, 2022 •

Copy link
Copy Markdown
Contributor

Wild guess: Could this be related to https://stackoverflow.com/a/7402354/331771?

It is indeed. The includes are no longer printed in VS after removing it from the environment given to CreateProcess

  LPCH a = GetEnvironmentStrings();
  std::string new_env;
  std::size_t len = strlen(a);
  while (len > 0) {
    std::string env = std::string(a, len);
    if (!util::starts_with(env, "VS_UNICODE_OUTPUT")) {
      new_env += env + '\0';
    }
    a += len + 1;
    len = strlen(a);
  }
  new_env += '\0';

  BOOL ret = CreateProcess(full_path.c_str(),
                           const_cast<char*>(args.c_str()),
                           nullptr,
                           nullptr,
                           1,
                           0,
                           (LPVOID)new_env.c_str(),
                           nullptr,
                           &si,
                           &pi);

edit:
Actually much simpler is to just SetEnvironmentVariable("VS_UNICODE_OUTPUT", NULL); before CreateProcess

@orgads

orgads commented Oct 1, 2022

Copy link
Copy Markdown
Contributor Author

Good catch @jrosdahl! Proposed PR #1170.

@justinpryzby

Copy link
Copy Markdown

LGTM. postgresql compiles fine with depend mode, which (in the case of no cache hits) is about 2x faster than without depend mode.
https://cirrus-ci.com/task/4805444794318848

@jrosdahl
jrosdahl merged commit 8b65880 into ccache:master Oct 5, 2022
@orgads
orgads deleted the msvc-autodepend branch October 5, 2022 17:35
@jrosdahl jrosdahl added this to the 4.7 milestone Oct 5, 2022
@jrosdahl

jrosdahl commented Oct 5, 2022

Copy link
Copy Markdown
Member

I merged this by mistake. Will revert.

I missed that it depended on #992.

jrosdahl added a commit that referenced this pull request Oct 5, 2022
@orgads

orgads commented Oct 5, 2022

Copy link
Copy Markdown
Contributor Author

Why? Do you have rejections on #992?

@jrosdahl

jrosdahl commented Oct 5, 2022

Copy link
Copy Markdown
Member

Why? Do you have rejections on #992?

Yes, documentation needs an update and unit tests would be appreciated.

@jrosdahl jrosdahl removed this from the 4.7 milestone Oct 5, 2022
@orgads

orgads commented Oct 5, 2022

Copy link
Copy Markdown
Contributor Author

Unit tests can be enabled with the other PR that enables them on Windows.

@jrosdahl

jrosdahl commented Oct 5, 2022

Copy link
Copy Markdown
Member

Unit tests can be enabled with the other PR that enables them on Windows.

Sorry, what do you mean?

@orgads

orgads commented Oct 5, 2022

Copy link
Copy Markdown
Contributor Author

#1133.

@jrosdahl

jrosdahl commented Oct 5, 2022

Copy link
Copy Markdown
Member

#1133.

Those are integration tests, not unit tests. I'm talking about something similar to unittest/test_Depfile.cpp which does not depend on a Windows runtime environment.

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.

5 participants