-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
AssignProjectConfiguration.cs
572 lines (484 loc) · 21.5 KB
/
AssignProjectConfiguration.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
// 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;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Text;
using System.Xml;
using Microsoft.Build.Framework;
using Microsoft.Build.Shared;
using Microsoft.Build.Utilities;
namespace Microsoft.Build.Tasks
{
public class AssignProjectConfiguration : ResolveProjectBase
{
#region Constructors
/// <summary>
/// default public constructor
/// </summary>
public AssignProjectConfiguration()
{
// do nothing
}
#endregion
#region Properties
/// <summary>
/// A special XML string containing a project configuration for each project - we need to simply
/// match the projects and assign the appropriate configuration names to them
/// </summary>
public string SolutionConfigurationContents
{
get
{
return _solutionConfigurationContents;
}
set
{
_solutionConfigurationContents = value;
}
}
private string _solutionConfigurationContents = null;
/// <summary>
/// Whether to use the solution dependency information passed in the solution blob
/// to add synthetic project references for the purposes of build ordering
/// </summary>
public bool AddSyntheticProjectReferencesForSolutionDependencies
{
get;
set;
}
/// <summary>
/// String containing a semicolon-delimited list of mappings from the platform names used
/// by most VS types to those used by .vcxprojs.
/// </summary>
/// <remarks>
/// E.g. "AnyCPU=Win32"
/// </remarks>
[SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Vcx", Justification = "Public API that has already shipped; VCX is a recognizable short form for .vcxproj")]
public string DefaultToVcxPlatformMapping
{
get
{
if (_defaultToVcxPlatformMapping == null)
{
_defaultToVcxPlatformMapping = "AnyCPU=Win32;X86=Win32;X64=X64;Itanium=Itanium";
}
return _defaultToVcxPlatformMapping;
}
set
{
_defaultToVcxPlatformMapping = value;
if (_defaultToVcxPlatformMapping != null && _defaultToVcxPlatformMapping.Length == 0)
{
_defaultToVcxPlatformMapping = null;
}
}
}
private string _defaultToVcxPlatformMapping = null;
/// <summary>
/// String containing a semicolon-delimited list of mappings from .vcxproj platform names
/// to the platform names use by most other VS project types.
/// </summary>
/// <remarks>
/// E.g. "Win32=AnyCPU"
/// </remarks>
[SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Vcx", Justification = "Public API that has already shipped; VCX is a recognizable short form for .vcxproj")]
public string VcxToDefaultPlatformMapping
{
get
{
if (_vcxToDefaultPlatformMapping == null)
{
if (String.Equals("Library", OutputType, StringComparison.OrdinalIgnoreCase))
{
_vcxToDefaultPlatformMapping = "Win32=AnyCPU;X64=X64;Itanium=Itanium";
}
else
{
_vcxToDefaultPlatformMapping = "Win32=X86;X64=X64;Itanium=Itanium";
}
}
return _vcxToDefaultPlatformMapping;
}
set
{
_vcxToDefaultPlatformMapping = value;
if (_vcxToDefaultPlatformMapping != null && _vcxToDefaultPlatformMapping.Length == 0)
{
_vcxToDefaultPlatformMapping = null;
}
}
}
private string _vcxToDefaultPlatformMapping = null;
/// <summary>
/// The current project's full path
/// </summary>
public string CurrentProject
{
get;
set;
}
/// <summary>
/// The current project's platform.
/// </summary>
public string CurrentProjectConfiguration
{
get
{
return _currentProjectConfiguration;
}
set
{
_currentProjectConfiguration = value;
}
}
private string _currentProjectConfiguration;
/// <summary>
/// The current project's platform.
/// </summary>
public string CurrentProjectPlatform
{
get
{
return _currentProjectPlatform;
}
set
{
_currentProjectPlatform = value;
}
}
/// <summary>
/// Should we build references even if they were disabled in the project configuration
/// </summary>
private bool _onlyReferenceAndBuildProjectsEnabledInSolutionConfiguration = false;
/// <summary>
/// Should we build references even if they were disabled in the project configuration
/// </summary>
public bool OnlyReferenceAndBuildProjectsEnabledInSolutionConfiguration
{
get
{
return _onlyReferenceAndBuildProjectsEnabledInSolutionConfiguration;
}
set
{
_onlyReferenceAndBuildProjectsEnabledInSolutionConfiguration = value;
}
}
// Whether to set the project reference's GlobalPropertiesToRemove metadata to contain
// Configuration and Platform.
private bool _shouldUnsetParentConfigurationAndPlatform = false;
/// <summary>
/// Whether to set the GlobalPropertiesToRemove metadata on the project reference such that
/// on an MSBuild call, the Configuration and Platform metadata will be unset, allowing the
/// child project to build in its default configuration / platform.
/// </summary>
public bool ShouldUnsetParentConfigurationAndPlatform
{
get
{
return _shouldUnsetParentConfigurationAndPlatform;
}
set
{
_shouldUnsetParentConfigurationAndPlatform = value;
}
}
private string _outputType;
/// <summary>
/// The output type for the project
/// </summary>
public string OutputType
{
get
{
return _outputType;
}
set
{
_outputType = value;
}
}
private string _currentProjectPlatform;
/// <summary>
/// True if we should use the default mappings to resolve the configuration/platform
/// of the passed in project references, false otherwise.
/// </summary>
public bool ResolveConfigurationPlatformUsingMappings
{
get
{
return _resolveConfigurationPlatformUsingMappings;
}
set
{
_resolveConfigurationPlatformUsingMappings = value;
}
}
private bool _resolveConfigurationPlatformUsingMappings;
/// <summary>
/// The list of resolved reference paths (preserving the original project reference attributes)
/// </summary>
[Output]
public ITaskItem[] AssignedProjects
{
get
{
return _assignedProjects;
}
set
{
_assignedProjects = value;
}
}
private ITaskItem[] _assignedProjects = null;
/// <summary>
/// The list of project reference items that could not be resolved using the pre-resolved list of outputs.
/// Since VS only pre-resolves non-MSBuild projects, this means that project references in this list
/// are in the MSBuild format.
/// </summary>
[Output]
public ITaskItem[] UnassignedProjects
{
get
{
return _unassignedProjects;
}
set
{
_unassignedProjects = value;
}
}
private ITaskItem[] _unassignedProjects = null;
private const string attrFullConfiguration = "FullConfiguration";
private const string buildReferenceMetadataName = "BuildReference";
private const string referenceOutputAssemblyMetadataName = "ReferenceOutputAssembly";
private const string buildProjectInSolutionAttribute = "BuildProjectInSolution";
private const string attrConfiguration = "Configuration";
private const string attrPlatform = "Platform";
private const string attrSetConfiguration = "SetConfiguration";
private const string attrSetPlatform = "SetPlatform";
private static readonly char[] s_configPlatformSeparator = new char[] { '|' };
private IDictionary<string, string> _vcxToDefaultMap;
private IDictionary<string, string> _defaultToVcxMap;
private bool _mappingsPopulated = false;
#endregion
#region ITask Members
/// <summary>
/// Main task method
/// </summary>
/// <returns></returns>
public override bool Execute()
{
try
{
if (!VerifyProjectReferenceItems(ProjectReferences, true /* treat problems as errors */))
{
return false;
}
ArrayList resolvedReferences = new ArrayList(ProjectReferences.GetLength(0));
ArrayList unresolvedReferences = new ArrayList(ProjectReferences.GetLength(0));
if (!String.IsNullOrEmpty(SolutionConfigurationContents))
{
CacheProjectElementsFromXml(SolutionConfigurationContents);
}
if (AddSyntheticProjectReferencesForSolutionDependencies)
{
// The solution may have had project to project dependencies expressed in it, which were passed in with the blob.
// Add those to the list of project references as if they were regular project references.
AddSyntheticProjectReferences(CurrentProject);
}
foreach (ITaskItem projectRef in ProjectReferences)
{
bool resolveSuccess = false;
ITaskItem resolvedReference;
resolveSuccess = ResolveProject(projectRef, out resolvedReference);
if (resolveSuccess)
{
resolvedReferences.Add(resolvedReference);
Log.LogMessageFromResources(MessageImportance.Low, "AssignProjectConfiguration.ProjectConfigurationResolutionSuccess", projectRef.ItemSpec, resolvedReference.GetMetadata(attrFullConfiguration));
}
else
{
// If the reference was unresolved, we want to undefine the Configuration and Platform
// global properties, so that the project will build using its default Configuration and
// Platform rather than that of its parent.
if (ShouldUnsetParentConfigurationAndPlatform)
{
string globalPropertiesToRemove = projectRef.GetMetadata("GlobalPropertiesToRemove");
if (!String.IsNullOrEmpty(globalPropertiesToRemove))
{
globalPropertiesToRemove += ";";
}
if (projectRef is ITaskItem2)
{
((ITaskItem2)projectRef).SetMetadataValueLiteral("GlobalPropertiesToRemove", globalPropertiesToRemove + "Configuration;Platform");
}
else
{
projectRef.SetMetadata("GlobalPropertiesToRemove", EscapingUtilities.Escape(globalPropertiesToRemove + "Configuration;Platform"));
}
}
unresolvedReferences.Add(projectRef);
// This is not an error - we pass unresolved references to UnresolvedProjectReferences for further
// processing in the .targets file. This means this project was not checked for building in the
// active solution configuration.
Log.LogMessageFromResources(MessageImportance.Low, "AssignProjectConfiguration.ProjectConfigurationUnresolved", projectRef.ItemSpec);
}
}
AssignedProjects = (ITaskItem[])resolvedReferences.ToArray(typeof(ITaskItem));
UnassignedProjects = (ITaskItem[])unresolvedReferences.ToArray(typeof(ITaskItem));
}
catch (XmlException e)
{
Log.LogErrorWithCodeFromResources("General.ErrorExecutingTask", this.GetType().Name, e.Message);
return false;
}
return true;
}
#endregion
#region Methods
/// <summary>
/// Given a project reference task item and an XML document containing project configurations,
/// find the configuration for that task item.
/// </summary>
/// <param name="resolvedPath">
/// resulting ITaskItem containing the resolved project item with the FullConfiguration,
/// Configuration and Platform attributes
/// </param>
/// <returns>true if resolved successfully</returns>
internal bool ResolveProject(ITaskItem projectRef, out ITaskItem resolvedProjectWithConfiguration)
{
XmlElement projectConfigurationElement = null;
string projectConfiguration = null;
if (!String.IsNullOrEmpty(SolutionConfigurationContents))
{
projectConfigurationElement = GetProjectElement(projectRef);
if (projectConfigurationElement != null)
{
projectConfiguration = projectConfigurationElement.InnerText;
}
}
if (projectConfiguration == null && ResolveConfigurationPlatformUsingMappings)
{
if (!_mappingsPopulated)
{
SetupDefaultPlatformMappings();
}
string transformedPlatform = null;
if (String.Equals(projectRef.GetMetadata("Extension"), ".vcxproj", StringComparison.OrdinalIgnoreCase))
{
if (_defaultToVcxMap.TryGetValue(CurrentProjectPlatform, out transformedPlatform))
{
projectConfiguration = CurrentProjectConfiguration + s_configPlatformSeparator[0] + transformedPlatform;
}
}
else
{
if (_vcxToDefaultMap.TryGetValue(CurrentProjectPlatform, out transformedPlatform))
{
projectConfiguration = CurrentProjectConfiguration + s_configPlatformSeparator[0] + transformedPlatform;
}
}
}
SetBuildInProjectAndReferenceOutputAssemblyMetadata(_onlyReferenceAndBuildProjectsEnabledInSolutionConfiguration, projectRef, projectConfigurationElement);
if (projectConfiguration != null)
{
if (!string.IsNullOrEmpty(projectConfiguration))
{
resolvedProjectWithConfiguration = projectRef;
resolvedProjectWithConfiguration.SetMetadata(attrFullConfiguration, projectConfiguration);
string[] configurationPlatformParts = projectConfiguration.Split(s_configPlatformSeparator);
resolvedProjectWithConfiguration.SetMetadata(attrSetConfiguration, "Configuration=" + configurationPlatformParts[0]);
resolvedProjectWithConfiguration.SetMetadata(attrConfiguration, configurationPlatformParts[0]);
if (configurationPlatformParts.Length > 1)
{
resolvedProjectWithConfiguration.SetMetadata(attrSetPlatform, "Platform=" + configurationPlatformParts[1]);
resolvedProjectWithConfiguration.SetMetadata(attrPlatform, configurationPlatformParts[1]);
}
else
{
resolvedProjectWithConfiguration.SetMetadata(attrSetPlatform, "Platform=");
}
return true;
}
}
resolvedProjectWithConfiguration = null;
return false;
}
/// <summary>
/// Given the project configuration blob and the project reference item, set the BuildInProject metadata and the ReferenceOutputAssembly metadata
/// based on the contents of the blob.
/// </summary>
internal static void SetBuildInProjectAndReferenceOutputAssemblyMetadata(bool onlyReferenceAndBuildProjectsEnabledInSolutionConfiguration, ITaskItem resolvedProjectWithConfiguration, XmlElement projectConfigurationElement)
{
if (projectConfigurationElement != null && resolvedProjectWithConfiguration != null && onlyReferenceAndBuildProjectsEnabledInSolutionConfiguration)
{
bool buildProject = false;
// The value of the specified attribute. An empty string is returned if a matching attribute is not found or if the attribute does not have a specified or default value.
string buildProjectInSolution = projectConfigurationElement.GetAttribute(buildProjectInSolutionAttribute);
// We could not parse out what was in the attribute, act as if it was not set in the first place.
if (bool.TryParse(buildProjectInSolution, out buildProject))
{
// If we do not want to build references disabled in the solution configuration blob
// and the solution configuration indicates the build for this project is disabled
// We need to set the BuildReferenceMetadata to false and the ReferenceOutputAssembly to false (if they are not already set to anything)
if (!buildProject)
{
string buildReferenceMetadata = resolvedProjectWithConfiguration.GetMetadata(buildReferenceMetadataName);
string referenceOutputAssemblyMetadata = resolvedProjectWithConfiguration.GetMetadata(referenceOutputAssemblyMetadataName);
if (buildReferenceMetadata.Length == 0)
{
resolvedProjectWithConfiguration.SetMetadata(buildReferenceMetadataName, "false");
}
if (referenceOutputAssemblyMetadata.Length == 0)
{
resolvedProjectWithConfiguration.SetMetadata(referenceOutputAssemblyMetadataName, "false");
}
}
}
}
}
/// <summary>
/// Given the contents of VcxToDefaultPlatformMapping and DefaultToVcxPlatformMapping properties,
/// fill out the maps that will be used to translate between the two.
/// </summary>
private void SetupDefaultPlatformMappings()
{
_vcxToDefaultMap = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
_defaultToVcxMap = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
if (!String.IsNullOrEmpty(VcxToDefaultPlatformMapping))
{
PopulateMappingDictionary(_vcxToDefaultMap, VcxToDefaultPlatformMapping);
}
if (!String.IsNullOrEmpty(DefaultToVcxPlatformMapping))
{
PopulateMappingDictionary(_defaultToVcxMap, DefaultToVcxPlatformMapping);
}
_mappingsPopulated = true;
}
/// <summary>
/// Given a dictionary to populate and a string of the format "a=b;c=d", populate the
/// dictionary with the given pairs.
/// </summary>
private void PopulateMappingDictionary(IDictionary<string, string> map, string mappingList)
{
string[] mappings = mappingList.Split(';');
foreach (string mapping in mappings)
{
string[] platforms = mapping.Split('=');
if (platforms == null || platforms.Length != 2)
{
Log.LogErrorFromResources("AssignProjectConfiguration.IllegalMappingString", mapping.Trim(), mappingList);
}
else
{
map.Add(platforms[0], platforms[1]);
}
}
}
#endregion
}
}