dotnet-installer/src/Microsoft.DotNet.Tools.Compiler/Program.cs

661 lines
26 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using Microsoft.Dnx.Runtime.Common.CommandLine;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.Tools.Common;
using Microsoft.Extensions.ProjectModel;
using Microsoft.Extensions.ProjectModel.Compilation;
2015-11-01 16:21:10 -08:00
using Microsoft.Extensions.ProjectModel.Graph;
using NuGet.Frameworks;
namespace Microsoft.DotNet.Tools.Compiler
{
public class Program
{
public static int Main(string[] args)
{
DebugHelper.HandleDebugSwitch(ref args);
var app = new CommandLineApplication();
app.Name = "dotnet compile";
app.FullName = ".NET Compiler";
app.Description = "Compiler for the .NET Platform";
app.HelpOption("-h|--help");
var output = app.Option("-o|--output <OUTPUT_DIR>", "Directory in which to place outputs", CommandOptionType.SingleValue);
var intermediateOutput = app.Option("-t|--temp-output <OUTPUT_DIR>", "Directory in which to place temporary outputs", CommandOptionType.SingleValue);
var framework = app.Option("-f|--framework <FRAMEWORK>", "Compile a specific framework", CommandOptionType.MultipleValue);
var configuration = app.Option("-c|--configuration <CONFIGURATION>", "Configuration under which to build", CommandOptionType.SingleValue);
var noProjectDependencies = app.Option("--no-project-dependencies", "Skips building project references.", CommandOptionType.NoValue);
var project = app.Argument("<PROJECT>", "The project to compile, defaults to the current directory. Can be a path to a project.json or a project directory");
2015-10-30 15:13:53 -07:00
var native = app.Option("-n|--native", "Compiles source to native machine code.", CommandOptionType.NoValue);
app.OnExecute(() =>
{
// Locate the project and get the name and full path
var path = project.Value;
if (string.IsNullOrEmpty(path))
{
path = Directory.GetCurrentDirectory();
}
var buildProjectReferences = !noProjectDependencies.HasValue();
2015-10-29 12:03:01 -07:00
var isNative = native.HasValue();
// Load project contexts for each framework and compile them
bool success = true;
if (framework.HasValue())
{
foreach (var context in framework.Values.Select(f => ProjectContext.Create(path, NuGetFramework.Parse(f))))
{
success &= Compile(context, configuration.Value() ?? Constants.DefaultConfiguration, output.Value(), intermediateOutput.Value(), buildProjectReferences);
2015-11-01 16:21:10 -08:00
2015-10-29 12:03:01 -07:00
if (isNative)
{
2015-10-30 15:20:14 -07:00
success &= CompileNative(context, configuration.Value() ?? Constants.DefaultConfiguration, output.Value(), buildProjectReferences);
2015-10-29 12:03:01 -07:00
}
}
}
else
{
foreach (var context in ProjectContext.CreateContextForEachFramework(path))
{
success &= Compile(context, configuration.Value() ?? Constants.DefaultConfiguration, output.Value(), intermediateOutput.Value(), buildProjectReferences);
2015-11-01 16:21:10 -08:00
2015-10-29 12:03:01 -07:00
if (isNative)
{
2015-10-30 15:34:50 -07:00
success &= CompileNative(context, configuration.Value() ?? Constants.DefaultConfiguration, output.Value(), buildProjectReferences);
2015-10-29 12:03:01 -07:00
}
}
}
return success ? 0 : 1;
});
try
{
return app.Execute(args);
}
catch (Exception ex)
{
#if DEBUG
Console.Error.WriteLine(ex);
#else
Console.Error.WriteLine(ex.Message);
#endif
return 1;
}
}
2015-10-30 15:13:53 -07:00
private static bool CompileNative(ProjectContext context, string configuration, string outputOptionValue, bool buildProjectReferences)
2015-10-30 14:48:09 -07:00
{
string outputPath = Path.Combine(GetOutputPath(context, configuration, outputOptionValue), "native");
2015-11-01 16:21:10 -08:00
2015-10-29 13:29:38 -07:00
var compilationOptions = context.ProjectFile.GetCompilerOptions(context.TargetFramework, configuration);
2015-10-29 12:03:01 -07:00
var managedBinaryPath = Path.Combine(outputPath, context.ProjectFile.Name + (compilationOptions.EmitEntryPoint.GetValueOrDefault() ? ".exe" : ".dll"));
2015-11-01 16:21:10 -08:00
2015-10-29 12:03:01 -07:00
// Do Native Compilation
2015-10-29 13:29:38 -07:00
var result = Command.Create($"dotnet-compile-native", $"\"{managedBinaryPath}\" \"{outputPath}\"")
2015-10-29 12:03:01 -07:00
.ForwardStdErr()
.ForwardStdOut()
.Execute();
2015-11-01 16:21:10 -08:00
2015-10-29 13:29:38 -07:00
return result.ExitCode == 0;
2015-10-29 12:03:01 -07:00
}
private static bool Compile(ProjectContext context, string configuration, string outputOptionValue, string intermediateOutputValue, bool buildProjectReferences)
{
// Set up Output Paths
2015-10-29 12:03:01 -07:00
string outputPath = GetOutputPath(context, configuration, outputOptionValue);
string intermediateOutputPath = GetIntermediateOutputPath(context, configuration, intermediateOutputValue, outputOptionValue);
2015-10-29 12:03:01 -07:00
Directory.CreateDirectory(outputPath);
Directory.CreateDirectory(intermediateOutputPath);
2015-10-29 12:03:01 -07:00
// Create the library exporter
var exporter = context.CreateExporter(configuration);
var diagnostics = new List<DiagnosticMessage>();
// Collect dependency diagnostics
diagnostics.AddRange(context.LibraryManager.GetAllDiagnostics());
// Gather exports for the project
2015-11-01 16:21:10 -08:00
var dependencies = exporter.GetDependencies().ToList();
if (buildProjectReferences)
{
var projects = new Dictionary<string, ProjectDescription>();
// Build project references
foreach (var dependency in dependencies)
{
var projectDependency = dependency.Library as ProjectDescription;
if (projectDependency != null && projectDependency.Project.Files.SourceFiles.Any())
{
projects[projectDependency.Identity.Name] = projectDependency;
}
}
foreach (var projectDependency in Sort(projects))
{
// Skip compiling project dependencies since we've already figured out the build order
var compileResult = Command.Create("dotnet-compile", $"--framework {projectDependency.Framework} --configuration {configuration} --output \"{outputPath}\" --temp-output \"{intermediateOutputPath}\" --no-project-dependencies \"{projectDependency.Project.ProjectDirectory}\"")
.ForwardStdOut()
.ForwardStdErr()
.Execute();
if (compileResult.ExitCode != 0)
{
return false;
}
}
projects.Clear();
}
2015-11-01 16:21:10 -08:00
Reporter.Output.WriteLine($"Compiling {context.RootProject.Identity.Name.Yellow()} for {context.TargetFramework.DotNetFrameworkName.Yellow()}");
// Dump dependency data
// TODO: Turn on only if verbose, we can look at the response
// file anyways
// ShowDependencyInfo(dependencies);
// Get compilation options
var outputName = GetProjectOutput(context.ProjectFile, context.TargetFramework, configuration, outputPath);
// Assemble args
var compilerArgs = new List<string>()
{
"-nostdlib",
"-nologo",
2015-10-15 12:56:07 -07:00
$"-out:\"{outputName}\""
};
2015-10-21 01:18:14 -07:00
// Default suppressions, some versions of mono don't support these
compilerArgs.Add("-nowarn:CS1701");
compilerArgs.Add("-nowarn:CS1702");
compilerArgs.Add("-nowarn:CS1705");
var compilationOptions = context.ProjectFile.GetCompilerOptions(context.TargetFramework, configuration);
// Add compilation options to the args
2015-10-21 01:18:14 -07:00
ApplyCompilationOptions(compilationOptions, compilerArgs);
foreach (var dependency in dependencies)
{
var projectDependency = dependency.Library as ProjectDescription;
if (projectDependency != null)
{
if (projectDependency.Project.Files.SourceFiles.Any())
{
var projectOutputPath = GetProjectOutput(projectDependency.Project, projectDependency.Framework, configuration, outputPath);
compilerArgs.Add($"-r:\"{projectOutputPath}\"");
}
}
else
{
compilerArgs.AddRange(dependency.CompilationAssemblies.Select(r => $"-r:\"{r.ResolvedPath}\""));
}
compilerArgs.AddRange(dependency.SourceReferences.Select(s => $"\"{s}\""));
}
// Add project source files
var sourceFiles = context.ProjectFile.Files.SourceFiles;
compilerArgs.AddRange(sourceFiles.Select(s => $"\"{s}\""));
if (!AddResources(context.ProjectFile, compilerArgs, intermediateOutputPath))
{
return false;
}
var compilerName = context.ProjectFile.CompilerName;
compilerName = compilerName ?? "csc";
// Write RSP file
var rsp = Path.Combine(intermediateOutputPath, $"dotnet-compile.{compilerName}.{context.ProjectFile.Name}.rsp");
File.WriteAllLines(rsp, compilerArgs);
var result = Command.Create($"dotnet-compile-{compilerName}", $"\"{rsp}\"")
.OnErrorLine(line =>
{
var diagnostic = ParseDiagnostic(context.ProjectDirectory, line);
if (diagnostic != null)
{
diagnostics.Add(diagnostic);
}
else
{
2015-11-01 16:21:10 -08:00
Reporter.Error.WriteLine(line);
}
})
.OnOutputLine(line =>
{
var diagnostic = ParseDiagnostic(context.ProjectDirectory, line);
if (diagnostic != null)
{
diagnostics.Add(diagnostic);
}
else
{
2015-11-01 16:21:10 -08:00
Reporter.Output.WriteLine(line);
}
})
.Execute();
foreach (var diag in diagnostics)
{
PrintDiagnostic(diag);
}
var success = result.ExitCode == 0;
if (success && compilationOptions.EmitEntryPoint.GetValueOrDefault())
{
var runtimeContext = ProjectContext.Create(context.ProjectDirectory, context.TargetFramework, new[] { RuntimeIdentifier.Current });
2015-11-01 16:21:10 -08:00
MakeRunnable(runtimeContext,
outputPath,
runtimeContext.CreateExporter(configuration));
}
PrintSummary(success, diagnostics);
return success;
}
private static string GetProjectOutput(Project project, NuGetFramework framework, string configuration, string outputPath)
{
var compilationOptions = project.GetCompilerOptions(framework, configuration);
var outputExtension = ".dll";
if (framework.IsDesktop() && compilationOptions.EmitEntryPoint.GetValueOrDefault())
{
outputExtension = ".exe";
}
return Path.Combine(outputPath, project.Name + outputExtension);
}
2015-10-29 12:03:01 -07:00
private static string GetOutputPath(ProjectContext context, string configuration, string outputOptionValue)
{
2015-10-30 14:48:09 -07:00
var outputPath = string.Empty;
2015-10-29 12:03:01 -07:00
if (string.IsNullOrEmpty(outputOptionValue))
{
outputPath = Path.Combine(
GetDefaultRootOutputPath(context, outputOptionValue),
Constants.BinDirectoryName,
configuration,
context.TargetFramework.GetTwoDigitShortFolderName());
}
else
{
outputPath = outputOptionValue;
}
return outputPath;
}
private static string GetIntermediateOutputPath(ProjectContext context, string configuration, string intermediateOutputValue, string outputOptionValue)
2015-10-29 12:03:01 -07:00
{
var intermediateOutputPath = string.Empty;
2015-10-29 12:03:01 -07:00
if (string.IsNullOrEmpty(intermediateOutputValue))
2015-10-29 12:03:01 -07:00
{
intermediateOutputPath = Path.Combine(
GetDefaultRootOutputPath(context, outputOptionValue),
Constants.ObjDirectoryName,
configuration,
context.TargetFramework.GetTwoDigitShortFolderName());
}
else
{
intermediateOutputPath = intermediateOutputValue;
2015-10-29 12:03:01 -07:00
}
return intermediateOutputPath;
}
2015-10-29 13:29:38 -07:00
private static string GetDefaultRootOutputPath(ProjectContext context, string outputOptionValue)
2015-10-29 12:03:01 -07:00
{
string rootOutputPath = string.Empty;
2015-10-29 12:03:01 -07:00
if (string.IsNullOrEmpty(outputOptionValue))
{
2015-11-01 16:21:10 -08:00
rootOutputPath = context.ProjectFile.ProjectDirectory;
2015-10-29 12:03:01 -07:00
}
return rootOutputPath;
}
private static void CleanOrCreateDirectory(string path)
{
if (Directory.Exists(path))
2015-11-01 16:21:10 -08:00
{
2015-10-30 14:56:10 -07:00
try
{
Directory.Delete(path, recursive: true);
}
2015-11-01 16:21:10 -08:00
catch (Exception e)
2015-10-30 14:56:10 -07:00
{
Console.WriteLine("Unable to remove directory: " + path);
Console.WriteLine(e.Message);
}
2015-10-29 12:03:01 -07:00
}
2015-11-01 16:21:10 -08:00
2015-10-29 13:29:38 -07:00
Directory.CreateDirectory(path);
2015-10-29 12:03:01 -07:00
}
private static void MakeRunnable(ProjectContext runtimeContext, string outputPath, LibraryExporter exporter)
{
if (runtimeContext.TargetFramework.IsDesktop())
{
// On desktop we need to copy dependencies since we don't own the host
2015-11-01 16:21:10 -08:00
foreach (var export in exporter.GetDependencies())
{
2015-11-01 16:21:10 -08:00
CopyExport(outputPath, export);
}
}
else
{
EmitHost(runtimeContext, outputPath, exporter);
}
}
2015-11-01 16:21:10 -08:00
private static void CopyExport(string outputPath, LibraryExport export)
{
CopyFiles(export.RuntimeAssemblies, outputPath);
CopyFiles(export.NativeLibraries, outputPath);
}
private static void EmitHost(ProjectContext runtimeContext, string outputPath, LibraryExporter exporter)
{
// Write the Host information file (basically a simplified form of the lock file)
var lines = new List<string>();
2015-11-01 16:21:10 -08:00
foreach (var export in exporter.GetAllExports())
{
2015-11-01 16:21:10 -08:00
if (export.Library == runtimeContext.RootProject)
{
continue;
}
2015-11-01 16:21:10 -08:00
if (export.Library is ProjectDescription)
{
// Copy project dependencies to the output folder
CopyFiles(export.RuntimeAssemblies, outputPath);
CopyFiles(export.NativeLibraries, outputPath);
}
else
{
lines.AddRange(GenerateLines(export, export.RuntimeAssemblies, "runtime"));
lines.AddRange(GenerateLines(export, export.NativeLibraries, "native"));
}
}
File.WriteAllLines(Path.Combine(outputPath, runtimeContext.ProjectFile.Name + ".deps"), lines);
// Copy the host in
CopyHost(Path.Combine(outputPath, runtimeContext.ProjectFile.Name + Constants.ExeSuffix));
}
private static void CopyHost(string target)
{
var hostPath = Path.Combine(AppContext.BaseDirectory, Constants.HostExecutableName);
File.Copy(hostPath, target, overwrite: true);
}
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)
{
2015-11-01 16:21:10 -08:00
Reporter.Output.WriteLine();
var errorCount = diagnostics.Count(d => d.Severity == DiagnosticMessageSeverity.Error);
var warningCount = diagnostics.Count(d => d.Severity == DiagnosticMessageSeverity.Warning);
if (errorCount > 0)
{
Reporter.Output.WriteLine("Compilation failed.".Red());
}
else
2015-10-18 07:32:42 -07:00
{
Reporter.Output.WriteLine("Compilation succeeded.".Green());
2015-10-18 07:32:42 -07:00
}
Reporter.Output.WriteLine($" {warningCount} Warning(s)");
Reporter.Output.WriteLine($" {errorCount} Error(s)");
2015-11-01 16:21:10 -08:00
Reporter.Output.WriteLine();
}
private static bool AddResources(Project project, List<string> compilerArgs, string intermediateOutputPath)
{
string root = PathUtility.EnsureTrailingSlash(project.ProjectDirectory);
foreach (var resourceFile in project.Files.ResourceFiles)
{
string resourceName = null;
string rootNamespace = null;
var resourcePath = resourceFile.Key;
if (string.IsNullOrEmpty(resourceFile.Value))
{
// No logical name, so use the file name
resourceName = ResourcePathUtility.GetResourceName(root, resourcePath);
rootNamespace = project.Name;
}
else
{
resourceName = CreateCSharpManifestResourceName.EnsureResourceExtension(resourceFile.Value, resourcePath);
rootNamespace = null;
}
var name = CreateCSharpManifestResourceName.CreateManifestName(resourceName, rootNamespace);
var fileName = resourcePath;
if (ResourcePathUtility.IsResxResourceFile(fileName))
{
var ext = Path.GetExtension(fileName);
if (string.Equals(ext, ".resx", StringComparison.OrdinalIgnoreCase))
{
// {file}.resx -> {file}.resources
var resourcesFile = Path.Combine(intermediateOutputPath, name);
var result = Command.Create("resgen", $"\"{fileName}\" \"{resourcesFile}\"")
.ForwardStdErr()
.ForwardStdOut()
.Execute();
if (result.ExitCode != 0)
{
return false;
}
// Use this as the resource name instead
fileName = resourcesFile;
}
}
compilerArgs.Add($"-resource:\"{fileName}\",{name}");
}
return true;
}
private static ISet<ProjectDescription> Sort(Dictionary<string, ProjectDescription> projects)
{
var outputs = new HashSet<ProjectDescription>();
foreach (var pair in projects)
{
Sort(pair.Value, projects, outputs);
}
return outputs;
}
private static void Sort(ProjectDescription project, Dictionary<string, ProjectDescription> projects, ISet<ProjectDescription> outputs)
{
// Sorts projects in dependency order so that we only build them once per chain
foreach (var dependency in project.Dependencies)
{
ProjectDescription projectDependency;
if (projects.TryGetValue(dependency.Name, out projectDependency))
{
Sort(projectDependency, projects, outputs);
}
}
outputs.Add(project);
}
private static DiagnosticMessage ParseDiagnostic(string projectRootPath, string line)
{
var error = CanonicalError.Parse(line);
if (error != null)
{
var severity = error.category == CanonicalError.Parts.Category.Error ?
DiagnosticMessageSeverity.Error : DiagnosticMessageSeverity.Warning;
return new DiagnosticMessage(
error.code,
error.text,
Path.IsPathRooted(error.origin) ? line : projectRootPath + Path.DirectorySeparatorChar + line,
Path.Combine(projectRootPath, error.origin),
severity,
error.line,
error.column,
error.endColumn,
error.endLine,
source: null);
}
return null;
}
private static void PrintDiagnostic(DiagnosticMessage diag)
{
switch (diag.Severity)
{
case DiagnosticMessageSeverity.Info:
Reporter.Error.WriteLine(diag.FormattedMessage);
break;
case DiagnosticMessageSeverity.Warning:
Reporter.Error.WriteLine(diag.FormattedMessage.Yellow().Bold());
break;
case DiagnosticMessageSeverity.Error:
Reporter.Error.WriteLine(diag.FormattedMessage.Red().Bold());
break;
}
}
2015-10-21 01:18:14 -07:00
private static void ApplyCompilationOptions(CompilerOptions compilationOptions, List<string> compilerArgs)
{
2015-10-15 12:56:07 -07:00
var targetType = compilationOptions.EmitEntryPoint.GetValueOrDefault() ? "exe" : "library";
2015-10-21 01:18:14 -07:00
compilerArgs.Add($"-target:{targetType}");
2015-10-15 12:56:07 -07:00
if (compilationOptions.AllowUnsafe.GetValueOrDefault())
{
2015-10-21 01:18:14 -07:00
compilerArgs.Add("-unsafe+");
}
2015-10-21 01:18:14 -07:00
compilerArgs.AddRange(compilationOptions.Defines.Select(d => $"-d:{d}"));
2015-10-15 12:56:07 -07:00
if (compilationOptions.Optimize.GetValueOrDefault())
{
2015-10-21 01:18:14 -07:00
compilerArgs.Add("-optimize");
}
if (!string.IsNullOrEmpty(compilationOptions.Platform))
{
2015-10-21 01:18:14 -07:00
compilerArgs.Add($"-platform:{compilationOptions.Platform}");
}
2015-10-15 12:56:07 -07:00
if (compilationOptions.WarningsAsErrors.GetValueOrDefault())
{
2015-10-21 01:18:14 -07:00
compilerArgs.Add("-warnaserror");
}
if (compilationOptions.DelaySign.GetValueOrDefault())
{
2015-10-21 01:18:14 -07:00
compilerArgs.Add("-delaysign+");
}
if (!string.IsNullOrEmpty(compilationOptions.KeyFile))
{
2015-10-21 01:18:14 -07:00
compilerArgs.Add($"-keyFile:\"{compilationOptions.KeyFile}\"");
}
2015-10-21 01:18:14 -07:00
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
2015-10-21 01:18:14 -07:00
compilerArgs.Add("-debug:full");
}
else
{
2015-10-21 01:18:14 -07:00
compilerArgs.Add("-debug:portable");
}
// TODO: OSS signing
}
private static void ShowDependencyInfo(IEnumerable<LibraryExport> dependencies)
{
foreach (var dependency in dependencies)
{
if (!dependency.Library.Resolved)
{
Reporter.Error.WriteLine($" Unable to resolve dependency {dependency.Library.Identity.ToString().Red().Bold()}");
Reporter.Error.WriteLine("");
}
else
{
Reporter.Output.WriteLine($" Using {dependency.Library.Identity.Type.Value.Cyan().Bold()} dependency {dependency.Library.Identity.ToString().Cyan().Bold()}");
Reporter.Output.WriteLine($" Path: {dependency.Library.Path}");
foreach (var metadataReference in dependency.CompilationAssemblies)
{
Reporter.Output.WriteLine($" Assembly: {metadataReference}");
}
foreach (var sourceReference in dependency.SourceReferences)
{
Reporter.Output.WriteLine($" Source: {sourceReference}");
}
Reporter.Output.WriteLine("");
}
}
}
private static void CopyFiles(IEnumerable<LibraryAsset> files, string outputPath)
{
foreach (var file in files)
{
File.Copy(file.ResolvedPath, Path.Combine(outputPath, Path.GetFileName(file.ResolvedPath)), overwrite: true);
}
}
}
}