Add a shared runtime aware host

And use it in dotnet-compile
This commit is contained in:
Andrew Stanton-Nurse 2015-10-21 15:21:14 -07:00
parent d8ab5357fd
commit e70a169b28
45 changed files with 1881 additions and 152 deletions

View file

@ -158,8 +158,13 @@ namespace Microsoft.DotNet.Tools.Compiler
// Get compilation options
var compilationOptions = context.ProjectFile.GetCompilerOptions(context.TargetFramework, configuration);
var outputName = Path.Combine(outputPath, context.ProjectFile.Name + (compilationOptions.EmitEntryPoint.GetValueOrDefault() ? ".exe" : ".dll"));
var outputExtension = ".dll";
if (context.TargetFramework.IsDesktop() && compilationOptions.EmitEntryPoint.GetValueOrDefault())
{
outputExtension = ".exe";
}
var outputName = Path.Combine(outputPath, context.ProjectFile.Name + outputExtension);
// Assemble args
var compilerArgs = new List<string>()
{
@ -172,13 +177,13 @@ namespace Microsoft.DotNet.Tools.Compiler
compilerArgs.Add("-nowarn:CS1701");
compilerArgs.Add("-nowarn:CS1702");
compilerArgs.Add("-nowarn:CS1705");
// Add compilation options to the args
ApplyCompilationOptions(compilationOptions, compilerArgs);
foreach (var dependency in dependencies)
{
compilerArgs.AddRange(dependency.CompilationAssemblies.Select(r => $"-r:\"{r}\""));
compilerArgs.AddRange(dependency.CompilationAssemblies.Select(r => $"-r:\"{r.ResolvedPath}\""));
compilerArgs.AddRange(dependency.SourceReferences.Select(s => $"\"{s}\""));
}
@ -233,11 +238,56 @@ namespace Microsoft.DotNet.Tools.Compiler
var success = result.ExitCode == 0;
if (success && !context.TargetFramework.IsDesktop() && compilationOptions.EmitEntryPoint.GetValueOrDefault())
{
var runtimeContext = ProjectContext.Create(context.ProjectDirectory, context.TargetFramework, new [] { RuntimeIdentifier.Current });
EmitHost(outputPath, context.ProjectFile.Name, runtimeContext.CreateExporter(configuration));
}
PrintSummary(success, diagnostics);
return success;
}
private static void EmitHost(string outputPath, string projectName, LibraryExporter exporter)
{
// Write the Host information file (basically a simplified form of the lock file)
List<string> lines = new List<string>();
foreach(var export in exporter.GetAllExports())
{
lines.AddRange(GenerateLines(export, export.RuntimeAssemblies, "runtime"));
lines.AddRange(GenerateLines(export, export.NativeLibraries, "native"));
}
File.WriteAllLines(Path.Combine(outputPath, projectName + ".deps"), lines);
// Copy the host in
CopyHost(Path.Combine(outputPath, projectName + Constants.ExeSuffix));
}
private static void CopyHost(string target)
{
var hostPath = Path.Combine(AppContext.BaseDirectory, Constants.HostExecutableName);
File.Copy(hostPath, target);
}
private static IEnumerable<string> GenerateLines(LibraryExport export, IEnumerable<LibraryAsset> items, string type)
{
return items.Select(item =>
EscapeCsv(export.Library.Identity.Type.Value) + "," +
EscapeCsv(export.Library.Identity.Name) + "," +
EscapeCsv(export.Library.Identity.Version.ToNormalizedString()) + "," +
EscapeCsv(export.Library.Hash) + "," +
EscapeCsv(type) + "," +
EscapeCsv(item.Name) + "," +
EscapeCsv(item.RelativePath) + ",");
}
private static string EscapeCsv(string input)
{
return "\"" + input.Replace("\\", "\\\\").Replace("\"", "\\\"") + "\"";
}
private static void PrintSummary(bool success, List<DiagnosticMessage> diagnostics)
{
Reporter.Output.Writer.WriteLine();
@ -351,7 +401,7 @@ namespace Microsoft.DotNet.Tools.Compiler
{
var severity = error.category == CanonicalError.Parts.Category.Error ?
DiagnosticMessageSeverity.Error : DiagnosticMessageSeverity.Warning;
return new DiagnosticMessage(
error.code,
error.text,
@ -367,7 +417,7 @@ namespace Microsoft.DotNet.Tools.Compiler
return null;
}
private static void PrintDiagnostic(DiagnosticMessage diag)
{
switch (diag.Severity)