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

[dotnet][macOS] Incorrect registrar.h used when --require-pinvoke-wrappers:true is used #15190

Closed
spouliot opened this issue Jun 2, 2022 · 2 comments · Fixed by #15214
Closed
Labels
bug If an issue is a bug or a pull request a bug fix dotnet An issue or pull request related to .NET (6)
Milestone

Comments

@spouliot
Copy link
Contributor

spouliot commented Jun 2, 2022

Steps to Reproduce

  1. build a dotnet6.0-macos project (default)
  2. build the same dotnet6.0-macos project with --require-pinvoke-wrappers:true

Expected Behavior

Both project should build without error and run correctly

Actual Behavior

Native compilation failures happens when --require-pinvoke-wrappers:true is used

Environment

Feature not yet released. Using xamarin-macios main 33b73a1

Build Logs

https://gist.github.com/spouliot/cad06d79b61a4545981170c4131965bc

macos.binlog.zip

registrar.h (working)

#pragma clang diagnostic ignored "-Wdeprecated-declarations"
#pragma clang diagnostic ignored "-Wtypedef-redefinition"
#pragma clang diagnostic ignored "-Wobjc-designated-initializers"
#pragma clang diagnostic ignored "-Wunguarded-availability-new"
#define CORECLR_RUNTIME
#include <stdarg.h>
#include <objc/objc.h>
#include <objc/runtime.h>
#include <objc/message.h>
#import <Foundation/Foundation.h>
#import <AppKit/AppKit.h>
#import <CoreGraphics/CoreGraphics.h>
#import <WebKit/WebKit.h>
#import <QuartzCore/QuartzCore.h>
#import <AuthenticationServices/AuthenticationServices.h>
#import <Intents/Intents.h>
#import <CoreImage/CoreImage.h>
#import <CoreImage/CIFilterBuiltins.h>
#import <CloudKit/CloudKit.h>
#import <AVFoundation/AVFoundation.h>

@class Microsoft_macOS__AppKit_NSApplicationDelegate;
@class UnoAppDelegate;
struct trampoline_struct_p {
	void *v0;
};
@class DopeTestUno_App;
...

registrar.h (non working)

#pragma clang diagnostic ignored "-Wdeprecated-declarations"
#pragma clang diagnostic ignored "-Wtypedef-redefinition"
#pragma clang diagnostic ignored "-Wobjc-designated-initializers"
#pragma clang diagnostic ignored "-Wunguarded-availability-new"
#define CORECLR_RUNTIME
#include <stdarg.h>
#include <objc/objc.h>
#include <objc/runtime.h>
#include <objc/message.h>
#import <WebKit/WebKit.h>
#import <AuthenticationServices/AuthenticationServices.h>
#import <Intents/Intents.h>
#import <CoreImage/CoreImage.h>
#import <CoreImage/CIFilterBuiltins.h>
#import <CloudKit/CloudKit.h>
#import <AVFoundation/AVFoundation.h>

@class Microsoft_macOS__AppKit_NSApplicationDelegate;
@class UnoAppDelegate;
@class DopeTestUno_App;
...

Note the lack of

struct trampoline_struct_p {
	void *v0;
};

which is the source of the compile time errors.

Example Project (If Possible)

See #15145 for the (dotnet) project being used

@rolfbjarne rolfbjarne added bug If an issue is a bug or a pull request a bug fix dotnet An issue or pull request related to .NET (6) labels Jun 2, 2022
@rolfbjarne rolfbjarne added this to the .NET 7 milestone Jun 2, 2022
@spouliot
Copy link
Contributor Author

spouliot commented Jun 4, 2022

The header file pinvokes.h contains the trampoline_struct_p definition (normal as it needs it too)

but there's a check not to duplicate the generation inside StaticRegistrar.cs

n = "struct trampoline_struct_" + name.ToString ();
if (!structures.Contains (n)) {
	structures.Add (n);
	declarations.WriteLine ("{0} {{\n{1}}};", n, body.ToString ());
}

so it ends up using trampoline_struct_p, inside registrar.mm, without having it defined inside registrar.h

@rolfbjarne
Copy link
Member

Yeah, the problem is that the pinvoke wrapper generation code uses a lot of logic from the static registrar, but doesn't properly clean up after itself, so when the static registrar runs it sees some leftover state from the pinvoke wrapper generation.

rolfbjarne added a commit to rolfbjarne/xamarin-macios that referenced this issue Jun 7, 2022
…ing the static registrar. Fixes xamarin#15190.

Otherwise the P/Invoke generator leaves partial results in the static
registrar class, essentially saying things like "we've processed CoreMidi, no
need to add an #include for this framework", and then we'd generate the static
registrar code and that code would lack the #include for CoreMidi.

Finishing the P/Invoke generator output will clear out any state stored in the
static registrar.

Fixes xamarin#15190.
rolfbjarne added a commit that referenced this issue Jun 9, 2022
…ing the static registrar. Fixes #15190. (#15214)

Otherwise the P/Invoke generator leaves partial results in the static
registrar class, essentially saying things like "we've processed CoreMidi, no
need to add an #include for this framework", and then we'd generate the static
registrar code and that code would lack the #include for CoreMidi.

Finishing the P/Invoke generator output will clear out any state stored in the
static registrar.

Also fix a few other issues to make the generated P/Invoke wrapper code work,
and add a test.

Fixes #15190.
@ghost ghost locked as resolved and limited conversation to collaborators Jul 9, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug If an issue is a bug or a pull request a bug fix dotnet An issue or pull request related to .NET (6)
Projects
None yet
2 participants