Create (de)serializer for CompilerOptions
This allows language-specific compile utilities to map a set of common arguments to their compiler's specific argument set.
This commit is contained in:
parent
b1d3ef4b75
commit
9d5f435ef5
12 changed files with 407 additions and 99 deletions
|
@ -1,36 +1,162 @@
|
|||
using System;
|
||||
using System.CommandLine;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using Microsoft.Dnx.Runtime.Common.CommandLine;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
|
||||
using Microsoft.DotNet.Cli.Compiler.Common;
|
||||
using Microsoft.DotNet.Cli.Utils;
|
||||
using Microsoft.Extensions.ProjectModel;
|
||||
|
||||
namespace Microsoft.DotNet.Tools.Compiler.Csc
|
||||
{
|
||||
public class Program
|
||||
{
|
||||
private const int ExitFailed = 1;
|
||||
|
||||
public static int Main(string[] args)
|
||||
{
|
||||
DebugHelper.HandleDebugSwitch(ref args);
|
||||
|
||||
var app = new CommandLineApplication();
|
||||
app.Name = "dotnet compile csc";
|
||||
app.FullName = "CSharp compiler";
|
||||
app.Description = "CSharp Compiler for the .NET Platform";
|
||||
app.HelpOption("-h|--help");
|
||||
CommonCompilerOptions commonOptions = null;
|
||||
string tempOutDir = null;
|
||||
IReadOnlyList<string> references = Array.Empty<string>();
|
||||
IReadOnlyList<string> resources = Array.Empty<string>();
|
||||
IReadOnlyList<string> sources = Array.Empty<string>();
|
||||
string outputName = null;
|
||||
|
||||
var responseFileArg = app.Argument("<CONFIG>", "The response file to pass to the compiler.");
|
||||
|
||||
app.OnExecute(() =>
|
||||
try
|
||||
{
|
||||
// Execute CSC!
|
||||
var result = RunCsc($"-noconfig @\"{responseFileArg.Value}\"")
|
||||
.ForwardStdErr()
|
||||
.ForwardStdOut()
|
||||
.Execute();
|
||||
ArgumentSyntax.Parse(args, syntax =>
|
||||
{
|
||||
commonOptions = CommonCompilerOptionsExtensions.Parse(syntax);
|
||||
|
||||
return result.ExitCode;
|
||||
});
|
||||
syntax.DefineOption("temp-output", ref tempOutDir, "Compilation temporary directory");
|
||||
|
||||
return app.Execute(args);
|
||||
syntax.DefineOption("out", ref outputName, "Name of the output assembly");
|
||||
|
||||
syntax.DefineOptionList("r|reference", ref references, "Path to a compiler metadata reference");
|
||||
|
||||
syntax.DefineOptionList("resource", ref resources, "Resources to embed");
|
||||
|
||||
syntax.DefineParameterList("source-files", ref sources, "Compilation sources");
|
||||
|
||||
if (tempOutDir == null)
|
||||
{
|
||||
syntax.ReportError("Option '--temp-output' is required");
|
||||
}
|
||||
});
|
||||
}
|
||||
catch (ArgumentSyntaxException)
|
||||
{
|
||||
return ExitFailed;
|
||||
}
|
||||
|
||||
var translated = TranslateCommonOptions(commonOptions);
|
||||
|
||||
var allArgs = new List<string>(translated);
|
||||
allArgs.AddRange(GetDefaultOptions());
|
||||
|
||||
if (outputName != null)
|
||||
{
|
||||
allArgs.Add($"-out:{outputName}");
|
||||
}
|
||||
|
||||
allArgs.AddRange(references.Select(r => $"-r:{r}"));
|
||||
allArgs.AddRange(resources.Select(resource => $"-resource:{resource}"));
|
||||
allArgs.AddRange(sources);
|
||||
|
||||
var rsp = Path.Combine(tempOutDir.Trim('"'), "dotnet-compile-csc.rsp");
|
||||
|
||||
File.WriteAllLines(rsp, allArgs, Encoding.UTF8);
|
||||
|
||||
// Execute CSC!
|
||||
var result = RunCsc($"-noconfig @\"{rsp}\"")
|
||||
.ForwardStdErr()
|
||||
.ForwardStdOut()
|
||||
.Execute();
|
||||
|
||||
return result.ExitCode;
|
||||
}
|
||||
|
||||
// TODO: Review if this is the place for default options
|
||||
private static IEnumerable<string> GetDefaultOptions()
|
||||
{
|
||||
var args = new List<string>()
|
||||
{
|
||||
"-nostdlib",
|
||||
"-nologo"
|
||||
};
|
||||
|
||||
args.Add(RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
|
||||
? "-debug:full"
|
||||
: "-debug:portable");
|
||||
|
||||
// TODO: Move mono args to mcs compiler
|
||||
args.Add("-nowarn:CS1701");
|
||||
args.Add("-nowarn:CS1702");
|
||||
args.Add("-nowarn:CS1705");
|
||||
|
||||
return args;
|
||||
}
|
||||
|
||||
private static IEnumerable<string> TranslateCommonOptions(CommonCompilerOptions options)
|
||||
{
|
||||
List<string> commonArgs = new List<string>();
|
||||
|
||||
if (options.Defines != null)
|
||||
{
|
||||
commonArgs.AddRange(options.Defines.Select(def => $"-d:{def}"));
|
||||
}
|
||||
|
||||
if (options.LanguageVersion != null)
|
||||
{
|
||||
commonArgs.Add($"-langversion:{options.LanguageVersion}");
|
||||
}
|
||||
|
||||
if (options.Platform != null)
|
||||
{
|
||||
commonArgs.Add($"-platform:{options.Platform}");
|
||||
}
|
||||
|
||||
if (options.AllowUnsafe == true)
|
||||
{
|
||||
commonArgs.Add("-unsafe");
|
||||
}
|
||||
|
||||
if (options.WarningsAsErrors == true)
|
||||
{
|
||||
commonArgs.Add("-warnaserror");
|
||||
}
|
||||
|
||||
if (options.Optimize == true)
|
||||
{
|
||||
commonArgs.Add("-optimize");
|
||||
}
|
||||
|
||||
if (options.KeyFile != null)
|
||||
{
|
||||
commonArgs.Add($"-keyfile:\"{options.KeyFile}\"");
|
||||
}
|
||||
|
||||
if (options.DelaySign == true)
|
||||
{
|
||||
commonArgs.Add("-delaysign");
|
||||
}
|
||||
|
||||
// TODO: What is this? What does it mean to sign without a key?
|
||||
// Is this "OSS" signing?
|
||||
// if (options.StrongName)
|
||||
|
||||
if (options.EmitEntryPoint != true)
|
||||
{
|
||||
commonArgs.Add("-t:library");
|
||||
}
|
||||
|
||||
return commonArgs;
|
||||
}
|
||||
|
||||
private static Command RunCsc(string cscArgs)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue