@ -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 < S tring> messages )
public bool ValidateOptions ( List < s tring> 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 o ptions = driver . Options ;
o ptions. LibraryName = _ options . OutputFileName ;
var driverO ptions = driver . Options ;
driverO ptions. LibraryName = options . OutputFileName ;
if ( _ options . InputLibraryName ! = String . Empty )
o ptions. SharedLibraryName = _ options . InputLibraryName ;
if ( ! string . IsNullOrEmpty ( options . InputLibraryName ) )
driverO ptions. SharedLibraryName = options . InputLibraryName ;
o ptions. GeneratorKind = _ options . Kind ;
o ptions. Headers . AddRange ( _ options . HeaderFiles ) ;
o ptions. Libraries . AddRange ( _ options . Libraries ) ;
driverO ptions. GeneratorKind = options . Kind ;
driverO ptions. Headers . AddRange ( options . HeaderFiles ) ;
driverO ptions. 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 ( S tring s in _ options . IncludeDirs )
foreach ( s tring s in options . IncludeDirs )
parserOptions . AddIncludeDirs ( s ) ;
foreach ( S tring s in _ options . LibraryDirs )
foreach ( s tring s in options . LibraryDirs )
parserOptions . AddLibraryDirs ( s ) ;
foreach ( KeyValuePair < String , S tring> d in _ options . Defines )
foreach ( KeyValuePair < string , s tring> 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 ) ;
}
o ptions. OutputDir = _ options . OutputDir ;
o ptions. OutputNamespace = _ options . OutputNamespace ;
o ptions. CheckSymbols = _ options . CheckSymbols ;
o ptions. UnityBuild = _ options . UnityBuild ;
driverO ptions. OutputDir = options . OutputDir ;
driverO ptions. OutputNamespace = options . OutputNamespace ;
driverO ptions. CheckSymbols = options . CheckSymbols ;
driverO ptions. UnityBuild = options . UnityBuild ;
}
private void SetupLinuxOptions ( ParserOptions o ptions)
private void SetupLinuxOptions ( ParserOptions parserO ptions)
{
o ptions. MicrosoftMode = false ;
o ptions. NoBuiltinIncludes = true ;
parserO ptions. MicrosoftMode = false ;
parserO ptions. NoBuiltinIncludes = true ;
var headersPath = string . Empty ;
@ -183,9 +186,9 @@ namespace CppSharp
@@ -183,9 +186,9 @@ namespace CppSharp
} ;
foreach ( var dir in systemIncludeDirs )
o ptions. AddSystemIncludeDirs ( Path . Combine ( headersPath , dir ) ) ;
parserO ptions. AddSystemIncludeDirs ( Path . Combine ( headersPath , dir ) ) ;
o ptions. AddDefines ( "_GLIBCXX_USE_CXX11_ABI=" + ( _ options . Cpp11ABI ? "1" : "0" ) ) ;
parserO ptions. 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 ) ;