From db3a9ffc204ba9422ed0c58a6485895e718f1f4e Mon Sep 17 00:00:00 2001 From: Livar Cunha Date: Fri, 20 Nov 2015 16:38:50 -0800 Subject: [PATCH] Moving validation into NativeCompileSettings. Left argument requirement and argument to enum validation in the ArgumentSyntax delegate --- .../ArgValues.cs | 6 +- .../EnumExtensions.cs | 10 -- .../NativeCompileSettings.cs | 154 +++++++++++++----- .../Program.cs | 129 ++++++--------- 4 files changed, 170 insertions(+), 129 deletions(-) diff --git a/src/Microsoft.DotNet.Tools.Compiler.Native/ArgValues.cs b/src/Microsoft.DotNet.Tools.Compiler.Native/ArgValues.cs index b7c15dea5..eca306705 100644 --- a/src/Microsoft.DotNet.Tools.Compiler.Native/ArgValues.cs +++ b/src/Microsoft.DotNet.Tools.Compiler.Native/ArgValues.cs @@ -18,9 +18,9 @@ namespace Microsoft.DotNet.Tools.Compiler.Native public string InputManagedAssemblyPath { get; set; } public string OutputDirectory { get; set; } public string IntermediateDirectory { get; set; } - public string BuildConfiguration { get; set; } - public string Architecture { get; set; } - public string NativeMode { get; set; } + public BuildConfiguration? BuildConfiguration { get; set; } + public ArchitectureMode Architecture { get; set; } + public NativeIntermediateMode? NativeMode { get; set; } public List ReferencePaths { get; set; } public string IlcArgs { get; set; } public List LinkLibPaths { get; set; } diff --git a/src/Microsoft.DotNet.Tools.Compiler.Native/EnumExtensions.cs b/src/Microsoft.DotNet.Tools.Compiler.Native/EnumExtensions.cs index a64dde5c2..a0f345a0c 100644 --- a/src/Microsoft.DotNet.Tools.Compiler.Native/EnumExtensions.cs +++ b/src/Microsoft.DotNet.Tools.Compiler.Native/EnumExtensions.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; namespace Microsoft.DotNet.Tools.Compiler.Native { diff --git a/src/Microsoft.DotNet.Tools.Compiler.Native/NativeCompileSettings.cs b/src/Microsoft.DotNet.Tools.Compiler.Native/NativeCompileSettings.cs index ac933b28b..567048c09 100644 --- a/src/Microsoft.DotNet.Tools.Compiler.Native/NativeCompileSettings.cs +++ b/src/Microsoft.DotNet.Tools.Compiler.Native/NativeCompileSettings.cs @@ -11,72 +11,138 @@ namespace Microsoft.DotNet.Tools.Compiler.Native private const NativeIntermediateMode DefaultNativeModel = NativeIntermediateMode.ryujit; private const ArchitectureMode DefaultArchitectureMode = ArchitectureMode.x64; + private string _inputManagedAssemblyPath; + private string _appDepSdkPath; + private string _ilcPath; + private string _outputDirectory; + private string _intermediateDirectory; + private readonly List _referencePaths; + private readonly List _linkLibPaths; + public string LogPath { get; set; } - public string InputManagedAssemblyPath { get; set; } - - public string OutputDirectory { get; set; } - public string IntermediateDirectory { get; set; } - public BuildConfiguration BuildType { get; set; } - - public string BuildTypeString + public string InputManagedAssemblyPath { + get + { + return _inputManagedAssemblyPath; + } set { - try + if(!File.Exists(value)) { - BuildType = EnumExtensions.Parse(value.ToLower()); - } - catch (Exception e) - { - throw new Exception("Invalid Configuration Option."); + throw new Exception($"Could not find the input managed assembly: {value}"); } + + _inputManagedAssemblyPath = Path.GetFullPath(value); } } + public string OutputDirectory + { + get + { + return _outputDirectory ?? GetDefaultOutputDirectory(); + } + set + { + _outputDirectory = value; + } + } + + public string IntermediateDirectory + { + get + { + return _intermediateDirectory ?? GetDefaultIntermediateDirectory(); + } + set + { + _intermediateDirectory = value; + } + } + + public BuildConfiguration BuildType { get; set; } + public ArchitectureMode Architecture { get; set; } public NativeIntermediateMode NativeMode { get; set; } public OSMode OS { get; set; } - - public List ReferencePaths { get; set; } - + + public IEnumerable ReferencePaths + { + get + { + var referencePaths = new List(_referencePaths) + { + Path.Combine(AppDepSDKPath, "*.dll") + }; + + return referencePaths; + } + } + // Optional Customization Points (Can be null) public string IlcArgs { get; set; } - public List LinkLibPaths { get; set; } - + public IEnumerable LinkLibPaths => _linkLibPaths; + // Required Customization Points (Must have default) - public string AppDepSDKPath { get; set; } - public string IlcPath { get; set; } + public string AppDepSDKPath { + get + { + return _appDepSdkPath; + } + set + { + if (!Directory.Exists(value)) + { + throw new Exception($"AppDepSDK Directory does not exist: {value}."); + } + + _appDepSdkPath = value; + } + } + + public string IlcPath + { + get + { + return _ilcPath; + } + set + { + if (!Directory.Exists(value)) + { + throw new Exception($"ILC Directory does not exist: {value}."); + } + + _ilcPath = value; + } + } private NativeCompileSettings() { - LinkLibPaths = new List(); - ReferencePaths = new List(); + _linkLibPaths = new List(); + _referencePaths = new List(); IlcPath = AppContext.BaseDirectory; Architecture = DefaultArchitectureMode; BuildType = DefaultBuiltType; NativeMode = DefaultNativeModel; - AppDepSDKPath = Path.Combine(AppContext.BaseDirectory, "appdepsdk"); - - ReferencePaths.Add(Path.Combine(AppDepSDKPath, "*.dll")); + AppDepSDKPath = Path.Combine(AppContext.BaseDirectory, "appdepsdk"); } public static NativeCompileSettings Default { get { - var nativeCompileSettings = new NativeCompileSettings - { + var defaultNativeCompileSettings = new NativeCompileSettings + { OS = RuntimeInformationExtensions.GetCurrentOS() }; - nativeCompileSettings.SetDefaultOutputDirectory(); - nativeCompileSettings.SetDefaultIntermediateDirectory(); - - return nativeCompileSettings; + return defaultNativeCompileSettings; } - } + } public string DetermineFinalOutputPath() { @@ -87,16 +153,26 @@ namespace Microsoft.DotNet.Tools.Compiler.Native var outFile = Path.Combine(outputDirectory, filename + Constants.ExeSuffix); return outFile; - } - - private void SetDefaultOutputDirectory() - { - OutputDirectory = GetOutputDirectory(Constants.BinDirectoryName); } - private void SetDefaultIntermediateDirectory() + public void AddReference(string reference) { - IntermediateDirectory = GetOutputDirectory(Constants.ObjDirectoryName); + _referencePaths.Add(Path.GetFullPath(reference)); + } + + public void AddLinkLibPath(string linkLibPath) + { + _linkLibPaths.Add(linkLibPath); + } + + private string GetDefaultOutputDirectory() + { + return GetOutputDirectory(Constants.BinDirectoryName); + } + + private string GetDefaultIntermediateDirectory() + { + return GetOutputDirectory(Constants.ObjDirectoryName); } private string GetOutputDirectory(string beginsWith) diff --git a/src/Microsoft.DotNet.Tools.Compiler.Native/Program.cs b/src/Microsoft.DotNet.Tools.Compiler.Native/Program.cs index 53e8fa7ba..b18e8009d 100644 --- a/src/Microsoft.DotNet.Tools.Compiler.Native/Program.cs +++ b/src/Microsoft.DotNet.Tools.Compiler.Native/Program.cs @@ -22,7 +22,9 @@ namespace Microsoft.DotNet.Tools.Compiler.Native 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; @@ -55,6 +57,35 @@ namespace Microsoft.DotNet.Tools.Compiler.Native 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) @@ -64,14 +95,14 @@ namespace Microsoft.DotNet.Tools.Compiler.Native Console.WriteLine($"Input Assembly: {inputAssembly}"); - return new ArgValues() + return new ArgValues { InputManagedAssemblyPath = inputAssembly, OutputDirectory = outputDirectory, IntermediateDirectory = temporaryOutputDirectory, - Architecture = "x64", - BuildConfiguration = configuration, - NativeMode = mode, + Architecture = ArchitectureMode.x64, + BuildConfiguration = buildConfiguration, + NativeMode = nativeMode, ReferencePaths = references.ToList(), IlcArgs = ilcArgs, IlcPath = ilcPath, @@ -114,7 +145,7 @@ namespace Microsoft.DotNet.Tools.Compiler.Native catch (Exception ex) { #if DEBUG - System.Console.WriteLine(ex); + Console.WriteLine(ex); #else Reporter.Error.WriteLine(ex.Message); #endif @@ -130,132 +161,76 @@ namespace Microsoft.DotNet.Tools.Compiler.Native return null; } - string content = null; + string content; try { content = File.ReadAllText(rspPath); } - catch (Exception e) + catch (Exception) { Reporter.Error.WriteLine("Unable to Read Response File"); return null; } - string[] nArgs = content.Split(new [] {"\r\n", "\n"}, StringSplitOptions.RemoveEmptyEntries); + var nArgs = content.Split(new [] {"\r\n", "\n"}, StringSplitOptions.RemoveEmptyEntries); return nArgs; } private static NativeCompileSettings ParseAndValidateArgs(ArgValues args) { var config = NativeCompileSettings.Default; - - // Managed Input - if (string.IsNullOrEmpty(args.InputManagedAssemblyPath) || !File.Exists(args.InputManagedAssemblyPath)) + + config.InputManagedAssemblyPath = args.InputManagedAssemblyPath; + config.Architecture = args.Architecture; + + if (args.BuildConfiguration.HasValue) { - //TODO make this message good - throw new Exception("Invalid Managed Assembly Argument."); - } + config.BuildType = args.BuildConfiguration.Value; + } - config.InputManagedAssemblyPath = Path.GetFullPath(args.InputManagedAssemblyPath); - - // Architecture - if(!string.IsNullOrEmpty(args.Architecture)) - { - try - { - config.Architecture = EnumExtensions.Parse(args.Architecture.ToLower()); - } - catch (Exception e) - { - throw new Exception("Invalid Architecture Option."); - } - } - - // BuildConfiguration - if(!string.IsNullOrEmpty(args.BuildConfiguration)) - { - try - { - config.BuildType = EnumExtensions.Parse(args.BuildConfiguration.ToLower()); - } - catch (Exception e) - { - throw new Exception("Invalid Configuration Option."); - } - } - - // TODO: track changing it when architeture or buildtype change Output if(!string.IsNullOrEmpty(args.OutputDirectory)) { config.OutputDirectory = args.OutputDirectory; } - - // TODO: same here Intermediate + if(!string.IsNullOrEmpty(args.IntermediateDirectory)) { config.IntermediateDirectory = args.IntermediateDirectory; } - // Mode - if (!string.IsNullOrEmpty(args.NativeMode)) + if (args.NativeMode.HasValue) { - try - { - config.NativeMode = EnumExtensions.Parse(args.NativeMode.ToLower()); - } - catch (Exception e) - { - throw new Exception("Invalid Mode Option."); - } + config.NativeMode = args.NativeMode.Value; } - // AppDeps (TEMP) if(!string.IsNullOrEmpty(args.AppDepSDKPath)) { - if (!Directory.Exists(args.AppDepSDKPath)) - { - throw new Exception("AppDepSDK Directory does not exist."); - } - config.AppDepSDKPath = args.AppDepSDKPath; - - var reference = Path.Combine(config.AppDepSDKPath, "*.dll"); - config.ReferencePaths.Add(reference); } - // IlcPath if (!string.IsNullOrEmpty(args.IlcPath)) - { - if (!Directory.Exists(args.IlcPath)) - { - throw new Exception("ILC Directory does not exist."); - } - + { config.IlcPath = args.IlcPath; } - // logpath if (!string.IsNullOrEmpty(args.LogPath)) { config.LogPath = Path.GetFullPath(args.LogPath); } - // CodeGenPath if (!string.IsNullOrEmpty(args.IlcArgs)) { config.IlcArgs = Path.GetFullPath(args.IlcArgs); } - // Reference Paths foreach (var reference in args.ReferencePaths) { - config.ReferencePaths.Add(Path.GetFullPath(reference)); + config.AddReference(reference); } - // Link Libs foreach (var lib in args.LinkLibPaths) { - config.LinkLibPaths.Add(lib); + config.AddLinkLibPath(lib); } return config;