|
| 1 | +{-# LANGUAGE DeriveGeneric #-} |
| 2 | +module Distribution.Simple.Build.PathsModule.Z (render, Z(..)) where |
| 3 | +import Distribution.ZinzaPrelude |
| 4 | +data Z |
| 5 | + = Z {zPackageName :: PackageName, |
| 6 | + zVersionDigits :: String, |
| 7 | + zSupportsCpp :: Bool, |
| 8 | + zSupportsNoRebindableSyntax :: Bool, |
| 9 | + zAbsolute :: Bool, |
| 10 | + zRelocatable :: Bool, |
| 11 | + zIsWindows :: Bool, |
| 12 | + zIsI386 :: Bool, |
| 13 | + zIsX8664 :: Bool, |
| 14 | + zBindir :: FilePath, |
| 15 | + zLibdir :: FilePath, |
| 16 | + zDynlibdir :: FilePath, |
| 17 | + zDatadir :: FilePath, |
| 18 | + zLibexecdir :: FilePath, |
| 19 | + zSysconfdir :: FilePath, |
| 20 | + zNot :: (Bool -> Bool), |
| 21 | + zManglePkgName :: (PackageName -> String)} |
| 22 | + deriving Generic |
| 23 | +render :: Z -> String |
| 24 | +render z_root = execWriter $ do |
| 25 | + if (zSupportsCpp z_root) |
| 26 | + then do |
| 27 | + tell "{-# LANGUAGE CPP #-}\n" |
| 28 | + return () |
| 29 | + else do |
| 30 | + return () |
| 31 | + if (zSupportsNoRebindableSyntax z_root) |
| 32 | + then do |
| 33 | + tell "{-# LANGUAGE NoRebindableSyntax #-}\n" |
| 34 | + return () |
| 35 | + else do |
| 36 | + return () |
| 37 | + if (zNot z_root (zAbsolute z_root)) |
| 38 | + then do |
| 39 | + tell "{-# LANGUAGE ForeignFunctionInterface #-}\n" |
| 40 | + return () |
| 41 | + else do |
| 42 | + return () |
| 43 | + tell "{-# OPTIONS_GHC -fno-warn-missing-import-lists #-}\n" |
| 44 | + tell "module Paths_" |
| 45 | + tell (zManglePkgName z_root (zPackageName z_root)) |
| 46 | + tell " (\n" |
| 47 | + tell " version,\n" |
| 48 | + tell " getBinDir, getLibDir, getDynLibDir, getDataDir, getLibexecDir,\n" |
| 49 | + tell " getDataFileName, getSysconfDir\n" |
| 50 | + tell " ) where\n" |
| 51 | + tell "\n" |
| 52 | + if (zNot z_root (zAbsolute z_root)) |
| 53 | + then do |
| 54 | + tell "import Foreign\n" |
| 55 | + tell "import Foreign.C\n" |
| 56 | + return () |
| 57 | + else do |
| 58 | + return () |
| 59 | + tell "\n" |
| 60 | + tell "import qualified Control.Exception as Exception\n" |
| 61 | + tell "import Data.Version (Version(..))\n" |
| 62 | + tell "import System.Environment (getEnv)\n" |
| 63 | + tell "import Prelude\n" |
| 64 | + tell "\n" |
| 65 | + if (zRelocatable z_root) |
| 66 | + then do |
| 67 | + tell "import System.Environment (getExecutablePath)\n" |
| 68 | + return () |
| 69 | + else do |
| 70 | + return () |
| 71 | + tell "\n" |
| 72 | + if (zSupportsCpp z_root) |
| 73 | + then do |
| 74 | + tell "#if defined(VERSION_base)\n" |
| 75 | + tell "\n" |
| 76 | + tell "#if MIN_VERSION_base(4,0,0)\n" |
| 77 | + tell "catchIO :: IO a -> (Exception.IOException -> IO a) -> IO a\n" |
| 78 | + tell "#else\n" |
| 79 | + tell "catchIO :: IO a -> (Exception.Exception -> IO a) -> IO a\n" |
| 80 | + tell "#endif\n" |
| 81 | + tell "\n" |
| 82 | + tell "#else\n" |
| 83 | + tell "catchIO :: IO a -> (Exception.IOException -> IO a) -> IO a\n" |
| 84 | + tell "#endif\n" |
| 85 | + tell "catchIO = Exception.catch\n" |
| 86 | + return () |
| 87 | + else do |
| 88 | + tell "catchIO :: IO a -> (Exception.IOException -> IO a) -> IO a\n" |
| 89 | + tell "catchIO = Exception.catch\n" |
| 90 | + return () |
| 91 | + tell "\n" |
| 92 | + tell "version :: Version\n" |
| 93 | + tell "version = Version " |
| 94 | + tell (zVersionDigits z_root) |
| 95 | + tell " []\n" |
| 96 | + tell "\n" |
| 97 | + tell "bindir, libdir, dynlibdir, datadir, libexecdir, sysconfdir :: FilePath\n" |
| 98 | + tell "\n" |
| 99 | + tell "\n" |
| 100 | + if (zRelocatable z_root) |
| 101 | + then do |
| 102 | + tell "\n" |
| 103 | + tell "\n" |
| 104 | + return () |
| 105 | + else do |
| 106 | + if (zAbsolute z_root) |
| 107 | + then do |
| 108 | + tell "bindir = " |
| 109 | + tell (zBindir z_root) |
| 110 | + tell "\n" |
| 111 | + tell "libdir = " |
| 112 | + tell (zLibdir z_root) |
| 113 | + tell "\n" |
| 114 | + tell "dynlibdir = " |
| 115 | + tell (zDynlibdir z_root) |
| 116 | + tell "\n" |
| 117 | + tell "datadir = " |
| 118 | + tell (zDatadir z_root) |
| 119 | + tell "\n" |
| 120 | + tell "libexecdir = " |
| 121 | + tell (zLibexecdir z_root) |
| 122 | + tell "\n" |
| 123 | + tell "sysconfdir = " |
| 124 | + tell (zSysconfdir z_root) |
| 125 | + tell "\n" |
| 126 | + tell "\n" |
| 127 | + tell "getBinDir, getLibDir, getDynLibDir, getDataDir, getLibexecDir, getSysconfDir :: IO FilePath\n" |
| 128 | + tell "getBinDir = catchIO (getEnv \"" |
| 129 | + tell (zManglePkgName z_root (zPackageName z_root)) |
| 130 | + tell "_bindir\") (\\_ -> return bindir)\n" |
| 131 | + tell "getLibDir = catchIO (getEnv \"" |
| 132 | + tell (zManglePkgName z_root (zPackageName z_root)) |
| 133 | + tell "_libdir\") (\\_ -> return libdir)\n" |
| 134 | + tell "getDynLibDir = catchIO (getEnv \"" |
| 135 | + tell (zManglePkgName z_root (zPackageName z_root)) |
| 136 | + tell "_dynlibdir\") (\\_ -> return dynlibdir)\n" |
| 137 | + tell "getDataDir = catchIO (getEnv \"" |
| 138 | + tell (zManglePkgName z_root (zPackageName z_root)) |
| 139 | + tell "_datadir\") (\\_ -> return datadir)\n" |
| 140 | + tell "getLibexecDir = catchIO (getEnv \"" |
| 141 | + tell (zManglePkgName z_root (zPackageName z_root)) |
| 142 | + tell "_libexecdir\") (\\_ -> return libexecdir)\n" |
| 143 | + tell "getSysconfDir = catchIO (getEnv \"" |
| 144 | + tell (zManglePkgName z_root (zPackageName z_root)) |
| 145 | + tell "_sysconfdir\") (\\_ -> return sysconfdir)\n" |
| 146 | + tell "\n" |
| 147 | + tell "getDataFileName :: FilePath -> IO FilePath\n" |
| 148 | + tell "getDataFileName name = do\n" |
| 149 | + tell " dir <- getDataDir\n" |
| 150 | + tell " return (dir ++ \"/\" ++ name)\n" |
| 151 | + return () |
| 152 | + else do |
| 153 | + tell "\n" |
| 154 | + tell "\n" |
| 155 | + return () |
| 156 | + return () |
| 157 | + tell "\n" |
| 158 | + if (zNot z_root (zAbsolute z_root)) |
| 159 | + then do |
| 160 | + tell "\n" |
| 161 | + tell "minusFileName :: FilePath -> String -> FilePath\n" |
| 162 | + tell "minusFileName dir \"\" = dir\n" |
| 163 | + tell "minusFileName dir \".\" = dir\n" |
| 164 | + tell "minusFileName dir suffix =\n" |
| 165 | + tell " minusFileName (fst (splitFileName dir)) (fst (splitFileName suffix))\n" |
| 166 | + tell "\n" |
| 167 | + tell "joinFileName :: String -> String -> FilePath\n" |
| 168 | + tell "joinFileName \"\" fname = fname\n" |
| 169 | + tell "joinFileName \".\" fname = fname\n" |
| 170 | + tell "joinFileName dir \"\" = dir\n" |
| 171 | + tell "joinFileName dir fname\n" |
| 172 | + tell " | isPathSeparator (last dir) = dir++fname\n" |
| 173 | + tell " | otherwise = dir++pathSeparator:fname\n" |
| 174 | + tell "\n" |
| 175 | + tell "splitFileName :: FilePath -> (String, String)\n" |
| 176 | + tell "splitFileName p = (reverse (path2++drive), reverse fname)\n" |
| 177 | + tell " where\n" |
| 178 | + tell " (path,drive) = case p of\n" |
| 179 | + tell " (c:':':p') -> (reverse p',[':',c])\n" |
| 180 | + tell " _ -> (reverse p ,\"\")\n" |
| 181 | + tell " (fname,path1) = break isPathSeparator path\n" |
| 182 | + tell " path2 = case path1 of\n" |
| 183 | + tell " [] -> \".\"\n" |
| 184 | + tell " [_] -> path1 -- don't remove the trailing slash if\n" |
| 185 | + tell " -- there is only one character\n" |
| 186 | + tell " (c:path') | isPathSeparator c -> path'\n" |
| 187 | + tell " _ -> path1\n" |
| 188 | + tell "\n" |
| 189 | + tell "pathSeparator :: Char\n" |
| 190 | + if (zIsWindows z_root) |
| 191 | + then do |
| 192 | + tell "pathSeparator = '\\\\\\\\'\n" |
| 193 | + return () |
| 194 | + else do |
| 195 | + tell "pathSeparator = '/'\n" |
| 196 | + return () |
| 197 | + tell "\n" |
| 198 | + tell "isPathSeparator :: Char -> Bool\n" |
| 199 | + if (zIsWindows z_root) |
| 200 | + then do |
| 201 | + tell "isPathSeparator c = c == '/' || c == '\\\\\\\\'\n" |
| 202 | + return () |
| 203 | + else do |
| 204 | + tell "isPathSeparator c = c == '/'\n" |
| 205 | + return () |
| 206 | + return () |
| 207 | + else do |
| 208 | + return () |
0 commit comments