<!--
***********************************************************************************************
Microsoft.NET.Windows.targets

WARNING:  DO NOT MODIFY this file unless you are knowledgeable about MSBuild and have
          created a backup copy.  Incorrect changes to this file will make it
          impossible to load or build your projects from the command-line or the IDE.

Copyright (c) .NET Foundation. All rights reserved.
***********************************************************************************************
-->

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

  <!--C++/CLI does not support nuget restore which is required for Windows SDK Ref. Also these are C# projections.-->
  <PropertyGroup Condition=" '$(DisableImplicitFrameworkReferences)' != 'true'
                             and '$(TargetFrameworkIdentifier)' == '.NETCoreApp'
                             and $([MSBuild]::VersionGreaterThanOrEquals($(_TargetFrameworkVersionWithoutV), '5.0'))
                             and '$(TargetPlatformIdentifier)' == 'Windows'
                             and '$(TargetPlatformVersion)' != ''
                             and $([MSBuild]::VersionGreaterThanOrEquals($(TargetPlatformVersion), '10.0'))
                             and '$(Language)' != 'C++'
                             and '$(IncludeWindowsSDKRefFrameworkReferences)' == ''">
    <IncludeWindowsSDKRefFrameworkReferences>true</IncludeWindowsSDKRefFrameworkReferences>
  </PropertyGroup>

  <!-- If UseWindowsSDKPreview is true, then allow any target platform version, not just the ones defined by the SDK -->
  <PropertyGroup Condition="'$(UseWindowsSDKPreview)' == 'true'">
    <TargetPlatformVersionSupported>true</TargetPlatformVersionSupported>
  </PropertyGroup>

  <!-- Set 7.0 as the TargetPlatformVersion for windows. -->
  <PropertyGroup Condition="'$(TargetPlatformIdentifier)' == 'windows' and '$(TargetPlatformVersion)' == ''" >
    <TargetPlatformVersion>7.0</TargetPlatformVersion>
  </PropertyGroup>

  <PropertyGroup Condition="'$(TargetPlatformIdentifier)' == 'Windows'">
    <!-- When targeting Windows, sync the SupportedOSPlatformVersion and TargetPlatformMinVersion.  If either one isn't set, set it to the
         other value. -->
    <SupportedOSPlatformVersion Condition="'$(SupportedOSPlatformVersion)' == ''">$(TargetPlatformMinVersion)</SupportedOSPlatformVersion>
    <TargetPlatformMinVersion Condition="'$(TargetPlatformMinVersion)' == ''">$(SupportedOSPlatformVersion)</TargetPlatformMinVersion>

    <!-- If neither were set, then use the TargetPlatformVersion value for both -->
    <SupportedOSPlatformVersion Condition="'$(SupportedOSPlatformVersion)' == ''">$(TargetPlatformVersion)</SupportedOSPlatformVersion>
    <TargetPlatformMinVersion Condition="'$(TargetPlatformMinVersion)' == ''">$(TargetPlatformVersion)</TargetPlatformMinVersion>
  </PropertyGroup>

  <!-- Used by analyzers in the Microsoft.Windows.SDK.NET.Ref package. -->
  <PropertyGroup Condition="'$(IncludeWindowsSDKRefFrameworkReferences)' == 'true'">
    <CsWinRTAotOptimizerEnabled Condition="'$(CsWinRTAotOptimizerEnabled)' == '' and $([MSBuild]::VersionGreaterThanOrEquals($(TargetFrameworkVersion), '6.0'))">true</CsWinRTAotOptimizerEnabled>
    <CsWinRTAotExportsEnabled Condition="'$(CsWinRTAotExportsEnabled)' == '' and '$(CsWinRTAotOptimizerEnabled)' == 'true' and '$(PublishAot)' == 'true'">true</CsWinRTAotExportsEnabled>
    <CsWinRTCcwLookupTableGeneratorEnabled Condition="'$(CsWinRTCcwLookupTableGeneratorEnabled)' == '' and '$(CsWinRTGenerateProjection)' != 'true'">true</CsWinRTCcwLookupTableGeneratorEnabled>
    <CsWinRTAotWarningLevel Condition="'$(CsWinRTAotWarningLevel)' == '' and '$(CsWinRTGenerateProjection)' != 'true' and ('$(PublishAot)' == 'true' or '$(IsAotCompatible)' == 'true')">1</CsWinRTAotWarningLevel>
  </PropertyGroup>

  <ItemGroup Condition="'$(IncludeWindowsSDKRefFrameworkReferences)' == 'true'">
    <CompilerVisibleProperty Include="CsWinRTAotOptimizerEnabled" />
    <CompilerVisibleProperty Include="CsWinRTAotExportsEnabled" />
    <CompilerVisibleProperty Include="CsWinRTRcwFactoryFallbackGeneratorForceOptIn" />
    <CompilerVisibleProperty Include="CsWinRTRcwFactoryFallbackGeneratorForceOptOut" />
    <CompilerVisibleProperty Include="CsWinRTCcwLookupTableGeneratorEnabled" />
    <CompilerVisibleProperty Include="CsWinRTMergeReferencedActivationFactories" />
    <CompilerVisibleProperty Include="CsWinRTAotWarningLevel" />
  </ItemGroup>

  <!-- Also add the Windows.UI.Xaml.* projections from the Windows SDK if 'UseUwp' is set in the project.
       Note: we only add this reference if the TFM is .NET 8 or above, as the Windows SDK projections
       that are used on .NET 6 and below do not have the Windows.UI.Xaml profile, so this wouldn't work. -->
  <ItemGroup Condition=" '$(IncludeWindowsSDKRefFrameworkReferences)' == 'true' ">
    <FrameworkReference Condition="'$(TargetFrameworkIdentifier)' == '.NETCoreApp' and $([MSBuild]::VersionGreaterThanOrEquals($(TargetFrameworkVersion), '8.0')) and '$(TargetPlatformIdentifier)' == 'Windows' and '$(UseUwp)' == 'true' " Include="Microsoft.Windows.SDK.NET.Ref.Xaml" />
  </ItemGroup>

  <!-- If WindowsSdkPackageVersion is specified and it is an older version before profile support was added, 
       add the non profile framework reference. -->
  <ItemGroup Condition=" '$(IncludeWindowsSDKRefFrameworkReferences)' == 'true'
                        and '$(WindowsSdkPackageVersion)' != ''
                        and '$(UseUwp)' != 'true'
                        and $([System.Version]::Parse('$(WindowsSdkPackageVersion.Split('-')[0])').Build) &lt;= 26100
                        and $([System.Version]::Parse('$(WindowsSdkPackageVersion.Split('-')[0])').Revision) &lt;= 34">
    <FrameworkReference Include="Microsoft.Windows.SDK.NET.Ref" IsImplicitlyDefined="true" Pack="false" PrivateAssets="All" />
  </ItemGroup>

  <!-- Default 'CsWinRTUseWindowsUIXamlProjections' to 'false', unless 'UseUwp' is set.
       This is to ensure that using the full Windows SDK projections will work correctly.
       We still allow users to override this, which can help in mixed WinAppSDK scenarios. -->
  <PropertyGroup>
    <CsWinRTUseWindowsUIXamlProjections Condition=" '$(CsWinRTUseWindowsUIXamlProjections)' == '' and '$(UseUwp)' == 'true' ">true</CsWinRTUseWindowsUIXamlProjections>
    <CsWinRTUseWindowsUIXamlProjections Condition=" '$(CsWinRTUseWindowsUIXamlProjections)' == '' ">false</CsWinRTUseWindowsUIXamlProjections>
  </PropertyGroup>

  <ItemGroup>

    <!-- Mark 'CsWinRTUseWindowsUIXamlProjections' as compiler visible, so the analyzers bundled in the Windows SDK projections can see it -->
    <CompilerVisibleProperty Include="CsWinRTUseWindowsUIXamlProjections" />
  </ItemGroup>

  <!-- Configure the Windows.UI.Xaml.* projections feature switch for CsWinRT from here. This is
       needed because there's no .targets from CsWinRT that's used when CsWinRT itself is not being
       directly referenced in the project. For instance, in a normal application (not authoring).
       Note: we only do this on 'net8.0-windows' or greater, as it's not supported otherwise.  -->
  <ItemGroup>
    <RuntimeHostConfigurationOption Include="CSWINRT_USE_WINDOWS_UI_XAML_PROJECTIONS"
                                    Value="$(CsWinRTUseWindowsUIXamlProjections)"
                                    Condition="'$(TargetFrameworkIdentifier)' == '.NETCoreApp' and $([MSBuild]::VersionGreaterThanOrEquals($(TargetFrameworkVersion), '8.0')) and '$(TargetPlatformIdentifier)' == 'Windows'"
                                    Trim="true" />
  </ItemGroup>

  <Target Name="_ErrorOnUnresolvedWindowsSDKAssemblyConflict"
          AfterTargets="ResolveAssemblyReferences"
          Condition=" '@(ResolveAssemblyReferenceUnresolvedAssemblyConflicts)' != '' ">
    <ItemGroup>
      <_WindowsSDKUnresolvedRef Include="@(ResolveAssemblyReferenceUnresolvedAssemblyConflicts)" Condition="'%(Identity)' == 'Microsoft.Windows.SDK.NET' " />
    </ItemGroup>

    <NETSdkError ResourceName="WindowsSDKVersionConflicts"
                 Condition="@(_WindowsSDKUnresolvedRef) != ''"/>
  </Target>

  <Target Name="RemoveManagedWinRTComponentWinMDReferences"
          AfterTargets="ResolveProjectReferences">
    
    <ItemGroup>
      <!-- Before we remove them from _ResolvedProjectReferencePaths, grab the implementation .dll's path -->
      <ManagedWinRTComponentImplementations 
        Include="@(_ResolvedProjectReferencePaths->'%(ManagedImplementation)')" 
        Condition="('%(_ResolvedProjectReferencePaths.Extension)' == '.winmd') And ('%(_ResolvedProjectReferencePaths.Implementation)' == 'WinRT.Host.dll')"
      />
    </ItemGroup>

    <!-- Managed WinRT components include both the WinMD and the .NET DLL in the result from GetTargetPath.  Managed projects need to only reference the .NET DLL, not the WinMD.
         The WinMD in this case can be identified because the Implementation metadata value is WinRT.Host.dll.  So here we remove any such WinMD references. -->
    <ItemGroup>
      <_ResolvedProjectReferencePaths Remove="@(_ResolvedProjectReferencePaths)"
                                      Condition="('%(_ResolvedProjectReferencePaths.Extension)' == '.winmd') And ('%(_ResolvedProjectReferencePaths.Implementation)' == 'WinRT.Host.dll')"
                                      />
    </ItemGroup>
    
  </Target>

  <Target Name="AddWinRTComponentImplementationReference" AfterTargets="ResolveProjectReferences" DependsOnTargets="RemoveManagedWinRTComponentWinMDReferences" Condition="'@(ManagedWinRTComponentImplementations)' != ''">
    
    <ItemGroup>
      <Reference Include="@(ManagedWinRTComponentImplementations)"/>
    </ItemGroup>

  </Target>

  <!-- Target to emit an error when 'UseUwp' is not set, but the 'Microsoft.Windows.SDK.NET.Ref.Xaml' item is transitively referenced -->
  <Target Name="_CheckForTransitiveWindowsSDKFullDependencies"
          AfterTargets="ResolvePackageAssets"
          Condition=" '$(TargetFrameworkIdentifier)' == '.NETCoreApp'
                      and '$(TargetPlatformIdentifier)' == 'Windows'
                      and '@(TransitiveFrameworkReference)' != ''
                      and '$(UseUwp)' != 'true' ">
    <ItemGroup>
      <_WindowsSDKXamlTransitiveFrameworkReference Include="@(TransitiveFrameworkReference)"
                                                   Condition="'%(Identity)' == 'Microsoft.Windows.SDK.NET.Ref.Xaml'" />
    </ItemGroup>
    <NetSdkError Condition="'@(_WindowsSDKXamlTransitiveFrameworkReference)' != ''"
                 ResourceName="WindowsSDKXamlMissingUseUwpProperty" />
  </Target>

  <!-- Target to emit an error when 'UseUwp' is set, but the TFM is not 'net8.0-windows' or greater -->
  <Target Name="_CheckForInvalidTfmForWindowsSDKXamlDependencies"
          AfterTargets="ResolvePackageAssets"
          Condition=" !('$(TargetFrameworkIdentifier)' == '.NETCoreApp' and $([MSBuild]::VersionGreaterThanOrEquals($(TargetFrameworkVersion), '8.0')) and '$(TargetPlatformIdentifier)' == 'Windows')
                      and '$(UseUwp)' == 'true' ">
    <NetSdkError ResourceName="WindowsSDKXamlInvalidTfm" />
  </Target>
</Project>
