0

I try to use ccache with VS2017 using the steps described here: https://github.com/ccache/ccache/wiki/MS-Visual-Studio

Turned out, it only works for ninja and nmake projects which is unacceptable for our working environment. Also the UseMultiToolTask property is only available in VS2019 and above.

Rewiring the compiler is not enough. ccache needs one cpp on the input, and is able to return one obj file on the output. CL.exe is compiling the cpp files in batches(.rsp file) which results in many .obj files in the output folder. I was able to get around this by changing one line in my Microsoft.CppCommon.targets file: I have replaced in Sources="@(ClCompile)" with Sources="%(ClCompile.Identity)" in the according CL task and with this hack the ccache is working perfectly.

Now my question is how to override CLCompile properly. I need to have a .targets file somewhere on my computer which will contain the override and I will import it in my vcxprojs. What should I write in that .targets file?

Robert
  • 53
  • 1
  • 6

1 Answers1

0

The ClCompile name is used for both an item collection and a target. The ClCompile target uses the ClCompile collection.

I assume you are asking how to override a target and specifically the ClCompile target.

In MSBuild a target can be overridden by redefining it. The last definition of a target is the version that will be executed.

In VS 2022 the element start tag for the ClCompile target is as follows. It may be a little different in VS 2017.

  <Target Name="ClCompile"
          Condition="'@(ClCompile)' != ''"
          DependsOnTargets="SelectClCompile">

Create a file named Directory.Build.targets. The Directory.Build.props and Directory.Build.targets files, if found, are automatically imported. By design Directory.Build.props is imported very early and Directory.Build.targets is imported very late and, more importantly, after Microsoft.CppCommon.targets. In the Directory.Build.targets file put your customized definition of the ClCompile target.

You can use the -preprocess switch to see the project after all imports have been performed to confirm that your redefinition is after the standard definition.

Jonathan Dodds
  • 2,654
  • 1
  • 10
  • 14