-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathReferenceTable.cs
3155 lines (2778 loc) · 150 KB
/
ReferenceTable.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
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// 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.Concurrent;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Runtime.Versioning;
using Microsoft.Build.Framework;
using Microsoft.Build.Shared;
using Microsoft.Build.Tasks.AssemblyDependency;
using Microsoft.Build.Utilities;
#if (!STANDALONEBUILD)
using Microsoft.Internal.Performance;
#endif
using FrameworkNameVersioning = System.Runtime.Versioning.FrameworkName;
using SystemProcessorArchitecture = System.Reflection.ProcessorArchitecture;
namespace Microsoft.Build.Tasks
{
/// <summary>
/// A table of references.
/// </summary>
sealed internal class ReferenceTable
{
/// <summary>version 4.0</summary>
private static readonly Version s_targetFrameworkVersion_40 = new Version("4.0");
/// <summary>
/// A mapping of a framework identifier to the most current redist list on the system based on the target framework identifier on the moniker.
/// This is used to determine if an assembly is in a redist list for the framework targeted by the moniker.
/// </summary>
private static Dictionary<string, Tuple<RedistList, string>> s_monikerToHighestRedistList = new Dictionary<string, Tuple<RedistList, string>>(StringComparer.OrdinalIgnoreCase);
/// <summary>
/// The table of references.
/// Key is assemblyName;
/// Value is Reference.
/// </summary>
private Dictionary<AssemblyNameExtension, Reference> _references = new Dictionary<AssemblyNameExtension, Reference>(AssemblyNameComparer.GenericComparer);
/// <summary>
/// Reference simple names that were resolved by an external entity to RAR.
/// </summary>
private HashSet<string> _externallyResolvedPrimaryReferences = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
/// <summary>The table of remapped assemblies. Used for Unification.</summary>
private DependentAssembly[] _remappedAssemblies = Array.Empty<DependentAssembly>();
/// <summary>If true, then search for dependencies.</summary>
private bool _findDependencies = true;
/// <summary>
/// Should version be ignored for framework primary references
/// </summary>
private bool _ignoreVersionForFrameworkReferences = false;
/// <summary>If true, then search for satellite files.</summary>
private bool _findSatellites = true;
/// <summary>If true, then search for serialization assembly files.</summary>
private bool _findSerializationAssemblies = true;
/// <summary>If true, then search for related files.</summary>
private bool _findRelatedFiles = true;
/// <summary>
/// If true, then force framework assembly version check against the target framework version
/// If false, the default behavior is to disable version checks for target framework versions 4.5 and above.
/// </summary>
private bool _checkAssemblyVersionAgainstTargetFrameworkVersion = false;
/// <summary>Path to the FX.</summary>
private string[] _frameworkPaths;
/// <summary>The allowed assembly extensions.</summary>
private string[] _allowedAssemblyExtensions;
/// <summary>These are companion files that typically travel with assemblies</summary>
private string[] _relatedFileExtensions;
/// <summary>
/// Locations where sdks are installed. K:SDKName v: Resolved Reference item
/// </summary>
private Dictionary<string, ITaskItem> _resolvedSDKReferences;
/// <summary>Path to installed assembly XML tables.</summary>
private InstalledAssemblies _installedAssemblies;
/// <summary>Like x86 or IA64\AMD64, the processor architecture being targetted.</summary>
private SystemProcessorArchitecture _targetProcessorArchitecture;
/// <summary>Delegate used for checking for the existence of a file.</summary>
private FileExists _fileExists;
/// <summary>Delegate used for checking for the existence of a directory.</summary>
private DirectoryExists _directoryExists;
/// <summary>Delegate used for getting directories.</summary>
private GetDirectories _getDirectories;
/// <summary>Delegate used for getting assembly names.</summary>
private GetAssemblyName _getAssemblyName;
/// <summary>Delegate used for finding dependencies of a file.</summary>
private GetAssemblyMetadata _getAssemblyMetadata;
/// <summary>Delegate used to get the image runtime version of a file</summary>
private GetAssemblyRuntimeVersion _getRuntimeVersion;
#if FEATURE_WIN32_REGISTRY
/// <summary> Delegate to get the base registry key for AssemblyFoldersEx</summary>
private OpenBaseKey _openBaseKey;
#endif
/// <summary>Version of the runtime we are targeting</summary>
private Version _targetedRuntimeVersion = null;
/// <summary>
/// Delegate used to get the machineType from the PE header of the dll.
/// </summary>
private ReadMachineTypeFromPEHeader _readMachineTypeFromPEHeader;
/// <summary>
/// Is the file a winMD file
/// </summary>
private IsWinMDFile _isWinMDFile;
/// <summary>version of the framework targeted by this project</summary>
private Version _projectTargetFramework;
/// <summary>
/// Target framework moniker we are targeting.
/// </summary>
private FrameworkNameVersioning _targetFrameworkMoniker;
/// <summary>
/// Searchpaths compiled into an array of resolvers.
/// </summary>
private Resolver[] _compiledSearchPaths;
/// <summary>
/// Logging helper to allow the logging of meessages from the Reference Table
/// </summary>
private TaskLoggingHelper _log;
/// <summary>
/// List of framework directories which are the highest on the machine
/// </summary>
private string[] _latestTargetFrameworkDirectories;
/// <summary>
/// List of assemblies which have been excluded from being referenced.
/// </summary>
private List<string> _listOfExcludedAssemblies = null;
/// <summary>
/// Should dependencies be set to copy local if the parent reference is in the GAC
/// </summary>
private bool _copyLocalDependenciesWhenParentReferenceInGac;
private bool _doNotCopyLocalIfInGac;
/// <summary>
/// Shoould the framework attribute version mismatch be ignored.
/// </summary>
private bool _ignoreFrameworkAttributeVersionMismatch;
/// <summary>
/// Delegate to determine if an assembly name is in the GAC.
/// </summary>
private GetAssemblyPathInGac _getAssemblyPathInGac;
/// <summary>
/// Build engine
/// </summary>
private IBuildEngine _buildEngine;
/// <summary>
/// Should a warning or error be emitted on architecture mismatch
/// </summary>
private WarnOrErrorOnTargetArchitectureMismatchBehavior _warnOrErrorOnTargetArchitectureMismatch = WarnOrErrorOnTargetArchitectureMismatchBehavior.Warning;
private readonly ConcurrentDictionary<string, AssemblyMetadata> _assemblyMetadataCache;
/// <summary>
/// When we exclude an assembly from resolution because it is part of out exclusion list we need to let the user know why this is.
/// There can be a number of reasons each for un-resolving a reference, these reasons are encapsulated by a different black list. We need to log a specific message
/// depending on which black list we have found the offending assembly in. This delegate allows one to tie a set of logging messages to a black list so that when we
/// discover an assembly in the black list we can log the correct message.
/// </summary>
internal delegate void LogExclusionReason(bool displayPrimaryReferenceMessage, AssemblyNameExtension assemblyName, Reference reference, ITaskItem referenceItem, string targetedFramework);
// Offset to the PE header
private const int PEOFFSET = 0x3c;
// PEHeader
private const int PEHEADER = 0x00004550;
/// <summary>
/// Construct.
/// </summary>
/// <param name="findDependencies">If true, then search for dependencies.</param>
/// <param name="findSatellites">If true, then search for satellite files.</param>
/// <param name="findSerializationAssemblies">If true, then search for serialization assembly files.</param>
/// <param name="findRelatedFiles">If true, then search for related files.</param>
/// <param name="searchPaths">Paths to search for dependent assemblies on.</param>
/// <param name="candidateAssemblyFiles">List of literal assembly file names to be considered when SearchPaths has {CandidateAssemblyFiles}.</param>
/// <param name="resolvedSDKItems">Resolved sdk items</param>
/// <param name="frameworkPaths">Path to the FX.</param>
/// <param name="installedAssemblies">Installed assembly XML tables.</param>
/// <param name="targetProcessorArchitecture">Like x86 or IA64\AMD64, the processor architecture being targetted.</param>
/// <param name="fileExists">Delegate used for checking for the existence of a file.</param>
/// <param name="directoryExists">Delegate used for files.</param>
/// <param name="getDirectories">Delegate used for getting directories.</param>
/// <param name="getAssemblyName">Delegate used for getting assembly names.</param>
/// <param name="getAssemblyMetadata">Delegate used for finding dependencies of a file.</param>
/// <param name="getRegistrySubKeyNames">Used to get registry subkey names.</param>
/// <param name="getRegistrySubKeyDefaultValue">Used to get registry default values.</param>
/// <param name="assemblyMetadataCache">Cache of metadata already read from paths.</param>
internal ReferenceTable
(
IBuildEngine buildEngine,
bool findDependencies,
bool findSatellites,
bool findSerializationAssemblies,
bool findRelatedFiles,
string[] searchPaths,
string[] allowedAssemblyExtensions,
string[] relatedFileExtensions,
string[] candidateAssemblyFiles,
ITaskItem[] resolvedSDKItems,
string[] frameworkPaths,
InstalledAssemblies installedAssemblies,
System.Reflection.ProcessorArchitecture targetProcessorArchitecture,
FileExists fileExists,
DirectoryExists directoryExists,
GetDirectories getDirectories,
GetAssemblyName getAssemblyName,
GetAssemblyMetadata getAssemblyMetadata,
#if FEATURE_WIN32_REGISTRY
GetRegistrySubKeyNames getRegistrySubKeyNames,
GetRegistrySubKeyDefaultValue getRegistrySubKeyDefaultValue,
OpenBaseKey openBaseKey,
#endif
GetAssemblyRuntimeVersion getRuntimeVersion,
Version targetedRuntimeVersion,
Version projectTargetFramework,
FrameworkNameVersioning targetFrameworkMoniker,
TaskLoggingHelper log,
string[] latestTargetFrameworkDirectories,
bool copyLocalDependenciesWhenParentReferenceInGac,
bool doNotCopyLocalIfInGac,
GetAssemblyPathInGac getAssemblyPathInGac,
IsWinMDFile isWinMDFile,
bool ignoreVersionForFrameworkReferences,
ReadMachineTypeFromPEHeader readMachineTypeFromPEHeader,
WarnOrErrorOnTargetArchitectureMismatchBehavior warnOrErrorOnTargetArchitectureMismatch,
bool ignoreFrameworkAttributeVersionMismatch,
bool unresolveFrameworkAssembliesFromHigherFrameworks,
ConcurrentDictionary<string, AssemblyMetadata> assemblyMetadataCache)
{
_buildEngine = buildEngine;
_log = log;
_findDependencies = findDependencies;
_findSatellites = findSatellites;
_findSerializationAssemblies = findSerializationAssemblies;
_findRelatedFiles = findRelatedFiles;
_frameworkPaths = frameworkPaths;
_allowedAssemblyExtensions = allowedAssemblyExtensions;
_relatedFileExtensions = relatedFileExtensions;
_installedAssemblies = installedAssemblies;
_targetProcessorArchitecture = targetProcessorArchitecture;
_fileExists = fileExists;
_directoryExists = directoryExists;
_getDirectories = getDirectories;
_getAssemblyName = getAssemblyName;
_getAssemblyMetadata = getAssemblyMetadata;
_getRuntimeVersion = getRuntimeVersion;
_projectTargetFramework = projectTargetFramework;
_targetedRuntimeVersion = targetedRuntimeVersion;
#if FEATURE_WIN32_REGISTRY
_openBaseKey = openBaseKey;
#endif
_targetFrameworkMoniker = targetFrameworkMoniker;
_latestTargetFrameworkDirectories = latestTargetFrameworkDirectories;
_copyLocalDependenciesWhenParentReferenceInGac = copyLocalDependenciesWhenParentReferenceInGac;
_doNotCopyLocalIfInGac = doNotCopyLocalIfInGac;
_getAssemblyPathInGac = getAssemblyPathInGac;
_isWinMDFile = isWinMDFile;
_readMachineTypeFromPEHeader = readMachineTypeFromPEHeader;
_warnOrErrorOnTargetArchitectureMismatch = warnOrErrorOnTargetArchitectureMismatch;
_ignoreFrameworkAttributeVersionMismatch = ignoreFrameworkAttributeVersionMismatch;
_assemblyMetadataCache = assemblyMetadataCache;
// Set condition for when to check assembly version against the target framework version
_checkAssemblyVersionAgainstTargetFrameworkVersion = unresolveFrameworkAssembliesFromHigherFrameworks || ((_projectTargetFramework ?? ReferenceTable.s_targetFrameworkVersion_40) <= ReferenceTable.s_targetFrameworkVersion_40);
// Convert the list of installed SDK's to a dictionary for faster lookup
_resolvedSDKReferences = new Dictionary<string, ITaskItem>(StringComparer.OrdinalIgnoreCase);
_ignoreVersionForFrameworkReferences = ignoreVersionForFrameworkReferences;
if (resolvedSDKItems != null)
{
foreach (ITaskItem resolvedSDK in resolvedSDKItems)
{
string sdkName = resolvedSDK.GetMetadata("SDKName");
if (sdkName.Length > 0)
{
if (!_resolvedSDKReferences.ContainsKey(sdkName))
{
_resolvedSDKReferences.Add(sdkName, resolvedSDK);
}
else
{
_resolvedSDKReferences[sdkName] = resolvedSDK;
}
}
}
}
// Compile searchpaths into fast resolver array.
_compiledSearchPaths = AssemblyResolution.CompileSearchPaths
(
buildEngine,
searchPaths,
candidateAssemblyFiles,
targetProcessorArchitecture,
frameworkPaths,
fileExists,
getAssemblyName,
#if FEATURE_WIN32_REGISTRY
getRegistrySubKeyNames,
getRegistrySubKeyDefaultValue,
openBaseKey,
#endif
installedAssemblies,
getRuntimeVersion,
targetedRuntimeVersion,
getAssemblyPathInGac,
log
);
}
/// <summary>
/// Set of resolvers the reference table uses.
/// </summary>
internal Resolver[] Resolvers
{
get { return _compiledSearchPaths; }
}
/// <summary>
/// Get a table of all vertices.
/// </summary>
/// <returns></returns>
internal Dictionary<AssemblyNameExtension, Reference> References
{
get
{
return _references;
}
}
/// <summary>
/// If assemblies have been marked for exclusion this contains the list of their full names
/// This may be null
/// </summary>
internal List<string> ListOfExcludedAssemblies
{
get
{
return _listOfExcludedAssemblies;
}
}
/// <summary>
/// Indicates that at least one reference was <see cref="Reference.ExternallyResolved"/> and
/// we skipped finding its dependencies as a result.
/// </summary>
/// <remarks>
/// This is currently used to perform a shallow search for System.Runtime/netstandard usage
/// within the externally resolved graph.
/// </remarks>
internal bool SkippedFindingExternallyResolvedDependencies { get; private set; }
/// <summary>
/// Force dependencies to be walked even when a reference is marked with ExternallyResolved=true
/// metadata.
/// </summary>
/// <remarks>
/// This is currently used to ensure that we suggest appropriate binding redirects for
/// assembly version conflicts within an externally resolved graph.
/// </remarks>
internal bool FindDependenciesOfExternallyResolvedReferences { get; set; }
/// <summary>
/// Adds a reference to the table.
/// </summary>
/// <param name="assemblyName">The assembly name to be used as a key.</param>
/// <param name="reference">The reference to add.</param>
internal void AddReference(AssemblyNameExtension assemblyName, Reference reference)
{
ErrorUtilities.VerifyThrow(assemblyName.Name != null, "Got an empty assembly name.");
if (_references.ContainsKey(assemblyName))
{
Reference referenceGoingToBeReplaced = _references[assemblyName];
foreach (AssemblyRemapping pair in referenceGoingToBeReplaced.RemappedAssemblyNames())
{
reference.AddRemapping(pair.From, pair.To);
}
}
_references[assemblyName] = reference;
}
/// <summary>
/// Find the reference that corresponds to the given path.
/// </summary>
/// <param name="assemblyName">The assembly name to find the reference for.</param>
/// <returns>'null' if no reference existed.</returns>
internal Reference GetReference(AssemblyNameExtension assemblyName)
{
ErrorUtilities.VerifyThrow(assemblyName.Name != null, "Got an empty assembly name.");
Reference referenceToReturn = null;
_references.TryGetValue(assemblyName, out referenceToReturn);
return referenceToReturn;
}
/// <summary>
/// Give an assembly file name, adjust a Reference to match it.
/// </summary>
/// <param name="reference">The reference to work on</param>
/// <param name="assemblyFileName">The path to the assembly file.</param>
/// <returns>The AssemblyName of assemblyFileName</returns>
private AssemblyNameExtension NameAssemblyFileReference
(
Reference reference,
string assemblyFileName
)
{
AssemblyNameExtension assemblyName = null;
if (!Path.IsPathRooted(assemblyFileName))
{
reference.FullPath = Path.GetFullPath(assemblyFileName);
}
else
{
reference.FullPath = assemblyFileName;
}
try
{
if (_directoryExists(assemblyFileName))
{
assemblyName = new AssemblyNameExtension("*directory*");
reference.AddError
(
new ReferenceResolutionException
(
ResourceUtilities.FormatResourceString("General.ExpectedFileGotDirectory", reference.FullPath),
null
)
);
reference.FullPath = String.Empty;
}
else
{
if (_fileExists(assemblyFileName))
{
assemblyName = _getAssemblyName(assemblyFileName);
if (assemblyName != null)
{
reference.ResolvedSearchPath = assemblyFileName;
}
}
if (assemblyName == null)
{
reference.AddError
(
new DependencyResolutionException(ResourceUtilities.FormatResourceString("General.ExpectedFileMissing", reference.FullPath), null)
);
}
}
}
catch (System.BadImageFormatException e)
{
reference.AddError(new DependencyResolutionException(e.Message, e));
}
catch (UnauthorizedAccessException e)
{
// If this isn't a valid assembly, then record the exception and continue on
reference.AddError(new DependencyResolutionException(e.Message, e));
}
// If couldn't resolve the assemly name then just use the simple name extracted from
// the file name.
if (assemblyName == null)
{
string simpleName = Path.GetFileNameWithoutExtension(assemblyFileName);
assemblyName = new AssemblyNameExtension(simpleName);
}
return assemblyName;
}
/// <summary>
/// Given a list of task items, add them all to this table and make them the only primary items.
/// </summary>
/// <param name="referenceAssemblyFiles">The task items which contain file names to add.</param>
/// <param name="referenceAssemblyNames">The task items which contain fusion names to add.</param>
/// <param name="exceptions">Exceptions encountered while setting primary items. Exceptions are logged, but it doesn't stop the resolution process.</param>
private void SetPrimaryItems
(
ITaskItem[] referenceAssemblyFiles,
ITaskItem[] referenceAssemblyNames,
ArrayList exceptions
)
{
// Loop over the referenceAssemblyFiles provided and add each one that doesn't exist.
// Set the primary flag to 'true'.
if (referenceAssemblyFiles != null)
{
for (int i = 0; i < referenceAssemblyFiles.Length; ++i)
{
SetPrimaryFileItem(referenceAssemblyFiles[i]);
}
}
// Loop over the referenceAssemblyNames provided and add each one that doesn't exist.
// Set the primary flag to 'true'.
if (referenceAssemblyNames != null)
{
for (int i = 0; i < referenceAssemblyNames.Length; ++i)
{
Exception e = SetPrimaryAssemblyReferenceItem(referenceAssemblyNames[i]);
if (e != null)
{
exceptions.Add(e);
}
}
}
}
/// <summary>
/// Given an item that refers to a assembly name, make it a primary reference.
/// </summary>
/// <param name="referenceAssemblyNames">The task item which contain fusion names to add.</param>
/// <returns>Resulting exception containing resolution failure details, if any: too costly to throw it.</returns>
private Exception SetPrimaryAssemblyReferenceItem
(
ITaskItem referenceAssemblyName
)
{
// Get the desired executable extension.
string executableExtension = referenceAssemblyName.GetMetadata(ItemMetadataNames.executableExtension);
// Get the assembly name, if possible.
string rawFileNameCandidate = referenceAssemblyName.ItemSpec;
AssemblyNameExtension assemblyName = null;
string itemSpec = referenceAssemblyName.ItemSpec;
string fusionName = referenceAssemblyName.GetMetadata(ItemMetadataNames.fusionName);
bool metadataFound = false;
bool result = MetadataConversionUtilities.TryConvertItemMetadataToBool(referenceAssemblyName, ItemMetadataNames.IgnoreVersionForFrameworkReference, out metadataFound);
bool ignoreVersionForFrameworkReference = false;
if (metadataFound)
{
ignoreVersionForFrameworkReference = result;
}
else
{
ignoreVersionForFrameworkReference = _ignoreVersionForFrameworkReferences;
}
TryConvertToAssemblyName(itemSpec, fusionName, ref assemblyName);
// Figure out the specific version value.
bool foundSpecificVersionMetadata = false;
bool wantSpecificVersion = MetadataConversionUtilities.TryConvertItemMetadataToBool(referenceAssemblyName, ItemMetadataNames.specificVersion, out foundSpecificVersionMetadata);
bool isSimpleName = (assemblyName != null && assemblyName.IsSimpleName);
// Create the reference.
Reference reference = new Reference(_isWinMDFile, _fileExists, _getRuntimeVersion);
reference.MakePrimaryAssemblyReference(referenceAssemblyName, wantSpecificVersion, executableExtension);
// Escape simple names.
// 1) If the itemSpec for the task is already a simple name
// 2) We have found the metadata and it is specifically set to false
if (assemblyName != null && (isSimpleName || (foundSpecificVersionMetadata && !wantSpecificVersion)))
{
assemblyName = new AssemblyNameExtension
(
AssemblyNameExtension.EscapeDisplayNameCharacters(assemblyName.Name)
);
isSimpleName = assemblyName.IsSimpleName;
}
// Set the HintPath if there is one.
reference.HintPath = referenceAssemblyName.GetMetadata(ItemMetadataNames.hintPath);
if (assemblyName != null && !wantSpecificVersion && !isSimpleName && reference.HintPath.Length == 0)
{
// Check to see if the assemblyname is in the framework list just use that fusion name
if (_installedAssemblies != null && ignoreVersionForFrameworkReference)
{
AssemblyEntry entry = _installedAssemblies.FindHighestVersionInRedistList(assemblyName);
if (entry != null)
{
assemblyName = entry.AssemblyNameExtension.Clone();
}
}
}
if (assemblyName != null && _installedAssemblies != null && !wantSpecificVersion && reference.HintPath.Length == 0)
{
AssemblyNameExtension remappedExtension = _installedAssemblies.RemapAssemblyExtension(assemblyName);
if (remappedExtension != null)
{
reference.AddRemapping(assemblyName.CloneImmutable(), remappedExtension.CloneImmutable());
assemblyName = remappedExtension;
}
}
// Embed Interop Types aka "NOPIAs" support is not available for Fx < 4.0
// So, we just ignore this setting on down-level platforms
if (_projectTargetFramework != null && _projectTargetFramework >= s_targetFrameworkVersion_40)
{
reference.EmbedInteropTypes = MetadataConversionUtilities.TryConvertItemMetadataToBool
(
referenceAssemblyName,
ItemMetadataNames.embedInteropTypes
);
}
// Set the AssemblyFolderKey if there is one.
reference.AssemblyFolderKey = referenceAssemblyName.GetMetadata(ItemMetadataNames.assemblyFolderKey);
// It's possible, especially in cases where the fusion name was passed in through the item
// that we'll have a better (more information) fusion name once we know the assembly path.
try
{
ResolveReference(assemblyName, rawFileNameCandidate, reference);
if (reference.IsResolved)
{
AssemblyNameExtension possiblyBetterAssemblyName = null;
try
{
// This may throw if, for example, the culture embedded in the assembly's manifest
// is not recognised by AssemblyName.GetAssemblyName
possiblyBetterAssemblyName = _getAssemblyName(reference.FullPath);
}
catch (ArgumentException)
{
// Give up trying to get a better name
possiblyBetterAssemblyName = null;
}
// Use the better name if it exists.
if (possiblyBetterAssemblyName != null && possiblyBetterAssemblyName.Name != null)
{
assemblyName = possiblyBetterAssemblyName;
}
}
}
catch (BadImageFormatException e)
{
// If this isn't a valid assembly, then record the exception and continue on
reference.AddError(new BadImageReferenceException(e.Message, e));
}
catch (FileNotFoundException e) // Why isn't this covered in NotExpectedException?
{
reference.AddError(new BadImageReferenceException(e.Message, e));
}
catch (FileLoadException e)
{
// Managed assembly was found but could not be loaded.
reference.AddError(new BadImageReferenceException(e.Message, e));
}
catch (Exception e) when (ExceptionHandling.IsIoRelatedException(e))
{
reference.AddError(new BadImageReferenceException(e.Message, e));
}
// If there is still no assembly name then this is a case where the assembly metadata
// just doesn't contain an assembly name. We want to try to tolerate this because
// mscorlib.dll (sometimes?) doesn't contain an assembly name.
if (assemblyName == null)
{
if (!reference.IsResolved)
{
// The file doesn't exist and the reference was unresolved, there's nothing we can do at this point.
// Return, rather than throw, the exception, as in some situations it can happen thousands of times.
return new InvalidReferenceAssemblyNameException(referenceAssemblyName.ItemSpec);
}
assemblyName = new AssemblyNameExtension
(
AssemblyNameExtension.EscapeDisplayNameCharacters(reference.FileNameWithoutExtension)
);
}
// Check to see if this is a prereq assembly.
if (_installedAssemblies == null)
{
reference.IsPrerequisite = false;
}
else
{
Version unifiedVersion = null;
bool isPrerequisite = false;
bool? isRedistRoot = null;
string redistName = null;
_installedAssemblies.GetInfo
(
assemblyName,
out unifiedVersion,
out isPrerequisite,
out isRedistRoot,
out redistName
);
reference.IsPrerequisite = isPrerequisite;
reference.IsRedistRoot = isRedistRoot;
reference.RedistName = redistName;
}
AddReference(assemblyName, reference);
if (reference.ExternallyResolved)
{
_externallyResolvedPrimaryReferences.Add(assemblyName.Name);
}
return null;
}
/// <summary>
/// Attempts to convert an itemSpec and fusionName into an assembly name.
/// AssemblyName is left unchanged if conversion wasn't possible.
/// </summary>
/// <param name="itemSpec"></param>
/// <param name="fusionName"></param>
/// <param name="assemblyName"></param>
private static void TryConvertToAssemblyName(string itemSpec, string fusionName, ref AssemblyNameExtension assemblyName)
{
// FusionName is used if available.
string finalName = fusionName;
if (finalName == null || finalName.Length == 0)
{
// Otherwise, its itemSpec.
finalName = itemSpec;
}
bool pathRooted = false;
try
{
pathRooted = Path.IsPathRooted(finalName);
}
catch (ArgumentException)
{
/* Eat this because it has invalid chars in to and cannot be a path, maybe it can be parsed as a fusion name.*/
}
if (!pathRooted)
{
// Now try to convert to an AssemblyName.
try
{
assemblyName = new AssemblyNameExtension(finalName, true /*throw if not valid*/);
}
catch (System.IO.FileLoadException)
{
// Not a valid AssemblyName. Maybe its a file name.
TryGatherAssemblyNameEssentials(finalName, ref assemblyName);
return;
}
}
else
{
// Maybe the string has a fusion name inside of it.
TryGatherAssemblyNameEssentials(finalName, ref assemblyName);
}
}
/// <summary>
/// Given a string that may be a fusion name, try to gather the four essential properties:
/// Name
/// Version
/// PublicKeyToken
/// Culture
/// </summary>
/// <param name="fusionName"></param>
/// <param name="assemblyName"></param>
private static void TryGatherAssemblyNameEssentials(string fusionName, ref AssemblyNameExtension assemblyName)
{
int firstComma = fusionName.IndexOf(',');
if (firstComma == -1)
{
return;
}
string name = fusionName.Substring(0, firstComma);
string version = null;
string publicKeyToken = null;
string culture = null;
TryGetAssemblyNameComponent(fusionName, "Version", ref version);
TryGetAssemblyNameComponent(fusionName, "PublicKeyToken", ref publicKeyToken);
TryGetAssemblyNameComponent(fusionName, "Culture", ref culture);
if (version == null || publicKeyToken == null || culture == null)
{
return;
}
string newFusionName = String.Format(CultureInfo.InvariantCulture,
"{0}, Version={1}, Culture={2}, PublicKeyToken={3}",
name, version, culture, publicKeyToken);
// Now try to convert to an AssemblyName.
try
{
assemblyName = new AssemblyNameExtension(newFusionName, true /* throw if not valid */);
}
catch (System.IO.FileLoadException)
{
// Not a valid AssemblyName. Maybe its a file name.
// TryGatherAssemblyNameEssentials
return;
}
}
/// <summary>
/// Attempt to get one field out of an assembly name.
/// </summary>
/// <param name="fusionName"></param>
/// <param name="component"></param>
/// <param name="value"></param>
private static void TryGetAssemblyNameComponent(string fusionName, string component, ref string value)
{
int position = fusionName.IndexOf(component + "=", StringComparison.Ordinal);
if (position == -1)
{
return;
}
position += component.Length + 1;
int nextDelimiter = fusionName.IndexOfAny(new char[] { ',', ' ' }, position);
if (nextDelimiter == -1)
{
value = fusionName.Substring(position);
}
else
{
value = fusionName.Substring(position, nextDelimiter - position);
}
}
/// <summary>
/// Given an item that refers to a file name, make it a primary reference.
/// </summary>
/// <param name="referenceAssemblyFile"></param>
private void SetPrimaryFileItem(ITaskItem referenceAssemblyFile)
{
try
{
// Create the reference.
Reference reference = new Reference(_isWinMDFile, _fileExists, _getRuntimeVersion);
bool hasSpecificVersionMetadata = MetadataConversionUtilities.TryConvertItemMetadataToBool(referenceAssemblyFile, ItemMetadataNames.specificVersion);
reference.MakePrimaryAssemblyReference
(
referenceAssemblyFile,
hasSpecificVersionMetadata,
Path.GetExtension(referenceAssemblyFile.ItemSpec)
);
AssemblyNameExtension assemblyName = NameAssemblyFileReference
(
reference,
referenceAssemblyFile.ItemSpec // Contains the assembly file name.
);
// Embed Interop Types aka "NOPIAs" support is not available for Fx < 4.0
// So, we just ignore this setting on down-level platforms
if (_projectTargetFramework >= s_targetFrameworkVersion_40)
{
reference.EmbedInteropTypes = MetadataConversionUtilities.TryConvertItemMetadataToBool
(
referenceAssemblyFile,
ItemMetadataNames.embedInteropTypes
);
}
AddReference(assemblyName, reference);
if (reference.ExternallyResolved)
{
_externallyResolvedPrimaryReferences.Add(assemblyName.Name);
}
}
catch (Exception e) when (ExceptionHandling.IsIoRelatedException(e))
{
throw new InvalidParameterValueException("AssemblyFiles", referenceAssemblyFile.ItemSpec, e.Message);
}
}
/// <summary>
/// Find related files like .pdbs and .xmls
/// </summary>
/// <param name="fullPath">Path to the parent assembly.</param>
/// <param name="reference">The reference to the parent assembly.</param>
private void FindRelatedFiles
(
Reference reference
)
{
string baseName = reference.FullPathWithoutExtension;
// Look for companion files like pdbs and xmls that ride along with
// assemblies.
foreach (string companionExtension in _relatedFileExtensions)
{
string companionFile = baseName + companionExtension;
if (_fileExists(companionFile))
{
reference.AddRelatedFileExtension(companionExtension);
}
}
// Native Winmd files may have a companion dll beside it.
// If this is not a primary reference or the implementation metadata is not set on the item we need to set the implmentation metadata.
if (reference.IsWinMDFile && (!reference.IsPrimary || String.IsNullOrEmpty(reference.PrimarySourceItem.GetMetadata(ItemMetadataNames.winmdImplmentationFile))) && !reference.IsManagedWinMDFile)
{
string companionFile = baseName + ".dll";
if (_fileExists(companionFile))
{
reference.ImplementationAssembly = companionFile;
}
}
}
/// <summary>
/// Find satellite assemblies.
/// </summary>
/// <param name="directoryName">Directory of the parrent assembly.</param>
/// <param name="fullPath">Path to the parent assembly.</param>
/// <param name="reference">The reference to the parent assembly.</param>
private void FindSatellites
(
Reference reference
)
{
try
{
// If the directory doesn't exist (which is possible in the situation
// where we were passed in a pre-resolved reference from a P2P reference
// that hasn't actually been built yet), then GetDirectories will throw.
// Avoid that by just short-circuiting here.
if (!_directoryExists(reference.DirectoryName))
{
return;
}
string[] subDirectories = _getDirectories(reference.DirectoryName, "*");
string sateliteFilename = reference.FileNameWithoutExtension + ".resources.dll";
foreach (string subDirectory in subDirectories)
{
// Is there a candidate satellite in that folder?
string cultureName = Path.GetFileName(subDirectory);
if (CultureInfoCache.IsValidCultureString(cultureName))
{
string satelliteAssembly = Path.Combine(subDirectory, sateliteFilename);
if (_fileExists(satelliteAssembly))
{
// This is valid satellite assembly.
reference.AddSatelliteFile(Path.Combine(cultureName, sateliteFilename));
}
}
}
}
catch (Exception e) when (ExceptionHandling.IsIoRelatedException(e))
{
if (_log != null)
{
_log.LogErrorFromResources("ResolveAssemblyReference.ProblemFindingSatelliteAssemblies", reference.FullPath, e.Message);
}
}
}
/// <summary>
/// Find serialization assemblies.
/// </summary>
/// <param name="directoryName">Directory of the parrent assembly.</param>
/// <param name="fullPath">Path to the parent assembly.</param>
/// <param name="reference">The reference to the parent assembly.</param>
private void FindSerializationAssemblies
(