This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 219
/
SharedFrameworkPublisher.cs
219 lines (184 loc) · 9.86 KB
/
SharedFrameworkPublisher.cs
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using Microsoft.DotNet.InternalAbstractions;
using Microsoft.DotNet.Cli.Build.Framework;
using Newtonsoft.Json;
using static Microsoft.DotNet.Cli.Build.Framework.BuildHelpers;
using static Microsoft.DotNet.Cli.Build.FS;
namespace Microsoft.DotNet.Cli.Build
{
public class SharedFrameworkPublisher
{
public static string s_sharedFrameworkName = "Microsoft.NETCore.App";
private string _sharedFrameworkTemplateSourceRoot;
private string _sharedFrameworkNugetVersion;
private string _sharedFrameworkRid;
private string _sharedFrameworkSourceRoot;
private string _repoRoot;
private string _corehostLockedDirectory;
private string _corehostLatestDirectory;
private Crossgen _crossgenUtil = new Crossgen(DependencyVersions.CoreCLRVersion, DependencyVersions.JitVersion);
private string _corehostPackageSource;
public SharedFrameworkPublisher(
string repoRoot,
string corehostLockedDirectory,
string corehostLatestDirectory,
string corehostPackageSource,
string sharedFrameworkNugetVersion)
{
_repoRoot = repoRoot;
_corehostLockedDirectory = corehostLockedDirectory;
_corehostLatestDirectory = corehostLatestDirectory;
_corehostPackageSource = corehostPackageSource;
_sharedFrameworkTemplateSourceRoot = Path.Combine(repoRoot, "src", "sharedframework", "framework");
_sharedFrameworkNugetVersion = sharedFrameworkNugetVersion;
_sharedFrameworkRid = ComputeSharedFrameworkRid();
_sharedFrameworkSourceRoot = GenerateSharedFrameworkProject(
_sharedFrameworkNugetVersion,
_sharedFrameworkTemplateSourceRoot,
_sharedFrameworkRid);
}
public static string GetSharedFrameworkPublishPath(string outputRootDirectory, string sharedFrameworkNugetVersion)
{
return Path.Combine(
outputRootDirectory,
"shared",
s_sharedFrameworkName,
sharedFrameworkNugetVersion);
}
private string ComputeSharedFrameworkRid()
{
return RuntimeEnvironment.OperatingSystemPlatform == Platform.Windows
? $"win7-{RuntimeEnvironment.RuntimeArchitecture}"
: RuntimeEnvironment.GetRuntimeIdentifier();
}
public void CopyMuxer(string sharedFrameworkPublishRoot)
{
File.Copy(
Path.Combine(_corehostLockedDirectory, HostArtifactNames.DotnetHostBaseName),
Path.Combine(sharedFrameworkPublishRoot, HostArtifactNames.DotnetHostBaseName), true);
}
public void CopyHostFxrToVersionedDirectory(string rootDirectory, string hostFxrVersion)
{
var hostFxrVersionedDirectory = Path.Combine(rootDirectory, "host", "fxr", hostFxrVersion);
FS.Mkdirp(hostFxrVersionedDirectory);
File.Copy(
Path.Combine(_corehostLockedDirectory, HostArtifactNames.DotnetHostFxrBaseName),
Path.Combine(hostFxrVersionedDirectory, HostArtifactNames.DotnetHostFxrBaseName), true);
}
public void PublishSharedFramework(string outputRootDirectory, string commitHash, DotNetCli dotnetCli, string hostFxrVersion)
{
dotnetCli.Restore(
"--verbosity", "verbose",
"--disable-parallel",
"--infer-runtimes",
"--fallbacksource", _corehostPackageSource)
.WorkingDirectory(_sharedFrameworkSourceRoot)
.Execute()
.EnsureSuccessful();
// We publish to a sub folder of the PublishRoot so tools like heat and zip can generate folder structures easier.
string sharedFrameworkNameAndVersionRoot = GetSharedFrameworkPublishPath(outputRootDirectory, _sharedFrameworkNugetVersion);
if (Directory.Exists(sharedFrameworkNameAndVersionRoot))
{
Utils.DeleteDirectory(sharedFrameworkNameAndVersionRoot);
}
dotnetCli.Publish(
"--output", sharedFrameworkNameAndVersionRoot,
"-r", _sharedFrameworkRid,
_sharedFrameworkSourceRoot)
.Execute()
.EnsureSuccessful();
// Clean up artifacts that dotnet-publish generates which we don't need
PublishMutationUtilties.CleanPublishOutput(
sharedFrameworkNameAndVersionRoot,
"framework",
deleteRuntimeConfigJson: true,
deleteDepsJson: false);
// Rename the .deps file
var destinationDeps = Path.Combine(sharedFrameworkNameAndVersionRoot, $"{s_sharedFrameworkName}.deps.json");
File.Move(Path.Combine(sharedFrameworkNameAndVersionRoot, "framework.deps.json"), destinationDeps);
PublishMutationUtilties.ChangeEntryPointLibraryName(destinationDeps, null);
// Generate RID fallback graph
GenerateRuntimeGraph(dotnetCli, destinationDeps);
CopyHostArtifactsToSharedFramework(sharedFrameworkNameAndVersionRoot, hostFxrVersion);
_crossgenUtil.CrossgenDirectory(sharedFrameworkNameAndVersionRoot, sharedFrameworkNameAndVersionRoot);
// Generate .version file for sharedfx
var version = _sharedFrameworkNugetVersion;
var content = $@"{commitHash}{Environment.NewLine}{version}{Environment.NewLine}";
File.WriteAllText(Path.Combine(sharedFrameworkNameAndVersionRoot, ".version"), content);
return;
}
private void GenerateRuntimeGraph(DotNetCli dotnetCli, string destinationDeps)
{
string runtimeGraphGeneratorRuntime = null;
switch (RuntimeEnvironment.OperatingSystemPlatform)
{
case Platform.Windows:
runtimeGraphGeneratorRuntime = "win";
break;
case Platform.Linux:
runtimeGraphGeneratorRuntime = "linux";
break;
case Platform.Darwin:
runtimeGraphGeneratorRuntime = "osx";
break;
}
if (!string.IsNullOrEmpty(runtimeGraphGeneratorRuntime))
{
var runtimeGraphGeneratorName = "RuntimeGraphGenerator";
var runtimeGraphGeneratorProject = Path.Combine(Dirs.RepoRoot, "tools", "independent", runtimeGraphGeneratorName);
var runtimeGraphGeneratorOutput = Path.Combine(Dirs.Output, "tools", "independent", runtimeGraphGeneratorName);
dotnetCli.Publish(
"--output", runtimeGraphGeneratorOutput,
runtimeGraphGeneratorProject).Execute().EnsureSuccessful();
var runtimeGraphGeneratorExe = Path.Combine(runtimeGraphGeneratorOutput, $"{runtimeGraphGeneratorName}{Constants.ExeSuffix}");
Cmd(runtimeGraphGeneratorExe, "--project", _sharedFrameworkSourceRoot, "--deps", destinationDeps, runtimeGraphGeneratorRuntime)
.Execute()
.EnsureSuccessful();
}
else
{
throw new Exception($"Could not determine rid graph generation runtime for platform {RuntimeEnvironment.OperatingSystemPlatform}");
}
}
private void CopyHostArtifactsToSharedFramework(string sharedFrameworkNameAndVersionRoot, string hostFxrVersion)
{
File.Copy(
Path.Combine(_corehostLockedDirectory, HostArtifactNames.DotnetHostBaseName),
Path.Combine(sharedFrameworkNameAndVersionRoot, HostArtifactNames.DotnetHostBaseName), true);
File.Copy(
Path.Combine(_corehostLockedDirectory, HostArtifactNames.DotnetHostBaseName),
Path.Combine(sharedFrameworkNameAndVersionRoot, $"corehost{Constants.ExeSuffix}"), true);
File.Copy(
Path.Combine(_corehostLockedDirectory, HostArtifactNames.DotnetHostFxrBaseName),
Path.Combine(sharedFrameworkNameAndVersionRoot, HostArtifactNames.DotnetHostFxrBaseName), true);
// Hostpolicy should be the latest and not the locked version as it is supposed to evolve for
// the framework and has a tight coupling with coreclr's API in the framework.
File.Copy(
Path.Combine(_corehostLatestDirectory, HostArtifactNames.HostPolicyBaseName),
Path.Combine(sharedFrameworkNameAndVersionRoot, HostArtifactNames.HostPolicyBaseName), true);
}
private string GenerateSharedFrameworkProject(
string sharedFrameworkNugetVersion,
string sharedFrameworkTemplatePath,
string rid)
{
string sharedFrameworkProjectPath = Path.Combine(Dirs.Intermediate, "sharedFramework", "framework");
Utils.DeleteDirectory(sharedFrameworkProjectPath);
CopyRecursive(sharedFrameworkTemplatePath, sharedFrameworkProjectPath, true);
string templateFile = Path.Combine(sharedFrameworkProjectPath, "project.json.template");
JObject sharedFrameworkProject = JsonUtils.ReadProject(templateFile);
sharedFrameworkProject["dependencies"]["Microsoft.NETCore.App"] = sharedFrameworkNugetVersion;
((JObject)sharedFrameworkProject["runtimes"]).RemoveAll();
sharedFrameworkProject["runtimes"][rid] = new JObject();
string projectJsonPath = Path.Combine(sharedFrameworkProjectPath, "project.json");
JsonUtils.WriteProject(sharedFrameworkProject, projectJsonPath);
Rm(templateFile);
return sharedFrameworkProjectPath;
}
}
}