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 (nullPtr, FunPtr, nullFunPtr,castPtrToFunPtr)
11import System.Posix.DynamicLinker
13import System.Win32.DLL
14import System.Win32.Types
18hostDynamicLibExt :: String
20hostDynamicLibExt = "so"
22hostDynamicLibExt = "dylib"
24hostDynamicLibExt = "dll"
26hostDynamicLibExt = "so"
28hostDynamicLibExt = error $ unwords
29 [ "Undefined file extension for dynamic libraries"
30 , "in Idris' Util.DynamicLinker."
34data ForeignFun = forall a. Fun { fun_name :: String
35 , fun_handle :: FunPtr a
38data DynamicLib = Lib { lib_name :: String
42instance Eq DynamicLib where
43 (Lib a _) == (Lib b _) = a == b
46tryLoadLib :: String -> IO (Maybe DynamicLib)
47tryLoadLib lib = do exactName <- doesFileExist lib
48 let filename = if exactName then lib else lib ++ "." ++ hostDynamicLibExt
49 handle <- dlopen filename [RTLD_NOW, RTLD_GLOBAL]
50 if undl handle == nullPtr
52 else return . Just $ Lib lib handle
55tryLoadFn :: String -> DynamicLib -> IO (Maybe ForeignFun)
56tryLoadFn fn (Lib _ h) = do cFn <- dlsym h fn
59 else return . Just $ Fun fn cFn
61tryLoadLib :: String -> IO (Maybe DynamicLib)
62tryLoadLib lib = do exactName <- doesFileExist lib
63 let filename = if exactName then lib else lib ++ "." ++ hostDynamicLibExt
64 handle <- loadLibrary filename
67 else return . Just $ Lib lib handle
69tryLoadFn :: String -> DynamicLib -> IO (Maybe ForeignFun)
70tryLoadFn fn (Lib _ h) = do cFn <- getProcAddress h fn
73 else return . Just $ Fun fn (castPtrToFunPtr cFn)
76-- no libffi, just add stubbs.
78data DynamicLib = Lib { lib_name :: String
83tryLoadLib :: String -> IO (Maybe DynamicLib)
84tryLoadLib 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