/usr/lib/hugs/packages/Cabal/Distribution/Program.hs is in libhugs-cabal-bundled 98.200609.21-5.3ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 | module Distribution.Program( Program(..)
, ProgramLocation(..)
, ProgramConfiguration(..)
, withProgramFlag
, programOptsFlag
, programOptsField
, defaultProgramConfiguration
, updateProgram
, userSpecifyPath
, userSpecifyArgs
, lookupProgram
, lookupPrograms
, rawSystemProgram
, rawSystemProgramConf
, simpleProgram
-- Programs
, ghcProgram
, ghcPkgProgram
, nhcProgram
, jhcProgram
, hugsProgram
, ranlibProgram
, arProgram
, alexProgram
, hsc2hsProgram
, c2hsProgram
, cpphsProgram
, haddockProgram
, greencardProgram
, ldProgram
, cppProgram
, pfesetupProgram
) where
import qualified Distribution.Compat.Map as Map
import Control.Monad(when)
import Data.Maybe(catMaybes)
import System.Exit (ExitCode)
import Distribution.Compat.Directory(findExecutable)
import Distribution.Simple.Utils (die, rawSystemVerbose, maybeExit)
-- |Represents a program which cabal may call.
data Program
= Program { -- |The simple name of the program, eg ghc
programName :: String
-- |The name of this program's binary, eg ghc-6.4
,programBinName :: String
-- |Default command-line args for this program
,programArgs :: [String]
-- |Location of the program. eg. \/usr\/bin\/ghc-6.4
,programLocation :: ProgramLocation
} deriving (Read, Show)
-- |Similar to Maybe, but tells us whether it's specifed by user or
-- not. This includes not just the path, but the program as well.
data ProgramLocation = EmptyLocation
| UserSpecified FilePath
| FoundOnSystem FilePath
deriving (Read, Show)
data ProgramConfiguration = ProgramConfiguration (Map.Map String Program)
-- Read & Show instances are based on listToFM
instance Show ProgramConfiguration where
show (ProgramConfiguration s) = show $ Map.toAscList s
instance Read ProgramConfiguration where
readsPrec p s = [(ProgramConfiguration $ Map.fromList $ s', r)
| (s', r) <- readsPrec p s ]
-- |The default list of programs and their arguments. These programs
-- are typically used internally to Cabal.
defaultProgramConfiguration :: ProgramConfiguration
defaultProgramConfiguration = progListToFM
[ haddockProgram
, pfesetupProgram
, ranlibProgram
, simpleProgram "runghc"
, simpleProgram "runhugs"
, arProgram]
-- haddock is currently the only one that really works.
{- [ ghcProgram
, ghcPkgProgram
, nhcProgram
, hugsProgram
, alexProgram
, hsc2hsProgram
, c2hsProgram
, cpphsProgram
, haddockProgram
, greencardProgram
, ldProgram
, cppProgram
, pfesetupProgram
, ranlib, ar
]-}
-- |The flag for giving a path to this program. eg --with-alex=\/usr\/bin\/alex
withProgramFlag :: Program -> String
withProgramFlag Program{programName=n} = "with-" ++ n
-- |The flag for giving args for this program.
-- eg --haddock-options=-s http:\/\/foo
programOptsFlag :: Program -> String
programOptsFlag Program{programName=n} = n ++ "-options"
-- |The foo.cabal field for giving args for this program.
-- eg haddock-options: -s http:\/\/foo
programOptsField :: Program -> String
programOptsField = programOptsFlag
-- ------------------------------------------------------------
-- * cabal programs
-- ------------------------------------------------------------
ghcProgram :: Program
ghcProgram = simpleProgram "ghc"
ghcPkgProgram :: Program
ghcPkgProgram = simpleProgram "ghc-pkg"
nhcProgram :: Program
nhcProgram = simpleProgram "nhc"
jhcProgram :: Program
jhcProgram = simpleProgram "jhc"
hugsProgram :: Program
hugsProgram = simpleProgram "hugs"
alexProgram :: Program
alexProgram = simpleProgram "alex"
ranlibProgram :: Program
ranlibProgram = simpleProgram "ranlib"
arProgram :: Program
arProgram = simpleProgram "ar"
hsc2hsProgram :: Program
hsc2hsProgram = simpleProgram "hsc2hs"
c2hsProgram :: Program
c2hsProgram = simpleProgram "c2hs"
cpphsProgram :: Program
cpphsProgram = simpleProgram "cpphs"
haddockProgram :: Program
haddockProgram = simpleProgram "haddock"
greencardProgram :: Program
greencardProgram = simpleProgram "greencard"
ldProgram :: Program
ldProgram = simpleProgram "ld"
cppProgram :: Program
cppProgram = simpleProgram "cpp"
pfesetupProgram :: Program
pfesetupProgram = simpleProgram "pfesetup"
-- ------------------------------------------------------------
-- * helpers
-- ------------------------------------------------------------
-- |Looks up a program in the given configuration. If there's no
-- location information in the configuration, then we use IO to look
-- on the system in PATH for the program. If the program is not in
-- the configuration at all, we return Nothing. FIX: should we build
-- a simpleProgram in that case? Do we want a way to specify NOT to
-- find it on the system (populate programLocation).
lookupProgram :: String -- simple name of program
-> ProgramConfiguration
-> IO (Maybe Program) -- the full program
lookupProgram name conf =
case lookupProgram' name conf of
Nothing -> return Nothing
Just p@Program{ programLocation= configLoc
, programBinName = binName}
-> do newLoc <- case configLoc of
EmptyLocation
-> do maybeLoc <- findExecutable binName
return $ maybe EmptyLocation FoundOnSystem maybeLoc
a -> return a
return $ Just p{programLocation=newLoc}
lookupPrograms :: ProgramConfiguration -> IO [(String, Maybe Program)]
lookupPrograms conf@(ProgramConfiguration fm) = do
let l = Map.elems fm
mapM (\p -> do fp <- lookupProgram (programName p) conf
return (programName p, fp)
) l
-- |User-specify this path. Basically override any path information
-- for this program in the configuration. If it's not a known
-- program, add it.
userSpecifyPath :: String -- ^Program name
-> FilePath -- ^user-specified path to filename
-> ProgramConfiguration
-> ProgramConfiguration
userSpecifyPath name path conf'@(ProgramConfiguration conf)
= case Map.lookup name conf of
Just p -> updateProgram (Just p{programLocation=UserSpecified path}) conf'
Nothing -> updateProgram (Just $ Program name name [] (UserSpecified path))
conf'
-- |User-specify the arguments for this program. Basically override
-- any args information for this program in the configuration. If it's
-- not a known program, add it.
userSpecifyArgs :: String -- ^Program name
-> String -- ^user-specified args
-> ProgramConfiguration
-> ProgramConfiguration
userSpecifyArgs name args conf'@(ProgramConfiguration conf)
= case Map.lookup name conf of
Just p -> updateProgram (Just p{programArgs=(words args)}) conf'
Nothing -> updateProgram (Just $ Program name name (words args) EmptyLocation) conf'
-- |Update this program's entry in the configuration. No changes if
-- you pass in Nothing.
updateProgram :: Maybe Program -> ProgramConfiguration -> ProgramConfiguration
updateProgram (Just p@Program{programName=n}) (ProgramConfiguration conf)
= ProgramConfiguration $ Map.insert n p conf
updateProgram Nothing conf = conf
-- |Runs the given program.
rawSystemProgram :: Int -- ^Verbosity
-> Program -- ^The program to run
-> [String] -- ^Any /extra/ arguments to add
-> IO ()
rawSystemProgram verbose (Program { programLocation=(UserSpecified p)
, programArgs=args
}) extraArgs
= maybeExit $ rawSystemVerbose verbose p (extraArgs ++ args)
rawSystemProgram verbose (Program { programLocation=(FoundOnSystem p)
, programArgs=args
}) extraArgs
= maybeExit $ rawSystemVerbose verbose p (args ++ extraArgs)
rawSystemProgram _ (Program { programLocation=EmptyLocation
, programName=n}) _
= die ("Error: Could not find location for program: " ++ n)
rawSystemProgramConf :: Int -- ^verbosity
-> String -- ^The name of the program to run
-> ProgramConfiguration -- ^look up the program here
-> [String] -- ^Any /extra/ arguments to add
-> IO ()
rawSystemProgramConf verbose progName programConf extraArgs
= do prog <- do mProg <- lookupProgram progName programConf
case mProg of
Nothing -> (die (progName ++ " command not found"))
Just h -> return h
rawSystemProgram verbose prog extraArgs
-- ------------------------------------------------------------
-- * Internal helpers
-- ------------------------------------------------------------
-- Export?
lookupProgram' :: String -> ProgramConfiguration -> Maybe Program
lookupProgram' s (ProgramConfiguration conf) = Map.lookup s conf
progListToFM :: [Program] -> ProgramConfiguration
progListToFM progs = foldl
(\ (ProgramConfiguration conf')
p@(Program {programName=n})
-> ProgramConfiguration (Map.insert n p conf'))
(ProgramConfiguration Map.empty)
progs
simpleProgram :: String -> Program
simpleProgram s = Program s s [] EmptyLocation
|