Search code examples
visual-studiomsbuildmsbuild-target

Setting CustomBuild's OutputItemType in a target doesn't work


If you set "Add Outputs to Item Type" property in Custom Build Tool like this,

Add outputs to item type

It will add the following line in .vcxproj file:

<OutputItemType>ClCompile</OutputItemType>

However, when I define my own target and try to use it, it doesn't work.

(...)

<Target Name="__SECompile" BeforeTargets="PreBuildEvent">

    <ItemGroup>
        <_SECompileMetadataSet Include="@(SECompile)">
            <Message>Processing %(Identity)</Message>
            <Outputs>$(ProjectDir)intermediate\%(Identity)</Outputs>
            <Command>python (path to a python script) "%(FullPath)" "$(ProjectDir)intermediate\%(Identity)"</Command>
            <OutputItemType>ClCompile</OutputItemType>
            <LinkObjects>false</LinkObjects>
        </_SECompileMetadataSet>
    </ItemGroup>

(...)
(Below is just a copy-paste from Microsoft.CppCommon.targets)

    <!-- Get out of date items (will create tlogs for all SECompile items) -->
    <GetOutOfDateItems
      Condition                 ="'$(SelectedFiles)' == ''"
      Sources                   ="@(_SECompileMetadataSet)"
      OutputsMetadataName       ="Outputs"
      DependenciesMetadataName  ="AdditionalInputs"
      CommandMetadataName       ="Command"
      TLogDirectory             ="$(TLogLocation)"
      TLogNamePrefix            ="SECompile"
      CheckForInterdependencies ="true"
      >
      <Output TaskParameter="OutOfDateSources" ItemName="_SECompile"/>
    </GetOutOfDateItems>

    <!-- Buidl items which can be built in parallel (ignored for selected files build)-->
    <ItemGroup Condition="'$(SelectedFiles)' == ''">
      <_ParallelSECompile Include="@(_SECompile)" />
    </ItemGroup>

    <ParallelCustomBuild
      Condition       ="'@(_ParallelSECompile)' != ''"
      Sources         ="@(_ParallelSECompile)"
      MaxProcesses    ="0"
      MaxItemsInBatch ="0"
      AcceptableNonZeroExitCodes  =""
    />

(...)

The python script is running and the message is being output, so the other properties are functioning. But OutputItemType doesn't work.

What I'm trying to do is basically do the exact same thing CustomBuild does, with its arguments predefined with specific values.

So the question is: Why doesn't it work when you set it manually in a target while it should be the same as it was set in the vcxproj file?


Solution

  • Got an answer through Visual Studio Feedback. Putting

    <ItemGroup Condition="'@(_SECompile)' != ''"> <ClCompile Include="%(_SECompile.Outputs)" Condition="'%(_SECompile.ExcludedFromBuild)' != 'true'" /> </ItemGroup>
    

    in the target would work.

    Original link: https://developercommunity.visualstudio.com/t/Setting-CustomBuilds-OutputItemType-in/1452940?entry=myfeedback&ref=native&refTime=1624191549072&refUserId=95d48ba8-b75f-4896-8bf5-5745bf012b77