using System; using System.Collections.Generic; using System.Diagnostics; using Microsoft.Build.Framework; using Microsoft.Build.Utilities; using System.IO; namespace ILLink.Tasks { public class SetAssemblyActions : Task { /// /// Paths to the assembly files that should be considered as /// input to the linker. /// [Required] public ITaskItem [] AssemblyPaths { get; set; } /// /// Application assembly names. /// [Required] public ITaskItem [] ApplicationAssemblyNames { get; set; } /// /// Platform assembly names. /// [Required] public ITaskItem [] PlatformAssemblyNames { get; set; } /// /// Action to perform on used application assemblies. /// [Required] public ITaskItem UsedApplicationAssemblyAction { get; set; } /// /// Action to perform on unused application assemblies. /// [Required] public ITaskItem UnusedApplicationAssemblyAction { get; set; } /// /// Action to perform on used platform assemblies. /// [Required] public ITaskItem UsedPlatformAssemblyAction { get; set; } /// /// Action to perform on unused platform assemblies. /// [Required] public ITaskItem UnusedPlatformAssemblyAction { get; set; } [Output] public ITaskItem [] AssemblyPathsWithActions { get; set; } public override bool Execute () { string applicationAssemblyAction; string usedApplicationAssemblyAction = UsedApplicationAssemblyAction.ItemSpec; string unusedApplicationAssemblyAction = UnusedApplicationAssemblyAction.ItemSpec; if (!GetAssemblyAction (usedApplicationAssemblyAction.ToLower (), unusedApplicationAssemblyAction.ToLower (), out applicationAssemblyAction)) { Log.LogError ("Unsupported combination of application assembly actions: {0}, {1}.", usedApplicationAssemblyAction, unusedApplicationAssemblyAction); return false; } string platformAssemblyAction; string usedPlatformAssemblyAction = UsedPlatformAssemblyAction.ItemSpec; string unusedPlatformAssemblyAction = UnusedPlatformAssemblyAction.ItemSpec; if (!GetAssemblyAction (usedPlatformAssemblyAction.ToLower (), unusedPlatformAssemblyAction.ToLower (), out platformAssemblyAction)) { Log.LogError ("Unsupported combination of platform assembly actions: {0}, {1}.", usedPlatformAssemblyAction, unusedPlatformAssemblyAction); return false; } List resultAssemblies = new List (); AddAssemblyActionMetadata (ApplicationAssemblyNames, applicationAssemblyAction, resultAssemblies); AddAssemblyActionMetadata (PlatformAssemblyNames, platformAssemblyAction, resultAssemblies); AssemblyPathsWithActions = resultAssemblies.ToArray (); return true; } bool GetAssemblyAction (string usedAssemblyAction, string unusedAssemblyAction, out string assemblyAction) { assemblyAction = "illegal"; if ((unusedAssemblyAction != usedAssemblyAction) && (unusedAssemblyAction != "delete")) { return false; } switch (usedAssemblyAction) { case "link": assemblyAction = "link"; break; case "copy": assemblyAction = (unusedAssemblyAction == "delete") ? "copyused" : "copy"; break; case "addbypassngen": assemblyAction = (unusedAssemblyAction == "delete") ? "addbypassngenused" : "addbypassngen"; break; case "skip": if (unusedAssemblyAction != usedAssemblyAction) { return false; } assemblyAction = "skip"; break; default: return false; } return true; } void AddAssemblyActionMetadata (ITaskItem [] assemblies, string action, List resultList) { HashSet assemblyHashSet = new HashSet (); foreach (var assembly in assemblies) { assemblyHashSet.Add (assembly.ItemSpec); } foreach (var assembly in AssemblyPaths) { if (assemblyHashSet.Contains (Path.GetFileNameWithoutExtension (assembly.ItemSpec))) { assembly.SetMetadata ("action", action); resultList.Add (assembly); } } } } }