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 8 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 @@ -10,7 +10,7 @@ namespace CppSharp
private static OptionSet optionSet = new OptionSet();
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;
@ -34,7 +34,7 @@ namespace CppSharp @@ -34,7 +34,7 @@ namespace CppSharp
optionSet.Add("h|help", "shows the help", hl => { showHelp = (hl != null); });
List<String> additionalArguments = null;
List<string> additionalArguments = null;
try
{
@ -53,7 +53,7 @@ namespace CppSharp @@ -53,7 +53,7 @@ namespace CppSharp
return false;
}
foreach(String s in additionalArguments)
foreach(string s in additionalArguments)
HandleAdditionalArgument(s, messages);
return true;
@ -61,8 +61,7 @@ namespace CppSharp @@ -61,8 +61,7 @@ namespace CppSharp
static void ShowHelp()
{
String appName = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
appName = Path.GetFileName(appName);
string appName = Platform.IsWindows ? "CppSharp.CLI.exe" : "CppSharp.CLI";
Console.WriteLine();
Console.WriteLine("Usage: {0} [OPTIONS]+ [FILES]+", appName);
@ -90,20 +89,20 @@ namespace CppSharp @@ -90,20 +89,20 @@ namespace CppSharp
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))
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<String> messages)
static void HandleOutputArg(string arg, List<string> 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 +116,7 @@ namespace CppSharp @@ -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)
messages.Add("Invalid definition name for option -D.");
@ -125,9 +124,9 @@ namespace CppSharp @@ -125,9 +124,9 @@ namespace CppSharp
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);
try
@ -141,20 +140,20 @@ namespace CppSharp @@ -141,20 +140,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<String> messages)
static void GetFilesFromPath(string path, List<string> 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 +167,28 @@ namespace CppSharp @@ -168,28 +167,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<String> messages)
static void GetGeneratorKind(string generator, List<string> messages)
{
switch (generator.ToLower())
{
@ -201,10 +200,10 @@ namespace CppSharp @@ -201,10 +200,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<String> messages)
static void GetDestinationPlatform(string platform, List<string> messages)
{
switch (platform.ToLower())
{
@ -219,10 +218,10 @@ namespace CppSharp @@ -219,10 +218,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<String> messages)
static void GetDestinationArchitecture(string architecture, List<string> messages)
{
switch (architecture.ToLower())
{
@ -234,23 +233,23 @@ namespace CppSharp @@ -234,23 +233,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<String> messages)
static void PrintMessages(List<string> messages)
{
foreach (String m in messages)
foreach (string m in messages)
Console.WriteLine(m);
}
static void Main(string[] args)
{
List<String> messages = new List<String>();
List<string> messages = new List<string>();
bool helpShown = false;
try
{
if (ParseCommandLineArgs(args, messages, ref helpShown) == false)
if (!ParseCommandLineArgs(args, messages, ref helpShown))
{
PrintMessages(messages);
@ -266,7 +265,7 @@ namespace CppSharp @@ -266,7 +265,7 @@ namespace CppSharp
if (validOptions)
gen.Run();
else if (helpShown == false)
else if (!helpShown)
ShowHelp();
}
catch (Exception ex)

154
src/CLI/Generator.cs

@ -6,161 +6,164 @@ using System; @@ -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<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");
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<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);
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 @@ -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 @@ -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);

55
src/CLI/Options.cs

@ -1,9 +1,4 @@ @@ -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 @@ -16,52 +11,34 @@ namespace CppSharp
class Options
{
private List<String> _headerFiles = 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; } }
public List<string> HeaderFiles { get; } = new List<string>();
private List<String> _libraryDirs = new List<string>();
public List<String> LibraryDirs { get { return _libraryDirs; } set { _libraryDirs = value; } }
public List<string> IncludeDirs { get; } = new List<string>();
private List<String> _libraries = new List<string>();
public List<String> Libraries { get { return _libraries; } set { _libraries = value; } }
public List<string> LibraryDirs { get; } = new List<string>();
private Dictionary<String, String> _defines = new Dictionary<String, String>();
public Dictionary<String, String> Defines { get { return _defines; } set { _defines = value; } }
public List<string> Libraries { get; } = new List<string>();
private String _outputDir = "";
public String OutputDir { get { return _outputDir; } set { _outputDir = value; } }
public Dictionary<string, string> Defines { get; } = new Dictionary<string, string>();
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; }
}
}
Loading…
Cancel
Save