on ff0fbfa help sign in
Idris-dev/src/Util/DynamicLinker.hs
37 lines 1.3 kB history raw
1-- | Platform-specific dynamic linking support. Add new platforms to this file
2-- through conditional compilation.
3{-# LANGUAGE ExistentialQuantification, CPP #-}
4module Util.DynamicLinker where
6import Foreign.LibFFI
7import Foreign.Ptr (nullPtr, FunPtr, nullFunPtr)
8import System.Posix.DynamicLinker
10hostDynamicLibExt :: String
11#ifdef LINUX
12hostDynamicLibExt = "so"
13#elif MACOSX
14hostDynamicLibExt = "dylib"
15#elif WINDOWS
16hostDynamicLibExt = "dll"
17#endif
19data DynamicLib = Lib { lib_name :: String
20 , lib_handle :: DL
21 }
23tryLoadLib :: String -> IO (Maybe DynamicLib)
24tryLoadLib lib = do handle <- dlopen (lib ++ "." ++ hostDynamicLibExt) [RTLD_NOW]
25 if undl handle == nullPtr
26 then return Nothing
27 else return . Just $ Lib lib handle
29data ForeignFun = forall a. Fun { fun_name :: String
30 , fun_handle :: FunPtr a
31 }
33tryLoadFn :: String -> DynamicLib -> IO (Maybe ForeignFun)
34tryLoadFn fn (Lib _ h) = do cFn <- dlsym h fn
35 if cFn == nullFunPtr
36 then return Nothing
37 else return . Just $ Fun fn cFn