Browse Source

Merge pull request #757 from zillemarco/master

Changed code to fit repository style and fixed wrong CLI application name
pull/337/merge
João Matos 9 years ago committed by GitHub
parent
commit
7beb072f0b
  1. 67
      src/CLI/CLI.cs
  2. 154
      src/CLI/Generator.cs
  3. 55
      src/CLI/Options.cs

67
src/CLI/CLI.cs

@ -10,7 +10,7 @@ namespace CppSharp
private static OptionSet optionSet = new OptionSet(); private static OptionSet optionSet = new OptionSet();
private static Options options = new Options(); private static Options options = new Options();
static bool ParseCommandLineArgs(string[] args, List<String> messages, ref bool helpShown) static bool ParseCommandLineArgs(string[] args, List<string> messages, ref bool helpShown)
{ {
var showHelp = false; var showHelp = false;
@ -34,7 +34,7 @@ namespace CppSharp
optionSet.Add("h|help", "shows the help", hl => { showHelp = (hl != null); }); optionSet.Add("h|help", "shows the help", hl => { showHelp = (hl != null); });
List<String> additionalArguments = null; List<string> additionalArguments = null;
try try
{ {
@ -53,7 +53,7 @@ namespace CppSharp
return false; return false;
} }
foreach(String s in additionalArguments) foreach(string s in additionalArguments)
HandleAdditionalArgument(s, messages); HandleAdditionalArgument(s, messages);
return true; return true;
@ -61,8 +61,7 @@ namespace CppSharp
static void ShowHelp() static void ShowHelp()
{ {
String appName = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName; string appName = Platform.IsWindows ? "CppSharp.CLI.exe" : "CppSharp.CLI";
appName = Path.GetFileName(appName);
Console.WriteLine(); Console.WriteLine();
Console.WriteLine("Usage: {0} [OPTIONS]+ [FILES]+", appName); Console.WriteLine("Usage: {0} [OPTIONS]+ [FILES]+", appName);
@ -90,20 +89,20 @@ namespace CppSharp
Console.WriteLine(" contain only the bindings for that header file."); Console.WriteLine(" contain only the bindings for that header file.");
} }
static void AddIncludeDirs(String dir, List<String> messages) static void AddIncludeDirs(string dir, List<string> messages)
{ {
if (Directory.Exists(dir)) if (Directory.Exists(dir))
options.IncludeDirs.Add(dir); options.IncludeDirs.Add(dir);
else 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<String> messages) static void HandleOutputArg(string arg, List<string> messages)
{ {
try try
{ {
String dir = Path.GetDirectoryName(arg); string dir = Path.GetDirectoryName(arg);
String file = Path.GetFileNameWithoutExtension(arg); string file = Path.GetFileNameWithoutExtension(arg);
options.OutputDir = dir; options.OutputDir = dir;
options.OutputFileName = file; options.OutputFileName = file;
@ -117,7 +116,7 @@ namespace CppSharp
} }
} }
static void AddDefine(String name, String value, List<String> messages) static void AddDefine(string name, string value, List<string> messages)
{ {
if (name == null) if (name == null)
messages.Add("Invalid definition name for option -D."); messages.Add("Invalid definition name for option -D.");
@ -125,9 +124,9 @@ namespace CppSharp
options.Defines.Add(name, value); options.Defines.Add(name, value);
} }
static void HandleAdditionalArgument(String args, List<String> messages) static void HandleAdditionalArgument(string args, List<string> messages)
{ {
if (Path.IsPathRooted(args) == false) if (!Path.IsPathRooted(args))
args = Path.Combine(Directory.GetCurrentDirectory(), args); args = Path.Combine(Directory.GetCurrentDirectory(), args);
try try
@ -141,20 +140,20 @@ namespace CppSharp
options.HeaderFiles.Add(args); options.HeaderFiles.Add(args);
else 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) 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<String> messages) static void GetFilesFromPath(string path, List<string> messages)
{ {
path = path.Replace(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar); path = path.Replace(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
String searchPattern = String.Empty; string searchPattern = string.Empty;
int lastSeparatorPosition = path.LastIndexOf(Path.AltDirectorySeparatorChar); int lastSeparatorPosition = path.LastIndexOf(Path.AltDirectorySeparatorChar);
if (lastSeparatorPosition >= 0) if (lastSeparatorPosition >= 0)
@ -168,28 +167,28 @@ namespace CppSharp
try 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); options.HeaderFiles.Add(s);
} }
else 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); options.HeaderFiles.Add(s);
} }
} }
catch (Exception) 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<String> messages) static void GetGeneratorKind(string generator, List<string> messages)
{ {
switch (generator.ToLower()) switch (generator.ToLower())
{ {
@ -201,10 +200,10 @@ namespace CppSharp
return; 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<String> messages) static void GetDestinationPlatform(string platform, List<string> messages)
{ {
switch (platform.ToLower()) switch (platform.ToLower())
{ {
@ -219,10 +218,10 @@ namespace CppSharp
return; 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<String> messages) static void GetDestinationArchitecture(string architecture, List<string> messages)
{ {
switch (architecture.ToLower()) switch (architecture.ToLower())
{ {
@ -234,23 +233,23 @@ namespace CppSharp
return; 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<String> messages) static void PrintMessages(List<string> messages)
{ {
foreach (String m in messages) foreach (string m in messages)
Console.WriteLine(m); Console.WriteLine(m);
} }
static void Main(string[] args) static void Main(string[] args)
{ {
List<String> messages = new List<String>(); List<string> messages = new List<string>();
bool helpShown = false; bool helpShown = false;
try try
{ {
if (ParseCommandLineArgs(args, messages, ref helpShown) == false) if (!ParseCommandLineArgs(args, messages, ref helpShown))
{ {
PrintMessages(messages); PrintMessages(messages);
@ -266,7 +265,7 @@ namespace CppSharp
if (validOptions) if (validOptions)
gen.Run(); gen.Run();
else if (helpShown == false) else if (!helpShown)
ShowHelp(); ShowHelp();
} }
catch (Exception ex) catch (Exception ex)

154
src/CLI/Generator.cs

@ -6,161 +6,164 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Text;
using CppAbi = CppSharp.Parser.AST.CppAbi; using CppAbi = CppSharp.Parser.AST.CppAbi;
namespace CppSharp namespace CppSharp
{ {
class Generator : ILibrary class Generator : ILibrary
{ {
private Options _options; private Options options = null;
private string triple = "";
private String _triple = ""; private CppAbi abi = CppAbi.Microsoft;
private CppAbi _abi = CppAbi.Microsoft;
public Generator(Options options) public Generator(Options options)
{ {
if (options == null) if (options == null)
throw new ArgumentNullException("options"); throw new ArgumentNullException(nameof(options));
_options = options; this.options = options;
} }
public bool ValidateOptions(List<String> messages) public bool ValidateOptions(List<string> 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"); messages.Add("Cannot create bindings for a platform other that Windows from a Windows running machine");
return false; 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"); messages.Add("Cannot create bindings for a platform other that MacOS from a MacOS running machine");
return false; 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"); messages.Add("Cannot create bindings for a platform other that Linux from a Linux running machine");
return false; 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"); messages.Add("Cannot create bindings for languages other than C# from a non Windows machine");
return false; 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"); messages.Add("Cannot create bindings for architectures other than x64 for Linux machines");
return false; 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"); messages.Add("No source header file has been given to generate bindings from");
return false; return false;
} }
if (_options.OutputNamespace == String.Empty) if (string.IsNullOrEmpty(options.OutputNamespace))
{ {
messages.Add("Output namespace is empty"); messages.Add("Output namespace is empty");
return false; return false;
} }
if (_options.OutputFileName == String.Empty) if (string.IsNullOrEmpty(options.OutputFileName))
{ {
messages.Add("Output not specified"); messages.Add("Output not specified");
return false; 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"); 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; 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"); 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; 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) if (options.Platform == TargetPlatform.Windows)
_triple = "x86_64-";
else if(_options.Architecture == TargetArchitecture.x86)
_triple = "i686-";
if (_options.Platform == TargetPlatform.Windows)
{ {
_triple += "pc-win32-msvc"; tripleBuilder.Append("pc-win32-msvc");
_abi = CppAbi.Microsoft; abi = CppAbi.Microsoft;
} }
else if (_options.Platform == TargetPlatform.MacOS) else if (options.Platform == TargetPlatform.MacOS)
{ {
_triple += "apple-darwin12.4.0"; tripleBuilder.Append("apple-darwin12.4.0");
_abi = CppAbi.Itanium; abi = CppAbi.Itanium;
} }
else if (_options.Platform == TargetPlatform.Linux) else if (options.Platform == TargetPlatform.Linux)
{ {
_triple += "linux-gnu"; tripleBuilder.Append("linux-gnu");
_abi = CppAbi.Itanium; abi = CppAbi.Itanium;
if(_options.Cpp11ABI) if(options.Cpp11ABI)
_triple += "-cxx11abi"; tripleBuilder.Append("-cxx11abi");
} }
triple = tripleBuilder.ToString();
return true; return true;
} }
public void Setup(Driver driver) public void Setup(Driver driver)
{ {
var parserOptions = driver.ParserOptions; var parserOptions = driver.ParserOptions;
parserOptions.TargetTriple = _triple; parserOptions.TargetTriple = triple;
parserOptions.Abi = _abi; parserOptions.Abi = abi;
var options = driver.Options; var driverOptions = driver.Options;
options.LibraryName = _options.OutputFileName; driverOptions.LibraryName = options.OutputFileName;
if(_options.InputLibraryName != String.Empty) if(!string.IsNullOrEmpty(options.InputLibraryName))
options.SharedLibraryName = _options.InputLibraryName; driverOptions.SharedLibraryName = options.InputLibraryName;
options.GeneratorKind = _options.Kind; driverOptions.GeneratorKind = options.Kind;
options.Headers.AddRange(_options.HeaderFiles); driverOptions.Headers.AddRange(options.HeaderFiles);
options.Libraries.AddRange(_options.Libraries); driverOptions.Libraries.AddRange(options.Libraries);
if (_abi == CppAbi.Microsoft) if (abi == CppAbi.Microsoft)
parserOptions.MicrosoftMode = true; parserOptions.MicrosoftMode = true;
if (_triple.Contains("apple")) if (triple.Contains("apple"))
SetupMacOptions(parserOptions); SetupMacOptions(parserOptions);
if (_triple.Contains("linux")) if (triple.Contains("linux"))
SetupLinuxOptions(parserOptions); SetupLinuxOptions(parserOptions);
foreach (String s in _options.IncludeDirs) foreach (string s in options.IncludeDirs)
parserOptions.AddIncludeDirs(s); parserOptions.AddIncludeDirs(s);
foreach (String s in _options.LibraryDirs) foreach (string s in options.LibraryDirs)
parserOptions.AddLibraryDirs(s); parserOptions.AddLibraryDirs(s);
foreach (KeyValuePair<String, String> d in _options.Defines) foreach (KeyValuePair<string, string> d in options.Defines)
{ {
if(d.Value == null || d.Value == String.Empty) if(string.IsNullOrEmpty(d.Value))
parserOptions.AddDefines(d.Key); parserOptions.AddDefines(d.Key);
else else
parserOptions.AddDefines(d.Key + "=" + d.Value); parserOptions.AddDefines(d.Key + "=" + d.Value);
} }
options.OutputDir = _options.OutputDir; driverOptions.OutputDir = options.OutputDir;
options.OutputNamespace = _options.OutputNamespace; driverOptions.OutputNamespace = options.OutputNamespace;
options.CheckSymbols = _options.CheckSymbols; driverOptions.CheckSymbols = options.CheckSymbols;
options.UnityBuild = _options.UnityBuild; driverOptions.UnityBuild = options.UnityBuild;
} }
private void SetupLinuxOptions(ParserOptions options) private void SetupLinuxOptions(ParserOptions parserOptions)
{ {
options.MicrosoftMode = false; parserOptions.MicrosoftMode = false;
options.NoBuiltinIncludes = true; parserOptions.NoBuiltinIncludes = true;
var headersPath = string.Empty; var headersPath = string.Empty;
@ -183,9 +186,9 @@ namespace CppSharp
}; };
foreach (var dir in systemIncludeDirs) 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) private static void SetupMacOptions(ParserOptions options)
@ -237,51 +240,52 @@ namespace CppSharp
public void Run() public void Run()
{ {
String message = "Generating the "; StringBuilder messageBuilder = new StringBuilder();
messageBuilder.Append("Generating the ");
switch(_options.Kind) switch(options.Kind)
{ {
case GeneratorKind.CLI: case GeneratorKind.CLI:
message += "C++/CLI"; messageBuilder.Append("C++/CLI");
break; break;
case GeneratorKind.CSharp: case GeneratorKind.CSharp:
message += "C#"; messageBuilder.Append("C#");
break; break;
} }
message += " parser bindings for "; messageBuilder.Append(" parser bindings for ");
switch (_options.Platform) switch (options.Platform)
{ {
case TargetPlatform.Linux: case TargetPlatform.Linux:
message += "Linux"; messageBuilder.Append("Linux");
break; break;
case TargetPlatform.MacOS: case TargetPlatform.MacOS:
message += "OSX"; messageBuilder.Append("OSX");
break; break;
case TargetPlatform.Windows: case TargetPlatform.Windows:
message += "Windows"; messageBuilder.Append("Windows");
break; break;
} }
message += " "; messageBuilder.Append(" ");
switch (_options.Architecture) switch (options.Architecture)
{ {
case TargetArchitecture.x86: case TargetArchitecture.x86:
message += "x86"; messageBuilder.Append("x86");
break; break;
case TargetArchitecture.x64: case TargetArchitecture.x64:
message += "x64"; messageBuilder.Append("x64");
break; break;
} }
if(_options.Cpp11ABI) if(options.Cpp11ABI)
message += " (GCC C++11 ABI)"; messageBuilder.Append(" (GCC C++11 ABI)");
message += "..."; messageBuilder.Append("...");
Console.WriteLine(message); Console.WriteLine(messageBuilder.ToString());
ConsoleDriver.Run(this); ConsoleDriver.Run(this);

55
src/CLI/Options.cs

@ -1,9 +1,4 @@
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CppSharp.Generators; using CppSharp.Generators;
namespace CppSharp namespace CppSharp
@ -16,52 +11,34 @@ namespace CppSharp
class Options class Options
{ {
private List<String> _headerFiles = new List<string>(); public List<string> HeaderFiles { get; } = new List<string>();
public List<String> HeaderFiles { get { return _headerFiles; } set { _headerFiles = value; } }
private List<String> _includeDirs = new List<string>();
public List<String> IncludeDirs { get { return _includeDirs; } set { _includeDirs = value; } }
private List<String> _libraryDirs = new List<string>(); public List<string> IncludeDirs { get; } = new List<string>();
public List<String> LibraryDirs { get { return _libraryDirs; } set { _libraryDirs = value; } }
private List<String> _libraries = new List<string>(); public List<string> LibraryDirs { get; } = new List<string>();
public List<String> Libraries { get { return _libraries; } set { _libraries = value; } }
private Dictionary<String, String> _defines = new Dictionary<String, String>(); public List<string> Libraries { get; } = new List<string>();
public Dictionary<String, String> Defines { get { return _defines; } set { _defines = value; } }
private String _outputDir = ""; public Dictionary<string, string> Defines { get; } = new Dictionary<string, string>();
public String OutputDir { get { return _outputDir; } set { _outputDir = value; } }
private String _outputNamespace = ""; public string OutputDir { get; set; }
public String OutputNamespace { get { return _outputNamespace; } set { _outputNamespace = value; } }
private String _outputFileName = ""; public string OutputNamespace { get; set; }
public String OutputFileName { get { return _outputFileName; } set { _outputFileName = value; } }
private String _inputLibraryName = ""; public string OutputFileName { get; set; }
public String InputLibraryName { get { return _inputLibraryName; } set { _inputLibraryName = value; } }
private String _triple = ""; public string InputLibraryName { get; set; }
public String Triple { get { return _triple; } set { _triple = value; } }
public TargetPlatform Platform { get; set; } = TargetPlatform.Windows;
private TargetPlatform _platform = TargetPlatform.Windows;
public TargetPlatform Platform { get { return _platform; } set { _platform = value; } }
private TargetArchitecture _architecture = TargetArchitecture.x86; public TargetArchitecture Architecture { get; set; } = TargetArchitecture.x86;
public TargetArchitecture Architecture { get { return _architecture; } set { _architecture = value; } }
private GeneratorKind _kind = GeneratorKind.CSharp; public GeneratorKind Kind { get; set; } = GeneratorKind.CSharp;
public GeneratorKind Kind { get { return _kind; } set { _kind = value; } }
private bool _checkSymbols = false; public bool CheckSymbols { get; set; }
public bool CheckSymbols { get { return _checkSymbols; } set { _checkSymbols = value; } }
private bool _unityBuild = false; public bool UnityBuild { get; set; }
public bool UnityBuild { get { return _unityBuild; } set { _unityBuild = value; } }
private bool _cpp11ABI = false; public bool Cpp11ABI { get; set; }
public bool Cpp11ABI { get { return _cpp11ABI; } set { _cpp11ABI = value; } }
} }
} }
Loading…
Cancel
Save