on master help sign in
Make dynamic library extension platform-dependent
90f661bDavid Christiansen
idris.cabal+6
@@ -114,3 +114,9 @@ Executable idris
114 FlexibleInstances, TemplateHaskell
114 FlexibleInstances, TemplateHaskell
115 ghc-prof-options: -auto-all -caf-all
115 ghc-prof-options: -auto-all -caf-all
116 ghc-options: -rtsopts
116 ghc-options: -rtsopts
117+ if os(linux)
118+ cpp-options: -DLINUX
119+ if os(darwin)
120+ cpp-options: -DMACOSX
121+ if os(windows)
122+ cpp-options: -DWINDOWS
src/Util/DynamicLinker.hs+11−2
@@ -1,18 +1,27 @@
1-- | Platform-specific dynamic linking support. Add new platforms to this file
1-- | Platform-specific dynamic linking support. Add new platforms to this file
2-- through conditional compilation.
2-- through conditional compilation.
3−{-# LANGUAGE ExistentialQuantification #-}
3+{-# LANGUAGE ExistentialQuantification, CPP #-}
4module Util.DynamicLinker where
4module Util.DynamicLinker where
5
5
6import Foreign.LibFFI
6import Foreign.LibFFI
7import Foreign.Ptr (nullPtr, FunPtr, nullFunPtr)
7import Foreign.Ptr (nullPtr, FunPtr, nullFunPtr)
8import System.Posix.DynamicLinker
8import System.Posix.DynamicLinker
9
9
10+hostDynamicLibExt :: String
11+#ifdef LINUX
12+hostDynamicLibExt = "so"
13+#elif MACOSX
14+hostDynamicLibExt = "dylib"
15+#elif WINDOWS
16+hostDynamicLibExt = "dll"
17+#endif
18+
10data DynamicLib = Lib { lib_name :: String
19data DynamicLib = Lib { lib_name :: String
11 , lib_handle :: DL
20 , lib_handle :: DL
12 }
21 }
13
22
14tryLoadLib :: String -> IO (Maybe DynamicLib)
23tryLoadLib :: String -> IO (Maybe DynamicLib)
15−tryLoadLib lib = do handle <- dlopen lib [RTLD_NOW]
24+tryLoadLib lib = do handle <- dlopen (lib ++ "." ++ hostDynamicLibExt) [RTLD_NOW]
16 if undl handle == nullPtr
25 if undl handle == nullPtr
17 then return Nothing
26 then return Nothing
18 else return . Just $ Lib lib handle
27 else return . Just $ Lib lib handle
test/test022/test022.idr+1−1
@@ -1,6 +1,6 @@
1module Main
1module Main
2
2
3−%dynamic "libm.so"
3+%dynamic "libm"
4
4
5x : Float
5x : Float
6x = unsafePerformIO (mkForeign (FFun "sin" [FFloat] FFloat) 1.6)
6x = unsafePerformIO (mkForeign (FFun "sin" [FFloat] FFloat) 1.6)