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
|
using System.CodeDom.Compiler;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Web.Configuration;
using Microsoft.Build.Framework;
using Microsoft.Build.Tasks;
using Microsoft.Build.Utilities;
using FrameworkName=System.Runtime.Versioning.FrameworkName;
namespace System.Web.Compilation {
internal class AssemblyResolutionResult {
internal ICollection<string> ResolvedFiles {
get;
set;
}
internal ICollection<string> ResolvedFilesWithWarnings {
get;
set;
}
internal ICollection<Assembly> UnresolvedAssemblies {
get;
set;
}
internal ICollection<BuildErrorEventArgs> Errors {
get;
set;
}
internal ICollection<BuildWarningEventArgs> Warnings {
get;
set;
}
}
internal enum ReferenceAssemblyType {
FrameworkAssembly = 0,
FrameworkAssemblyOnlyPresentInHigherVersion = 1,
NonFrameworkAssembly = 2,
}
internal class AssemblyResolver {
/// <summary>
/// Keeps track of resolved assemblies and their locations. Value is null if the assembly was found only
/// in a higher version framework.
/// </summary>
private static Dictionary<Assembly, string> s_assemblyLocations;
private static Dictionary<Assembly, AssemblyResolutionResult> s_assemblyResults;
private static Dictionary<Assembly, ReferenceAssemblyType> s_assemblyTypes;
private static object s_lock = new object();
private static IList<string> s_targetFrameworkReferenceAssemblyPaths;
private static IList<string> s_higherFrameworkReferenceAssemblyPaths;
private static IList<string> s_fullProfileReferenceAssemblyPaths;
private static bool? s_needToCheckFullProfile;
private static bool? s_warnAsError = null;
private static object s_warnAsErrorLock = new object();
// Maps physical paths of reference assemblies to their versions as returned by AssemblyName.GetAssemblyName
private static readonly Lazy<ConcurrentDictionary<string, Version>> s_assemblyVersions =
new Lazy<ConcurrentDictionary<string, Version>>(
() => new ConcurrentDictionary<string, Version>(StringComparer.OrdinalIgnoreCase));
private static IList<string> TargetFrameworkReferenceAssemblyPaths {
get {
if (s_targetFrameworkReferenceAssemblyPaths == null) {
IList<string> paths = GetPathToReferenceAssemblies(MultiTargetingUtil.TargetFrameworkName);
int count = paths.Count;
if (MultiTargetingUtil.IsTargetFramework20 || MultiTargetingUtil.IsTargetFramework35) {
// Require 3.5 to be installed to be able to target pre-4.0
var fxPath35 = ToolLocationHelper.GetPathToDotNetFramework(TargetDotNetFrameworkVersion.Version35);
if (string.IsNullOrEmpty(fxPath35)) {
throw new HttpException(SR.GetString(SR.Downlevel_requires_35));
}
// For 2.0 and 3.5, verify that the reference assemblies are actually present.
// For 3.5, make sure the reference assemblies path do not consist of just 2.0 and 3.0 assemblies.
IList<string> assemblyPaths30 = GetPathToReferenceAssemblies(MultiTargetingUtil.FrameworkNameV30);
IList<string> assemblyPaths20 = GetPathToReferenceAssemblies(MultiTargetingUtil.FrameworkNameV20);
bool missing35assemblies = MultiTargetingUtil.IsTargetFramework35 && (assemblyPaths30.Count == count || assemblyPaths20.Count == count);
if (count == 0 || missing35assemblies) {
throw new HttpException(SR.GetString(SR.Reference_assemblies_not_found));
}
}
else {
// When we are performing a build through VS, we require the reference assemblies
// to be present.
if (BuildManagerHost.SupportsMultiTargeting && count == 0) {
throw new HttpException(SR.GetString(SR.Reference_assemblies_not_found));
}
}
s_targetFrameworkReferenceAssemblyPaths = paths;
}
return s_targetFrameworkReferenceAssemblyPaths;
}
}
/// <summary>
/// Returns a list of assembly paths containing assemblies from higher version frameworks.
/// </summary>
private static IList<string> HigherFrameworkReferenceAssemblyPaths {
get {
if (s_higherFrameworkReferenceAssemblyPaths == null) {
List<string> paths = new List<string>();
FrameworkName targetName = MultiTargetingUtil.TargetFrameworkName;
// Loop through each framework name, and find those that is equal in Identifier and Profile, but
// higher than the target version.
foreach (FrameworkName name in MultiTargetingUtil.KnownFrameworkNames) {
if (string.Equals(name.Identifier, targetName.Identifier, StringComparison.OrdinalIgnoreCase) &&
string.Equals(name.Profile, targetName.Profile, StringComparison.OrdinalIgnoreCase)) {
Version version = name.Version;
Version targetVersion = targetName.Version;
if (targetVersion < version) {
paths.AddRange(GetPathToReferenceAssemblies(name));
}
}
}
s_higherFrameworkReferenceAssemblyPaths = paths;
}
return s_higherFrameworkReferenceAssemblyPaths;
}
}
/// <summary>
/// Returns a list of assembly paths containing assemblies from full profile framework.
/// </summary>
private static IList<string> FullProfileReferenceAssemblyPaths {
get {
if (s_fullProfileReferenceAssemblyPaths == null) {
List<string> paths = new List<string>();
FrameworkName targetName = MultiTargetingUtil.TargetFrameworkName;
// Create a copy without the profile to get the full profile.
FrameworkName fullName = new FrameworkName(targetName.Identifier, targetName.Version);
paths.AddRange(GetPathToReferenceAssemblies(fullName));
s_fullProfileReferenceAssemblyPaths = paths;
}
return s_fullProfileReferenceAssemblyPaths;
}
}
/// <summary>
/// Checks whether we need to perform a check against the full profile to determine whether a reference
/// assembly can be used or not.
/// </summary>
private static bool NeedToCheckFullProfile {
get {
if (s_needToCheckFullProfile == null) {
// Find differences between the two sets of reference assembly paths.
var except = FullProfileReferenceAssemblyPaths.Except(TargetFrameworkReferenceAssemblyPaths,
StringComparer.OrdinalIgnoreCase);
if (except.Count() == 0) {
// If everything is the same, then there is no need for an extra check against the
// full profile.
s_needToCheckFullProfile = false;
}
else {
// If something is different, we will need an additional check against the full
// profile.
s_needToCheckFullProfile = true;
}
}
return s_needToCheckFullProfile.Value;
}
}
private static Dictionary<Assembly, string> AssemblyLocations {
get {
if (s_assemblyLocations == null) {
s_assemblyLocations = new Dictionary<Assembly, string>();
}
return s_assemblyLocations;
}
}
private static Dictionary<Assembly, AssemblyResolutionResult> AssemblyResolutionResults {
get {
if (s_assemblyResults == null) {
s_assemblyResults = new Dictionary<Assembly, AssemblyResolutionResult>();
}
return s_assemblyResults;
}
}
private static Dictionary<Assembly, ReferenceAssemblyType> ReferenceAssemblyTypes {
get {
if (s_assemblyTypes == null) {
s_assemblyTypes = new Dictionary<Assembly, ReferenceAssemblyType>();
}
return s_assemblyTypes;
}
}
private static ConcurrentDictionary<string, Version> AssemblyVersions {
get {
return s_assemblyVersions.Value;
}
}
/// <summary>
/// Returns the assembly version of the assembly found at the specified path using AssemblyName.GetAssemblyName.
/// Returns and stores null if GetAssemblyName throws.
/// </summary>
private static Version GetAssemblyVersion(string path) {
Version version = null;
var assemblyVersions = AssemblyVersions;
if (!assemblyVersions.TryGetValue(path, out version)) {
try {
AssemblyName resolvedAssemblyName = AssemblyName.GetAssemblyName(path);
version = resolvedAssemblyName.Version;
} catch {
// Ignore any exceptions thrown
}
assemblyVersions.TryAdd(path, version);
}
return version;
}
/// <summary>
/// Resolve a single assembly using the provided search paths and setting the targetframework directories.
/// </summary>
private static AssemblyResolutionResult ResolveAssembly(string assemblyName, IList<string> searchPaths, IList<string> targetFrameworkDirectories, bool checkDependencies) {
ResolveAssemblyReference rar = new ResolveAssemblyReference();
MockEngine engine = new MockEngine();
rar.BuildEngine = engine;
if (searchPaths != null) {
rar.SearchPaths = searchPaths.ToArray();
}
if (targetFrameworkDirectories != null) {
rar.TargetFrameworkDirectories = targetFrameworkDirectories.ToArray();
}
rar.Assemblies = new ITaskItem[] {
new TaskItem(assemblyName),
};
rar.Silent = true;
rar.Execute();
AssemblyResolutionResult result = new AssemblyResolutionResult();
List<string> resolvedFiles = new List<string>();
foreach (ITaskItem item in rar.ResolvedFiles) {
resolvedFiles.Add(item.ItemSpec);
}
if (checkDependencies) {
CheckOutOfRangeDependencies(assemblyName);
}
result.ResolvedFiles = resolvedFiles.ToArray();
result.Warnings = engine.Warnings;
result.Errors = engine.Errors;
return result;
}
/// <summary>
/// Check whether an assembly has dependencies to a framework assembly of a higher version,
/// report the issue as a warning or error.
/// </summary>
private static void CheckOutOfRangeDependencies(string assemblyName) {
string dependencies = null;
Assembly assembly = Assembly.Load(assemblyName);
AssemblyName aName = new AssemblyName(assemblyName);
// If the loaded assembly has a different version than the specified assembly,
// then it is likely that there was unification or binding redirect in place.
// If that is the case, then GetReferenceAssemblies won't be accurate for
// finding the references of the actual assembly, so we skip checking its references.
if (assembly.GetName().Version != aName.Version) {
return;
}
foreach (AssemblyName name in assembly.GetReferencedAssemblies()) {
try {
Assembly referenceAssembly = CompilationSection.LoadAndRecordAssembly(name);
string path;
ReferenceAssemblyType referenceAssemblyType =
GetPathToReferenceAssembly(referenceAssembly, out path, null, null, false /*checkDependencies*/);
// We need to check the following 2 conditions:
// 1. If the assembly is available in the target framework, we also need to
// verify that the version being referenced is no higher than what we have
// in the target framework.
// 2. If the assembly is only available in a higher version framework.
Version resolvedAssemblyVersion = GetAssemblyVersion(path);
if (resolvedAssemblyVersion == null) {
continue;
}
if ((referenceAssemblyType == ReferenceAssemblyType.FrameworkAssembly && resolvedAssemblyVersion < name.Version)
|| referenceAssemblyType == ReferenceAssemblyType.FrameworkAssemblyOnlyPresentInHigherVersion) {
if (dependencies == null) {
dependencies = name.FullName;
}
else {
dependencies += "; " + name.FullName;
}
}
}
catch {
// Ignore dependencies that are not found, as we are primarily concerned
// with framework assemblies that are on the machine.
}
}
if (dependencies != null) {
string message = SR.GetString(SR.Higher_dependencies, assemblyName, dependencies);
ReportWarningOrError(message);
}
}
private static void ReportWarningOrError(string message) {
if (WarnAsError) {
// Report the issue as an error.
throw new HttpCompileException(message);
}
else {
// Report the issue as a compiler warning.
CompilerError error = new CompilerError();
error.ErrorText = message;
error.IsWarning = true;
if (BuildManager.CBMCallback != null) {
BuildManager.CBMCallback.ReportCompilerError(error);
}
}
}
internal static ReferenceAssemblyType GetPathToReferenceAssembly(Assembly a, out string path) {
return GetPathToReferenceAssembly(a, out path, null, null);
}
private static void StoreResults(Assembly a, string path, AssemblyResolutionResult result, ReferenceAssemblyType assemblyType) {
lock (s_lock) {
if (!AssemblyLocations.ContainsKey(a)) {
AssemblyLocations.Add(a, path);
AssemblyResolutionResults.Add(a, result);
ReferenceAssemblyTypes.Add(a, assemblyType);
}
}
}
internal static ReferenceAssemblyType GetPathToReferenceAssembly(Assembly a, out string path,
ICollection<BuildErrorEventArgs> errors, ICollection<BuildWarningEventArgs> warnings) {
return GetPathToReferenceAssembly(a, out path, errors, warnings, true /*checkDependencies*/);
}
internal static ReferenceAssemblyType GetPathToReferenceAssembly(Assembly a, out string path,
ICollection<BuildErrorEventArgs> errors, ICollection<BuildWarningEventArgs> warnings,
bool checkDependencies) {
lock (s_lock) {
if (AssemblyLocations.TryGetValue(a, out path)) {
return ReferenceAssemblyTypes[a];
}
}
// If there are no reference assemblies available, just use the path to the loaded assembly.
if (TargetFrameworkReferenceAssemblyPaths == null || TargetFrameworkReferenceAssemblyPaths.Count == 0) {
path = System.Web.UI.Util.GetAssemblyCodeBase(a);
return ReferenceAssemblyType.FrameworkAssembly;
}
AssemblyResolutionResult result = null;
ReferenceAssemblyType referenceAssemblyType = ReferenceAssemblyType.NonFrameworkAssembly;
// If the assembly is generated by us, it is a non framework assembly and does not need to be resolved.
if (BuildResultCompiledAssemblyBase.AssemblyIsInCodegenDir(a)) {
path = System.Web.UI.Util.GetAssemblyCodeBase(a);
}
else {
// Try using the assembly full name.
referenceAssemblyType = GetPathToReferenceAssembly(a, out path, errors, warnings,
checkDependencies, true /*useFullName*/, out result);
}
StoreResults(a, path, result, referenceAssemblyType);
return referenceAssemblyType;
}
private static ReferenceAssemblyType GetPathToReferenceAssembly(Assembly a, out string path,
ICollection<BuildErrorEventArgs> errors, ICollection<BuildWarningEventArgs> warnings,
bool checkDependencies, bool useFullName, out AssemblyResolutionResult result) {
// 1. Find the assembly using RAR in the target framework.
// - If found, assembly is a framework assembly. Done
// 2. Find the assembly using RAR in higher frameworks.
// - If found, assembly is a framework assembly only present in a higher version. Done.
// 3. Find the assembly using RAR in the full profile framework.
// - If found, assembly is a framework assembly, but is only present in the full profile framework and not the current target profile. Done.
// 4. Is useFullName true?
// - Yes: Use GAC and directory of loaded assembly as search paths.
// - No: Use directory of loaded assembly as search path.
// - Use RAR to find assembly in search paths.
// - Check for out of range dependencies.
// 5. If useFullName
// - Check if the short name exists in a higher framework, if so, it is a framework assembly.
// Find the assembly in the target framework.
string assemblyName;
string partialName = a.GetName().Name;
if (useFullName) {
// Use the actual assembly name as specified in the config.
assemblyName = CompilationSection.GetOriginalAssemblyName(a);
}
else {
assemblyName = partialName;
}
result = ResolveAssembly(assemblyName, TargetFrameworkReferenceAssemblyPaths,
TargetFrameworkReferenceAssemblyPaths, false /*checkDependencies*/);
if (result.ResolvedFiles != null && result.ResolvedFiles.Count > 0) {
path = result.ResolvedFiles.FirstOrDefault();
return ReferenceAssemblyType.FrameworkAssembly;
}
// At this point, the assembly was not found in the target framework.
// Try finding it in the latest framework.
result = ResolveAssembly(assemblyName, HigherFrameworkReferenceAssemblyPaths, HigherFrameworkReferenceAssemblyPaths,
false /*checkDependencies*/);
if (result.ResolvedFiles != null && result.ResolvedFiles.Count > 0) {
path = result.ResolvedFiles.FirstOrDefault();
// Assembly was found in a target framework of a later version.
return ReferenceAssemblyType.FrameworkAssemblyOnlyPresentInHigherVersion;
}
// Try to find the assembly in the full profile, in case the user
// is using an assembly that is not in the target profile framework.
// For example, System.Web is not present in the Client profile, but is present in the full profile.
if (NeedToCheckFullProfile) {
result = ResolveAssembly(assemblyName, FullProfileReferenceAssemblyPaths, FullProfileReferenceAssemblyPaths,
false /*checkDependencies*/);
if (result.ResolvedFiles != null && result.ResolvedFiles.Count > 0) {
// Assembly was found in the full profile, but not in the target profile.
path = result.ResolvedFiles.FirstOrDefault();
// Report warning/error message.
string profile = "";
if (!string.IsNullOrEmpty(MultiTargetingUtil.TargetFrameworkName.Profile)) {
profile = " '" + MultiTargetingUtil.TargetFrameworkName.Profile + "'";
}
ReportWarningOrError(SR.GetString(SR.Assembly_not_found_in_profile, assemblyName, profile));
// Return as OnlyPresentInHigherVersion so that it will not be used as a reference assembly.
return ReferenceAssemblyType.FrameworkAssemblyOnlyPresentInHigherVersion;
}
}
// Assembly is not found in the framework.
// Check whether it has any references to assemblies of a higher version.
List<string> searchPaths = new List<string>();
searchPaths.AddRange(TargetFrameworkReferenceAssemblyPaths);
searchPaths.Add(Path.GetDirectoryName(a.Location));
// If we are using full names, include the GAC so that we can retrieve the actual
// specified version of an OOB assembly even if it is unified/redirected to a later version.
// For example, System.Web.Extensions 1.0.61025 is available from the GAC, but the actual
// loaded assembly is 4.0 due to unification.
if (useFullName) {
searchPaths.Add("{GAC}");
}
// When checking dependencies of a custom assembly, use the full
// name of the assembly as it might have a strong name or
// be in the GAC.
if (!useFullName) {
assemblyName = a.GetName().FullName;
}
result = ResolveAssembly(assemblyName, searchPaths, TargetFrameworkReferenceAssemblyPaths, checkDependencies);
// Use the actual resolved path, in case the loaded assembly is different from the specified assembly
// due to unification or binding redirect.
path = result.ResolvedFiles.FirstOrDefault();
if (string.IsNullOrEmpty(path)) {
// In some cases, we might not be able to resolve the path to the assembly successfully, for example when
// the config specifies the full name as System.Web 4.0.10101.0. Assembly.Load returns the 4.0.0.0 version,
// but we can't find any actual assembly with such a full name.
path = System.Web.UI.Util.GetAssemblyCodeBase(a);
}
// If we are using full names, do another check using the partial name to see if the assembly is part of
// a higher framework.
// If so, then this is an OOB assembly that later got rolled into the framework, so we consider the assembly
// as a framework assembly.
if (useFullName) {
AssemblyResolutionResult r = ResolveAssembly(partialName, HigherFrameworkReferenceAssemblyPaths, HigherFrameworkReferenceAssemblyPaths,
false /*checkDependencies*/);
if (r.ResolvedFiles != null && r.ResolvedFiles.Count > 0) {
return ReferenceAssemblyType.FrameworkAssembly;
}
}
return ReferenceAssemblyType.NonFrameworkAssembly;
}
private static IList<string> GetPathToReferenceAssemblies(FrameworkName frameworkName){
return ToolLocationHelper.GetPathToReferenceAssemblies(frameworkName);
}
/// <summary>
/// Returns true if any of the codedom providers has warnAsError set to true.
/// </summary>
private static bool WarnAsError {
get {
if (s_warnAsError == null) {
lock (s_warnAsErrorLock) {
// Check again, in case it was already set by another thread while the current thread
// was waiting to acquire the lock
if (s_warnAsError == null) {
// Set default value to false
s_warnAsError = false;
CompilerInfo[] compilerInfoArray = CodeDomProvider.GetAllCompilerInfo();
foreach (CompilerInfo info in compilerInfoArray) {
if (info == null || !info.IsCodeDomProviderTypeValid) {
continue;
}
if (CompilationUtil.WarnAsError(info.CodeDomProviderType)) {
s_warnAsError = true;
break;
}
}
}
}
}
return s_warnAsError.Value;
}
}
}
/// Adapted the following code from \\ddindex2\sources2\OrcasSP\vsproject\xmake\Shared\UnitTests
internal class MockEngine : IBuildEngine {
private List<BuildMessageEventArgs> messages = new List<BuildMessageEventArgs>();
private List<BuildWarningEventArgs> warnings = new List<BuildWarningEventArgs>();
private List<BuildErrorEventArgs> errors = new List<BuildErrorEventArgs>();
private List<CustomBuildEventArgs> customEvents = new List<CustomBuildEventArgs>();
internal MockEngine() {
}
internal ICollection<BuildMessageEventArgs> Messages {
get { return messages; }
}
internal ICollection<BuildWarningEventArgs> Warnings {
get { return warnings; }
}
internal ICollection<BuildErrorEventArgs> Errors {
get { return errors; }
}
internal ICollection<CustomBuildEventArgs> CustomEvents {
get { return customEvents; }
}
public virtual void LogErrorEvent(BuildErrorEventArgs eventArgs) {
errors.Add(eventArgs);
}
public virtual void LogWarningEvent(BuildWarningEventArgs eventArgs) {
warnings.Add(eventArgs);
}
public virtual void LogCustomEvent(CustomBuildEventArgs eventArgs) {
customEvents.Add(eventArgs);
}
public virtual void LogMessageEvent(BuildMessageEventArgs eventArgs) {
messages.Add(eventArgs);
}
public bool ContinueOnError {
get {
return false;
}
}
public string ProjectFileOfTaskNode {
get {
return String.Empty;
}
}
public int LineNumberOfTaskNode {
get {
return 0;
}
}
public int ColumnNumberOfTaskNode {
get {
return 0;
}
}
public bool BuildProjectFile(string projectFileName, string[] targetNames, System.Collections.IDictionary globalProperties, System.Collections.IDictionary targetOutputs) {
throw new NotImplementedException();
}
}
}
|