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 = error $ unwords
27 [ "Undefined file extension for dynamic libraries"
28 , "in Idris' Util.DynamicLinker."
32data ForeignFun = forall a. Fun { fun_name :: String
33 , fun_handle :: FunPtr a
36data DynamicLib = Lib { lib_name :: String
40instance Eq DynamicLib where
41 (Lib a _) == (Lib b _) = a == b
44tryLoadLib :: String -> IO (Maybe DynamicLib)
45tryLoadLib lib = do exactName <- doesFileExist lib
46 let filename = if exactName then lib else lib ++ "." ++ hostDynamicLibExt
47 handle <- dlopen filename [RTLD_NOW, RTLD_GLOBAL]
48 if undl handle == nullPtr
50 else return . Just $ Lib lib handle
53tryLoadFn :: String -> DynamicLib -> IO (Maybe ForeignFun)
54tryLoadFn fn (Lib _ h) = do cFn <- dlsym h fn
57 else return . Just $ Fun fn cFn
59tryLoadLib :: String -> IO (Maybe DynamicLib)
60tryLoadLib lib = do exactName <- doesFileExist lib
61 let filename = if exactName then lib else lib ++ "." ++ hostDynamicLibExt
62 handle <- loadLibrary filename
65 else return . Just $ Lib lib handle
67tryLoadFn :: String -> DynamicLib -> IO (Maybe ForeignFun)
68tryLoadFn fn (Lib _ h) = do cFn <- getProcAddress h fn
71 else return . Just $ Fun fn (castPtrToFunPtr cFn)
74-- no libffi, just add stubbs.
76data DynamicLib = Lib { lib_name :: String
81tryLoadLib :: String -> IO (Maybe DynamicLib)
82tryLoadLib 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