haskell / c2hs

c2hs is a pre-processor for Haskell FFI bindings to C libraries
http://hackage.haskell.org/package/c2hs
Other
198 stars 50 forks source link

Hack to avoid Apple's block syntax. #143

Closed acowley closed 9 years ago

acowley commented 9 years ago

This performs a textual substitution of "(^" with "(*" when initially reading a header file before passing it to the language-c parser. This treats any block type definitions as function pointer type definitions. While this is not right, it is unlikely that correct treatment of this functionality is depended upon by any c2hs clients.

Without something to address this, c2hs can no longer be used to work with Apple's OpenCL framework.

Fixes #138

ian-ross commented 9 years ago

Thanks for this -- it's not very pretty, but it seems as though it ought to be fairly harmless and it's certainly better than waiting for me to write a patch for language-c to do it!

ian-ross commented 9 years ago

@acowley Been thinking about this, and although this is fine as an interim solution so that you can do stuff, I still think I'd like to write a patch for language-c -- call me paranoid, but I get itchy about global string replacements like this. (The OCD imp that sits on my shoulder says "What happens if you do the replacement inside a string constant? It could make something go WRONG! Woe! Disaster! Rains of fish and frogs!" You know the kind of thing...) Anyway, I'll write a little patch for language-c to parse the blocks syntax as a normal function pointer. Not sure when it will happen, but it will.

acowley commented 9 years ago

Sure, it's a ridiculous little hack. I think since the output of it is only going to language-c's parser, it's unlikely anything will go wrong, but I have no reservations about trying to find a better solution. I'd even go for a worse-er solution: we could only do the substitution on lines that start with a typedef.

I finally got this moving for two reasons. First, the next OS X (already in developers' hands) is going to make modifying some system files very, very difficult, so it's possible that even enterprising folks won't be able to work around this problem by editing headers to appease language-c. Second, I'm in the process of preparing some OpenCL-using code for release, and requiring that one edit a system header to get things working is a pretty big road block for potential OS X users.