1-- | Platform-specific dynamic linking support. Add new platforms to this file
2-- through conditional compilation.
3{-# LANGUAGE ExistentialQuantification, CPP #-}
4module Util.DynamicLinker where
8import Foreign.Ptr (Ptr(), nullPtr, FunPtr, nullFunPtr, castPtrToFunPtr)
11import System.Posix.DynamicLinker
13import qualified Control.Exception as Exception (catch, IOException)
14import System.Win32.DLL
15import System.Win32.Types
19hostDynamicLibExt :: String
21hostDynamicLibExt = "so"
23hostDynamicLibExt = "dylib"
25hostDynamicLibExt = "dll"
27hostDynamicLibExt = "so"
29hostDynamicLibExt = "so"
31hostDynamicLibExt = error $ unwords
32 [ "Undefined file extension for dynamic libraries"
33 , "in Idris' Util.DynamicLinker."
37data ForeignFun = forall a. Fun { fun_name :: String
38 , fun_handle :: FunPtr a
41data DynamicLib = Lib { lib_name :: String
45instance Eq DynamicLib where
46 (Lib a _) == (Lib b _) = a == b
49tryLoadLib :: String -> IO (Maybe DynamicLib)
50tryLoadLib lib = do exactName <- doesFileExist lib
51 let filename = if exactName then lib else lib ++ "." ++ hostDynamicLibExt
52 handle <- dlopen filename [RTLD_NOW, RTLD_GLOBAL]
53 if undl handle == nullPtr
55 else return . Just $ Lib lib handle
58tryLoadFn :: String -> DynamicLib -> IO (Maybe ForeignFun)
59tryLoadFn fn (Lib _ h) = do cFn <- dlsym h fn
62 else return . Just $ Fun fn cFn
64tryLoadLib :: String -> IO (Maybe DynamicLib)
65tryLoadLib lib = do exactName <- doesFileExist lib
66 let filename = if exactName then lib else lib ++ "." ++ hostDynamicLibExt
67 handle <- Exception.catch (loadLibrary filename) nullPtrOnException
70 else return . Just $ Lib lib handle
71 where nullPtrOnException :: Exception.IOException -> IO DL
72 nullPtrOnException e = return nullPtr
73 -- `show e` will however give broken error message
75tryLoadFn :: String -> DynamicLib -> IO (Maybe ForeignFun)
76tryLoadFn fn (Lib _ h) = do cFn <- getProcAddress h fn
79 else return . Just $ Fun fn (castPtrToFunPtr cFn)
82-- no libffi, just add stubbs.
84data DynamicLib = Lib { lib_name :: String
89tryLoadLib :: String -> IO (Maybe DynamicLib)
90tryLoadLib lib = do putStrLn $ "WARNING: Cannot load '" ++ lib ++ "' at compile time because Idris was compiled without libffi support."
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list