{-# LANGUAGE CPP #-}
module GHC.BaseDir where
import Prelude
import Data.List (stripPrefix)
import Data.Maybe (listToMaybe)
import System.FilePath
#if defined(mingw32_HOST_OS)
import System.Environment (getExecutablePath)
#elif defined(darwin_HOST_OS) || defined(linux_HOST_OS) || defined(freebsd_HOST_OS) || defined(openbsd_HOST_OS) || defined(netbsd_HOST_OS)
import System.Environment (getExecutablePath)
#endif
expandTopDir :: FilePath -> String -> String
expandTopDir :: FilePath -> FilePath -> FilePath
expandTopDir = FilePath -> FilePath -> FilePath -> FilePath
expandPathVar FilePath
"topdir"
expandPathVar :: String -> FilePath -> String -> String
expandPathVar :: FilePath -> FilePath -> FilePath -> FilePath
expandPathVar FilePath
var FilePath
value FilePath
str
| Just FilePath
str' <- forall a. Eq a => [a] -> [a] -> Maybe [a]
stripPrefix (Char
'$'forall a. a -> [a] -> [a]
:FilePath
var) FilePath
str
, forall b a. b -> (a -> b) -> Maybe a -> b
maybe Bool
True Char -> Bool
isPathSeparator (forall a. [a] -> Maybe a
listToMaybe FilePath
str')
= FilePath
value forall a. [a] -> [a] -> [a]
++ FilePath -> FilePath -> FilePath -> FilePath
expandPathVar FilePath
var FilePath
value FilePath
str'
expandPathVar FilePath
var FilePath
value (Char
x:FilePath
xs) = Char
x forall a. a -> [a] -> [a]
: FilePath -> FilePath -> FilePath -> FilePath
expandPathVar FilePath
var FilePath
value FilePath
xs
expandPathVar FilePath
_ FilePath
_ [] = []
getBaseDir :: IO (Maybe String)
#if defined(mingw32_HOST_OS)
getBaseDir = Just . (\p -> p </> "lib") . rootDir <$> getExecutablePath
where
rootDir :: FilePath -> FilePath
rootDir = takeDirectory . takeDirectory . normalise
#elif defined(darwin_HOST_OS) || defined(linux_HOST_OS) || defined(freebsd_HOST_OS) || defined(openbsd_HOST_OS) || defined(netbsd_HOST_OS)
getBaseDir :: IO (Maybe FilePath)
getBaseDir = forall a. a -> Maybe a
Just forall b c a. (b -> c) -> (a -> b) -> a -> c
. (\FilePath
p -> FilePath
p FilePath -> FilePath -> FilePath
</> FilePath
"lib") forall b c a. (b -> c) -> (a -> b) -> a -> c
. FilePath -> FilePath
takeDirectory forall b c a. (b -> c) -> (a -> b) -> a -> c
. FilePath -> FilePath
takeDirectory forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IO FilePath
getExecutablePath
#else
getBaseDir = return Nothing
#endif