This repository has been archived by the owner on Apr 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Executable.cs
345 lines (289 loc) · 13.2 KB
/
Executable.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml.Linq;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.Files;
using Microsoft.DotNet.ProjectModel;
using Microsoft.DotNet.ProjectModel.Compilation;
using Microsoft.DotNet.ProjectModel.Files;
using Microsoft.DotNet.ProjectModel.Graph;
using Microsoft.DotNet.Tools.Common;
using Microsoft.Extensions.DependencyModel;
using NuGet.Frameworks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace Microsoft.DotNet.Cli.Compiler.Common
{
public class Executable
{
private readonly ProjectContext _context;
private readonly LibraryExporter _exporter;
private readonly string _configuration;
private readonly OutputPaths _outputPaths;
private readonly string _runtimeOutputPath;
private readonly string _intermediateOutputPath;
private readonly CommonCompilerOptions _compilerOptions;
public Executable(ProjectContext context, OutputPaths outputPaths, LibraryExporter exporter, string configuration)
: this(context, outputPaths, outputPaths.RuntimeOutputPath, outputPaths.IntermediateOutputDirectoryPath, exporter, configuration) { }
public Executable(ProjectContext context, OutputPaths outputPaths, string runtimeOutputPath, string intermediateOutputDirectoryPath, LibraryExporter exporter, string configuration)
{
_context = context;
_outputPaths = outputPaths;
_runtimeOutputPath = runtimeOutputPath;
_intermediateOutputPath = intermediateOutputDirectoryPath;
_exporter = exporter;
_configuration = configuration;
_compilerOptions = _context.ProjectFile.GetCompilerOptions(_context.TargetFramework, configuration);
}
public void MakeCompilationOutputRunnable()
{
CopyContentFiles();
ExportRuntimeAssets();
}
private void VerifyCoreClrPresenceInPackageGraph()
{
var isCoreClrPresent = _exporter
.GetAllExports()
.SelectMany(e => e.NativeLibraryGroups)
.SelectMany(g => g.Assets)
.Select(a => a.FileName)
.Where(f => Constants.LibCoreClrBinaryNames.Contains(f))
.Any();
// coreclr should be present for standalone apps
if (!isCoreClrPresent)
{
throw new InvalidOperationException("Expected coreclr library not found in package graph. Please try running dotnet restore again.");
}
}
private void ExportRuntimeAssets()
{
if (_context.TargetFramework.IsDesktop())
{
MakeCompilationOutputRunnableForFullFramework();
}
else
{
MakeCompilationOutputRunnableForCoreCLR();
}
}
private void MakeCompilationOutputRunnableForFullFramework()
{
var dependencies = _exporter.GetDependencies();
CopyAssemblies(dependencies);
CopyAssets(dependencies);
GenerateBindingRedirects(_exporter);
}
private void MakeCompilationOutputRunnableForCoreCLR()
{
WriteDepsFileAndCopyProjectDependencies(_exporter);
var emitEntryPoint = _compilerOptions.EmitEntryPoint ?? false;
if (emitEntryPoint && !_context.IsPortable)
{
// TODO: Pick a host based on the RID
VerifyCoreClrPresenceInPackageGraph();
CoreHost.CopyTo(_runtimeOutputPath, _compilerOptions.OutputName + Constants.ExeSuffix);
}
}
private void CopyContentFiles()
{
var contentFiles = new ContentFiles(_context);
if (_compilerOptions.CopyToOutputInclude != null)
{
var includeEntries = IncludeFilesResolver.GetIncludeFiles(
_compilerOptions.CopyToOutputInclude,
PathUtility.EnsureTrailingSlash(_runtimeOutputPath),
diagnostics: null);
contentFiles.StructuredCopyTo(_runtimeOutputPath, includeEntries);
}
else
{
contentFiles.StructuredCopyTo(_runtimeOutputPath);
}
}
private void CopyAssemblies(IEnumerable<LibraryExport> libraryExports)
{
foreach (var libraryExport in libraryExports)
{
libraryExport.RuntimeAssemblyGroups.GetDefaultAssets().CopyTo(_runtimeOutputPath);
libraryExport.NativeLibraryGroups.GetDefaultAssets().CopyTo(_runtimeOutputPath);
foreach (var group in libraryExport.ResourceAssemblies.GroupBy(r => r.Locale))
{
var localeSpecificDir = Path.Combine(_runtimeOutputPath, group.Key);
if (!Directory.Exists(localeSpecificDir))
{
Directory.CreateDirectory(localeSpecificDir);
}
group.Select(r => r.Asset).CopyTo(localeSpecificDir);
}
}
}
private void CopyAssets(IEnumerable<LibraryExport> libraryExports)
{
foreach (var libraryExport in libraryExports)
{
libraryExport.RuntimeAssets.StructuredCopyTo(
_runtimeOutputPath,
_intermediateOutputPath);
}
}
private void WriteDepsFileAndCopyProjectDependencies(LibraryExporter exporter)
{
var exports = exporter.GetAllExports().ToList();
var exportsLookup = exports.ToDictionary(e => e.Library.Identity.Name);
var platformExclusionList = _context.GetPlatformExclusionList(exportsLookup);
var filteredExports = exports.FilterExports(platformExclusionList);
WriteConfigurationFiles(exports, filteredExports, exports, includeDevConfig: true);
var projectExports = exporter.GetAllProjectTypeDependencies();
CopyAssemblies(projectExports);
CopyAssets(projectExports);
var packageExports = exporter.GetDependencies(LibraryType.Package);
CopyAssets(packageExports);
}
public void WriteConfigurationFiles(
IEnumerable<LibraryExport> allExports,
IEnumerable<LibraryExport> depsRuntimeExports,
IEnumerable<LibraryExport> depsCompilationExports,
bool includeDevConfig)
{
WriteDeps(depsRuntimeExports, depsCompilationExports);
if (_context.ProjectFile.HasRuntimeOutput(_configuration))
{
WriteRuntimeConfig(allExports);
if (includeDevConfig)
{
WriteDevRuntimeConfig();
}
}
}
private void WriteRuntimeConfig(IEnumerable<LibraryExport> allExports)
{
if (!_context.TargetFramework.IsDesktop())
{
// TODO: Suppress this file if there's nothing to write? RuntimeOutputFiles would have to be updated
// in order to prevent breaking incremental compilation...
var json = new JObject();
var runtimeOptions = new JObject();
json.Add("runtimeOptions", runtimeOptions);
WriteFramework(runtimeOptions, allExports);
WriteRuntimeOptions(runtimeOptions);
var runtimeConfigJsonFile =
Path.Combine(_runtimeOutputPath, _compilerOptions.OutputName + FileNameSuffixes.RuntimeConfigJson);
using (var writer = new JsonTextWriter(new StreamWriter(File.Create(runtimeConfigJsonFile))))
{
writer.Formatting = Formatting.Indented;
json.WriteTo(writer);
}
}
}
private void WriteFramework(JObject runtimeOptions, IEnumerable<LibraryExport> allExports)
{
var redistPackage = _context.PlatformLibrary;
if (redistPackage != null)
{
var packageName = redistPackage.Identity.Name;
var redistExport = allExports.FirstOrDefault(e => e.Library.Identity.Name.Equals(packageName));
if (redistExport == null)
{
throw new InvalidOperationException($"Platform package '{packageName}' was not present in the graph.");
}
else
{
var framework = new JObject(
new JProperty("name", redistExport.Library.Identity.Name),
new JProperty("version", redistExport.Library.Identity.Version.ToNormalizedString()));
runtimeOptions.Add("framework", framework);
}
}
}
private void WriteRuntimeOptions(JObject runtimeOptions)
{
if (string.IsNullOrEmpty(_context.ProjectFile.RawRuntimeOptions))
{
return;
}
var runtimeOptionsFromProjectJson = JObject.Parse(_context.ProjectFile.RawRuntimeOptions);
foreach (var runtimeOption in runtimeOptionsFromProjectJson)
{
runtimeOptions.Add(runtimeOption.Key, runtimeOption.Value);
}
}
private void WriteDevRuntimeConfig()
{
if (_context.TargetFramework.IsDesktop())
{
return;
}
var json = new JObject();
var runtimeOptions = new JObject();
json.Add("runtimeOptions", runtimeOptions);
AddAdditionalProbingPaths(runtimeOptions);
var runtimeConfigDevJsonFile =
Path.Combine(_runtimeOutputPath, _compilerOptions.OutputName + FileNameSuffixes.RuntimeConfigDevJson);
using (var writer = new JsonTextWriter(new StreamWriter(File.Create(runtimeConfigDevJsonFile))))
{
writer.Formatting = Formatting.Indented;
json.WriteTo(writer);
}
}
private void AddAdditionalProbingPaths(JObject runtimeOptions)
{
if (_context.LockFile != null)
{
var additionalProbingPaths = new JArray();
foreach (var packageFolder in _context.LockFile.PackageFolders)
{
additionalProbingPaths.Add(packageFolder.Path);
}
runtimeOptions.Add("additionalProbingPaths", additionalProbingPaths);
}
}
public void WriteDeps(IEnumerable<LibraryExport> runtimeExports, IEnumerable<LibraryExport> compilationExports)
{
Directory.CreateDirectory(_runtimeOutputPath);
var includeCompile = _compilerOptions.PreserveCompilationContext == true;
var dependencyContext = new DependencyContextBuilder().Build(
compilerOptions: includeCompile ? _compilerOptions : null,
compilationExports: includeCompile ? compilationExports : null,
runtimeExports: runtimeExports,
portable: _context.IsPortable,
target: _context.TargetFramework,
runtime: _context.RuntimeIdentifier ?? string.Empty);
var writer = new DependencyContextWriter();
var depsJsonFilePath = Path.Combine(_runtimeOutputPath, _compilerOptions.OutputName + FileNameSuffixes.DepsJson);
using (var fileStream = File.Create(depsJsonFilePath))
{
writer.Write(dependencyContext, fileStream);
}
}
public void GenerateBindingRedirects(LibraryExporter exporter)
{
var outputName = _outputPaths.RuntimeFiles.Assembly;
var configFile = outputName + Constants.ConfigSuffix;
var existingConfig = new DirectoryInfo(_context.ProjectDirectory)
.EnumerateFiles()
.FirstOrDefault(f => f.Name.Equals("app.config", StringComparison.OrdinalIgnoreCase));
if (existingConfig != null)
{
File.Copy(existingConfig.FullName, configFile, true);
}
List<string> configFiles = new List<string>();
configFiles.Add(configFile);
foreach (var export in exporter.GetDependencies())
{
var dependencyExecutables = export.RuntimeAssemblyGroups.GetDefaultAssets()
.Where(asset => asset.FileName.ToLower().EndsWith(FileNameSuffixes.DotNet.Exe))
.Select(asset => Path.Combine(_runtimeOutputPath, asset.FileName));
foreach (var executable in dependencyExecutables)
{
configFile = executable + Constants.ConfigSuffix;
configFiles.Add(configFile);
}
}
exporter.GetAllExports().GenerateBindingRedirects(configFiles);
}
}
}