MyNixOS website logo
Description

Dynamically load Haskell libraries.

Load and execute functions from Haskell dynamic libraries without being restricted to a single RTS/GHC version.

See README.md below for more information.

Haskell dynamic library loader

Load and execute functions from Haskell dynamic libraries without being restricted to a single RTS/GHC version.

This package consists of the initial loader executable rts-loader written in plain C and a Haskell library providing a convinient interface to it. The initial loader executable is implemented in C since writing it in Haskell would mean linking against the RTS which means we can't load libraries compiled with another version of GHC.

In order to get Cabal to compile a pure C executable while not linking against the RTS still some hacks in Setup.hs are employed to call gcc directly so beware.

Usage

The example below will locate the package "foopkg" in the package databases configured for the Cabal project in the current directory and execute the function "main" from the module "Lib". The package can be found in tests/lib in the rts-loader source tree.

To run the example, execute the following commands in the rts-loader source tree.

$ cabal install --only-dependencies && cabal configure && cabal build && cabal install tests/lib
$ rts_loader_libexecdir=dist/build/rts-loader dist/build/rts-loader-example/rts-loader-example

rts_loader_libexecdir=... is only required if you didn't install rts-loader into a sandbox or elsewhere already.

import System.Loader.RTS
import System.Process

import Distribution.Verbosity
import Distribution.Package
import Distribution.Simple.Configure

import Distribution.Simple.LocalBuildInfo
import Distribution.Simple.Configure (getPersistBuildConfig)

main :: IO ()
main = do
  lbi <- getPersistBuildConfig "dist"

  let verbosity = normal
      comp = compiler lbi
      pkg_db_stack = withPackageDB lbi
      prog_conf = withPrograms lbi

  pidx <- getInstalledPackages verbosity comp pkg_db_stack prog_conf

  loader_exe <- loaderExecutablePath
  loader_args <- loaderInvocation (SymbolIdentifier "Lib" "main") [] <$>
    (resolveLibraryInfo comp [WayDyn] $
      lookupLibraryInfo pidx $
        PQName $ PackageName "foopkg")

  print =<< rawSystem loader_exe loader_args

We use information written by cabal configure (see getPersistBuildConfig) for convinience only this is not actually required to use the loader.

Metadata

Version

0.0.0.3

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows