on master help sign in
Merge branch 'fixWin' of https://github.com/melted/Idris-dev into melted-fixWin
0881704Edwin Brady
idris.cabal+4−1
@@ -111,7 +111,7 @@ Executable idris
111 haskeline>=0.7, split, directory,
111 haskeline>=0.7, split, directory,
112 containers, process, transformers, filepath,
112 containers, process, transformers, filepath,
113 directory, binary, bytestring, text, pretty,
113 directory, binary, bytestring, text, pretty,
114− language-java>=0.2.2, libffi, unix
114+ language-java>=0.2.2, libffi
115
115
116 Extensions: MultiParamTypeClasses, FunctionalDependencies,
116 Extensions: MultiParamTypeClasses, FunctionalDependencies,
117 FlexibleInstances, TemplateHaskell
117 FlexibleInstances, TemplateHaskell
@@ -119,7 +119,10 @@ Executable idris
119 ghc-options: -rtsopts
119 ghc-options: -rtsopts
120 if os(linux)
120 if os(linux)
121 cpp-options: -DLINUX
121 cpp-options: -DLINUX
122+ build-depends: unix
122 if os(darwin)
123 if os(darwin)
123 cpp-options: -DMACOSX
124 cpp-options: -DMACOSX
125+ build-depends: unix
124 if os(windows)
126 if os(windows)
125 cpp-options: -DWINDOWS
127 cpp-options: -DWINDOWS
128+ build-depends: Win32
src/Util/DynamicLinker.hs+25−4
@@ -4,8 +4,14 @@
4module Util.DynamicLinker where
4module Util.DynamicLinker where
5
5
6import Foreign.LibFFI
6import Foreign.LibFFI
7−import Foreign.Ptr (nullPtr, FunPtr, nullFunPtr)
7+import Foreign.Ptr (nullPtr, FunPtr, nullFunPtr,castPtrToFunPtr)
8+#ifndef WINDOWS
8import System.Posix.DynamicLinker
9import System.Posix.DynamicLinker
10+#else
11+import System.Win32.DLL
12+import System.Win32.Types
13+type DL = HMODULE
14+#endif
9
15
10hostDynamicLibExt :: String
16hostDynamicLibExt :: String
11#ifdef LINUX
17#ifdef LINUX
@@ -16,22 +22,37 @@ hostDynamicLibExt = "dylib"
16hostDynamicLibExt = "dll"
22hostDynamicLibExt = "dll"
17#endif
23#endif
18
24
25+data ForeignFun = forall a. Fun { fun_name :: String
26+ , fun_handle :: FunPtr a
27+ }
28+
19data DynamicLib = Lib { lib_name :: String
29data DynamicLib = Lib { lib_name :: String
20 , lib_handle :: DL
30 , lib_handle :: DL
21 }
31 }
22
32
33+#ifndef WINDOWS
23tryLoadLib :: String -> IO (Maybe DynamicLib)
34tryLoadLib :: String -> IO (Maybe DynamicLib)
24tryLoadLib lib = do handle <- dlopen (lib ++ "." ++ hostDynamicLibExt) [RTLD_NOW]
35tryLoadLib lib = do handle <- dlopen (lib ++ "." ++ hostDynamicLibExt) [RTLD_NOW]
25 if undl handle == nullPtr
36 if undl handle == nullPtr
26 then return Nothing
37 then return Nothing
27 else return . Just $ Lib lib handle
38 else return . Just $ Lib lib handle
28
39
29−data ForeignFun = forall a. Fun { fun_name :: String
30− , fun_handle :: FunPtr a
31− }
32
40
33tryLoadFn :: String -> DynamicLib -> IO (Maybe ForeignFun)
41tryLoadFn :: String -> DynamicLib -> IO (Maybe ForeignFun)
34tryLoadFn fn (Lib _ h) = do cFn <- dlsym h fn
42tryLoadFn fn (Lib _ h) = do cFn <- dlsym h fn
35 if cFn == nullFunPtr
43 if cFn == nullFunPtr
36 then return Nothing
44 then return Nothing
37 else return . Just $ Fun fn cFn
45 else return . Just $ Fun fn cFn
46+#else
47+tryLoadLib :: String -> IO (Maybe DynamicLib)
48+tryLoadLib lib = do handle <- loadLibrary (lib ++ "." ++ hostDynamicLibExt)
49+ if handle == nullPtr
50+ then return Nothing
51+ else return . Just $ Lib lib handle
52+
53+tryLoadFn :: String -> DynamicLib -> IO (Maybe ForeignFun)
54+tryLoadFn fn (Lib _ h) = do cFn <- getProcAddress h fn
55+ if cFn == nullPtr
56+ then return Nothing
57+ else return . Just $ Fun fn (castPtrToFunPtr cFn)
58+#endif