Re: [Haskell-cafe] cabal: problem building ffi shared library and significance of __stginit

2010-05-19 Thread Duncan Coutts
On Tue, 2010-05-18 at 17:31 -0400, Anthony LODI wrote:
> Hello,
> 
> I'm trying to build some haskell code as a .so/.dll so that it can
> ultimately be used by msvc.  I have it working when I compile by hand
> (listed below) but I can't get the exact same thing built/linked with
> cabal.  On linux everything builds fine, but when I try to link the
> resulting .so file, I get an error about a missing
> '__stginit_CInterface' reference.  Indeed I couldn't find that name in
> any of the cabal-generated .dyn_o files.  I checked the output of
> 'cabal build -v' and it seems to be executing about the same thing
> that I'm executing manually so I'm not sure what could be going wrong.
>  On windows cabal won't even configure since '--enable-shared' seems
> to imply '-dynamic' (right?), and that's not currently supported.
> 
> Also, when I remove the line 'hs_add_root(__stginit_CInterface);', and
> the corresponding forward declaration, the program runs fine!  Does
> ghc no longer need this call or are my toy programs just being lucky
> sofar?

For reference for other people, Anthony and I worked this out today.

full example:
http://pastebin.com/aLdyFMPg

The difference between doing it manually and building a library via
Cabal is the package name.

When building directly with ghc, the default package name is "" aka the
main package. When building a ghc/Haskell package, the package name gets
set (ghc -package-name test-0.0). This package name gets encoded into
the symbol names. So we get:

   __stginit_testzm0zi0_CInterface
vs __stginit_CInterface

(testzm0zi0 is the Z-encoding of test-0.0)

What is bad here is that the __stginit stuff is even necessary. Anthony
is going to file a ghc ticket and/or complain on the ghc users list,
citing this example.

Duncan

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] cabal: problem building ffi shared library and significance of __stginit

2010-05-18 Thread Anthony LODI
Hello,

I'm trying to build some haskell code as a .so/.dll so that it can
ultimately be used by msvc.  I have it working when I compile by hand
(listed below) but I can't get the exact same thing built/linked with
cabal.  On linux everything builds fine, but when I try to link the
resulting .so file, I get an error about a missing
'__stginit_CInterface' reference.  Indeed I couldn't find that name in
any of the cabal-generated .dyn_o files.  I checked the output of
'cabal build -v' and it seems to be executing about the same thing
that I'm executing manually so I'm not sure what could be going wrong.
 On windows cabal won't even configure since '--enable-shared' seems
to imply '-dynamic' (right?), and that's not currently supported.

Also, when I remove the line 'hs_add_root(__stginit_CInterface);', and
the corresponding forward declaration, the program runs fine!  Does
ghc no longer need this call or are my toy programs just being lucky
sofar?

Cheers,

- Anthony LODI


=

(archlinux32 -- 2.6.32)

> hsc2hs CInterface.hsc

> ghc --make CInterface

> cat dll_init.h

#pragma once

extern void __stginit_CInterface(void);

extern void sp_init(int*, char***);
extern void sp_exit(void);

> cat dll_init.c

#include "HsFFI.h"
#include "dll_init.h"

void sp_init(int* p_argc, char*** p_argv) {
  hs_init(p_argc, p_argv);
  hs_add_root(__stginit_CInterface);
}

void sp_exit(void) {
  hs_exit();
}

> ghc -c dll_init.c

> ghc -o libfoo.so -shared -dynamic CInterface.o CInterface_stub.o dll_init.o

> ghc -c ctest.c

> ghc -threaded -o ctest -L. -lfoo ctest.o -optl-Wl,-rpath,'$ORIGIN'

> ./ctest

=

(winxp is similar except without '-threaded' or '-dynamic')

=

> cat test.cabal

...

library
  build-depends: base >=4 && <5
  exposed-modules: CInterface
  c-source: dll_init.c
  ghc_options: -threaded

> cabal clean
> cabal configure --enable-shared
> cabal build
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe