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?
The
ClCompilename is used for both an item collection and a target. TheClCompiletarget uses theClCompilecollection.I assume you are asking how to override a target and specifically the
ClCompiletarget.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
ClCompiletarget is as follows. It may be a little different in VS 2017.Create a file named
Directory.Build.targets. TheDirectory.Build.propsandDirectory.Build.targetsfiles, if found, are automatically imported. By designDirectory.Build.propsis imported very early andDirectory.Build.targetsis imported very late and, more importantly, afterMicrosoft.CppCommon.targets. In theDirectory.Build.targetsfile put your customized definition of theClCompiletarget.You can use the
-preprocessswitch to see the project after all imports have been performed to confirm that your redefinition is after the standard definition.