ClangSharp icon indicating copy to clipboard operation
ClangSharp copied to clipboard

[PInvokeGenerator] Provide a mechanism to wrap a generated file in a conditional

Open DaZombieKiller opened this issue 3 years ago • 0 comments

I am currently experimenting with generating multiple variants of a structure for interop purposes, based on various #ifdefs:

struct example
{
    int a;
#ifdef VERSION_2_OR_NEWER
    int b;
#endif
#ifdef VERSION_3_OR_NEWER
    int c;
#endif
};

And would like to generate several C# source files similar to the following:

#if VERSION_2
namespace MyNamespace;

public struct example
{
    public int a;
    public int b;
}
#endif
#if VERSION_3_OR_NEWER
namespace MyNamespace;

public struct example
{
    public int a;
    public int b;
    public int c;
}
#endif

Ordinarily, this would be handled by conditionally excluding files in the .csproj file, but this particular project is in Unity. Unfortunately, Unity takes ownership of all .csproj files (they are essentially temporary files used only for IDE integration, not compilation), which removes my ability to do things this way.

Ideally, ClangSharpPInvokeGenerator could provide a --with-condition parameter that I could use to wrap a generated file in the appropriate #if.

Alternatively, a mechanism to allow generation of code such as the following may be useful:

namespace MyNamespace;

public struct example
{
    public int a;
#if VERSION_2_OR_NEWER
    public int b;
#endif
#if VERSION_3_OR_NEWER
    public int c;
#endif
}

However I can see how this could easily grow in complexity, and would require significant effort to ensure that the correct layout of the structure is retained throughout all permutations of the type.

DaZombieKiller avatar Aug 05 '22 14:08 DaZombieKiller