Skip to content

Extern blob is duplicated per platform #8509

Closed
@klutzy

Description

@klutzy

Usually C bindings requires different calling conventions per platform. For example, linux-32 usually uses cdecl, win-32 uses stdcall, and win-64 uses win64-specific convention.
So if we want to bind a function universally, we have to duplicate extern ... {} block for each platform, e.g.

#[cfg(target_os = "linux")]
extern "cdecl" {
    fn func();
    ...
}
#[cfg(target_os = "win32", target_arch = "x86")]
extern "stdcall" {
    fn func();
    ...
}
#[cfg(target_os = "win32", target_arch = "x86_64")]
extern { // this is identical to extern "C" or extern "cdecl"
    fn func();
    ...
}

which is inconvenient and fragile. (I actually copied large extern blob for Mingw-w64 support (#8488).)

Other option is to generate binding code per platform from template. lifthrasiir/rust-opengles-angle uses the approach for egl binding.

I think it's good to add default calling convention keyword e.g. "platform" so that

extern "platform" {
    fn func();
    ...
}

where "platform" is interpreted as cdecl for linux-32, stdcall for win32, and so on. It would solve the issue for most cases.

EDIT: in irc @luqmana suggested to change extern {} (no abi specified) as platform-native callconv. (It's currently interpreted as "cdecl".) I think it's the best option.

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