From 9b2ef9e520529fce9401b87358e122f8f59756dd Mon Sep 17 00:00:00 2001 From: Marco Zille Date: Sat, 11 Feb 2017 17:30:24 +0100 Subject: [PATCH] Fixed up CLI code to match repository's style --- src/CLI/CLI.cs | 64 +++++++++--------- src/CLI/Generator.cs | 154 ++++++++++++++++++++++--------------------- src/CLI/Options.cs | 55 +++++----------- 3 files changed, 127 insertions(+), 146 deletions(-) diff --git a/src/CLI/CLI.cs b/src/CLI/CLI.cs index 61dbac6b..d3b9bb92 100644 --- a/src/CLI/CLI.cs +++ b/src/CLI/CLI.cs @@ -10,7 +10,7 @@ namespace CppSharp private static OptionSet optionSet = new OptionSet(); private static Options options = new Options(); - static bool ParseCommandLineArgs(string[] args, List messages, ref bool helpShown) + static bool ParseCommandLineArgs(string[] args, List messages, ref bool helpShown) { var showHelp = false; @@ -34,7 +34,7 @@ namespace CppSharp optionSet.Add("h|help", "shows the help", hl => { showHelp = (hl != null); }); - List additionalArguments = null; + List additionalArguments = null; try { @@ -53,7 +53,7 @@ namespace CppSharp return false; } - foreach(String s in additionalArguments) + foreach(string s in additionalArguments) HandleAdditionalArgument(s, messages); return true; @@ -90,20 +90,20 @@ namespace CppSharp Console.WriteLine(" contain only the bindings for that header file."); } - static void AddIncludeDirs(String dir, List messages) + static void AddIncludeDirs(string dir, List messages) { if (Directory.Exists(dir)) options.IncludeDirs.Add(dir); else - messages.Add(String.Format("Directory {0} doesn't exist. Not adding as include directory.", dir)); + messages.Add(string.Format("Directory {0} doesn't exist. Not adding as include directory.", dir)); } - static void HandleOutputArg(String arg, List messages) + static void HandleOutputArg(string arg, List messages) { try { - String dir = Path.GetDirectoryName(arg); - String file = Path.GetFileNameWithoutExtension(arg); + string dir = Path.GetDirectoryName(arg); + string file = Path.GetFileNameWithoutExtension(arg); options.OutputDir = dir; options.OutputFileName = file; @@ -117,7 +117,7 @@ namespace CppSharp } } - static void AddDefine(String name, String value, List messages) + static void AddDefine(string name, string value, List messages) { if (name == null) messages.Add("Invalid definition name for option -D."); @@ -125,9 +125,9 @@ namespace CppSharp options.Defines.Add(name, value); } - static void HandleAdditionalArgument(String args, List messages) + static void HandleAdditionalArgument(string args, List messages) { - if (Path.IsPathRooted(args) == false) + if (!Path.IsPathRooted(args)) args = Path.Combine(Directory.GetCurrentDirectory(), args); try @@ -141,20 +141,20 @@ namespace CppSharp options.HeaderFiles.Add(args); else { - messages.Add(String.Format("File {0} doesn't exist. Not adding to the list of files to generate bindings from.", args)); + messages.Add(string.Format("File {0} doesn't exist. Not adding to the list of files to generate bindings from.", args)); } } catch(Exception) { - messages.Add(String.Format("Error while looking for files inside path {0}. Ignoring.", args)); + messages.Add(string.Format("Error while looking for files inside path {0}. Ignoring.", args)); } } - static void GetFilesFromPath(String path, List messages) + static void GetFilesFromPath(string path, List messages) { path = path.Replace(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar); - String searchPattern = String.Empty; + string searchPattern = string.Empty; int lastSeparatorPosition = path.LastIndexOf(Path.AltDirectorySeparatorChar); if (lastSeparatorPosition >= 0) @@ -168,28 +168,28 @@ namespace CppSharp try { - if (searchPattern != String.Empty) + if (!string.IsNullOrEmpty(searchPattern)) { - String[] files = Directory.GetFiles(path, searchPattern); + string[] files = Directory.GetFiles(path, searchPattern); - foreach (String s in files) + foreach (string s in files) options.HeaderFiles.Add(s); } else { - String[] files = Directory.GetFiles(path); + string[] files = Directory.GetFiles(path); - foreach (String s in files) + foreach (string s in files) options.HeaderFiles.Add(s); } } catch (Exception) { - messages.Add(String.Format("Error while looking for files inside path {0}. Ignoring.", path)); + messages.Add(string.Format("Error while looking for files inside path {0}. Ignoring.", path)); } } - static void GetGeneratorKind(String generator, List messages) + static void GetGeneratorKind(string generator, List messages) { switch (generator.ToLower()) { @@ -201,10 +201,10 @@ namespace CppSharp return; } - messages.Add(String.Format("Unknown generator kind: {0}. Defaulting to {1}", generator, options.Kind.ToString())); + messages.Add(string.Format("Unknown generator kind: {0}. Defaulting to {1}", generator, options.Kind.ToString())); } - static void GetDestinationPlatform(String platform, List messages) + static void GetDestinationPlatform(string platform, List messages) { switch (platform.ToLower()) { @@ -219,10 +219,10 @@ namespace CppSharp return; } - messages.Add(String.Format("Unknown target platform: {0}. Defaulting to {1}", platform, options.Platform.ToString())); + messages.Add(string.Format("Unknown target platform: {0}. Defaulting to {1}", platform, options.Platform.ToString())); } - static void GetDestinationArchitecture(String architecture, List messages) + static void GetDestinationArchitecture(string architecture, List messages) { switch (architecture.ToLower()) { @@ -234,23 +234,23 @@ namespace CppSharp return; } - messages.Add(String.Format("Unknown target architecture: {0}. Defaulting to {1}", architecture, options.Architecture.ToString())); + messages.Add(string.Format("Unknown target architecture: {0}. Defaulting to {1}", architecture, options.Architecture.ToString())); } - static void PrintMessages(List messages) + static void PrintMessages(List messages) { - foreach (String m in messages) + foreach (string m in messages) Console.WriteLine(m); } static void Main(string[] args) { - List messages = new List(); + List messages = new List(); bool helpShown = false; try { - if (ParseCommandLineArgs(args, messages, ref helpShown) == false) + if (!ParseCommandLineArgs(args, messages, ref helpShown)) { PrintMessages(messages); @@ -266,7 +266,7 @@ namespace CppSharp if (validOptions) gen.Run(); - else if (helpShown == false) + else if (!helpShown) ShowHelp(); } catch (Exception ex) diff --git a/src/CLI/Generator.cs b/src/CLI/Generator.cs index d3b9096c..5b4a9038 100644 --- a/src/CLI/Generator.cs +++ b/src/CLI/Generator.cs @@ -6,161 +6,164 @@ using System; using System.Collections.Generic; using System.IO; using System.Linq; +using System.Text; using CppAbi = CppSharp.Parser.AST.CppAbi; namespace CppSharp { class Generator : ILibrary { - private Options _options; - - private String _triple = ""; - private CppAbi _abi = CppAbi.Microsoft; + private Options options = null; + private string triple = ""; + private CppAbi abi = CppAbi.Microsoft; public Generator(Options options) { if (options == null) - throw new ArgumentNullException("options"); + throw new ArgumentNullException(nameof(options)); - _options = options; + this.options = options; } - public bool ValidateOptions(List messages) + public bool ValidateOptions(List messages) { - if (Platform.IsWindows && _options.Platform != TargetPlatform.Windows) + if (Platform.IsWindows && options.Platform != TargetPlatform.Windows) { messages.Add("Cannot create bindings for a platform other that Windows from a Windows running machine"); return false; } - else if (Platform.IsMacOS && _options.Platform != TargetPlatform.MacOS) + else if (Platform.IsMacOS && options.Platform != TargetPlatform.MacOS) { messages.Add("Cannot create bindings for a platform other that MacOS from a MacOS running machine"); return false; } - else if (Platform.IsLinux && _options.Platform != TargetPlatform.Linux) + else if (Platform.IsLinux && options.Platform != TargetPlatform.Linux) { messages.Add("Cannot create bindings for a platform other that Linux from a Linux running machine"); return false; } - if (_options.Platform != TargetPlatform.Windows && _options.Kind != GeneratorKind.CSharp) + if (options.Platform != TargetPlatform.Windows && options.Kind != GeneratorKind.CSharp) { messages.Add("Cannot create bindings for languages other than C# from a non Windows machine"); return false; } - if (_options.Platform == TargetPlatform.Linux && _options.Architecture != TargetArchitecture.x64) + if (options.Platform == TargetPlatform.Linux && options.Architecture != TargetArchitecture.x64) { messages.Add("Cannot create bindings for architectures other than x64 for Linux machines"); return false; } - if (_options.HeaderFiles.Count == 0) + if (options.HeaderFiles.Count == 0) { messages.Add("No source header file has been given to generate bindings from"); return false; } - if (_options.OutputNamespace == String.Empty) + if (string.IsNullOrEmpty(options.OutputNamespace)) { messages.Add("Output namespace is empty"); return false; } - if (_options.OutputFileName == String.Empty) + if (string.IsNullOrEmpty(options.OutputFileName)) { messages.Add("Output not specified"); return false; } - if (_options.InputLibraryName == String.Empty && _options.CheckSymbols == false) + if (string.IsNullOrEmpty(options.InputLibraryName) && !options.CheckSymbols) { messages.Add("Input library name not specified and check symbols not enabled. Either set the input library name or the check symbols flag"); return false; } - if (_options.InputLibraryName == String.Empty && _options.CheckSymbols == true && _options.Libraries.Count == 0) + if (string.IsNullOrEmpty(options.InputLibraryName) && options.CheckSymbols && options.Libraries.Count == 0) { messages.Add("Input library name not specified and check symbols is enabled but no libraries were given. Either set the input library name or add at least one library"); return false; } + + StringBuilder tripleBuilder = new StringBuilder(); + if (options.Architecture == TargetArchitecture.x64) + tripleBuilder.Append("x86_64-"); + else if(options.Architecture == TargetArchitecture.x86) + tripleBuilder.Append("i686-"); - if (_options.Architecture == TargetArchitecture.x64) - _triple = "x86_64-"; - else if(_options.Architecture == TargetArchitecture.x86) - _triple = "i686-"; - - if (_options.Platform == TargetPlatform.Windows) + if (options.Platform == TargetPlatform.Windows) { - _triple += "pc-win32-msvc"; - _abi = CppAbi.Microsoft; + tripleBuilder.Append("pc-win32-msvc"); + abi = CppAbi.Microsoft; } - else if (_options.Platform == TargetPlatform.MacOS) + else if (options.Platform == TargetPlatform.MacOS) { - _triple += "apple-darwin12.4.0"; - _abi = CppAbi.Itanium; + tripleBuilder.Append("apple-darwin12.4.0"); + abi = CppAbi.Itanium; } - else if (_options.Platform == TargetPlatform.Linux) + else if (options.Platform == TargetPlatform.Linux) { - _triple += "linux-gnu"; - _abi = CppAbi.Itanium; + tripleBuilder.Append("linux-gnu"); + abi = CppAbi.Itanium; - if(_options.Cpp11ABI) - _triple += "-cxx11abi"; + if(options.Cpp11ABI) + tripleBuilder.Append("-cxx11abi"); } + triple = tripleBuilder.ToString(); + return true; } public void Setup(Driver driver) { var parserOptions = driver.ParserOptions; - parserOptions.TargetTriple = _triple; - parserOptions.Abi = _abi; + parserOptions.TargetTriple = triple; + parserOptions.Abi = abi; - var options = driver.Options; - options.LibraryName = _options.OutputFileName; + var driverOptions = driver.Options; + driverOptions.LibraryName = options.OutputFileName; - if(_options.InputLibraryName != String.Empty) - options.SharedLibraryName = _options.InputLibraryName; + if(!string.IsNullOrEmpty(options.InputLibraryName)) + driverOptions.SharedLibraryName = options.InputLibraryName; - options.GeneratorKind = _options.Kind; - options.Headers.AddRange(_options.HeaderFiles); - options.Libraries.AddRange(_options.Libraries); + driverOptions.GeneratorKind = options.Kind; + driverOptions.Headers.AddRange(options.HeaderFiles); + driverOptions.Libraries.AddRange(options.Libraries); - if (_abi == CppAbi.Microsoft) + if (abi == CppAbi.Microsoft) parserOptions.MicrosoftMode = true; - if (_triple.Contains("apple")) + if (triple.Contains("apple")) SetupMacOptions(parserOptions); - if (_triple.Contains("linux")) + if (triple.Contains("linux")) SetupLinuxOptions(parserOptions); - foreach (String s in _options.IncludeDirs) + foreach (string s in options.IncludeDirs) parserOptions.AddIncludeDirs(s); - foreach (String s in _options.LibraryDirs) + foreach (string s in options.LibraryDirs) parserOptions.AddLibraryDirs(s); - foreach (KeyValuePair d in _options.Defines) + foreach (KeyValuePair d in options.Defines) { - if(d.Value == null || d.Value == String.Empty) + if(string.IsNullOrEmpty(d.Value)) parserOptions.AddDefines(d.Key); else parserOptions.AddDefines(d.Key + "=" + d.Value); } - options.OutputDir = _options.OutputDir; - options.OutputNamespace = _options.OutputNamespace; - options.CheckSymbols = _options.CheckSymbols; - options.UnityBuild = _options.UnityBuild; + driverOptions.OutputDir = options.OutputDir; + driverOptions.OutputNamespace = options.OutputNamespace; + driverOptions.CheckSymbols = options.CheckSymbols; + driverOptions.UnityBuild = options.UnityBuild; } - private void SetupLinuxOptions(ParserOptions options) + private void SetupLinuxOptions(ParserOptions parserOptions) { - options.MicrosoftMode = false; - options.NoBuiltinIncludes = true; + parserOptions.MicrosoftMode = false; + parserOptions.NoBuiltinIncludes = true; var headersPath = string.Empty; @@ -183,9 +186,9 @@ namespace CppSharp }; foreach (var dir in systemIncludeDirs) - options.AddSystemIncludeDirs(Path.Combine(headersPath, dir)); + parserOptions.AddSystemIncludeDirs(Path.Combine(headersPath, dir)); - options.AddDefines("_GLIBCXX_USE_CXX11_ABI=" + (_options.Cpp11ABI ? "1" : "0")); + parserOptions.AddDefines("_GLIBCXX_USE_CXX11_ABI=" + (options.Cpp11ABI ? "1" : "0")); } private static void SetupMacOptions(ParserOptions options) @@ -237,51 +240,52 @@ namespace CppSharp public void Run() { - String message = "Generating the "; + StringBuilder messageBuilder = new StringBuilder(); + messageBuilder.Append("Generating the "); - switch(_options.Kind) + switch(options.Kind) { case GeneratorKind.CLI: - message += "C++/CLI"; + messageBuilder.Append("C++/CLI"); break; case GeneratorKind.CSharp: - message += "C#"; + messageBuilder.Append("C#"); break; } - message += " parser bindings for "; + messageBuilder.Append(" parser bindings for "); - switch (_options.Platform) + switch (options.Platform) { case TargetPlatform.Linux: - message += "Linux"; + messageBuilder.Append("Linux"); break; case TargetPlatform.MacOS: - message += "OSX"; + messageBuilder.Append("OSX"); break; case TargetPlatform.Windows: - message += "Windows"; + messageBuilder.Append("Windows"); break; } - message += " "; + messageBuilder.Append(" "); - switch (_options.Architecture) + switch (options.Architecture) { case TargetArchitecture.x86: - message += "x86"; + messageBuilder.Append("x86"); break; case TargetArchitecture.x64: - message += "x64"; + messageBuilder.Append("x64"); break; } - if(_options.Cpp11ABI) - message += " (GCC C++11 ABI)"; + if(options.Cpp11ABI) + messageBuilder.Append(" (GCC C++11 ABI)"); - message += "..."; + messageBuilder.Append("..."); - Console.WriteLine(message); + Console.WriteLine(messageBuilder.ToString()); ConsoleDriver.Run(this); diff --git a/src/CLI/Options.cs b/src/CLI/Options.cs index dd80205e..18075af4 100644 --- a/src/CLI/Options.cs +++ b/src/CLI/Options.cs @@ -1,9 +1,4 @@ -using System; using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - using CppSharp.Generators; namespace CppSharp @@ -16,52 +11,34 @@ namespace CppSharp class Options { - private List _headerFiles = new List(); - public List HeaderFiles { get { return _headerFiles; } set { _headerFiles = value; } } - - private List _includeDirs = new List(); - public List IncludeDirs { get { return _includeDirs; } set { _includeDirs = value; } } + public List HeaderFiles { get; } = new List(); - private List _libraryDirs = new List(); - public List LibraryDirs { get { return _libraryDirs; } set { _libraryDirs = value; } } + public List IncludeDirs { get; } = new List(); - private List _libraries = new List(); - public List Libraries { get { return _libraries; } set { _libraries = value; } } + public List LibraryDirs { get; } = new List(); - private Dictionary _defines = new Dictionary(); - public Dictionary Defines { get { return _defines; } set { _defines = value; } } + public List Libraries { get; } = new List(); - private String _outputDir = ""; - public String OutputDir { get { return _outputDir; } set { _outputDir = value; } } + public Dictionary Defines { get; } = new Dictionary(); - private String _outputNamespace = ""; - public String OutputNamespace { get { return _outputNamespace; } set { _outputNamespace = value; } } + public string OutputDir { get; set; } - private String _outputFileName = ""; - public String OutputFileName { get { return _outputFileName; } set { _outputFileName = value; } } + public string OutputNamespace { get; set; } - private String _inputLibraryName = ""; - public String InputLibraryName { get { return _inputLibraryName; } set { _inputLibraryName = value; } } + public string OutputFileName { get; set; } - private String _triple = ""; - public String Triple { get { return _triple; } set { _triple = value; } } - - private TargetPlatform _platform = TargetPlatform.Windows; - public TargetPlatform Platform { get { return _platform; } set { _platform = value; } } + public string InputLibraryName { get; set; } + + public TargetPlatform Platform { get; set; } = TargetPlatform.Windows; - private TargetArchitecture _architecture = TargetArchitecture.x86; - public TargetArchitecture Architecture { get { return _architecture; } set { _architecture = value; } } + public TargetArchitecture Architecture { get; set; } = TargetArchitecture.x86; - private GeneratorKind _kind = GeneratorKind.CSharp; - public GeneratorKind Kind { get { return _kind; } set { _kind = value; } } + public GeneratorKind Kind { get; set; } = GeneratorKind.CSharp; - private bool _checkSymbols = false; - public bool CheckSymbols { get { return _checkSymbols; } set { _checkSymbols = value; } } + public bool CheckSymbols { get; set; } - private bool _unityBuild = false; - public bool UnityBuild { get { return _unityBuild; } set { _unityBuild = value; } } + public bool UnityBuild { get; set; } - private bool _cpp11ABI = false; - public bool Cpp11ABI { get { return _cpp11ABI; } set { _cpp11ABI = value; } } + public bool Cpp11ABI { get; set; } } } \ No newline at end of file