1

For a custom build target, I would like to have full path with correct version of cl.exe which is being called in ClCompile target.

From retrieve path to cl.exe, I get it is %VCINSTALLDIR%\bin\x86\Hostx86\cl.exe or %VCINSTALLDIR%\bin\x64\Hostx64\cl.exe, But I do not want to write program for x84 vs x64 logic.

In VCTargets\Microsoft.CppCommon.Targets, I find that there is property ToolExe and ToolPath are being set to $(CLToolExe) and $(CLToolPath).

Overriding these properties also changed the path to compiler successfully for me, so it's correct that these are the properties names. Override Compiler in MSBuild

But when I try printing %(CL.ToolPath) or $(CLToolPath) in my custom target exec class, it prints nothing. I also looked in Visual Studio and couldn't find where it was being set.

Is there a variable which can provide cl.exe full path or is there any way I can print property ToolPath of CL Task in my custom target?

1 Answers1

0

1) If you already uses the $(CLToolExe) and $(CLToolPath) to set your new cl.exe and then want to get the full path of it, you should use this $(CLToolPath)$(CLToolExe) to get the new value rather than %(CL.ToolPath)(there is no such way like this).

This is my test result:

Actually, you can overwrite these properties directly under your xxx.csporj file rather than in Microsoft.CppCommon.Targets file and when you build your project, it will use the new cl.exe.

CLToolExe

Then, I use a custom target to get the value:

<Target Name="test123" AfterTargets="ClCompile">
        <Exec Command="$(CLToolPath)$(CLToolExe)" />
        <Message Importance="high" Text="$(CLToolPath)$(CLToolExe)"></Message>
</Target>

enter image description here

2) If you did not change to the new value and use the default cl.exe without any changes, you should use $(ClCompilerPath) to get the default value.

 <Target Name="test123" AfterTargets="ClCompile">       
       <Message Importance="high" Text="$(ClCompilerPath)"></Message>
 </Target>

enter image description here

Note: $(ClCompilerPath) cannot get the new value of $(CLToolPath)$(CLToolExe) since they are not related and can only get the default value.

Mr Qian
  • 21,064
  • 1
  • 31
  • 41
  • 1
    $(ClCompilerPath) prints nothing for me. I tried this target 123 exactly. – Mahima Chugh Nov 02 '20 at 17:37
  • What is your project type? Did you miss some system `targets` or `props` file in the `xxx.vcxproj` file? Could you please share your `xxx.vcxproj` file with us? The normal c++ projects are compiled by cl.exe and the property `ClCompilerPath` is the system property which defines that the full path of `cl.exe`. And it should not be empty. Besides, if you create a new default c++ project, does it print the property `ClCompilerPath`? – Mr Qian Nov 03 '20 at 02:58
  • @MrQian at least in VS2022 the rules are pretty clear and `ClCompilerPath` only ever gets populated when invoking code analysis. So it's nothing that would work generally ... – 0xC0000022L Jun 12 '23 at 15:34