From: Manuel M T C. <ch...@cs...> - 2005-08-21 12:43:12
|
[This reply only address the second issue, to keep the discussion better structured.] Duncan Coutts: > > Not including any .h in foreign imports is not a portable way of using > > the FFI. > > Sorry, I don't think I was clear enough. > > We use the -#include on the command line (and in the package.conf file) > to specify the header files to include. So we are compiling with the > correct .h files specified. So the point is that we want to use the .h > files specified in the package.conf file (or on the commandline during > the build itself) rather than lots of little .h files that just #include > the main .h file. > > So for example we build the modules like this: > > ghc -c gtk/Graphics/UI/Gtk/Buttons/Button.hs -o > gtk/Graphics/UI/Gtk/Buttons/Button.o -fffi -iglib:gtk -package-name gtk > '-#include<gtk/gtk.h>' -I/usr/include/glib-2.0 > -I/usr/lib/glib-2.0/include -DXTHREADS -D_REENTRANT -DXUSE_MTSAFE_API > -I/usr/include/gtk-2.0 -I/usr/lib/gtk-2.0/include > -I/usr/include/pango-1.0 -I/usr/include/freetype2 -I/usr/include/atk-1.0 > > So we specify the '-#include<gtk/gtk.h>' and all the -I dirs on the > command line. And the gtk.package.conf file contains: Well, the problem is that -#include is GHC specific. > extra-libraries: "gtk-x11-2.0","gdk-x11-2.0","atk-1.0","pangoxft-1.0","pangox-1.0","pango-1.0","gdk_pixbuf-2.0","m","gmodule-2.0","dl" > include-dirs: "/usr/include/gtk-2.0", "/usr/lib/gtk-2.0/include", "/usr/include/pango-1.0", "/usr/include/freetype2", "/usr/include/atk-1.0" > includes: gtk/gtk.h > > So that importing modules will get the correct include (and search > path). > > So we want to generate import decls like this: > > foreign import ccall safe "gtk_button_pressed" > gtk_button_pressed :: ((Ptr Button) -> (IO ())) > > instead of like this: > > foreign import ccall safe "gtk/Graphics/UI/Gtk/Buttons/Button.h gtk_button_pressed" > gtk_button_pressed :: ((Ptr Button) -> (IO ())) > > where gtk/Graphics/UI/Gtk/Buttons/Button.h contains nothing but: > #include <gtk/gtk.h> > > Since that way we do not need to distribute lots of little .h files. We > directly include the apprpriate system header. I understand that you want to get rid of the many .h files. My proposal is to do that by generating foreign import ccall safe "gtk/gtk.h gtk_button_pressed" gtk_button_pressed :: ((Ptr Button) -> (IO ())) This gets rid of the .h file, but is still portable. Obviously, this can only be done in cases, where the "little .h file" contains only a single #include. And it should only be done when a special command line option is given, but that should not be a problem for you. > Another reason for this is that ghc corrently does not inline FFI calls > which are declared in the latter form above (since it would also have to > propogate the .h files across module boundaries, and it currently > doesn't do that). > > See: > http://haskell.org/ghc/docs/latest/html/users_guide/sec-ffi-ghc.html#glasgow-foreign-headers > > > The only feasible "optimisation" for c2hs seems to be that if > > it generates a "foo.h" with only > > > > #include "lib.h" > > We currently do not put the #include "lib.h" into the .chs module since > we already have to pre-process the .chs module with cpp. That's why we > specify it on the c2hs and ghc command line. Why do you do that? (The solution that I propose above will still work when you specify the .h on the c2hs command line.) Manuel |