RE: Summary of current change suggestions
foreign import ccall "<gtk/gtk.h>:gtk_window_new" windowNew :: CInt -> IO (Ptr Window)
If the <.> are omitted in the include specification, it corresponds to an #include"...".
Don't forget you might need multiple includes, eg. <sys/types.h> & <sys/socket.h> for socket(), and ordering is important. Multiple includes could be separated by commas. I agree that adding the include spec to the foreign import declaration is a good idea because of the cross-module inlining issue (I'd forgotten about that). Marcin: I guess hsc2hs should have some magic to insert the include specs automatically :) Cheers, Simon
Thu, 22 Feb 2001 12:08:22 -0000, Simon Marlow <simonmar@microsoft.com> pisze:
Don't forget you might need multiple includes, eg. <sys/types.h> & <sys/socket.h> for socket(), and ordering is important.
Easy with my proposal: foreign import "socket" include "<sys/types.h>" include "<sys/socket.h>" socket_c :: CInt -> CInt -> CInt -> IO CInt include options accumulate instead of being overridden. I insist on being able to write: foreign default include "<sys/types.h>" include "<sys/socket.h>"
Marcin: I guess hsc2hs should have some magic to insert the include specs automatically :)
Ugly magic. 'foreign default' is better. #let my_includes name = "\"<sys/types.h>:<sys/socket.h>:%s\"", name foreign import #my_includes "socket" socket_c :: CInt -> CInt -> CInt -> IO CInt How would I omit the foreign name (to be derived from the Haskell name) but specify headers? No, making it a part of a string is bad. Optional attributes should be expressed with an open set of defaultable pseudo-keywords like current 'unsafe'. -- __("< Marcin Kowalczyk * qrczak@knm.org.pl [Bhttp://qrczak.ids.net.pl/ \__/ ^^ SYGNATURA ZASTÊPCZA QRCZAK
On 22-Feb-2001, Marcin 'Qrczak' Kowalczyk <qrczak@knm.org.pl> wrote:
Thu, 22 Feb 2001 12:08:22 -0000, Simon Marlow <simonmar@microsoft.com> pisze:
Don't forget you might need multiple includes, eg. <sys/types.h> & <sys/socket.h> for socket(), and ordering is important.
Easy with my proposal:
foreign import "socket" include "<sys/types.h>" include "<sys/socket.h>" socket_c :: CInt -> CInt -> CInt -> IO CInt
include options accumulate instead of being overridden.
OK, now how about things where you need to #define symbols before including the header file? Or where the header file to include varies depending on the system? E.g. consider the following code. #include "config.h" #ifdef HAVE_SIGCONTEXT_STRUCT /* ** Some versions of Linux call it struct sigcontext_struct, some call it ** struct sigcontext. The following #define eliminates the differences. */ #define sigcontext_struct sigcontext /* must be before #include <signal.h> */ struct sigcontext; /* this forward decl avoids a gcc warning in signal.h */ /* ** On some systems (e.g. most versions of Linux) we need to #define ** __KERNEL__ to get sigcontext_struct from <signal.h>. */ #define __KERNEL__ #include <signal.h> #undef __KERNEL__ /* ** Some versions of Linux define it in <signal.h>, others define it in ** <asm/sigcontext.h>. We try both. */ #ifdef HAVE_ASM_SIGCONTEXT #include <asm/sigcontext.h> #endif #else #include <signal.h> #endif -- Fergus Henderson <fjh@cs.mu.oz.au> | "I have always known that the pursuit | of excellence is a lethal habit" WWW: <http://www.cs.mu.oz.au/~fjh> | -- the last words of T. S. Garp.
On Fri, 23 Feb 2001, Fergus Henderson wrote:
OK, now how about things where you need to #define symbols before including the header file?
Choose one: 1. Write a forwarding header which #defines and #includes. 2. Use compiler-specific switches to get #defines (-D and -optc-D in ghc). 3. Extend the proposed FFI to have define as well as include. Unfortunately option 1 doesn't always work well on ghc, because Stg.h (included into .hc files) indirectly includes headers like <stdlib.h> *before* user-specified -#include options, so symbols like _BSD_SOURCE are defined too late to have any effects. This should be fixed. hsc2hs provides a mix of option 1 and option 2. You write #define in .hsc source before #include and it usually works, but sometimes it's too late. I hope this mess can be somewhat sorted out; symbols go into several files and it is messy.
Or where the header file to include varies depending on the system?
When you have conditional compilation (either via hsc2hs or via cpp), wrap the proposed 'foreign default include "..."' in appropriate #ifdefs. -- Marcin 'Qrczak' Kowalczyk
"Marcin 'Qrczak' Kowalczyk" <mk167280@zodiac.mimuw.edu.pl> wrote,
On Fri, 23 Feb 2001, Fergus Henderson wrote:
OK, now how about things where you need to #define symbols before including the header file?
Choose one: 1. Write a forwarding header which #defines and #includes.
That's exactly the way to do it.
3. Extend the proposed FFI to have define as well as include.
We can't extend the FFI to implement half the features of all the language we want to bind to. I agree with Alastair and SimonPJ that we have reached the maximum in add on features to Haskell for supporting foreign bindings. I am happy about any change that makes things more orthogonal or so, but I don't think we should add any more features without simplyfing something or taking something else out.
Unfortunately option 1 doesn't always work well on ghc, because Stg.h (included into .hc files) indirectly includes headers like <stdlib.h> *before* user-specified -#include options, so symbols like _BSD_SOURCE are defined too late to have any effects. This should be fixed.
Yes, but it's an implementation problem, not one of the FFI.
hsc2hs provides a mix of option 1 and option 2. You write #define in .hsc source before #include and it usually works, but sometimes it's too late. I hope this mess can be somewhat sorted out; symbols go into several files and it is messy.
Or where the header file to include varies depending on the system?
When you have conditional compilation (either via hsc2hs or via cpp), wrap the proposed 'foreign default include "..."' in appropriate #ifdefs.
Yep - tool support is of course fine, too. Cheers, Manuel
participants (5)
-
Fergus Henderson -
Manuel M. T. Chakravarty -
Marcin 'Qrczak' Kowalczyk -
qrczak@knm.org.pl -
Simon Marlow