<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <ItemGroup>
    <Natvis Include="$(SolutionDir)tools\ConsoleTypes.natvis" />
  </ItemGroup>

  <!--
    The Hybrid CRT model statically links the runtime and STL and dynamically
    links the UCRT instead of the VC++ CRT. The UCRT ships with Windows.
    WinAppSDK asserts that this is "supported according to the CRT maintainer."

    This must come before Microsoft.Cpp.targets because it manipulates ClCompile.RuntimeLibrary.
  -->
  <ItemDefinitionGroup Condition="'$(EnableHybridCRT)'=='true' and '$(Configuration)'=='Debug'">
    <ClCompile>
      <!-- We use MultiThreadedDebug, rather than MultiThreadedDebugDLL, to avoid DLL dependencies on VCRUNTIME140d.dll and MSVCP140d.dll. -->
      <RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
    </ClCompile>
    <Link>
      <!-- Link statically against the runtime and STL, but link dynamically against the CRT by ignoring the static CRT
           lib and instead linking against the Universal CRT DLL import library. This "hybrid" linking mechanism is
           supported according to the CRT maintainer. Dynamic linking against the CRT makes the binaries a bit smaller
           than they would otherwise be if the CRT, runtime, and STL were all statically linked in. -->
      <IgnoreSpecificDefaultLibraries>%(IgnoreSpecificDefaultLibraries);libucrtd.lib</IgnoreSpecificDefaultLibraries>
      <AdditionalOptions>%(AdditionalOptions) /defaultlib:ucrtd.lib</AdditionalOptions>
    </Link>
  </ItemDefinitionGroup>
  <ItemDefinitionGroup Condition="'$(EnableHybridCRT)'=='true' and ('$(Configuration)'=='Release' or '$(Configuration)'=='AuditMode')">
    <ClCompile>
      <!-- We use MultiThreaded, rather than MultiThreadedDLL, to avoid DLL dependencies on VCRUNTIME140.dll and MSVCP140.dll. -->
      <RuntimeLibrary>MultiThreaded</RuntimeLibrary>
    </ClCompile>
    <Link>
      <!-- Link statically against the runtime and STL, but link dynamically against the CRT by ignoring the static CRT
           lib and instead linking against the Universal CRT DLL import library. This "hybrid" linking mechanism is
           supported according to the CRT maintainer. Dynamic linking against the CRT makes the binaries a bit smaller
           than they would otherwise be if the CRT, runtime, and STL were all statically linked in. -->
      <IgnoreSpecificDefaultLibraries>%(IgnoreSpecificDefaultLibraries);libucrt.lib</IgnoreSpecificDefaultLibraries>
      <AdditionalOptions>%(AdditionalOptions) /defaultlib:ucrt.lib</AdditionalOptions>
    </Link>
  </ItemDefinitionGroup>

  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />

  <ItemDefinitionGroup>
    <!-- Definition the program database type has to come after Microsoft.Cpp.props or it will be
         rewritten to /ZI (edit-and-continue) type every time. -->
    <ClCompile>
      <DebugInformationFormat>ProgramDatabase</DebugInformationFormat>
    </ClCompile>
    <Link>
      <AdditionalDependencies>onecoreuap_apiset.lib;d3dcompiler.lib;dwmapi.lib;uxtheme.lib;shlwapi.lib;ntdll.lib;%(AdditionalDependencies)</AdditionalDependencies>
    </Link>
  </ItemDefinitionGroup>

  <!-- DLLs Only -->
  <ItemDefinitionGroup Condition="'$(ConfigurationType)' == 'DynamicLibrary'">
    <ClCompile>
      <PreprocessorDefinitions>_USRDLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
    </ClCompile>
  </ItemDefinitionGroup>

  <!-- Static Libs Only -->
  <ItemDefinitionGroup Condition="'$(ConfigurationType)' == 'StaticLibrary'">
    <ClCompile>
      <PreprocessorDefinitions>_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
    </ClCompile>
  </ItemDefinitionGroup>

  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />

  <!-- Exclude our dependencies from static analysis. CAExcludePath can only be
       set after we've imported Microsoft.Cpp.targets -->
  <PropertyGroup>
    <CAExcludePath>$(SolutionDir)\dep\;$(CAExcludePath)</CAExcludePath>
  </PropertyGroup>

  <Target Name="_ComputePrecompToCleanUp">
    <ItemGroup>
      <PCHFileToClean Include="$(IntDir)\**\*.pch" />
      <PCHFileToClean Include="$(IntDir)\**\precomp.obj" />
      <_PCHFileToCleanWithTimestamp Include="@(PCHFileToClean)" Condition="'%(Identity)' != ''">
        <LastWriteTime>$([System.IO.File]::GetLastWriteTime('%(Identity)'))</LastWriteTime>
      </_PCHFileToCleanWithTimestamp>
    </ItemGroup>
  </Target>
  <!-- Instead of outright deleting the PCHs, we want to trick the "project freshness detector" by
       emitting empty files that look suspiciously like the PCHs it's expecting.
       It's of utmost importance that their timestamps match up. -->
  <Target Name="CleanUpPrecompForSmallCIAgents"
          DependsOnTargets="_ComputePrecompToCleanUp"
          AfterTargets="AfterBuild"
          Condition="'$(OpenConsoleCleanPCH)' == 'true' and '$(AGENT_ID)' != '' and !$(ProjectName.Contains('TerminalApp'))">
    <!-- We just need to keep *TerminalApp*'s PCHs because they get rebuilt more often. -->
    <Delete Files="@(_PCHFileToCleanWithTimestamp)"/>
    <Touch Files="@(_PCHFileToCleanWithTimestamp)" Time="%(LastWriteTime)" AlwaysCreate="true" />
    <Message Text="PCH and Precomp object @(_PCHFileToCleanWithTimestamp) has been deleted for $(ProjectName)." />
  </Target>

  <PropertyGroup>
    <BuildDependsOn>
      OCCallFeatureFlagGenerator;
      $(BuildDependsOn)
    </BuildDependsOn>
  </PropertyGroup>

  <Target Name="OCCallFeatureFlagGenerator">
    <MSBuild Projects="$(SolutionDir)\build\rules\GenerateFeatureFlags.proj" />
  </Target>

  <ItemDefinitionGroup>
    <ClCompile>
      <ForcedIncludeFiles>$(SolutionDir)\bin\$(Configuration)\inc\TilFeatureStaging.h;%(ForcedIncludeFiles)</ForcedIncludeFiles>
    </ClCompile>
  </ItemDefinitionGroup>

  <!-- For C++ projects, bring in the vcpkg targets at the end -->
  <Import Project="$(VcpkgRoot)/scripts/buildsystems/msbuild/vcpkg.targets" Condition="'$(MSBuildProjectExtension)'=='.vcxproj'" />
</Project>
