From 4838de25af0d10ef06446baa10c338b3886ea1c0 Mon Sep 17 00:00:00 2001 From: Swaroop Sridhar Date: Tue, 22 Oct 2019 17:16:04 -0700 Subject: [PATCH] Generate RID graph in self-contained builds Fixes #3361 --- .../GivenADependencyContextBuilder.cs | 4 ++-- .../DependencyContextBuilder.cs | 23 +++++++++++++------ .../GenerateDepsFile.cs | 11 ++++++--- .../Microsoft.NET.DesignerSupport.targets | 1 + .../targets/Microsoft.NET.Publish.targets | 3 ++- .../targets/Microsoft.NET.Sdk.targets | 4 ++-- 6 files changed, 31 insertions(+), 15 deletions(-) diff --git a/src/Tasks/Microsoft.NET.Build.Tasks.UnitTests/GivenADependencyContextBuilder.cs b/src/Tasks/Microsoft.NET.Build.Tasks.UnitTests/GivenADependencyContextBuilder.cs index 89182936943a..2bd5405597af 100644 --- a/src/Tasks/Microsoft.NET.Build.Tasks.UnitTests/GivenADependencyContextBuilder.cs +++ b/src/Tasks/Microsoft.NET.Build.Tasks.UnitTests/GivenADependencyContextBuilder.cs @@ -64,7 +64,7 @@ public void ItBuildsDependencyContextsFromProjectLockFiles( resolvedNuGetFiles = Array.Empty(); } - DependencyContext dependencyContext = new DependencyContextBuilder(mainProject, projectContext, includeRuntimeFileVersions: false) + DependencyContext dependencyContext = new DependencyContextBuilder(mainProject, projectContext, includeRuntimeFileVersions: false, runtimeGraph:null) .WithDirectReferences(directReferences) .WithCompilationOptions(compilationOptions) .WithResolvedNuGetFiles((ResolvedFile[]) resolvedNuGetFiles) @@ -261,7 +261,7 @@ private DependencyContext BuildDependencyContextWithReferenceAssemblies(bool use useCompilationOptions ? CreateCompilationOptions() : null; - DependencyContext dependencyContext = new DependencyContextBuilder(mainProject, projectContext, includeRuntimeFileVersions: false) + DependencyContext dependencyContext = new DependencyContextBuilder(mainProject, projectContext, includeRuntimeFileVersions: false, runtimeGraph: null) .WithReferenceAssemblies(ReferenceInfo.CreateReferenceInfos(referencePaths)) .WithCompilationOptions(compilationOptions) .Build(); diff --git a/src/Tasks/Microsoft.NET.Build.Tasks/DependencyContextBuilder.cs b/src/Tasks/Microsoft.NET.Build.Tasks/DependencyContextBuilder.cs index 1fa421b789bf..837153d1b54d 100644 --- a/src/Tasks/Microsoft.NET.Build.Tasks/DependencyContextBuilder.cs +++ b/src/Tasks/Microsoft.NET.Build.Tasks/DependencyContextBuilder.cs @@ -7,6 +7,7 @@ using NuGet.Packaging; using NuGet.Packaging.Core; using NuGet.ProjectModel; +using NuGet.RuntimeModel; using NuGet.Versioning; namespace Microsoft.NET.Build.Tasks @@ -36,6 +37,7 @@ internal class DependencyContextBuilder private string _runtimeIdentifier; private bool _isPortable; private HashSet _usedLibraryNames; + private readonly RuntimeGraph _runtimeGraph; private Dictionary _referenceLibraryNames; @@ -44,10 +46,11 @@ internal class DependencyContextBuilder private const string NetCorePlatformLibrary = "Microsoft.NETCore.App"; - public DependencyContextBuilder(SingleProjectInfo mainProjectInfo, ProjectContext projectContext, bool includeRuntimeFileVersions) + public DependencyContextBuilder(SingleProjectInfo mainProjectInfo, ProjectContext projectContext, bool includeRuntimeFileVersions, RuntimeGraph runtimeGraph) { _mainProjectInfo = mainProjectInfo; _includeRuntimeFileVersions = includeRuntimeFileVersions; + _runtimeGraph = runtimeGraph; var libraryLookup = new LockFileLookup(projectContext.LockFile); @@ -322,19 +325,25 @@ public DependencyContext Build() } } - var targetInfo = new TargetInfo( _dotnetFrameworkName, _runtimeIdentifier, runtimeSignature: string.Empty, _isPortable); + var runtimeFallbackGraph = + (_runtimeGraph == null) ? new RuntimeFallbacks[] { } : + _runtimeGraph.Runtimes + .Select(runtimeDict => _runtimeGraph.ExpandRuntime(runtimeDict.Key)) + .Where(expansion => expansion.Contains(_runtimeIdentifier)) + .Select(expansion => new RuntimeFallbacks(expansion.First(), expansion.Skip(1))); // ExpandRuntime return runtime itself as first item. + return new DependencyContext( - targetInfo, - _compilationOptions ?? CompilationOptions.Default, - compilationLibraries, - runtimeLibraries, - new RuntimeFallbacks[] { }); + targetInfo, + _compilationOptions ?? CompilationOptions.Default, + compilationLibraries, + runtimeLibraries, + runtimeFallbackGraph); } private RuntimeLibrary GetProjectRuntimeLibrary() diff --git a/src/Tasks/Microsoft.NET.Build.Tasks/GenerateDepsFile.cs b/src/Tasks/Microsoft.NET.Build.Tasks/GenerateDepsFile.cs index 10a78956bb40..87e1eb00801f 100644 --- a/src/Tasks/Microsoft.NET.Build.Tasks/GenerateDepsFile.cs +++ b/src/Tasks/Microsoft.NET.Build.Tasks/GenerateDepsFile.cs @@ -5,10 +5,9 @@ using Microsoft.Build.Utilities; using Microsoft.Extensions.DependencyModel; using Newtonsoft.Json; -using Newtonsoft.Json.Linq; using NuGet.Packaging.Core; +using NuGet.RuntimeModel; using NuGet.ProjectModel; -using NuGet.Versioning; using System; using System.Collections.Generic; using System.IO; @@ -92,6 +91,9 @@ public class GenerateDepsFile : TaskBase public bool IncludeRuntimeFileVersions { get; set; } + [Required] + public string RuntimeGraphPath { get; set; } + List _filesWritten = new List(); [Output] @@ -155,7 +157,10 @@ private void WriteDepsFile(string depsFilePath) RuntimeFrameworks, IsSelfContained); - var builder = new DependencyContextBuilder(mainProject, projectContext, IncludeRuntimeFileVersions); + RuntimeGraph runtimeGraph = + IsSelfContained ? new RuntimeGraphCache(this).GetRuntimeGraph(RuntimeGraphPath) : null; + + var builder = new DependencyContextBuilder(mainProject, projectContext, IncludeRuntimeFileVersions, runtimeGraph); builder = builder .WithMainProjectInDepsFile(IncludeMainProject) diff --git a/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.DesignerSupport.targets b/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.DesignerSupport.targets index 76dc53f93a2b..66d21afd7a87 100644 --- a/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.DesignerSupport.targets +++ b/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.DesignerSupport.targets @@ -67,6 +67,7 @@ Copyright (c) .NET Foundation. All rights reserved. ResolvedNuGetFiles="@(NativeCopyLocalItems);@(ResourceCopyLocalItems);@(RuntimeCopyLocalItems)" ResolvedRuntimeTargetsFiles="@(RuntimeTargetsCopyLocalItems)" TargetFramework="$(TargetFrameworkMoniker)" + RuntimeGraphPath="$(BundledRuntimeIdentifierGraphFile)" /> diff --git a/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Publish.targets b/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Publish.targets index 8bdd31a4022d..25f40da15b25 100644 --- a/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Publish.targets +++ b/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Publish.targets @@ -933,7 +933,8 @@ Copyright (c) .NET Foundation. All rights reserved. ResolvedRuntimeTargetsFiles="@(RuntimeTargetsCopyLocalItems)" UserRuntimeAssemblies="@(UserRuntimeAssembly)" IsSelfContained="$(SelfContained)" - IncludeRuntimeFileVersions="$(IncludeFileVersionsInDependencyFile)"/> + IncludeRuntimeFileVersions="$(IncludeFileVersionsInDependencyFile)" + RuntimeGraphPath="$(BundledRuntimeIdentifierGraphFile)"/> <_FilesToBundle Include="$(PublishDepsFilePath)" Condition="'$(PublishSingleFile)' == 'true'"> diff --git a/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Sdk.targets b/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Sdk.targets index facdb64fa036..6a876350ad50 100644 --- a/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Sdk.targets +++ b/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Sdk.targets @@ -212,8 +212,8 @@ Copyright (c) .NET Foundation. All rights reserved. UserRuntimeAssemblies="@(UserRuntimeAssembly)" ResolvedRuntimeTargetsFiles="@(RuntimeTargetsCopyLocalItems)" IsSelfContained="$(SelfContained)" - IncludeRuntimeFileVersions="$(IncludeFileVersionsInDependencyFile)"> - + IncludeRuntimeFileVersions="$(IncludeFileVersionsInDependencyFile)" + RuntimeGraphPath="$(BundledRuntimeIdentifierGraphFile)"/>