4 {% if supportsNoRebindableSyntax
%}
5 {-# LANGUAGE NoRebindableSyntax #-}
8 {-# LANGUAGE ForeignFunctionInterface #-}
11 #if __GLASGOW_HASKELL__
>= 810
12 {-# OPTIONS_GHC -Wno-prepositive-qualified-module #-}
15 {-# OPTIONS_GHC -fno-warn-missing-import-lists #-}
16 {-# OPTIONS_GHC -w #-}
17 module Paths_
{{ manglePkgName packageName
}} (
19 getBinDir
, getLibDir
, getDynLibDir
, getDataDir
, getLibexecDir
,
20 getDataFileName
, getSysconfDir
28 import qualified Control
.Exception
as Exception
29 import qualified Data
.List
as List
30 import Data
.Version
(Version
(..))
31 import System
.Environment
(getEnv)
35 import System
.Environment
(getExecutablePath
)
39 #if defined
(VERSION_base
)
41 #if MIN_VERSION_base
(4,0,0)
42 catchIO
:: IO a
-> (Exception
.IOException
-> IO a
) -> IO a
44 catchIO
:: IO a
-> (Exception
.Exception
-> IO a
) -> IO a
48 catchIO
:: IO a
-> (Exception
.IOException
-> IO a
) -> IO a
50 catchIO
= Exception
.catch
52 catchIO
:: IO a
-> (Exception
.IOException
-> IO a
) -> IO a
53 catchIO
= Exception
.catch
57 version
= Version
{{ versionDigits
}} []
59 getDataFileName
:: FilePath -> IO FilePath
60 getDataFileName name
= do
62 return (dir `joinFileName` name
)
64 getBinDir
, getLibDir
, getDynLibDir
, getDataDir
, getLibexecDir
, getSysconfDir
:: IO FilePath
66 {% defblock function_defs
%}
67 minusFileName
:: FilePath -> String -> FilePath
68 minusFileName dir
"" = dir
69 minusFileName dir
"." = dir
70 minusFileName dir suffix
=
71 minusFileName
(fst (splitFileName dir
)) (fst (splitFileName suffix
))
73 splitFileName
:: FilePath -> (String, String)
74 splitFileName p
= (reverse (path2
++drive
), reverse fname
)
76 (path
,drive
) = case p
of
77 (c
:':':p
') -> (reverse p
',[':',c
])
79 (fname
,path1
) = break isPathSeparator path
82 [_
] -> path1
-- don't remove the trailing slash if
83 -- there is only one character
84 (c
:path
') | isPathSeparator c
-> path
'
89 {# ######################################################################### #}
93 getPrefixDirReloc
:: FilePath -> IO FilePath
94 getPrefixDirReloc dirRel
= do
95 exePath
<- getExecutablePath
96 let (dir
,_
) = splitFileName exePath
97 return ((dir `minusFileName`
{{ bindir
}}) `joinFileName` dirRel
)
99 getBinDir
= catchIO
(getEnv "{{ manglePkgName packageName }}_bindir") (\_
-> getPrefixDirReloc
$ {{ bindir
}})
100 getLibDir
= catchIO
(getEnv "{{ manglePkgName packageName }}_libdir") (\_
-> getPrefixDirReloc
$ {{ libdir
}})
101 getDynLibDir
= catchIO
(getEnv "{{ manglePkgName packageName }}_dynlibdir") (\_
-> getPrefixDirReloc
$ {{ dynlibdir
}})
102 getDataDir
= catchIO
(getEnv "{{ manglePkgName packageName }}_datadir") (\_
-> getPrefixDirReloc
$ {{ datadir
}})
103 getLibexecDir
= catchIO
(getEnv "{{ manglePkgName packageName }}_libexecdir") (\_
-> getPrefixDirReloc
$ {{ libexecdir
}})
104 getSysconfDir
= catchIO
(getEnv "{{ manglePkgName packageName }}_sysconfdir") (\_
-> getPrefixDirReloc
$ {{ sysconfdir
}})
106 {% useblock function_defs
%}
110 bindir
, libdir
, dynlibdir
, datadir
, libexecdir
, sysconfdir
:: FilePath
111 bindir
= {{ bindir
}}
112 libdir
= {{ libdir
}}
113 dynlibdir
= {{ dynlibdir
}}
114 datadir
= {{ datadir
}}
115 libexecdir
= {{ libexecdir
}}
116 sysconfdir
= {{ sysconfdir
}}
118 getBinDir
= catchIO
(getEnv "{{ manglePkgName packageName }}_bindir") (\_
-> return bindir
)
119 getLibDir
= catchIO
(getEnv "{{ manglePkgName packageName }}_libdir") (\_
-> return libdir
)
120 getDynLibDir
= catchIO
(getEnv "{{ manglePkgName packageName }}_dynlibdir") (\_
-> return dynlibdir
)
121 getDataDir
= catchIO
(getEnv "{{ manglePkgName packageName }}_datadir") (\_
-> return datadir
)
122 getLibexecDir
= catchIO
(getEnv "{{ manglePkgName packageName }}_libexecdir") (\_
-> return libexecdir
)
123 getSysconfDir
= catchIO
(getEnv "{{ manglePkgName packageName }}_sysconfdir") (\_
-> return sysconfdir
)
128 prefix
= {{ prefix
}}
130 getBinDir
= getPrefixDirRel
$ {{ bindir
}}
131 getLibDir
= {{ libdir
}}
132 getDynLibDir
= {{ dynlibdir
}}
133 getDataDir
= catchIO
(getEnv "{{ manglePkgName packageName }}_datadir") (\_
-> {{ datadir
}})
134 getLibexecDir
= {{ libexecdir
}}
135 getSysconfDir
= {{ sysconfdir
}}
137 getPrefixDirRel
:: FilePath -> IO FilePath
138 getPrefixDirRel dirRel
= try_size
2048 -- plenty, PATH_MAX is 512 under Win32.
140 try_size size
= allocaArray
(fromIntegral size
) $ \buf
-> do
141 ret
<- c_GetModuleFileName nullPtr buf size
143 0 -> return (prefix `joinFileName` dirRel
)
145 exePath
<- peekCWString buf
146 let (bindir
,_
) = splitFileName exePath
147 return ((bindir `minusFileName`
{{ bindir
}}) `joinFileName` dirRel
)
148 |
otherwise -> try_size
(size
* 2)
150 {% useblock function_defs
%}
153 foreign import stdcall unsafe
"windows.h GetModuleFileNameW"
154 c_GetModuleFileName
:: Ptr
() -> CWString
-> Int32
-> IO Int32
156 foreign import ccall unsafe
"windows.h GetModuleFileNameW"
157 c_GetModuleFileName
:: Ptr
() -> CWString
-> Int32
-> IO Int32
159 -- win32 supported only with I386, X86_64
160 c_GetModuleFileName
:: Ptr
() -> CWString
-> Int32
-> IO Int32
161 c_GetModuleFileName
= _
166 notRelocAbsoluteOrWindows
:: ()
167 notRelocAbsoluteOrWindows
= _
172 {# ######################################################################### #}
174 joinFileName
:: String -> String -> FilePath
175 joinFileName
"" fname
= fname
176 joinFileName
"." fname
= fname
177 joinFileName dir
"" = dir
178 joinFileName dir fname
179 | isPathSeparator
(List
.last dir
) = dir
++ fname
180 |
otherwise = dir
++ pathSeparator
: fname
182 pathSeparator
:: Char
189 isPathSeparator
:: Char -> Bool
191 isPathSeparator c
= c
== '/' || c
== '\\'
193 isPathSeparator c
= c
== '/'