Search code examples
c#msbuildgame-developmentmsbuild-target

Copy output of custom target in one CSPROJ into another CSPROJ


So I'm developing a game engine framework for myself in C#/.NET and I have been trying to simplify building shader files (the rendering engine consumes SPIR-V files, which I want to generate from GLSL source). My current approach has been to use a custom .targets file which is imported into a CSPROJ, and that file defines a custom target like this:

<Target
    Name="Vert"
    AfterTargets="Build">
    <MakeDir Directories="$(OutputPath)%(VertShader.RelativeDir)"/>
    <Exec Command='"$(ENGINE_ROOT)\tools\glslang\windows-x64\glslangValidator.exe" -V %(VertShader.Identity) %(VertShader.OptLevel) -o $(OutputPath)%(VertShader.Identity).spv'></Exec>
</Target>

The general idea behind this is it takes VertShader items, runs the .vert file through glslang, and spits the output .vert.spv file into the build output directory. This works great, almost.

The problem is, if I have Project A that has a bunch of shaders, and then Project B which has a reference to Project A, I want to copy the .vert.spv shaders that Project A generates into the build output directory of Project B, but I haven't had much luck wrangling the target syntax to do what I want.

Is there any way I can get this working the way I'm describing?


Solution

  • Alright so I actually did manage to figure it out on my own I think, adding an answer in case it's helpful to anyone else. Adding this to my .targets file appears to allow them to be copied to any dependent project. I believe this is basically registering all of the files I'm creating with a list of files that MSBuild copies to whatever the current build output directory is (which causes them to get copied to any project that references this one)

    <Target
        Name="IncludeVert"
        BeforeTargets="GetCopyToOutputDirectoryItems">
        <ItemGroup>
            <CompiledVerts Include="@(VertShader -> '$(OutputPath)%(RelativeDir)%(Filename).vert.spv' )">
                <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
                <TargetPath>%(RelativeDir)%(Filename).vert.spv</TargetPath>
            </CompiledVerts>
            <AllItemsFullPathWithTargetPath Include="@(CompiledVerts->'%(FullPath)')"  />
        </ItemGroup>
    </Target>