C# projects can reference other projects. This is done via @(ProjectReference)
:
<ItemGroup>
<ProjectReference Include="Path\To\OtherProject.csproj" />
</ItemGroup>
However, sometimes you don't want to reference the "default" (same build $(Configuration)
) project, but instead
a build variation based upon setting some MSBuild properties.
This seems straightforward: just use %(ProjectReference.AdditionalProperties)
!
<ItemGroup>
<ProjectReference
Include="Path\To\OtherProject.csproj"
AdditionalProperties="UseNativeAot=True"
/>
</ItemGroup>
…except that doesn't actually work by default. It may work "in the small", but if you build a top-level
.sln
and OtherProject.csproj
is referenced by multiple other projects, you may find that the project
specifying %(ProjectReference.AdditionalProperties)
doesn't actually
use/get an assembly that was built with those additional properties set. The first build "wins"!
In order to make this work as you would expect, OtherProject.csproj
needs to override
$(OutputPath)
and $(IntermediateOutputPath)
so that these properties are taken into consideration:
<PropertyGroup>
<IntermediateOutputPath Condition=" '$(UseNativeAot)' == 'true' ">$(BaseIntermediateOutputPath)$(Configuration)+NativeAOT\$(TargetFramework.ToLowerInvariant())\</IntermediateOutputPath>
<OutputPath Condition=" '$(UseNativeAot)' == 'true' ">bin\$(Configuration)+NativeAOT-$(DotNetTargetFramework.ToLowerInvariant())\</OutputPath>
</PropertyGroup>
That way, properties which make "uniquely different" assemblies get different build & output directories, and things will rebuild appropriately.
See also: https://github.com/xamarin/Java.Interop/commit/b5fc4cf223e9f2c34a632d06f13a3b293ac47f4b