forked from dotnet/msbuild
/
BuildRequestData.cs
308 lines (269 loc) · 16 KB
/
BuildRequestData.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Collections.Generic;
using Microsoft.Build.Collections;
using Microsoft.Build.Evaluation;
using Microsoft.Build.Shared;
namespace Microsoft.Build.Execution
{
/// <summary>
/// Flags providing additional control over the build request
/// </summary>
[Flags]
public enum BuildRequestDataFlags
{
/// <summary>
/// No flags.
/// </summary>
None = 0,
/// <summary>
/// When this flag is present, the existing ProjectInstance in the build will be replaced by this one.
/// </summary>
ReplaceExistingProjectInstance = 1 << 0,
/// <summary>
/// When this flag is present, the <see cref="BuildResult"/> issued in response to this request will
/// include <see cref="BuildResult.ProjectStateAfterBuild"/>.
/// </summary>
ProvideProjectStateAfterBuild = 1 << 1,
/// <summary>
/// When this flag is present and the project has previously been built on a node whose affinity is
/// incompatible with the affinity this request requires, we will ignore the project state (but not
/// target results) that were previously generated.
/// </summary>
/// <remarks>
/// This usually is not desired behavior. It is only provided for those cases where the client
/// knows that the new build request does not depend on project state generated by a previous request. Setting
/// this flag can provide a performance boost in the case of incompatible node affinities, as MSBuild would
/// otherwise have to serialize the project state from one node to another, which may be
/// expensive depending on how much data the project previously generated.
///
/// This flag has no effect on target results, so if a previous request already built a target, the new
/// request will not re-build that target (nor will any of the project state mutations which previously
/// occurred as a consequence of building that target be re-applied.)
/// </remarks>
IgnoreExistingProjectState = 1 << 2,
/// <summary>
/// When this flag is present, caches including the <see cref="ProjectRootElementCacheBase"/> will be cleared
/// after the build request completes. This is used when the build request is known to modify a lot of
/// state such as restoring packages or generating parts of the import graph.
/// </summary>
ClearCachesAfterBuild = 1 << 3,
/// <summary>
/// When this flag is present, the top level target(s) in the build request will be skipped if those targets
/// are not defined in the Project to build. This only applies to this build request (if another target calls
/// the "missing target" at any other point this will still result in an error).
/// </summary>
SkipNonexistentTargets = 1 << 4,
/// <summary>
/// When this flag is present, the <see cref="BuildResult"/> issued in response to this request will
/// include a <see cref="BuildResult.ProjectStateAfterBuild"/> that includes ONLY the
/// explicitly-requested properties, items, and metadata.
/// </summary>
ProvideSubsetOfStateAfterBuild = 1 << 5,
/// <summary>
/// When this flag is present, projects loaded during build will ignore missing imports (<see cref="ProjectLoadSettings.IgnoreMissingImports"/> and <see cref="ProjectLoadSettings.IgnoreInvalidImports"/>).
/// This is especially useful during a restore since some imports might come from packages that haven't been restored yet.
/// </summary>
IgnoreMissingEmptyAndInvalidImports = 1 << 6,
/// <summary>
/// When this flag is present, an unresolved MSBuild project SDK will fail the build. This flag is used to
/// change the <see cref="IgnoreMissingEmptyAndInvalidImports" /> behavior to still fail when an SDK is missing
/// because those are more fatal.
/// </summary>
FailOnUnresolvedSdk = 1 << 7,
}
/// <summary>
/// BuildRequestData encapsulates all of the data needed to submit a build request.
/// </summary>
public class BuildRequestData
{
/// <summary>
/// Constructs a BuildRequestData for build requests based on project instances.
/// </summary>
/// <param name="projectInstance">The instance to build.</param>
/// <param name="targetsToBuild">The targets to build.</param>
public BuildRequestData(ProjectInstance projectInstance, string[] targetsToBuild)
: this(projectInstance, targetsToBuild, null, BuildRequestDataFlags.None)
{
}
/// <summary>
/// Constructs a BuildRequestData for build requests based on project instances.
/// </summary>
/// <param name="projectInstance">The instance to build.</param>
/// <param name="targetsToBuild">The targets to build.</param>
/// <param name="hostServices">The host services to use, if any. May be null.</param>
public BuildRequestData(ProjectInstance projectInstance, string[] targetsToBuild, HostServices hostServices)
: this(projectInstance, targetsToBuild, hostServices, BuildRequestDataFlags.None)
{
}
/// <summary>
/// Constructs a BuildRequestData for build requests based on project instances.
/// </summary>
/// <param name="projectInstance">The instance to build.</param>
/// <param name="targetsToBuild">The targets to build.</param>
/// <param name="hostServices">The host services to use, if any. May be null.</param>
/// <param name="flags">Flags controlling this build request.</param>
public BuildRequestData(ProjectInstance projectInstance, string[] targetsToBuild, HostServices hostServices, BuildRequestDataFlags flags)
: this(projectInstance, targetsToBuild, hostServices, flags, null)
{
}
/// <summary>
/// Constructs a BuildRequestData for build requests based on project instances.
/// </summary>
/// <param name="projectInstance">The instance to build.</param>
/// <param name="targetsToBuild">The targets to build.</param>
/// <param name="hostServices">The host services to use, if any. May be null.</param>
/// <param name="flags">Flags controlling this build request.</param>
/// <param name="propertiesToTransfer">The list of properties whose values should be transferred from the project to any out-of-proc node.</param>
public BuildRequestData(ProjectInstance projectInstance, string[] targetsToBuild, HostServices hostServices, BuildRequestDataFlags flags, IEnumerable<string> propertiesToTransfer)
: this(targetsToBuild, hostServices, flags)
{
ErrorUtilities.VerifyThrowArgumentNull(projectInstance, nameof(projectInstance));
foreach (string targetName in targetsToBuild)
{
ErrorUtilities.VerifyThrowArgumentNull(targetName, "target");
}
ProjectInstance = projectInstance;
ProjectFullPath = projectInstance.FullPath;
GlobalPropertiesDictionary = projectInstance.GlobalPropertiesDictionary;
ExplicitlySpecifiedToolsVersion = projectInstance.ExplicitToolsVersion;
if (propertiesToTransfer != null)
{
PropertiesToTransfer = new List<string>(propertiesToTransfer);
}
}
/// <summary>
/// Constructs a BuildRequestData for build requests based on project instances.
/// </summary>
/// <param name="projectInstance">The instance to build.</param>
/// <param name="targetsToBuild">The targets to build.</param>
/// <param name="hostServices">The host services to use, if any. May be null.</param>
/// <param name="flags">Flags controlling this build request.</param>
/// <param name="propertiesToTransfer">The list of properties whose values should be transferred from the project to any out-of-proc node.</param>
/// <param name="requestedProjectState">A <see cref="Execution.RequestedProjectState"/> describing properties, items, and metadata that should be returned. Requires setting <see cref="BuildRequestDataFlags.ProvideSubsetOfStateAfterBuild"/>.</param>
public BuildRequestData(ProjectInstance projectInstance, string[] targetsToBuild, HostServices hostServices, BuildRequestDataFlags flags, IEnumerable<string> propertiesToTransfer, RequestedProjectState requestedProjectState)
: this(projectInstance, targetsToBuild, hostServices, flags, propertiesToTransfer)
{
ErrorUtilities.VerifyThrowArgumentNull(requestedProjectState, nameof(requestedProjectState));
RequestedProjectState = requestedProjectState;
}
/// <summary>
/// Constructs a BuildRequestData for build requests based on project files.
/// </summary>
/// <param name="projectFullPath">The full path to the project file.</param>
/// <param name="globalProperties">The global properties which should be used during evaluation of the project. Cannot be null.</param>
/// <param name="toolsVersion">The tools version to use for the build. May be null.</param>
/// <param name="targetsToBuild">The targets to build.</param>
/// <param name="hostServices">The host services to use. May be null.</param>
public BuildRequestData(string projectFullPath, IDictionary<string, string> globalProperties, string toolsVersion, string[] targetsToBuild, HostServices hostServices)
: this(projectFullPath, globalProperties, toolsVersion, targetsToBuild, hostServices, BuildRequestDataFlags.None)
{
}
/// <summary>
/// Constructs a BuildRequestData for build requests based on project files.
/// </summary>
/// <param name="projectFullPath">The full path to the project file.</param>
/// <param name="globalProperties">The global properties which should be used during evaluation of the project. Cannot be null.</param>
/// <param name="toolsVersion">The tools version to use for the build. May be null.</param>
/// <param name="targetsToBuild">The targets to build.</param>
/// <param name="hostServices">The host services to use. May be null.</param>
/// <param name="flags">The <see cref="BuildRequestDataFlags"/> to use.</param>
/// <param name="requestedProjectState">A <see cref="Execution.RequestedProjectState"/> describing properties, items, and metadata that should be returned. Requires setting <see cref="BuildRequestDataFlags.ProvideSubsetOfStateAfterBuild"/>.</param>
public BuildRequestData(string projectFullPath, IDictionary<string, string> globalProperties,
string toolsVersion, string[] targetsToBuild, HostServices hostServices, BuildRequestDataFlags flags,
RequestedProjectState requestedProjectState)
: this(projectFullPath, globalProperties, toolsVersion, targetsToBuild, hostServices, flags)
{
ErrorUtilities.VerifyThrowArgumentNull(requestedProjectState, nameof(requestedProjectState));
RequestedProjectState = requestedProjectState;
}
/// <summary>
/// Constructs a BuildRequestData for build requests based on project files.
/// </summary>
/// <param name="projectFullPath">The full path to the project file.</param>
/// <param name="globalProperties">The global properties which should be used during evaluation of the project. Cannot be null.</param>
/// <param name="toolsVersion">The tools version to use for the build. May be null.</param>
/// <param name="targetsToBuild">The targets to build.</param>
/// <param name="hostServices">The host services to use. May be null.</param>
/// <param name="flags">The <see cref="BuildRequestDataFlags"/> to use.</param>
public BuildRequestData(string projectFullPath, IDictionary<string, string> globalProperties, string toolsVersion, string[] targetsToBuild, HostServices hostServices, BuildRequestDataFlags flags)
: this(targetsToBuild, hostServices, flags)
{
ErrorUtilities.VerifyThrowArgumentLength(projectFullPath, nameof(projectFullPath));
ErrorUtilities.VerifyThrowArgumentNull(globalProperties, nameof(globalProperties));
ProjectFullPath = FileUtilities.NormalizePath(projectFullPath);
TargetNames = (ICollection<string>)targetsToBuild.Clone();
GlobalPropertiesDictionary = new PropertyDictionary<ProjectPropertyInstance>(globalProperties.Count);
foreach (KeyValuePair<string, string> propertyPair in globalProperties)
{
GlobalPropertiesDictionary.Set(ProjectPropertyInstance.Create(propertyPair.Key, propertyPair.Value));
}
ExplicitlySpecifiedToolsVersion = toolsVersion;
}
/// <summary>
/// Common constructor.
/// </summary>
private BuildRequestData(string[] targetsToBuild, HostServices hostServices, BuildRequestDataFlags flags)
{
ErrorUtilities.VerifyThrowArgumentNull(targetsToBuild, nameof(targetsToBuild));
HostServices = hostServices;
TargetNames = new List<string>(targetsToBuild);
Flags = flags;
}
/// <summary>
/// The actual project, in the case where the project doesn't come from disk.
/// May be null.
/// </summary>
/// <value>The project instance.</value>
public ProjectInstance ProjectInstance
{
get;
}
/// <summary>The project file.</summary>
/// <value>The project file to be built.</value>
public string ProjectFullPath { get; internal set; }
/// <summary>
/// The name of the targets to build.
/// </summary>
/// <value>An array of targets in the project to be built.</value>
public ICollection<string> TargetNames { get; }
/// <summary>
/// Extra flags for this BuildRequest.
/// </summary>
public BuildRequestDataFlags Flags { get; }
/// <summary>
/// The global properties to use.
/// </summary>
/// <value>The set of global properties to be used to build this request.</value>
public ICollection<ProjectPropertyInstance> GlobalProperties => (GlobalPropertiesDictionary == null) ?
(ICollection<ProjectPropertyInstance>)ReadOnlyEmptyCollection<ProjectPropertyInstance>.Instance :
new ReadOnlyCollection<ProjectPropertyInstance>(GlobalPropertiesDictionary);
/// <summary>
/// The explicitly requested tools version to use.
/// </summary>
public string ExplicitlySpecifiedToolsVersion { get; }
/// <summary>
/// Gets the HostServices object for this request.
/// </summary>
public HostServices HostServices { get; }
/// <summary>
/// Returns a list of properties to transfer out of proc for the build.
/// </summary>
public IEnumerable<string> PropertiesToTransfer { get; }
/// <summary>
/// Returns the properties, items, and metadata that will be returned
/// by this build.
/// </summary>
public RequestedProjectState RequestedProjectState { get; }
/// <summary>
/// Whether the tools version used originated from an explicit specification,
/// for example from an MSBuild task or /tv switch.
/// </summary>
internal bool ExplicitToolsVersionSpecified => ExplicitlySpecifiedToolsVersion != null;
/// <summary>
/// Returns the global properties as a dictionary.
/// </summary>
internal PropertyDictionary<ProjectPropertyInstance> GlobalPropertiesDictionary { get; }
}
}