From 39fd1ed5c52b8d07d95e6972be4515f5e64f5eab Mon Sep 17 00:00:00 2001 From: Livar Cunha Date: Fri, 20 Nov 2015 17:23:19 -0800 Subject: [PATCH] Refactoring the arguments parser into a separate Parser and refactoring the creation of the NativeCompilerSettings into a method of ArgValues. --- .../ArgValues.cs | 77 ++++++-- .../ArgumentsParser.cs | 108 ++++++++++++ .../NativeCompileSettings.cs | 14 +- .../Program.cs | 165 +----------------- 4 files changed, 186 insertions(+), 178 deletions(-) create mode 100644 src/Microsoft.DotNet.Tools.Compiler.Native/ArgumentsParser.cs diff --git a/src/Microsoft.DotNet.Tools.Compiler.Native/ArgValues.cs b/src/Microsoft.DotNet.Tools.Compiler.Native/ArgValues.cs index eca306705..161a4e495 100644 --- a/src/Microsoft.DotNet.Tools.Compiler.Native/ArgValues.cs +++ b/src/Microsoft.DotNet.Tools.Compiler.Native/ArgValues.cs @@ -1,14 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Runtime.InteropServices; -using System.IO; - -using Microsoft.Dnx.Runtime.Common.CommandLine; -using Microsoft.DotNet.Cli.Utils; -using Microsoft.DotNet.Tools.Common; +using System.Collections.Generic; namespace Microsoft.DotNet.Tools.Compiler.Native { @@ -21,11 +11,70 @@ namespace Microsoft.DotNet.Tools.Compiler.Native public BuildConfiguration? BuildConfiguration { get; set; } public ArchitectureMode Architecture { get; set; } public NativeIntermediateMode? NativeMode { get; set; } - public List ReferencePaths { get; set; } + public IEnumerable ReferencePaths { get; set; } public string IlcArgs { get; set; } - public List LinkLibPaths { get; set; } + public IEnumerable LinkLibPaths { get; set; } public string AppDepSDKPath { get; set; } public string IlcPath { get; set; } - } + public NativeCompileSettings GetNativeCompileSettings() + { + var config = NativeCompileSettings.Default; + + config.InputManagedAssemblyPath = InputManagedAssemblyPath; + config.Architecture = Architecture; + + if (BuildConfiguration.HasValue) + { + config.BuildType = BuildConfiguration.Value; + } + + if (!string.IsNullOrEmpty(OutputDirectory)) + { + config.OutputDirectory = OutputDirectory; + } + + if (!string.IsNullOrEmpty(IntermediateDirectory)) + { + config.IntermediateDirectory = IntermediateDirectory; + } + + if (NativeMode.HasValue) + { + config.NativeMode = NativeMode.Value; + } + + if (!string.IsNullOrEmpty(AppDepSDKPath)) + { + config.AppDepSDKPath = AppDepSDKPath; + } + + if (!string.IsNullOrEmpty(IlcPath)) + { + config.IlcPath = IlcPath; + } + + if (!string.IsNullOrEmpty(LogPath)) + { + config.LogPath = LogPath; + } + + if (!string.IsNullOrEmpty(IlcArgs)) + { + config.IlcArgs = IlcArgs; + } + + foreach (var reference in ReferencePaths) + { + config.AddReference(reference); + } + + foreach (var lib in LinkLibPaths) + { + config.AddLinkLibPath(lib); + } + + return config; + } + } } diff --git a/src/Microsoft.DotNet.Tools.Compiler.Native/ArgumentsParser.cs b/src/Microsoft.DotNet.Tools.Compiler.Native/ArgumentsParser.cs new file mode 100644 index 000000000..3189f6978 --- /dev/null +++ b/src/Microsoft.DotNet.Tools.Compiler.Native/ArgumentsParser.cs @@ -0,0 +1,108 @@ +using System; +using System.Collections.Generic; +using System.CommandLine; + +namespace Microsoft.DotNet.Tools.Compiler.Native +{ + internal static class ArgumentsParser + { + internal static ArgValues Parse(IEnumerable args) + { + string inputAssembly = null; + string outputDirectory = null; + string temporaryOutputDirectory = null; + string configuration = null; + BuildConfiguration? buildConfiguration = null; + string mode = null; + NativeIntermediateMode? nativeMode = null; + string ilcArgs = null; + string ilcPath = null; + string appDepSdk = null; + string logPath = null; + + IReadOnlyList references = Array.Empty(); + IReadOnlyList linklib = Array.Empty(); + + try + { + ArgumentSyntax.Parse(args, syntax => + { + syntax.DefineOption("output", ref outputDirectory, "Output Directory for native executable."); + syntax.DefineOption("temp-output", ref temporaryOutputDirectory, "Directory for intermediate files."); + + syntax.DefineOption("configuration", ref configuration, + "debug/release build configuration. Defaults to debug."); + syntax.DefineOption("mode", ref mode, "Code Generation mode. Defaults to ryujit."); + + syntax.DefineOptionList("reference", ref references, + "Use to specify Managed DLL references of the app."); + + // Custom Extensibility Points to support CoreRT workflow TODO better descriptions + syntax.DefineOption("ilcargs", ref ilcArgs, "Use to specify custom arguments for the IL Compiler."); + syntax.DefineOption("ilcpath", ref ilcPath, "Use to plug in a custom built ilc.exe"); + syntax.DefineOptionList("linklib", ref linklib, "Use to link in additional static libs"); + + // TEMPORARY Hack until CoreRT compatible Framework Libs are available + syntax.DefineOption("appdepsdk", ref appDepSdk, "Use to plug in custom appdepsdk path"); + + // Optional Log Path + syntax.DefineOption("logpath", ref logPath, "Use to dump Native Compilation Logs to a file."); + + syntax.DefineParameter("INPUT_ASSEMBLY", ref inputAssembly, + "The managed input assembly to compile to native."); + + if (string.IsNullOrWhiteSpace(inputAssembly)) + { + syntax.ReportError("Input Assembly is a required parameter."); + } + + if (!string.IsNullOrEmpty(configuration)) + { + try + { + buildConfiguration = EnumExtensions.Parse(configuration); + } + catch (ArgumentException) + { + syntax.ReportError($"Invalid Configuration Option: {configuration}"); + } + } + + if (!string.IsNullOrEmpty(mode)) + { + try + { + nativeMode = EnumExtensions.Parse(mode); + } + catch (ArgumentException) + { + syntax.ReportError($"Invalid Mode Option: {mode}"); + } + } + }); + } + catch (ArgumentSyntaxException) + { + //return ExitFailed; + } + + Console.WriteLine($"Input Assembly: {inputAssembly}"); + + return new ArgValues + { + InputManagedAssemblyPath = inputAssembly, + OutputDirectory = outputDirectory, + IntermediateDirectory = temporaryOutputDirectory, + Architecture = ArchitectureMode.x64, + BuildConfiguration = buildConfiguration, + NativeMode = nativeMode, + ReferencePaths = references, + IlcArgs = ilcArgs, + IlcPath = ilcPath, + LinkLibPaths = linklib, + AppDepSDKPath = appDepSdk, + LogPath = logPath + }; + } + } +} diff --git a/src/Microsoft.DotNet.Tools.Compiler.Native/NativeCompileSettings.cs b/src/Microsoft.DotNet.Tools.Compiler.Native/NativeCompileSettings.cs index 567048c09..c2717d7a9 100644 --- a/src/Microsoft.DotNet.Tools.Compiler.Native/NativeCompileSettings.cs +++ b/src/Microsoft.DotNet.Tools.Compiler.Native/NativeCompileSettings.cs @@ -16,10 +16,16 @@ namespace Microsoft.DotNet.Tools.Compiler.Native private string _ilcPath; private string _outputDirectory; private string _intermediateDirectory; + private string _logPath; + private string _ilcArgs; private readonly List _referencePaths; private readonly List _linkLibPaths; - public string LogPath { get; set; } + public string LogPath + { + get { return _logPath; } + set { _logPath = Path.GetFullPath(value); } + } public string InputManagedAssemblyPath { @@ -82,7 +88,11 @@ namespace Microsoft.DotNet.Tools.Compiler.Native } // Optional Customization Points (Can be null) - public string IlcArgs { get; set; } + public string IlcArgs + { + get { return _ilcArgs; } + set { _ilcArgs = Path.GetFullPath(value); } + } public IEnumerable LinkLibPaths => _linkLibPaths; // Required Customization Points (Must have default) diff --git a/src/Microsoft.DotNet.Tools.Compiler.Native/Program.cs b/src/Microsoft.DotNet.Tools.Compiler.Native/Program.cs index b18e8009d..491ac6c92 100644 --- a/src/Microsoft.DotNet.Tools.Compiler.Native/Program.cs +++ b/src/Microsoft.DotNet.Tools.Compiler.Native/Program.cs @@ -1,8 +1,5 @@ using System; -using System.Collections.Generic; -using System.CommandLine; using System.IO; -using System.Linq; using Microsoft.DotNet.Cli.Utils; namespace Microsoft.DotNet.Tools.Compiler.Native @@ -14,103 +11,7 @@ namespace Microsoft.DotNet.Tools.Compiler.Native DebugHelper.HandleDebugSwitch(ref args); return ExecuteApp(args); - } - - private static ArgValues GetArgs(string[] args) - { - string inputAssembly = null; - string outputDirectory = null; - string temporaryOutputDirectory = null; - string configuration = null; - BuildConfiguration? buildConfiguration = null; - string mode = null; - NativeIntermediateMode? nativeMode = null; - string ilcArgs = null; - string ilcPath = null; - string appDepSdk = null; - string logPath = null; - - IReadOnlyList references = Array.Empty(); - IReadOnlyList linklib = Array.Empty(); - - try - { - ArgumentSyntax.Parse(args, syntax => - { - syntax.DefineOption("output", ref outputDirectory, "Output Directory for native executable."); - syntax.DefineOption("temp-output", ref temporaryOutputDirectory, "Directory for intermediate files."); - - syntax.DefineOption("configuration", ref configuration, "debug/release build configuration. Defaults to debug."); - syntax.DefineOption("mode", ref mode, "Code Generation mode. Defaults to ryujit."); - - syntax.DefineOptionList("reference", ref references, "Use to specify Managed DLL references of the app."); - - // Custom Extensibility Points to support CoreRT workflow TODO better descriptions - syntax.DefineOption("ilcargs", ref ilcArgs, "Use to specify custom arguments for the IL Compiler."); - syntax.DefineOption("ilcpath", ref ilcPath, "Use to plug in a custom built ilc.exe"); - syntax.DefineOptionList("linklib", ref linklib, "Use to link in additional static libs"); - - // TEMPORARY Hack until CoreRT compatible Framework Libs are available - syntax.DefineOption("appdepsdk", ref appDepSdk, "Use to plug in custom appdepsdk path"); - - // Optional Log Path - syntax.DefineOption("logpath", ref logPath, "Use to dump Native Compilation Logs to a file."); - - syntax.DefineParameter("INPUT_ASSEMBLY", ref inputAssembly, "The managed input assembly to compile to native."); - - if (string.IsNullOrWhiteSpace(inputAssembly)) - { - syntax.ReportError("Input Assembly is a required parameter."); - } - - if (!string.IsNullOrEmpty(configuration)) - { - try - { - buildConfiguration = EnumExtensions.Parse(configuration); - } - catch (ArgumentException) - { - syntax.ReportError($"Invalid Configuration Option: {configuration}"); - } - } - - if (!string.IsNullOrEmpty(mode)) - { - try - { - nativeMode = EnumExtensions.Parse(mode); - } - catch (ArgumentException) - { - syntax.ReportError($"Invalid Mode Option: {mode}"); - } - } - }); - } - catch (ArgumentSyntaxException) - { - //return ExitFailed; - } - - Console.WriteLine($"Input Assembly: {inputAssembly}"); - - return new ArgValues - { - InputManagedAssemblyPath = inputAssembly, - OutputDirectory = outputDirectory, - IntermediateDirectory = temporaryOutputDirectory, - Architecture = ArchitectureMode.x64, - BuildConfiguration = buildConfiguration, - NativeMode = nativeMode, - ReferencePaths = references.ToList(), - IlcArgs = ilcArgs, - IlcPath = ilcPath, - LinkLibPaths = linklib.ToList(), - AppDepSDKPath = appDepSdk, - LogPath = logPath - }; - } + } private static int ExecuteApp(string[] args) { @@ -130,8 +31,8 @@ namespace Microsoft.DotNet.Tools.Compiler.Native try { - var cmdLineArgs = GetArgs(args); - var config = ParseAndValidateArgs(cmdLineArgs); + var cmdLineArgs = ArgumentsParser.Parse(args); + var config = cmdLineArgs.GetNativeCompileSettings(); DirectoryExtensions.CleanOrCreateDirectory(config.OutputDirectory); DirectoryExtensions.CleanOrCreateDirectory(config.IntermediateDirectory); @@ -175,65 +76,5 @@ namespace Microsoft.DotNet.Tools.Compiler.Native var nArgs = content.Split(new [] {"\r\n", "\n"}, StringSplitOptions.RemoveEmptyEntries); return nArgs; } - - private static NativeCompileSettings ParseAndValidateArgs(ArgValues args) - { - var config = NativeCompileSettings.Default; - - config.InputManagedAssemblyPath = args.InputManagedAssemblyPath; - config.Architecture = args.Architecture; - - if (args.BuildConfiguration.HasValue) - { - config.BuildType = args.BuildConfiguration.Value; - } - - if(!string.IsNullOrEmpty(args.OutputDirectory)) - { - config.OutputDirectory = args.OutputDirectory; - } - - if(!string.IsNullOrEmpty(args.IntermediateDirectory)) - { - config.IntermediateDirectory = args.IntermediateDirectory; - } - - if (args.NativeMode.HasValue) - { - config.NativeMode = args.NativeMode.Value; - } - - if(!string.IsNullOrEmpty(args.AppDepSDKPath)) - { - config.AppDepSDKPath = args.AppDepSDKPath; - } - - if (!string.IsNullOrEmpty(args.IlcPath)) - { - config.IlcPath = args.IlcPath; - } - - if (!string.IsNullOrEmpty(args.LogPath)) - { - config.LogPath = Path.GetFullPath(args.LogPath); - } - - if (!string.IsNullOrEmpty(args.IlcArgs)) - { - config.IlcArgs = Path.GetFullPath(args.IlcArgs); - } - - foreach (var reference in args.ReferencePaths) - { - config.AddReference(reference); - } - - foreach (var lib in args.LinkLibPaths) - { - config.AddLinkLibPath(lib); - } - - return config; - } } }