CppSharp icon indicating copy to clipboard operation
CppSharp copied to clipboard

error: blocks support disabled

Open LuKePicci opened this issue 4 years ago • 6 comments

Brief Description

Hi all, I'm having trouble in generating C# bindings for macOS Endpoint Security API. Below you have more details and the full error message, but I would kindly ask you the following:

  • Is what I'm trying to achieve doable with CppSharp or not?
  • Would it generate meaningful bindings if I manage to enable blocks support?
  • If not, could I manually write C# p/invoke bindings for that single problematic function expecting that block type as argument and let CppSharp deal with the remaining part of those headers?

Cheers, LuKe

OS: macOS 10.15.7 SDK: .NET Core 3.1

Used headers
  • EndpointSecurity/EndpointSecurity.h, which is a wrapper for:
    • EndpointSecurity/ESTypes.h
    • EndpointSecurity/ESMessage.h
    • EndpointSecurity/ESClient.h

located at: /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/

The problematic line is at ESClient.h(187,15) and contains this block type definition: https://developer.apple.com/documentation/endpointsecurity/es_handler_block_t

Used settings

Nothing special so far, same as in CppSharp's Getting Started page. I built nupkgs using dotnet pack command, then referenced CppSharp.Generator from inside a new project and implemented Setup() adding just EndpointSecurity/EndpointSecurity.h as header, which is found as expected.

I had to set DYLD_LIBRARY_PATH env variable in project "Run" settings to /full_path_to/CppSharp/bin/Release_x64 to let it load libCppSharp**.dylib, and I copied lib folder from inside the same folder into my project bin/Release output folder.

Stack trace
Error parsing 'EndpointSecurity/EndpointSecurity.h'
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/EndpointSecurity/ESClient.h(187,15): error: blocks support disabled - compile with -fblocks or pick a deployment target that supports them
CppSharp has encountered an error while parsing code.

LuKePicci avatar Jan 15 '21 11:01 LuKePicci

Try adding the parserOptions.TargetTriple = "x86_64-apple-darwin" as an option to setup the parser in Mac mode.

I don't think we have explicit blocks support atm, so you might need to extend CppSharp to bind that correctly.

tritao avatar Jan 15 '21 11:01 tritao

Hi, @tritao adding parserOptions.TargetTriple = "x86_64-apple-darwin" I get an additional error about a missing def:

Error parsing 'EndpointSecurity/EndpointSecurity.h'
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/EndpointSecurity/ESMessage.h(19,3): This header requires __DARWIN_64_BIT_INO_T
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/EndpointSecurity/ESClient.h(187,15): error: blocks support disabled - compile with -fblocks or pick a deployment target that supports them
CppSharp has encountered an error while parsing code.

LuKePicci avatar Jan 15 '21 11:01 LuKePicci

It's possible that header is not meant to be included standalone, or only after some other foundation header has been included.

Anyway you can try to set Defines option with it, and to pass -fblocks explicitly as well to see if that gets you going.

Btw what happens if you try calling Clang on a file with just that include? Does it work?

tritao avatar Jan 15 '21 12:01 tritao

or only after some other foundation header has been included

I see a Foundation/Foundation.h being included in a sample app using that API, however adding it to module.Headers made no difference, it seems to get parsed fine but that flag is still missing

Btw what happens if you try calling Clang on a file with just that include? Does it work?

Yes, running clang (which is /usr/bin/clang) onto a minimal:

#include <EndpointSecurity/EndpointSecurity.h>

int main() {}

just works, no Foundation.h included, no -fblocks passed

Anyway you can try to set Defines option with it,

I wonder why I don't get the missing __DARWIN_64_BIT_INO_T error when TargetTriple is not being set by me in Setup() I added a driver.ParserOptions.AddDefines("__DARWI_64_BIT_INO_T") and it now goes on

and to pass -fblocks explicitly as well to see if that gets you going.

adding driver.ParserOptions.AddArguments("-fblocks") made the blocks support error go away

Now I see no more errors, it says Processing code... , Generating code... and Generated Std.cs which is quite empty (ie. just five using statements

LuKePicci avatar Jan 15 '21 12:01 LuKePicci

Right now we skip almost all system declarations by default (with an exception for C++ standard library types) for performance reasons. This could be the reason why its not generating anything. We should add an option to disable this for these cases of binding system headers.

You could try to change the Parser::IsSupported methods to just return true: https://github.com/mono/CppSharp/blob/master/src/CppParser/Parser.cpp#L899

Another easier alternative that might work is to copy the headers you need to a local folder (outside system include folders) and try to bind those instead.

tritao avatar Jan 15 '21 13:01 tritao

Moving those headers to the desktop worked, it's now generating the expected code. Of course it crashes as soon as it finds the block_t def. I managed to get it complete by commenting out that block_t definition and the only function using it.

Now I need to figure out how to translate that block_t into C#, it could be similar to a delegate.

LuKePicci avatar Jan 15 '21 15:01 LuKePicci