Skip to content

Commit

Permalink
Add --excludeFunctions command line argument
Browse files Browse the repository at this point in the history
Exclude functions that are manually defined in Generated.Custom.cs
  • Loading branch information
amaitland authored and benpye committed Apr 15, 2018
1 parent 6e34ae7 commit c7ebb7c
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 10 deletions.
13 changes: 11 additions & 2 deletions ClangSharpPInvokeGenerator/FunctionVisitor.cs
Expand Up @@ -13,12 +13,21 @@ internal sealed class FunctionVisitor : ICXCursorVisitor

private readonly string prefixStrip;

public FunctionVisitor(TextWriter tw, string libraryPath, string prefixStrip)
public FunctionVisitor(TextWriter tw, string libraryPath, string prefixStrip, string[] excludeFunctionsArray)
{
this.prefixStrip = prefixStrip;
this.tw = tw;
this.tw.WriteLine(" private const string libraryPath = \"" + libraryPath + "\";");
this.tw.WriteLine();

if(excludeFunctionsArray != null)
{
//For all the functions we're excluding add them to the visitedFunctions set so they're ignored
foreach(var func in excludeFunctionsArray)
{
visitedFunctions.Add(func);
}
}
}

public CXChildVisitResult Visit(CXCursor cursor, CXCursor parent, IntPtr data)
Expand Down Expand Up @@ -50,4 +59,4 @@ public CXChildVisitResult Visit(CXCursor cursor, CXCursor parent, IntPtr data)
return CXChildVisitResult.CXChildVisit_Recurse;
}
}
}
}
24 changes: 18 additions & 6 deletions ClangSharpPInvokeGenerator/Program.cs
Expand Up @@ -23,6 +23,8 @@ public static void Main(string[] args)
string libraryPath = string.Empty;
string prefixStrip = string.Empty;
string methodClassName = "Methods";
string excludeFunctions = "";
string[] excludeFunctionsArray = null;

foreach (KeyValuePair<string, string> match in matches)
{
Expand Down Expand Up @@ -60,6 +62,11 @@ public static void Main(string[] args)
{
methodClassName = match.Value;
}

if (string.Equals(match.Key, "--e") || string.Equals(match.Key, "--excludeFunctions"))
{
excludeFunctions = match.Value;
}
}

var errorList = new List<string>();
Expand All @@ -85,16 +92,21 @@ public static void Main(string[] args)

if (errorList.Any())
{
Console.WriteLine("Usage: ClangPInvokeGenerator --file [fileLocation] --libraryPath [library.dll] --output [output.cs] --namespace [Namespace] --include [headerFileIncludeDirs]");
Console.WriteLine("Usage: ClangPInvokeGenerator --file [fileLocation] --libraryPath [library.dll] --output [output.cs] --namespace [Namespace] --include [headerFileIncludeDirs] --excludeFunctions [func1,func2]");
foreach (var error in errorList)
{
Console.WriteLine(error);
}
}

if(!string.IsNullOrEmpty(excludeFunctions))
{
excludeFunctionsArray = excludeFunctions.Split(',').Select(x => x.Trim()).ToArray();
}

var createIndex = clang.createIndex(0, 0);
string[] arr = { "-x", "c++" };

arr = arr.Concat(includeDirs.Select(x => "-I" + x)).ToArray();

List<CXTranslationUnit> translationUnits = new List<CXTranslationUnit>();
Expand All @@ -104,7 +116,7 @@ public static void Main(string[] args)
CXTranslationUnit translationUnit;
CXUnsavedFile[] unsavedFile = new CXUnsavedFile[0];
var translationUnitError = clang.parseTranslationUnit2(createIndex, file, arr, 3, unsavedFile, 0, 0, out translationUnit);

if (translationUnitError != CXErrorCode.CXError_Success)
{
Console.WriteLine("Error: " + translationUnitError);
Expand Down Expand Up @@ -141,7 +153,7 @@ public static void Main(string[] args)
{
clang.visitChildren(clang.getTranslationUnitCursor(tu), typeDefVisitor.Visit, new CXClientData(IntPtr.Zero));
}

var enumVisitor = new EnumVisitor(sw);
foreach (var tu in translationUnits)
{
Expand All @@ -151,7 +163,7 @@ public static void Main(string[] args)
sw.WriteLine(" public static partial class " + methodClassName);
sw.WriteLine(" {");
{
var functionVisitor = new FunctionVisitor(sw, libraryPath, prefixStrip);
var functionVisitor = new FunctionVisitor(sw, libraryPath, prefixStrip, excludeFunctionsArray);
foreach (var tu in translationUnits)
{
clang.visitChildren(clang.getTranslationUnitCursor(tu), functionVisitor.Visit, new CXClientData(IntPtr.Zero));
Expand All @@ -169,4 +181,4 @@ public static void Main(string[] args)
clang.disposeIndex(createIndex);
}
}
}
}
2 changes: 1 addition & 1 deletion build.bat
Expand Up @@ -9,5 +9,5 @@ if [%2]==[] (
)

csc /out:ClangSharpPInvokeGenerator.exe ClangSharpPInvokeGenerator\*.cs
ClangSharpPInvokeGenerator.exe --m clang --p clang_ --namespace ClangSharp --output Generated.cs --libraryPath %1 --include %2 --file %2/clang-c/Index.h --file %2/clang-c/CXString.h --file %2/clang-c/Documentation.h --file %2/clang-c/CXErrorCode.h --file %2/clang-c/BuildSystem.h --file %2/clang-c/CXCompilationDatabase.h
ClangSharpPInvokeGenerator.exe --m clang --p clang_ --namespace ClangSharp --output Generated.cs --libraryPath %1 --include %2 --file %2/clang-c/Index.h --file %2/clang-c/CXString.h --file %2/clang-c/Documentation.h --file %2/clang-c/CXErrorCode.h --file %2/clang-c/BuildSystem.h --file %2/clang-c/CXCompilationDatabase.h --excludeFunctions clang_index_getClientEntity,clang_index_setClientEntity,clang_createTranslationUnitFromSourceFile,clang_parseTranslationUnit,clang_parseTranslationUnit2,clang_parseTranslationUnit2FullArgv,clang_reparseTranslationUnit,clang_codeCompleteAt,clang_indexSourceFile,clang_indexSourceFileFullArgv
csc /target:library /out:ClangSharp.dll Generated.cs Extensions.cs
3 changes: 2 additions & 1 deletion build.sh
Expand Up @@ -19,6 +19,7 @@ dotnet run -p ClangSharpPInvokeGenerator \
--file $2/clang-c/Documentation.h \
--file $2/clang-c/CXErrorCode.h \
--file $2/clang-c/BuildSystem.h \
--file $2/clang-c/CXCompilationDatabase.h
--file $2/clang-c/CXCompilationDatabase.h \
--excludeFunctions clang_index_getClientEntity,clang_index_setClientEntity,clang_createTranslationUnitFromSourceFile,clang_parseTranslationUnit,clang_parseTranslationUnit2,clang_parseTranslationUnit2FullArgv,clang_reparseTranslationUnit,clang_codeCompleteAt,clang_indexSourceFile,clang_indexSourceFileFullArgv

dotnet build ClangSharp

0 comments on commit c7ebb7c

Please sign in to comment.