Skip to content

Incomplete bindings generated when using the --include parameter #474

@f2bo

Description

@f2bo

I'm generating mappings for a library where a few of its functions have parameters with types defined in headers external to the library, for example, timespec defined in <time.h>. It's my understanding that I need to traverse this header and since I do not want to bring in every definition in these headers but just generate bindings for the few types used by the library, I use the --include parameter to specify them.

I originally assumed that its behavior would mirror --exclude and that I just need to specify the names to include.

--traverse
C:/Program Files (x86)/Windows Kits/10/Include/10.0.22000.0/ucrt/time.h
C:/Program Files (x86)/Windows Kits/10/Include/10.0.22000.0/ucrt/corecrt_wtime.h

--include
timespec
tm

However, when used in this manner, while it does generate bindings for the specified names, these definitions are empty.

public partial struct timespec
{
}

After some trial and error, I found that specifying the name of every member that needs to be included does work (wildcards are not supported). For example,

--include
timespec
    timespec::tv_sec
    timespec::tv_nsec
tm
    tm::tm_sec
    tm::tm_min
    tm::tm_hour
    tm::tm_mday
    tm::tm_mon
    tm::tm_year
    tm::tm_wday
    tm::tm_yday
    tm::tm_isdst

This seems quite inconvenient and error prone, but lets say that it needs to be this way. However, I'm still having trouble with the definition for sockaddr_in in <winsock.h>.

struct sockaddr_in {
    short   sin_family;
    u_short sin_port;
    struct  in_addr sin_addr;
    char    sin_zero[8];
};

This also requires a definition for in_addr in <inaddr.h>.

typedef struct in_addr {
    union {
        struct { UCHAR s_b1,s_b2,s_b3,s_b4; } S_un_b;
        struct { USHORT s_w1,s_w2; } S_un_w;
        ULONG S_addr;
    } S_un;
#define s_addr  S_un.S_addr /* can be used for most tcp & ip code */
#define s_host  S_un.S_un_b.s_b2    // host on imp
#define s_net   S_un.S_un_b.s_b1    // network
#define s_imp   S_un.S_un_w.s_w2    // imp
#define s_impno S_un.S_un_b.s_b4    // imp #
#define s_lh    S_un.S_un_b.s_b3    // logical host
} IN_ADDR, *PIN_ADDR, FAR *LPIN_ADDR;

I've tried all sorts of combinations but while I can produce a definition for sockaddr_in, I'm unable to generate a complete binding for in_addr.

internal partial struct in_addr
{
    [NativeTypeName("__AnonymousRecord_inaddr_L26_C9")]
    public _S_un_e__Union S_un;
}

internal unsafe partial struct sockaddr_in
{
    public short sin_family;

    [NativeTypeName("u_short")]
    public ushort sin_port;

    [NativeTypeName("struct in_addr")]
    public in_addr sin_addr;

    [NativeTypeName("char[8]")]
    public fixed sbyte sin_zero[8];
}
For completeness, this is the binding generated for `in_addr` when I remove the --include parameter and bindings are generated for every member.
internal partial struct in_addr
{
    [NativeTypeName("__AnonymousRecord_inaddr_L26_C9")]
    public _S_un_e__Union S_un;

    [StructLayout(LayoutKind.Explicit)]
    internal partial struct _S_un_e__Union
    {
        [FieldOffset(0)]
        [NativeTypeName("__AnonymousRecord_inaddr_L27_C17")]
        public _S_un_b_e__Struct S_un_b;

        [FieldOffset(0)]
        [NativeTypeName("__AnonymousRecord_inaddr_L28_C17")]
        public _S_un_w_e__Struct S_un_w;

        [FieldOffset(0)]
        [NativeTypeName("ULONG")]
        public uint S_addr;

        internal partial struct _S_un_b_e__Struct
        {
            [NativeTypeName("UCHAR")]
            public byte s_b1;

            [NativeTypeName("UCHAR")]
            public byte s_b2;

            [NativeTypeName("UCHAR")]
            public byte s_b3;

            [NativeTypeName("UCHAR")]
            public byte s_b4;
        }

        internal partial struct _S_un_w_e__Struct
        {
            public ushort s_w1;

            public ushort s_w2;
        }
    }
}

Any suggestions on how this should be handled? Thanks.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions