Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option to only generate 1:1 bindings, and nothing else #976

Open
Sergio0694 opened this issue Jun 24, 2023 · 2 comments
Open

Option to only generate 1:1 bindings, and nothing else #976

Sergio0694 opened this issue Jun 24, 2023 · 2 comments
Labels
enhancement New feature or request

Comments

@Sergio0694
Copy link
Member

Sergio0694 commented Jun 24, 2023

Overview

Currently, even when setting "disableMarshalling": true in the JSON file, CsWin32 will generate a lot of "helper" stuff by default (eg. friendlier wrappers for P/Invoke-s and generated COM types with managed signatures, vtable types, interface types, etc.). It would be nice to have an option to just have CsWin32 generate the 1:1 bindings for the specified APIs, and literally nothing else. In many cases, these additional wrappers are just not needed at all, because people would just be building their own abstractions on top of them anyway, and keeping them around just increases the IL size unnecessarily. It would be nice to just be able to tell CsWin32

"Literally just get me 1:1 bindings for these APIs as if I was using the C++ header directly"

Describe the solution you'd like

We could have a new "disableManagedHelpers" (or, some other name) boolean flag in the JSON, set to false by default. When set, CsWin32 would stop generating everything that's not directly required by the requested APIs. That is, it would skip:

  • All P/Invoke wrappers
  • All helper methods over COM types
  • All vtable types
  • All interface types for COM structs

Ie. you'd only get raw P/Invoke methods, and raw 1:1 blittable bindings for COM types.

Note: this flag would only cause CsWin32 to skip generating some stuff it's currently generating, so it should be fairly low cost to implement. Just add the new flag and pass it through, and add some checks to skip generating those optional APIs.

Describe alternatives you've considered

Just doing nothing, which would keep the current situation as is.

Additional thoughts

Related to this, but maybe a separate proposal — it would be nice if this also made CsWin32 stop generating all helper wrappers such as HRESULT, HWND, BOOL etc. as well. Instead, the real underlying value should be used, and CsWin32 could just generate a few global using type aliases for them. This would also match C++, as they're just defines there.

As in, something like:

global using BOOL = System.Int32;
global using HWND = System.IntPtr;

internal static extern BOOL SetWindowPos(HWND hWnd, HWND hWndInsertAfter, int X, int Y, int cx, int cy, winmdroot.UI.WindowsAndMessaging.SET_WINDOW_POS_FLAGS uFlags);
@Sergio0694 Sergio0694 added the enhancement New feature or request label Jun 24, 2023
@AArnott
Copy link
Member

AArnott commented Jun 27, 2023

It's an interesting idea. I'm not opposed to it, but I can't say when it will bubble up to the top of the priority list either, as we have correctness issues or scenario blocking enhancements to make that may come first. But if you're interested in sending a PR, let me know and we can review the design proposal more closely before you start work.

You didn't mention enums. Would you prefer no enums and only emit individual enum values as constants on an individually required basis? That would lose the type information as no global using trick is a substitute for an enum's grouping of constants.

@AArnott
Copy link
Member

AArnott commented Jan 26, 2024

FYI #1117 gets you closer to your goal here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants