$(TargetPath) not evalutated on loading macro?

507 Views Asked by At

I've a file custom.props where I define some macro to be used within the project. In the example, I've this:

<VST2_32_COMMAND_ARGS>$(TargetPath) /noload /nosave /noexc /noft</VST2_32_COMMAND_ARGS>

When I load the project, and I look at Properties, Debugging, Command Arguments, I can access to that macro VST2_32_COMMAND_ARGS. But the string is evalutated as /noload /nosave /noexc /noft

Basically, $(TargetPath) is not evaluted. In my case, that path point to a DLL, so it should be somethings like this:

"C:\Program Files (x86)\VstPlugins\Plug\vst2\Win32\bin\MyPlug.dll" /noload /nosave /noexc /noft

But its empty. How could I fix it? Also tried this:

<VST2_32_COMMAND_ARGS>"$(TargetPath)" /noload /nosave /noexc /noft</VST2_32_COMMAND_ARGS>

but the result is:

"" /noload /nosave /noexc /noft
1

There are 1 best solutions below

8
Leo Liu On

$(TargetPath) not evalutated on loading macro?

To resolve this issue you should import your custom.props file after importing file Microsoft.Cpp.targets:

   <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
   <Import Project="Custom.props" />

That because the value of Macros for Build Commands and Properties set by the file Microsoft.Cpp.Current.targets:

  <Target Name="SetUserMacroEnvironmentVariables" 
          Condition="'@(BuildMacro)' != '' and '$(DesignTimeBuild)' != 'true'">

    <SetEnv Condition="'%(BuildMacro.EnvironmentVariable)' == 'true'"
        Name   ="@(BuildMacro)"
        Value  ="%(BuildMacro.Value)"
        Prefix ="false">
      <Output TaskParameter="OutputEnvironmentVariable" PropertyName="%(BuildMacro.Identity)"/>
    </SetEnv>

  </Target>

And the file Microsoft.Cpp.Current.targets is imported by the file Microsoft.Cpp.targets:

 <Import Condition="'$(_Redirect)' != 'true'" Project="$(VCTargetsPath)\Microsoft.Cpp.Current.targets" />

So if you call the some macro in your custom file before importing the file Microsoft.Cpp.targets, MSBuild could not get the value of macro.

You can get those .targets files at following path:

C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\VC\VCTargets

To verify the custom value of VST2_32_COMMAND_ARGS, I add a simple target to output that value. So you project file should be like:

  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />

  <ImportGroup Label="ExtensionTargets">
  </ImportGroup>

  <Import Project="Custom.props" />

  <Target Name="TestByCustom" AfterTargets="Build">
    <Message Text="$(VST2_32_COMMAND_ARGS)"></Message>
  </Target>

After build completed, we could get the value of $(TargetPath):

enter image description here

My custom.props file:

<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
   <VST2_32_COMMAND_ARGS>"$(TargetPath)" /noload /nosave /noexc /noft</VST2_32_COMMAND_ARGS>
  </PropertyGroup>

</Project>

Hope this helps.