如何使 MSBuild 在引用的项目中正确跟踪使用外部工具生成的文件?

How to make MSBuild correctly track files generated with an external tool across referenced projects?

我有 MSBuild 代码,它使用特定的构建操作获取文件(本例中为 CompileFoo)并生成输出文件(具有不同的扩展名)。这是我到目前为止的代码:

<Target Name="BuildFoo" BeforeTargets="Compile"
    Inputs="@(CompileFoo)"
    Outputs="@(CompileFoo -> '$(OutputPath)%(RelativeDir)%(Filename).bin' )" >

    <!-- makefoo doesn't know how to create directories: -->
    <MakeDir Directories="$(OutputPath)%(CompileFoo.RelativeDir)"/>
    <Exec Command="makefoo -o &quot;$(OutputPath)%(CompileFoo.RelativeDir)%(CompileFoo.Filename).bin&quot; &quot;%(CompileFoo.Identity)&quot;" />

    <ItemGroup>
        <!-- Required so we can handle Clean: -->
        <FileWrites Include="@(CompileFoo -> '$(OutputPath)%(RelativeDir)%(Filename).bin' )"/>
    </ItemGroup>
</Target>

如果我将它包含在生成最终 EXE 的项目中,这会非常有用。

但现在我想让它在生成一个 DLL 的项目中工作,该 DLL 由 EXE(C# 和程序集引用)引用,我需要获取那些生成的项目(.bin 中的文件示例)从DLL的输出目录,到EXE的输出目录。

我正在尝试得到类似于此的效果,当它出现在 DLL 项目中时:

<Content Include="Test\Test.txt"><CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory></Content>

在这种情况下,Test\Test.txt 文件最终位于 EXE 的输出文件夹中。虽然我不确定这是完全一样的事情。 (它是从原始文件复制,还是从 DLL 输出文件夹中复制?)

我正在尝试获得相当兼容的东西 - 特别是可以在 VS2010 和 VS 上运行的东西 Mac。

这里的技巧是使 GetCopyToOutputDirectoryItems 目标 return 成为一个额外的 AllItemsFullPathWithTargetPath 项目:

<Target Name="IncludeFoo" BeforeTargets="GetCopyToOutputDirectoryItems">
  <ItemGroup>
    <CompiledFoos Include="@(CompileFoo -> '$(OutputPath)%(RelativeDir)%(Filename).bin' )">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <TargetPath>%(RelativeDir)%(FileName).bin</TargetPath>
    </CompiledFoos>
    <AllItemsFullPathWithTargetPath Include="@(CompiledFoos->'%(FullPath)')"  />
  </ItemGroup>
</Target>

(测试使用当前的 MSBuild 15 版本)(使用 VS2010 -AR 测试的编辑版本)