Skip to content

Commit

Permalink
Added support for directly wrapping entire sets of interdependent lib…
Browse files Browse the repository at this point in the history
…raries.

It's realised by using modules. Users now have to define one module for each library they want wrapped while setting the driver up.

Signed-off-by: Dimitar Dobrev <dpldobrev@protonmail.com>
  • Loading branch information
ddobrev committed Jun 2, 2016
1 parent 373d867 commit b41dc26
Show file tree
Hide file tree
Showing 33 changed files with 318 additions and 212 deletions.
2 changes: 1 addition & 1 deletion examples/SDL/SDL.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public void Setup(Driver driver)
options.LibraryName = "SDL";
options.Headers.Add("SDL.h");
var sdlPath = Path.Combine(GetExamplesDirectory("SDL"), "SDL-2.0/include");
options.Module.IncludeDirs.Add(sdlPath);
options.addIncludeDirs(sdlPath);
options.OutputDir = "SDL";
}

Expand Down
9 changes: 2 additions & 7 deletions src/AST/Declaration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,7 @@ public enum GenerationKind
/// <summary>
/// Declaration is generated to be used internally.
/// </summary>
Internal,
/// <summary>
/// Declaration was already generated in a linked assembly.
/// </summary>
Link,
Internal
}

/// <summary>
Expand Down Expand Up @@ -252,8 +248,7 @@ public bool IsDeclared
{
var k = GenerationKind;
return k == GenerationKind.Generate
|| k == GenerationKind.Internal
|| k == GenerationKind.Link;
|| k == GenerationKind.Internal;
}
}

Expand Down
51 changes: 51 additions & 0 deletions src/AST/Expression.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;

namespace CppSharp.AST
{
Expand All @@ -7,6 +9,8 @@ public abstract class Expression : Statement
public string DebugText;

public abstract TV Visit<TV>(IExpressionVisitor<TV> visitor);

public abstract Expression Clone();
}

public class BuiltinTypeExpression : Expression
Expand Down Expand Up @@ -39,6 +43,19 @@ public override T Visit<T>(IExpressionVisitor<T> visitor)
{
return visitor.VisitExpression(this);
}

public override Expression Clone()
{
return new BuiltinTypeExpression
{
Value = this.Value,
Type = this.Type,
DebugText = this.DebugText,
Class = this.Class,
Declaration = this.Declaration,
String = this.String
};
}
}

public class BinaryOperator : Expression
Expand All @@ -59,6 +76,16 @@ public override T Visit<T>(IExpressionVisitor<T> visitor)
{
return visitor.VisitExpression(this);
}

public override Expression Clone()
{
return new BinaryOperator(LHS.Clone(), RHS.Clone(), OpcodeStr)
{
DebugText = this.DebugText,
Declaration = this.Declaration,
String = this.String
};
}
}

public class CallExpr : Expression
Expand All @@ -75,6 +102,18 @@ public override T Visit<T>(IExpressionVisitor<T> visitor)
{
return visitor.VisitExpression(this);
}

public override Expression Clone()
{
var clone = new CallExpr
{
DebugText = this.DebugText,
Declaration = this.Declaration,
String = this.String
};
clone.Arguments.AddRange(Arguments.Select(a => a.Clone()));
return clone;
}
}

public class CXXConstructExpr : Expression
Expand All @@ -91,6 +130,18 @@ public override T Visit<T>(IExpressionVisitor<T> visitor)
{
return visitor.VisitExpression(this);
}

public override Expression Clone()
{
var clone = new CXXConstructExpr
{
DebugText = this.DebugText,
Declaration = this.Declaration,
String = this.String
};
clone.Arguments.AddRange(Arguments.Select(a => a.Clone()));
return clone;
}
}

public interface IExpressionVisitor<out T>
Expand Down
7 changes: 6 additions & 1 deletion src/Generator/Module.cs → src/AST/Module.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Collections.Generic;

namespace CppSharp
namespace CppSharp.AST
{
public class Module
{
Expand All @@ -12,6 +12,8 @@ public Module()
Libraries = new List<string>();
Defines = new List<string>();
Undefines = new List<string>();
Units = new List<TranslationUnit>();
CodeFiles = new List<string>();
}

public List<string> IncludeDirs { get; private set; }
Expand All @@ -22,6 +24,9 @@ public Module()
public List<string> Undefines { get; set; }
public string OutputNamespace { get; set; }

public List<TranslationUnit> Units { get; private set; }
public List<string> CodeFiles { get; private set; }

public string SharedLibraryName
{
get
Expand Down
8 changes: 5 additions & 3 deletions src/AST/SymbolContext.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;

namespace CppSharp.AST
{
Expand Down Expand Up @@ -79,8 +80,9 @@ public void IndexSymbols()
{
foreach (var symbol in library.Symbols)
{
Symbols[symbol] = library;
if (symbol.StartsWith("__"))
if (!Symbols.ContainsKey(symbol))
Symbols[symbol] = library;
if (symbol.StartsWith("__", StringComparison.Ordinal))
{
string stripped = symbol.Substring(1);
if (!Symbols.ContainsKey(stripped))
Expand Down
9 changes: 9 additions & 0 deletions src/AST/TranslationUnit.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
Expand All @@ -24,6 +25,12 @@ public TranslationUnit(string file) : this()
/// Contains the macros present in the unit.
public List<MacroDefinition> Macros;

public Module Module
{
get { return (Module) module.Target; }
set { module = new WeakReference(value); }
}

public bool IsSystemHeader { get; set; }

public bool IsValid { get { return FilePath != "<invalid>"; } }
Expand Down Expand Up @@ -75,5 +82,7 @@ public string FileRelativePath
(fileRelativePath = Path.Combine(FileRelativeDirectory, FileName));
}
}

private WeakReference module;
}
}
4 changes: 2 additions & 2 deletions src/CppParser/Bindings/ParserGen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ public void Setup(Driver driver)
SetupLinuxOptions(options);

var basePath = Path.Combine(GetSourceDirectory("src"), "CppParser");
options.Module.IncludeDirs.Add(basePath);
options.Module.LibraryDirs.Add(".");
options.addIncludeDirs(basePath);
options.addLibraryDirs(".");

options.OutputDir = Path.Combine(GetSourceDirectory("src"), "CppParser",
"Bindings", Kind.ToString());
Expand Down
12 changes: 6 additions & 6 deletions src/CppParser/Bootstrap/Bootstrap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,16 @@ public void Setup(Driver driver)
options.MicrosoftMode = false;
options.TargetTriple = "i686-apple-darwin12.4.0";

options.Module.Defines.Add("__STDC_LIMIT_MACROS");
options.Module.Defines.Add("__STDC_CONSTANT_MACROS");
options.addDefines ("__STDC_LIMIT_MACROS");
options.addDefines ("__STDC_CONSTANT_MACROS");

var llvmPath = Path.Combine (GetSourceDirectory ("deps"), "llvm");
var clangPath = Path.Combine(llvmPath, "tools", "clang");

options.Module.IncludeDirs.Add(Path.Combine(llvmPath, "include"));
options.Module.IncludeDirs.Add(Path.Combine(llvmPath, "build", "include"));
options.Module.IncludeDirs.Add(Path.Combine (llvmPath, "build", "tools", "clang", "include"));
options.Module.IncludeDirs.Add(Path.Combine(clangPath, "include"));
options.addIncludeDirs(Path.Combine(llvmPath, "include"));
options.addIncludeDirs(Path.Combine(llvmPath, "build", "include"));
options.addIncludeDirs (Path.Combine (llvmPath, "build", "tools", "clang", "include"));
options.addIncludeDirs(Path.Combine(clangPath, "include"));
}

public void SetupPasses(Driver driver)
Expand Down
4 changes: 1 addition & 3 deletions src/Generator.Tests/ASTTestFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,11 @@ protected void ParseLibrary(params string[] files)
Options = new DriverOptions();

var testsPath = GeneratorTest.GetTestsDirectory("Native");
Options.Module.IncludeDirs.Add(testsPath);
Options.addIncludeDirs(testsPath);

Options.Headers.AddRange(files);

Driver = new Driver(Options, new TextDiagnosticPrinter());
foreach (var includeDir in Options.Module.IncludeDirs)
Options.addIncludeDirs(includeDir);
Driver.SetupIncludes();
Driver.BuildParseOptions();
if (!Driver.ParseCode())
Expand Down
2 changes: 1 addition & 1 deletion src/Generator.Tests/GeneratorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public virtual void Setup(Driver driver)
options.TargetTriple = Environment.Is64BitProcess ? "x86_64-apple-darwin" : "i686-apple-darwin";

var path = Path.GetFullPath(GetTestsDirectory(name));
options.Module.IncludeDirs.Add(path);
options.addIncludeDirs(path);

// Remove this hardcoded path once we update our LLVM binary packages to bundle
// the built-in Clang includes.
Expand Down
4 changes: 1 addition & 3 deletions src/Generator.Tests/ReadNativeDependenciesTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,9 @@ public void TestReadDependenciesOSX()
private static IList<string> GetDependencies(string library)
{
var driverOptions = new DriverOptions();
driverOptions.Module.LibraryDirs.Add(GeneratorTest.GetTestsDirectory("Native"));
driverOptions.addLibraryDirs(GeneratorTest.GetTestsDirectory("Native"));
driverOptions.Libraries.Add(library);
var driver = new Driver(driverOptions, new TextDiagnosticPrinter());
foreach (var libraryDir in driverOptions.Module.LibraryDirs)
driverOptions.addLibraryDirs(libraryDir);
Assert.IsTrue(driver.ParseLibraries());
var dependencies = driver.Symbols.Libraries[0].Dependencies;
return dependencies;
Expand Down
Loading

0 comments on commit b41dc26

Please sign in to comment.