Skip to content

Commit

Permalink
Upgrade cppsharp / run dotnet format after codegen (#156)
Browse files Browse the repository at this point in the history
* upgrade cppsharp to latest

* dotnet format

* remove commented code and // todo

* ci

* ci
  • Loading branch information
hanabi1224 authored Mar 12, 2021
1 parent 6c53dde commit 7e3c642
Show file tree
Hide file tree
Showing 24 changed files with 260 additions and 159 deletions.
13 changes: 8 additions & 5 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,10 @@ jobs:
os: [ubuntu-latest, windows-latest]
steps:
- uses: actions/checkout@v2
- name: Build Library
- name: Library Build & Test
run: |
dotnet build -c Release FFmpeg.AutoGen
- name: Build Example
run: |
dotnet build -c Release FFmpeg.AutoGen.Example
dotnet build -c Release
dotnet test -c Release
- name: Build Redist windows x64
if: matrix.os == 'windows-latest'
run: |
Expand All @@ -39,3 +37,8 @@ jobs:
with:
path: FFmpeg.AutoGen.Redist.windows.x64/bin/Release/*.nupkg
if-no-files-found: error
- name: Codegen Build & Test
if: matrix.os == 'windows-latest'
run: |
dotnet run -c Release -p FFmpeg.AutoGen.CppSharpUnsafeGenerator -- -i FFmpeg
dotnet build FFmpeg.AutoGen
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ language: csharp
mono: none
dotnet: 5.0.200
script:
- dotnet build -c Release ./FFmpeg.AutoGen
- dotnet build -c Release ./FFmpeg.AutoGen.Example
- dotnet build -c Release
- dotnet test -c Release
4 changes: 2 additions & 2 deletions FFmpeg.AutoGen.CppSharpUnsafeGenerator/CliOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@ public static CliOptions ParseArgumentsStrict(string[] args)
private void Normalize()
{
// Support for the original path setup
const string solutionDir = "../../../../../";
string solutionDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "../../../../../");

if (string.IsNullOrWhiteSpace(FFmpegDir) &&
string.IsNullOrWhiteSpace(FFmpegIncludesDir) &&
string.IsNullOrWhiteSpace(FFmpegBinDir))
FFmpegDir = Path.Combine(solutionDir, "ffmpeg");
FFmpegDir = Path.Combine(solutionDir, "FFmpeg");

if (string.IsNullOrWhiteSpace(OutputDir)) OutputDir = Path.Combine(solutionDir, "FFmpeg.AutoGen/");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net472</TargetFramework>
<TargetFramework>net5</TargetFramework>
<Platforms>AnyCPU;x64</Platforms>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="CommandLineParser" Version="2.8.0" />
<PackageReference Include="CppSharp" Version="0.10.3" />
<PackageReference Include="CppSharp" Version="0.11.2" />
<PackageReference Include="RCore.ClangMacroParser" Version="0.0.0.3" />
</ItemGroup>

</Project>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public static IEnumerable<FunctionExport> LoadFunctionExports(string path)
var libraryVersion = int.Parse(libraryNameParts[1]);

var exports = GetExports(libraryPath);
foreach (var export in exports) yield return new FunctionExport { LibraryName = libraryName, LibraryVersion = libraryVersion, Name = export};
foreach (var export in exports) yield return new FunctionExport { LibraryName = libraryName, LibraryVersion = libraryVersion, Name = export };
}
}

Expand Down
20 changes: 13 additions & 7 deletions FFmpeg.AutoGen.CppSharpUnsafeGenerator/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using CppSharp;
using CppSharp.AST;
using CppSharp.Parser;
Expand Down Expand Up @@ -56,7 +57,7 @@ public void WriteLibraries(string combine)
"public static Dictionary<string, int> LibraryVersionMap = new Dictionary<string, int>");
using (writer.BeginBlock(true))
{
var libraryVersionMap = Exports.Select(x => new {x.LibraryName, x.LibraryVersion}).Distinct()
var libraryVersionMap = Exports.Select(x => new { x.LibraryName, x.LibraryVersion }).Distinct()
.ToDictionary(x => x.LibraryName, x => x.LibraryVersion);
foreach (var pair in libraryVersionMap) writer.WriteLine($"{{\"{pair.Key}\", {pair.Value}}},");
}
Expand Down Expand Up @@ -208,16 +209,21 @@ private ASTContext ParseInternal(string[] sourceFiles)
LanguageVersion = LanguageVersion.C99_GNU
};

parserOptions.SetupMSVC(VisualStudioVersion.Latest);
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
parserOptions.SetupMSVC(VisualStudioVersion.Latest);
}
else
{
throw new PlatformNotSupportedException();
}

foreach (var includeDir in IncludeDirs) parserOptions.AddIncludeDirs(includeDir);

foreach (var define in Defines) parserOptions.AddDefines(define);

var clangParser = new ClangParser(new CppSharp.Parser.AST.ASTContext());
clangParser.SourcesParsed += OnSourceFileParsed;
clangParser.ParseSourceFiles(sourceFiles, parserOptions);
return ClangParser.ConvertASTContext(clangParser.ASTContext);
var result = ClangParser.ParseSourceFiles(sourceFiles, parserOptions);
OnSourceFileParsed(sourceFiles, result);
return ClangParser.ConvertASTContext(parserOptions.ASTContext);
}

private void OnSourceFileParsed(IEnumerable<string> files, ParserResult result)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ private static object ConvertValue(ulong value, PrimitiveType primitiveType)
switch (primitiveType)
{
case PrimitiveType.Int:
return (int) value;
return (int)value;
case PrimitiveType.UInt:
return (uint) value;
return (uint)value;
case PrimitiveType.Long:
return (long) value;
return (long)value;
case PrimitiveType.ULong:
return value;
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,9 @@ private TypeDefinition GetReturnTypeName(Type type, string name)
}
};
case PrimitiveType.Void:
return new TypeDefinition {Name = "void*"};
return new TypeDefinition { Name = "void*" };
default:
return new TypeDefinition {Name = TypeHelper.GetTypeName(type)};
return new TypeDefinition { Name = TypeHelper.GetTypeName(type) };
}
}

Expand All @@ -135,11 +135,11 @@ private TypeDefinition GetParameterType(Type type, string name)
switch (builtinType.Type)
{
case PrimitiveType.Char:
return new TypeDefinition {Name = "string", Attributes = new[] {MarshalAsUtf8Macros}};
return new TypeDefinition { Name = "string", Attributes = new[] { MarshalAsUtf8Macros } };
case PrimitiveType.Void:
return new TypeDefinition {Name = "void*"};
return new TypeDefinition { Name = "void*" };
default:
return new TypeDefinition {Name = TypeHelper.GetTypeName(type)};
return new TypeDefinition { Name = TypeHelper.GetTypeName(type) };
}
}

Expand All @@ -149,7 +149,7 @@ private TypeDefinition GetParameterType(Type type, string name)
arrayType.Type is PointerType arrayPointerType &&
!(arrayPointerType.Pointee is BuiltinType || arrayPointerType.Pointee is TypedefType typedefType &&
typedefType.Declaration.Type is BuiltinType))
return new TypeDefinition {Name = TypeHelper.GetTypeName(arrayPointerType) + "*"};
return new TypeDefinition { Name = TypeHelper.GetTypeName(arrayPointerType) + "*" };

return _context.StructureProcessor.GetTypeDefinition(type, name);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,22 @@ internal class MacroPostProcessor

public void Process(IReadOnlyList<MacroDefinition> macros)
{
_macroExpressionMap = macros.ToDictionary(x => x.Name, x =>
_macroExpressionMap = new Dictionary<string, IExpression>(macros.Count);
foreach (var x in macros)
{
try
{
return Parser.Parse(x.Expression);
_macroExpressionMap.Add(x.Name, Parser.Parse(x.Expression));
}
catch (NotSupportedException)
{
Trace.TraceError($"Cannot parse macro expression: {x.Expression}");
return null;
}
});
catch (Exception e)
{
Trace.TraceError($"Cannot parse macro expression: {x.Expression}: {e.Message}");
}
}

foreach (var macro in macros) Process(macro);
}
Expand All @@ -52,7 +56,7 @@ private void Process(MacroDefinition macro)
macro.Content = $"{macro.Name} = {macro.Expression}";
macro.Expression = Serialize(expression);
macro.IsConst = IsConst(expression);
macro.IsValid = true;
macro.IsValid = !typeOrAlias.IsAlias || _astProcessor.TypeAliases.ContainsKey(typeOrAlias.Alias);
}

private static string CleanUp(string expression)
Expand Down Expand Up @@ -95,19 +99,19 @@ private IExpression Rewrite(IExpression expression)
switch (expression)
{
case BinaryExpression e:
{
var left = Rewrite(e.Left);
var right = Rewrite(e.Right);
var leftType = DeduceType(left);
var rightType = DeduceType(right);
if (e.OperationType.IsBitwise() && leftType.Precedence != rightType.Precedence)
{
var toType = leftType.Precedence > rightType.Precedence ? rightType : leftType;
if (leftType != toType) left = new CastExpression(toType.ToString(), left);
if (rightType != toType) right = new CastExpression(toType.ToString(), right);
var left = Rewrite(e.Left);
var right = Rewrite(e.Right);
var leftType = DeduceType(left);
var rightType = DeduceType(right);
if (e.OperationType.IsBitwise() && leftType.Precedence != rightType.Precedence)
{
var toType = leftType.Precedence > rightType.Precedence ? rightType : leftType;
if (leftType != toType) left = new CastExpression(toType.ToString(), left);
if (rightType != toType) right = new CastExpression(toType.ToString(), right);
}
return new BinaryExpression(left, e.OperationType, right);
}
return new BinaryExpression(left, e.OperationType, right);
}
case UnaryExpression e: return new UnaryExpression(e.OperationType, Rewrite(e.Operand));
case CastExpression e: return new CastExpression(e.TargetType, Rewrite(e.Operand));
case CallExpression e: return new CallExpression(e.Name, e.Arguments.Select(Rewrite));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ internal TypeDefinition GetTypeDefinition(Type type, string name = null)
case PointerType pointerType:
return GetTypeDefinition(pointerType, name);
default:
return new TypeDefinition {Name = TypeHelper.GetTypeName(type)};
return new TypeDefinition { Name = TypeHelper.GetTypeName(type) };
}
}

Expand Down Expand Up @@ -132,7 +132,7 @@ private static StructureField GetBitField(IEnumerable<string> names, long bitCou
return new StructureField
{
Name = fieldName,
FieldType = new TypeDefinition {Name = fieldType},
FieldType = new TypeDefinition { Name = fieldType },
Content = string.Join(" ", comments.Where(x => !string.IsNullOrWhiteSpace(x)).Select(x => x.Trim()))
};
}
Expand Down Expand Up @@ -161,14 +161,14 @@ private TypeDefinition GetFieldTypeForNestedDeclaration(Declaration declaration,
if (@class != null)
{
MakeDefinition(@class, typeName);
return new TypeDefinition {Name = typeName};
return new TypeDefinition { Name = typeName };
}

var @enum = declaration as Enumeration;
if (@enum != null)
{
_context.EnumerationProcessor.MakeDefinition(@enum, typeName);
return new TypeDefinition {Name = typeName};
return new TypeDefinition { Name = typeName };
}

throw new NotSupportedException();
Expand All @@ -177,7 +177,7 @@ private TypeDefinition GetFieldTypeForNestedDeclaration(Declaration declaration,

private TypeDefinition GetFieldTypeForFixedArray(ArrayType arrayType)
{
var fixedSize = (int) arrayType.Size;
var fixedSize = (int)arrayType.Size;

var elementType = arrayType.Type;
var elementTypeDefinition = GetTypeDefinition(elementType);
Expand All @@ -186,7 +186,7 @@ private TypeDefinition GetFieldTypeForFixedArray(ArrayType arrayType)
if (elementType.IsPointer())
name = TypeHelper.GetTypeName(elementType.GetPointee()) + "_ptrArray" + fixedSize;
if (elementType is ArrayType)
name = TypeHelper.GetTypeName(((ArrayType) elementType).Type) + "_arrayOfArray" + fixedSize;
name = TypeHelper.GetTypeName(((ArrayType)elementType).Type) + "_arrayOfArray" + fixedSize;

if (!_context.IsKnownUnitName(name))
{
Expand All @@ -200,7 +200,7 @@ private TypeDefinition GetFieldTypeForFixedArray(ArrayType arrayType)
_context.AddUnit(fixedArray);
}

return new TypeDefinition {Name = name, ByReference = !arrayType.QualifiedType.Qualifiers.IsConst};
return new TypeDefinition { Name = name, ByReference = !arrayType.QualifiedType.Qualifiers.IsConst };
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ private static string GetTypeName(TypedefType type)
return type.Declaration.Name;
}
}

private static string GetTypeName(PrimitiveType type)
{
switch (type)
Expand Down
36 changes: 33 additions & 3 deletions FFmpeg.AutoGen.CppSharpUnsafeGenerator/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using FFmpeg.AutoGen.CppSharpUnsafeGenerator.Processors;
Expand Down Expand Up @@ -41,11 +42,11 @@ internal static void Main(string[] args)
astProcessor.WellKnownMaros.Add("AV_VERSION_INT", typeof(int));
astProcessor.WellKnownMaros.Add("AV_VERSION", typeof(string));

var defines = new[] {"__STDC_CONSTANT_MACROS"};
var defines = new[] { "__STDC_CONSTANT_MACROS" };

var g = new Generator(astProcessor)
{
IncludeDirs = new[] {options.FFmpegIncludesDir},
IncludeDirs = new[] { options.FFmpegIncludesDir },
Defines = defines,
Exports = exports,
Namespace = options.Namespace,
Expand Down Expand Up @@ -95,6 +96,35 @@ internal static void Main(string[] args)
g.WriteIncompleteStructures(Path.Combine(options.OutputDir, "FFmpeg.structs.incomplete.g.cs"));
g.WriteExportFunctions(Path.Combine(options.OutputDir, "FFmpeg.functions.export.g.cs"));
g.WriteInlineFunctions(Path.Combine(options.OutputDir, "FFmpeg.functions.inline.g.cs"));

// Run latest dotnet format
{
using var p = Process.Start(new ProcessStartInfo
{
FileName = "dotnet",
Arguments = "tool install --global dotnet-format",
WorkingDirectory = options.OutputDir,
});
p.WaitForExit();
}
{
using var p = Process.Start(new ProcessStartInfo
{
FileName = "dotnet",
Arguments = "tool update --global dotnet-format",
WorkingDirectory = options.OutputDir,
});
p.WaitForExit();
}
{
using var p = Process.Start(new ProcessStartInfo
{
FileName = "dotnet",
Arguments = "format",
WorkingDirectory = options.OutputDir,
});
p.WaitForExit();
}
}
}
}
}
Loading

0 comments on commit 7e3c642

Please sign in to comment.