From 7a3e5e91441bc557050b016f1964fbb86c9a8f67 Mon Sep 17 00:00:00 2001 From: josetr <37419832+josetr@users.noreply.github.com> Date: Sun, 13 Mar 2022 08:39:04 +0000 Subject: [PATCH] Add option to conditionally generate bindings --- .../Generators/CSharp/CSharpSources.cs | 83 +++++++++++++------ src/Generator/Options.cs | 2 + 2 files changed, 59 insertions(+), 26 deletions(-) diff --git a/src/Generator/Generators/CSharp/CSharpSources.cs b/src/Generator/Generators/CSharp/CSharpSources.cs index 02cb11ec..f2ce2ed2 100644 --- a/src/Generator/Generators/CSharp/CSharpSources.cs +++ b/src/Generator/Generators/CSharp/CSharpSources.cs @@ -113,8 +113,8 @@ namespace CppSharp.Generators.CSharp group spec by module.OutputNamespace into @group select @group) { - using (!string.IsNullOrEmpty(group.Key) - ? PushWriteBlock(BlockKind.Namespace, $"namespace {group.Key}", NewLineKind.BeforeNextBlock) + using (!string.IsNullOrEmpty(group.Key) + ? PushWriteBlock(BlockKind.Namespace, $"namespace {group.Key}", NewLineKind.BeforeNextBlock) : default) { foreach (var template in from s in @group @@ -188,8 +188,8 @@ namespace CppSharp.Generators.CSharp var shouldGenerateNamespace = !@namespace.IsInline && !isTranslationUnit && context.Declarations.Any(d => d.IsGenerated || (d is Class && !d.IsIncomplete)); - using var _ = shouldGenerateNamespace - ? PushWriteBlock(BlockKind.Namespace, $"namespace {context.Name}", NewLineKind.BeforeNextBlock) + using var _ = shouldGenerateNamespace + ? PushWriteBlock(BlockKind.Namespace, $"namespace {context.Name}", NewLineKind.BeforeNextBlock) : default; return base.VisitNamespace(@namespace); @@ -253,30 +253,40 @@ namespace CppSharp.Generators.CSharp var classes = EnumerateClasses().ToList(); if (classes.FindAll(cls => cls.IsValueType && cls.Name == parentName && context.QualifiedLogicalName == cls.Namespace.QualifiedLogicalName).Any()) keyword = "struct"; - WriteLine($"public unsafe partial {keyword} {parentName}"); - WriteOpenBraceAndIndent(); - PushBlock(BlockKind.InternalsClass); - GenerateClassInternalHead(new Class { Name = parentName }); + if (Options.GenerateTypesOnly && Options.PutAllGlobalsInGlobalClass) + parentName = "Globals"; + + WriteLine($"public unsafe partial {keyword} {parentName}"); WriteOpenBraceAndIndent(); - // Generate all the internal function declarations. - foreach (var function in context.Functions) + if (Options.GenerateBindings) { - if ((!function.IsGenerated && !function.IsInternal) || function.IsSynthetized) - continue; + PushBlock(BlockKind.InternalsClass); + GenerateClassInternalHead(new Class { Name = parentName }); + WriteOpenBraceAndIndent(); - GenerateInternalFunction(function); - } + if (Options.GenerateBindings) + { + // Generate all the internal function declarations. + foreach (var function in context.Functions) + { + if ((!function.IsGenerated && !function.IsInternal) || function.IsSynthetized) + continue; - UnindentAndWriteCloseBrace(); - PopBlock(NewLineKind.BeforeNextBlock); + GenerateInternalFunction(function); + } + } - foreach (var function in context.Functions) - { - if (!function.IsGenerated) continue; + UnindentAndWriteCloseBrace(); + PopBlock(NewLineKind.BeforeNextBlock); + + foreach (var function in context.Functions) + { + if (!function.IsGenerated) continue; - GenerateFunction(function, parentName); + GenerateFunction(function, parentName); + } } foreach (var variable in context.Variables.Where( @@ -409,7 +419,7 @@ namespace CppSharp.Generators.CSharp if (!@class.IsGenerated) goto exit; - if (ShouldGenerateClassNativeField(@class)) + if (Options.GenerateBindings && ShouldGenerateClassNativeField(@class)) { PushBlock(BlockKind.Field); if (@class.IsValueType) @@ -483,9 +493,12 @@ internal static bool {Helpers.TryGetNativeToManagedMappingIdentifier}(IntPtr nat WriteLine($"private bool __{prop.Field.OriginalName}_OwnsNativeMemory = false;"); } - GenerateClassConstructors(@class); + if (Options.GenerateBindings) + { + GenerateClassConstructors(@class); + GenerateClassMethods(@class.Methods); + } - GenerateClassMethods(@class.Methods); GenerateClassVariables(@class); GenerateClassProperties(@class); @@ -512,6 +525,9 @@ internal static bool {Helpers.TryGetNativeToManagedMappingIdentifier}(IntPtr nat GenerateClassSpecifier(@class); var shouldInheritFromIDisposable = !@class.HasBase; + if (Options.GenerateTypesOnly) + shouldInheritFromIDisposable = false; + if (shouldInheritFromIDisposable) Write(" : IDisposable"); @@ -570,6 +586,8 @@ internal static bool {Helpers.TryGetNativeToManagedMappingIdentifier}(IntPtr nat public void GenerateClassInternals(Class @class) { + if (!Options.GenerateBindings) + return; var sequentialLayout = Options.GenerateSequentialLayout && CanUseSequentialLayout(@class); PushBlock(BlockKind.InternalsClass); @@ -800,7 +818,7 @@ internal static bool {Helpers.TryGetNativeToManagedMappingIdentifier}(IntPtr nat } } - if (@class.IsGenerated && isBindingGen && @class.IsRefType && !@class.IsOpaque) + if (Options.GenerateBindings && @class.IsGenerated && isBindingGen && @class.IsRefType && !@class.IsOpaque) { bases.Add("IDisposable"); } @@ -1549,13 +1567,26 @@ internal static bool {Helpers.TryGetNativeToManagedMappingIdentifier}(IntPtr nat if (prop.IsVirtual && !isOverride && !prop.IsPure) Write("virtual "); - WriteLine($"{printedType} {GetPropertyName(prop)}"); + Write($"{printedType} {GetPropertyName(prop)}"); } else { - WriteLine($@"{printedType} { + Write($@"{printedType} { prop.ExplicitInterfaceImpl.Name}.{GetPropertyName(prop)}"); } + + if (Options.GenerateTypesOnly) + { + if (prop.HasSetter) + Write(@$" {{ get; set; }}"); + else + Write(@$" {{ get; }}"); + NewLine(); + PopBlock(NewLineKind.Never); + continue; + } + + WriteLine(String.Empty); WriteOpenBraceAndIndent(); if (prop.Field != null) diff --git a/src/Generator/Options.cs b/src/Generator/Options.cs index 7b6153e7..3b84a2ae 100644 --- a/src/Generator/Options.cs +++ b/src/Generator/Options.cs @@ -93,6 +93,8 @@ namespace CppSharp /// true to generate class templates; otherwise, false. /// public bool GenerateClassTemplates { get; set; } + public bool GenerateTypesOnly { get; set; } = false; + internal bool GenerateBindings => !GenerateTypesOnly; public bool AllowRenaming { get; set; } = true; public bool PutAllGlobalsInGlobalClass { get; set; } = false; public bool GenerateInternalImports;