Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new TargetBuiltReasons #10092

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
26 changes: 24 additions & 2 deletions src/Build/BackEnd/Components/RequestBuilder/TargetBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,21 @@ public async Task<BuildResult> BuildTargets(ProjectLoggingContext loggingContext
}
else
{
targets.Add(new TargetSpecification(targetName, targetExists ? targetInstance.Location : _projectInstance.ProjectFileLocation));
TargetBuiltReason buildReason = TargetBuiltReason.None;
if (entry.Request.Targets.Contains(targetName))
{
buildReason = TargetBuiltReason.EntryTarget;
}
else if (configuration.ProjectInitialTargets.Contains(targetName))
{
buildReason = TargetBuiltReason.InitialTarget;
}
else if (configuration.ProjectDefaultTargets.Contains(targetName))
{
buildReason = TargetBuiltReason.DefaultTarget;
}

targets.Add(new TargetSpecification(targetName, targetExists ? targetInstance.Location : _projectInstance.ProjectFileLocation, buildReason));
}
}

Expand Down Expand Up @@ -737,7 +751,15 @@ private async Task<bool> PushTargets(IList<TargetSpecification> targets, TargetE

// Add to the list of targets to push. We don't actually put it on the stack here because we could run into a circular dependency
// during this loop, in which case the target stack would be out of whack.
TargetEntry newEntry = new TargetEntry(_requestEntry, this as ITargetBuilderCallback, targetSpecification, baseLookup, parentTargetEntry, buildReason, _componentHost, stopProcessingOnCompletion);
TargetEntry newEntry;
if (buildReason == TargetBuiltReason.None)
{
newEntry = new TargetEntry(_requestEntry, this as ITargetBuilderCallback, targetSpecification, baseLookup, parentTargetEntry, targetSpecification._targetBuiltReason, _componentHost, stopProcessingOnCompletion);
maridematte marked this conversation as resolved.
Show resolved Hide resolved
}
else
{
newEntry = new TargetEntry(_requestEntry, this as ITargetBuilderCallback, targetSpecification, baseLookup, parentTargetEntry, buildReason, _componentHost, stopProcessingOnCompletion);
}
newEntry.ErrorTarget = addAsErrorTarget;
targetsToPush.Add(newEntry);
stopProcessingOnCompletion = false; // The first target on the stack (the last one to be run) always inherits the stopProcessing flag.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics;
using Microsoft.Build.Framework;
using Microsoft.Build.Shared;
using ElementLocation = Microsoft.Build.Construction.ElementLocation;

Expand All @@ -18,18 +19,22 @@ internal class TargetSpecification : ITranslatable
private string _targetName;
private ElementLocation _referenceLocation;

internal TargetBuiltReason _targetBuiltReason;

/// <summary>
/// Construct a target specification.
/// </summary>
/// <param name="targetName">The name of the target</param>
/// <param name="referenceLocation">The location from which it was referred.</param>
internal TargetSpecification(string targetName, ElementLocation referenceLocation)
/// <param name="targetBuiltReason">Reason the target is being built</param>
internal TargetSpecification(string targetName, ElementLocation referenceLocation, TargetBuiltReason targetBuiltReason = TargetBuiltReason.None)
{
ErrorUtilities.VerifyThrowArgumentLength(targetName, nameof(targetName));
ErrorUtilities.VerifyThrowArgumentNull(referenceLocation, nameof(referenceLocation));

this._targetName = targetName;
this._referenceLocation = referenceLocation;
this._targetBuiltReason = targetBuiltReason;
}

private TargetSpecification()
Expand All @@ -41,6 +46,8 @@ private TargetSpecification()
/// </summary>
public string TargetName => _targetName;

public TargetBuiltReason TargetBuiltReason => _targetBuiltReason;

/// <summary>
/// Gets or sets the reference location
/// </summary>
Expand Down
18 changes: 17 additions & 1 deletion src/Framework/TargetBuiltReason.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,22 @@ public enum TargetBuiltReason
/// <summary>
/// The target was part of the parent's AfterTargets list.
/// </summary>
AfterTargets
AfterTargets,

/// <summary>
/// The target was defined as an initial target of the project.
/// </summary>
InitialTarget,

maridematte marked this conversation as resolved.
Show resolved Hide resolved

/// <summary>
/// The target was the default target of the project
/// </summary>
DefaultTarget,
maridematte marked this conversation as resolved.
Show resolved Hide resolved

/// <summary>
/// The target was the target explicitly called to be built.
/// </summary>
EntryTarget
}
}