Skip to content

Commit

Permalink
Generate RID graph in self-contained builds
Browse files Browse the repository at this point in the history
  • Loading branch information
swaroop-sridhar committed Oct 24, 2019
1 parent 9d766f7 commit 4838de2
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public void ItBuildsDependencyContextsFromProjectLockFiles(
resolvedNuGetFiles = Array.Empty<ResolvedFile>();
}

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)
Expand Down Expand Up @@ -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();
Expand Down
23 changes: 16 additions & 7 deletions src/Tasks/Microsoft.NET.Build.Tasks/DependencyContextBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -36,6 +37,7 @@ internal class DependencyContextBuilder
private string _runtimeIdentifier;
private bool _isPortable;
private HashSet<string> _usedLibraryNames;
private readonly RuntimeGraph _runtimeGraph;

private Dictionary<ReferenceInfo, string> _referenceLibraryNames;

Expand All @@ -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);

Expand Down Expand Up @@ -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()
Expand Down
11 changes: 8 additions & 3 deletions src/Tasks/Microsoft.NET.Build.Tasks/GenerateDepsFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -92,6 +91,9 @@ public class GenerateDepsFile : TaskBase

public bool IncludeRuntimeFileVersions { get; set; }

[Required]
public string RuntimeGraphPath { get; set; }

List<ITaskItem> _filesWritten = new List<ITaskItem>();

[Output]
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ Copyright (c) .NET Foundation. All rights reserved.
ResolvedNuGetFiles="@(NativeCopyLocalItems);@(ResourceCopyLocalItems);@(RuntimeCopyLocalItems)"
ResolvedRuntimeTargetsFiles="@(RuntimeTargetsCopyLocalItems)"
TargetFramework="$(TargetFrameworkMoniker)"
RuntimeGraphPath="$(BundledRuntimeIdentifierGraphFile)"
/>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -933,7 +933,8 @@ Copyright (c) .NET Foundation. All rights reserved.
ResolvedRuntimeTargetsFiles="@(RuntimeTargetsCopyLocalItems)"
UserRuntimeAssemblies="@(UserRuntimeAssembly)"
IsSelfContained="$(SelfContained)"
IncludeRuntimeFileVersions="$(IncludeFileVersionsInDependencyFile)"/>
IncludeRuntimeFileVersions="$(IncludeFileVersionsInDependencyFile)"
RuntimeGraphPath="$(BundledRuntimeIdentifierGraphFile)"/>

<ItemGroup>
<_FilesToBundle Include="$(PublishDepsFilePath)" Condition="'$(PublishSingleFile)' == 'true'">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,8 @@ Copyright (c) .NET Foundation. All rights reserved.
UserRuntimeAssemblies="@(UserRuntimeAssembly)"
ResolvedRuntimeTargetsFiles="@(RuntimeTargetsCopyLocalItems)"
IsSelfContained="$(SelfContained)"
IncludeRuntimeFileVersions="$(IncludeFileVersionsInDependencyFile)">
</GenerateDepsFile>
IncludeRuntimeFileVersions="$(IncludeFileVersionsInDependencyFile)"
RuntimeGraphPath="$(BundledRuntimeIdentifierGraphFile)"/>

<ItemGroup>
<!-- Do this in an ItemGroup instead of as an output parameter of the GenerateDepsFile task so that it still gets added to the item set
Expand Down

0 comments on commit 4838de2

Please sign in to comment.