Skip to content

Instantly share code, notes, and snippets.

@baronfel
Created June 24, 2023 18:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save baronfel/0843c389f57ef5710eb9450934b6665f to your computer and use it in GitHub Desktop.
Save baronfel/0843c389f57ef5710eb9450934b6665f to your computer and use it in GitHub Desktop.
An MSBuild target that adds RID-matching subvariants of source files to the compilation
<Project>
<!-- This is a very simple version of what we'd actually want. We actually would need to:
* get the target RID
* get the OS portion of the RID graph _only_, no versions, no architectures
* walk that graph in most-specific to least-specific order, including the first (most specific) items that match the expected pattern along the way
NOTE: .NET SDK Default Glob Include patterns wreck this entire feature -->
<Target Name="ExpandRIDCompileItems" DependsOnTargets="AddImplicitDefineConstants" BeforeTargets="CoreCompile">
<PropertyGroup>
<RidDefines Condition="'$(RuntimeIdentifier)' != ''">$(RuntimeIdentifier.Split('-')[0])</RidDefines>
<DefineConstants Condition="'$(RidDefines)' != ''">$(DefineConstants);$(RidDefines)</DefineConstants>
</PropertyGroup>
<ItemGroup>
<_DefineItems Include="$(RidDefines)" />
<_CompileFiles Include="@(_DefineItems)" CompileItemName="%(Compile.Filename)" CompileItemExtension="%(Compile.Filename)" Dir="%(Compile.RelativeDir)" Ext="%(Compile.Extension)" />
<_RIDSpecificCompileItem Include="%(_CompileFiles.Dir)%(_CompileFiles.CompileItemName).%(_CompileFiles.Identity)%(_CompileFiles.Ext)">
<DependentUpon>%(_CompileFiles.Dir)%(_CompileFiles.CompileItemName)%(_CompileFiles.Ext)</DependentUpon>
</_RIDSpecificCompileItem>
<Compile Include="@(_RIDSpecificCompileItem)" Condition="Exists(%(_RIDSpecificCompileItem.Identity))" />
</ItemGroup>
</Target>
</Project>
</Project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment