Automatically initialize the GHC RTS by calling hs_init
(and deinitialize it by calling hs_exit
) when your library loads, using GCC constructors and destructors.
auto-init
is not yet published on Hackage, but when it is, it will be as simple as adding auto-init
to the build-depends
field of your foreign-library
stanza.
Since there is no portable way to get the program's arguments list in a constructor, the RTS is simply initialized with a nulled out argc and argv. This means that the results of getProgName
and getArgs
are likely to be "<unknown>"
and []
, but importantly means that you must pass your RTS options in the GHCRTS
environmental variable rather than in the args, which truthfully suits options for shared libraries much better. If you feel it's important to set the RTS options to something specific for your project, you may copy cbits/auto_init.c
into your own project, modify it, and add it to extra-source-files
and c-sources
. Note that hs_init
copies argc and argv, so they can be safely allocated on the stack and thus destroyed when init
returns.
Though it should be noted that if hs_init
has already been called by the running application that even if you set your own RTS opts via vendoring and modifying cbits/auto_init.c
as described above, the first invocation's options will supersede yours. Subsequent inits are simply ignored.