From 5e3263fd95f81e61efd1a95a3233e13bf161ab8f Mon Sep 17 00:00:00 2001 From: triton Date: Fri, 1 Feb 2013 01:28:23 +0000 Subject: [PATCH] Moved the startup code to its own file. --- src/Generator/CodeGenerator.cs | 135 ------------------------------- src/Generator/Program.cs | 141 +++++++++++++++++++++++++++++++++ 2 files changed, 141 insertions(+), 135 deletions(-) create mode 100644 src/Generator/Program.cs diff --git a/src/Generator/CodeGenerator.cs b/src/Generator/CodeGenerator.cs index 1b8985e6..c7d7a324 100644 --- a/src/Generator/CodeGenerator.cs +++ b/src/Generator/CodeGenerator.cs @@ -129,139 +129,4 @@ namespace Cxxi public string Assembly; public int toolset2use; } - - public class Program - { - static void ShowHelp(OptionSet options) - { - var module = System.Diagnostics.Process.GetCurrentProcess().MainModule; - var exeName = Path.GetFileName(module.FileName); - Console.WriteLine("Usage: " + exeName + " [options]+ headers"); - Console.WriteLine("Generates .NET bindings from C/C++ header files."); - Console.WriteLine(); - Console.WriteLine("Options:"); - - -Console.WriteLine(@" - --vs, --visualstudio=VALUE - Visual studio version to use for - system include folders. - Valid values: - 0 - autoprobe. (Default) - 11 - 2012, 10 - 2010, 9 - 2008, 8 - 2005 - Number can be find out from VSx0COMNTOOLS environment variable. - - -D, --defines=VALUE - Specify define for preprocessor. - (E.g. WIN32, DEBUG) - -I, --include=VALUE - Add include path - --ns, --namespace=VALUE - Namespace where C# wrappers will reside - - -o, --outdir=VALUE - Output folder - --debug - --lib, --library=VALUE - -t, --template=VALUE - -a, --assembly=VALUE - - -v, --verbose - -h, -?, --help -"); - - } - - static bool ParseCommandLineOptions(String[] args, Options options) - { - var set = new OptionSet() - { - // Parser options - { "D|defines=", v => options.Defines.Add(v) }, - { "I|include=", v => options.IncludeDirs.Add(v) }, - // Generator options - { "ns|namespace=", v => options.OutputNamespace = v }, - { "o|outdir=", v => options.OutputDir = v }, - { "debug", v => options.OutputDebug = true }, - { "lib|library=", v => options.LibraryName = v }, - { "t|template=", v => options.Template = v }, - { "a|assembly=", v => options.Assembly = v }, - // Misc. options - { "vs|visualstudio=", v => { Int32.TryParse(v, out options.toolset2use); } }, - { "v|verbose", v => { options.Verbose = true; } }, - { "h|?|help", v => options.ShowHelpText = v != null }, - }; - - if (args.Length == 0 || options.ShowHelpText) - { - ShowHelp(set); - return false; - } - - try - { - options.Headers = set.Parse(args); - } - catch (OptionException) - { - Console.WriteLine("Error parsing the command line."); - ShowHelp(set); - return false; - } - - return true; - } - - static bool ParseLibraryAssembly(string path, out ILibrary library) - { - library = null; - - if (string.IsNullOrWhiteSpace(path)) - { - return true; - } - - try - { - var fullPath = Path.GetFullPath(path); - - var assembly = Assembly.LoadFile(fullPath); - var types = assembly.FindDerivedTypes(typeof(ILibrary)); - - foreach (var type in types) - { - var attrs = type.GetCustomAttributes( typeof(LibraryTransformAttribute), true); - if (attrs == null) continue; - - Console.WriteLine("Found library transform: {0}", type.Name); - library = (ILibrary)Activator.CreateInstance(type); - } - } - catch (Exception ex) - { - Console.WriteLine("Error: assembly '{0}' could not be loaded: {1}", path, ex.Message); - return false; - } - - return true; - } - - public static void Main(String[] args) - { - var options = new Options(); - - if (!ParseCommandLineOptions(args, options)) - return; - - // We need to know absolute pathes in order to determine correct include pathes. - for (int i = 0; i < options.IncludeDirs.Count; i++) - { - if (options.IncludeDirs[i] == ".") options.IncludeDirs[i] = Directory.GetCurrentDirectory(); - } - - ILibrary library = null; - if (!ParseLibraryAssembly(options.Assembly, out library)) - return; - - var codeGenerator = new CodeGenerator(options, library); - codeGenerator.ParseCode(); - codeGenerator.ProcessCode(); - codeGenerator.GenerateCode(); - } - } } \ No newline at end of file diff --git a/src/Generator/Program.cs b/src/Generator/Program.cs new file mode 100644 index 00000000..b1bfee4a --- /dev/null +++ b/src/Generator/Program.cs @@ -0,0 +1,141 @@ +using System; +using System.IO; +using System.Reflection; +using Mono.Options; + +namespace Cxxi +{ + public class Program + { + static void ShowHelp(OptionSet options) + { + var module = System.Diagnostics.Process.GetCurrentProcess().MainModule; + var exeName = Path.GetFileName(module.FileName); + Console.WriteLine("Usage: " + exeName + " [options]+ headers"); + Console.WriteLine("Generates .NET bindings from C/C++ header files."); + Console.WriteLine(); + Console.WriteLine("Options:"); + + + Console.WriteLine(@" + --vs, --visualstudio=VALUE - Visual studio version to use for + system include folders. + Valid values: + 0 - autoprobe. (Default) + 11 - 2012, 10 - 2010, 9 - 2008, 8 - 2005 + Number can be find out from VSx0COMNTOOLS environment variable. + + -D, --defines=VALUE - Specify define for preprocessor. + (E.g. WIN32, DEBUG) + -I, --include=VALUE - Add include path + --ns, --namespace=VALUE - Namespace where C# wrappers will reside + + -o, --outdir=VALUE - Output folder + --debug + --lib, --library=VALUE + -t, --template=VALUE + -a, --assembly=VALUE + + -v, --verbose + -h, -?, --help +"); + } + + static bool ParseCommandLineOptions(String[] args, Options options) + { + var set = new OptionSet() + { + // Parser options + { "D|defines=", v => options.Defines.Add(v) }, + { "I|include=", v => options.IncludeDirs.Add(v) }, + // Generator options + { "ns|namespace=", v => options.OutputNamespace = v }, + { "o|outdir=", v => options.OutputDir = v }, + { "debug", v => options.OutputDebug = true }, + { "lib|library=", v => options.LibraryName = v }, + { "t|template=", v => options.Template = v }, + { "a|assembly=", v => options.Assembly = v }, + // Misc. options + { "vs|visualstudio=", v => Int32.TryParse(v, out options.ToolsetToUse) }, + { "v|verbose", v => { options.Verbose = true; } }, + { "h|?|help", v => options.ShowHelpText = v != null }, + }; + + if (args.Length == 0 || options.ShowHelpText) + { + ShowHelp(set); + return false; + } + + try + { + options.Headers = set.Parse(args); + } + catch (OptionException) + { + Console.WriteLine("Error parsing the command line."); + ShowHelp(set); + return false; + } + + return true; + } + + static bool ParseLibraryAssembly(string path, out ILibrary library) + { + library = null; + + if (string.IsNullOrWhiteSpace(path)) + { + return true; + } + + try + { + var fullPath = Path.GetFullPath(path); + + var assembly = Assembly.LoadFile(fullPath); + var types = assembly.FindDerivedTypes(typeof(ILibrary)); + + foreach (var type in types) + { + var attrs = type.GetCustomAttributes(typeof(LibraryTransformAttribute), true); + if (attrs == null) continue; + + Console.WriteLine("Found library transform: {0}", type.Name); + library = (ILibrary)Activator.CreateInstance(type); + } + } + catch (Exception ex) + { + Console.WriteLine("Error: assembly '{0}' could not be loaded: {1}", path, ex.Message); + return false; + } + + return true; + } + + public static void Main(String[] args) + { + var options = new Options(); + + if (!ParseCommandLineOptions(args, options)) + return; + + // We need to know absolute pathes in order to determine correct include pathes. + for (int i = 0; i < options.IncludeDirs.Count; i++) + { + if (options.IncludeDirs[i] == ".") options.IncludeDirs[i] = Directory.GetCurrentDirectory(); + } + + ILibrary library = null; + if (!ParseLibraryAssembly(options.Assembly, out library)) + return; + + var codeGenerator = new CodeGenerator(options, library); + codeGenerator.ParseCode(); + codeGenerator.ProcessCode(); + codeGenerator.GenerateCode(); + } + } +}