<!-- Licensed to the .NET Foundation under one or more agreements. The .NET Foundation licenses this file to you under the MIT license. See the License.txt file in the project root for more information. -->
<Project>
  <Import Project="InitializeSourceControlInformation.targets"/>
  
  <UsingTask TaskName="Microsoft.SourceLink.Common.GenerateSourceLinkFile" AssemblyFile="$(_MicrosoftSourceLinkCommonAssemblyFile)"/>

  <Target Name="_SetSourceLinkFilePath">
    <PropertyGroup>
      <_SourceLinkFilePath>$(IntermediateOutputPath)$(MSBuildProjectName).sourcelink.json</_SourceLinkFilePath>
    </PropertyGroup>
  </Target>

  <!--
    Triggers SetEmbeddedFilesFromSourceControlManagerUntrackedFiles target defined by a source control package Microsoft.Build.Tasks.{Git|Tfvc|...}.
    Assumes that all targets that generate source files and add them to Compile items run before BeforeCompile target.
    This is a convention established by common targets.
    
    Disabled for design-time build since calculating untracked files is non-trivial operation
    and embedding them only affects the content of the generated PDB, which has no impact on design-time build.
  -->
  <Target Name="_SetEmbeddedFilesFromSourceControlManagerUntrackedFiles"
          DependsOnTargets="BeforeCompile;SetEmbeddedFilesFromSourceControlManagerUntrackedFiles"
          BeforeTargets="CoreCompile"
          Condition="'$(EmbedUntrackedSources)' == 'true' and '$(EmbedAllSources)' != 'true' and '$(DebugType)' != 'none' and '$(EnableSourceControlManagerQueries)' == 'true' and '$(DesignTimeBuild)' != 'true'" />

  <!--
    If defined populates MappedPath metadata of SourceRoot items.
  -->
  <Target Name="_InitializeSourceRootMappedPathsOpt"
          DependsOnTargets="InitializeSourceRootMappedPaths"
          Condition="'$(SourceRootMappedPathsFeatureSupported)' == 'true'"/>

  <!-- 
    Add compiler targets: C++ generates PDB with SourceLink in Link phase.
  -->
  <PropertyGroup Condition="'$(Language)' == 'C++'">
    <_GenerateSourceLinkFileBeforeTargets>Link</_GenerateSourceLinkFileBeforeTargets>
    <_GenerateSourceLinkFileDependsOnTargets>ComputeLinkSwitches</_GenerateSourceLinkFileDependsOnTargets>
  </PropertyGroup>

  <PropertyGroup Condition="'$(Language)' != 'C++'">
    <_GenerateSourceLinkFileBeforeTargets>CoreCompile</_GenerateSourceLinkFileBeforeTargets>
    <_GenerateSourceLinkFileDependsOnTargets/>
  </PropertyGroup>

  <!--
    Each source control provider package adds its SourceLinkUrl initialization target to SourceLinkUrlInitializerTargets.
    This target shall initialize SourceLinkUrl of all items that don't have it initialized yet and belong to the source control provider.
  -->
  <Target Name="_GenerateSourceLinkFile"
          DependsOnTargets="_SetSourceLinkFilePath;$(_GenerateSourceLinkFileDependsOnTargets);_InitializeSourceRootMappedPathsOpt;$(SourceLinkUrlInitializerTargets)"
          Condition="'$(EnableSourceLink)' == 'true' and '$(DebugType)' != 'none'"
          Outputs="$(_SourceLinkFilePath)">

    <!--- Suppress warning if targets are imported from an SDK without a package reference. -->
    <Microsoft.SourceLink.Common.GenerateSourceLinkFile
      SourceRoots="@(SourceRoot)"
      NoWarnOnMissingSourceControlInformation="$(PkgMicrosoft_SourceLink_Common.Equals(''))"
      OutputFile="$(_SourceLinkFilePath)">

      <!-- Set SourceLink property passed to compilers -->
      <Output TaskParameter="SourceLink" PropertyName="SourceLink" />
    </Microsoft.SourceLink.Common.GenerateSourceLinkFile>

    <!--
      Include the output file whenever it exists, even if it hasn't been written to (it was up-to-date).
      This is needed so that incremental clean doesn't delete the file.
    -->
    <ItemGroup Condition="'$(SourceLink)' != ''">
      <FileWrites Include="$(SourceLink)" />
    </ItemGroup>

    <!-- C++ Link task currently doesn't recognize SourceLink property -->
    <ItemGroup Condition="'$(Language)' == 'C++' and '$(SourceLink)' != ''">
      <Link Update="@(Link)">
        <AdditionalOptions>%(Link.AdditionalOptions) /sourcelink:"$(SourceLink)"</AdditionalOptions>
      </Link>
    </ItemGroup>
  </Target>

  <Target Name="GenerateSourceLinkFile"
          DependsOnTargets="InitializeSourceControlInformation;_GenerateSourceLinkFile"
          Condition="'$(SourceControlInformationFeatureSupported)' == 'true'"
          BeforeTargets="$(_GenerateSourceLinkFileBeforeTargets)"/>
  
</Project>
