|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text; |
| 5 | +using Microsoft.Build.Framework; |
| 6 | + |
| 7 | +namespace Microsoft.NET.Build.Tasks |
| 8 | +{ |
| 9 | + public class GetDefaultPlatformTargetForNetFramework : TaskBase |
| 10 | + { |
| 11 | + public ITaskItem[] PackageDependencies { get; set; } |
| 12 | + |
| 13 | + public ITaskItem[] NativeCopyLocalItems { get; set; } |
| 14 | + |
| 15 | + [Output] |
| 16 | + public string DefaultPlatformTarget { get; private set; } |
| 17 | + |
| 18 | + private const string X86 = "x86"; |
| 19 | + private const string AnyCPU = "AnyCPU"; |
| 20 | + |
| 21 | + protected override void ExecuteCore() |
| 22 | + { |
| 23 | + // For .NET Framework projects, the SDK will select a default RuntimeIdentifier and PlatformTarget. If no |
| 24 | + // native assets are found from NuGet packages, then the PlatformTarget will be reset to AnyCPU. See the |
| 25 | + // comments in Microsoft.NET.RuntimeIdentifierInference.targets for details. |
| 26 | + // |
| 27 | + // Prior to the .NET Core 3.0 SDK, .NET Framework projects would only have a RuntimeIdentifier graph if the |
| 28 | + // Microsoft.NETCore.Platforms package was (transitively) referenced. This meant that native assets would |
| 29 | + // only be selected if the platforms package was referenced or if the RuntimeIdentifier matched exactly. |
| 30 | + // |
| 31 | + // Now that the RuntimeIdentifier graph is provided in the SDK, the logic in this task preserves the PlatformTarget |
| 32 | + // behavior from earlier SDKs, even though with the RuntimeIdentifier graph supplied, there may be native |
| 33 | + // assets selected where in prior SDKs there would not have been. |
| 34 | + |
| 35 | + if (NativeCopyLocalItems == null || NativeCopyLocalItems.Length == 0) |
| 36 | + { |
| 37 | + DefaultPlatformTarget = AnyCPU; |
| 38 | + return; |
| 39 | + } |
| 40 | + |
| 41 | + foreach (var packageDependency in PackageDependencies ?? Enumerable.Empty<ITaskItem>()) |
| 42 | + { |
| 43 | + // If the Platforms package is in the dependencies, then any native assets imply an X86 default PlatformTarget |
| 44 | + if (packageDependency.ItemSpec.Equals("Microsoft.NETCore.Platforms", StringComparison.OrdinalIgnoreCase)) |
| 45 | + { |
| 46 | + DefaultPlatformTarget = X86; |
| 47 | + return; |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + foreach (var nativeItem in NativeCopyLocalItems) |
| 52 | + { |
| 53 | + // If the Platforms package was not referenced, but there are native assets for the exact RID win7-x86, |
| 54 | + // then the default PlatformTarget should be x86. |
| 55 | + string pathInPackage = nativeItem.GetMetadata(MetadataKeys.PathInPackage); |
| 56 | + if (pathInPackage.StartsWith("runtimes/win7-x86/", StringComparison.OrdinalIgnoreCase)) |
| 57 | + { |
| 58 | + DefaultPlatformTarget = X86; |
| 59 | + return; |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + // Otherwise, there would have been no native assets selected on pre-3.0 SDKs, so use AnyCPU as the |
| 64 | + // default PlatformTarget |
| 65 | + DefaultPlatformTarget = AnyCPU; |
| 66 | + } |
| 67 | + } |
| 68 | +} |
0 commit comments