/usr/lib/hugs/packages/Cabal/Distribution/Simple/Utils.hs is in libhugs-cabal-bundled 98.200609.21-5.3.
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 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 | {-# OPTIONS_GHC -cpp -fffi #-}
-----------------------------------------------------------------------------
-- |
-- Module : Distribution.Simple.Utils
-- Copyright : Isaac Jones, Simon Marlow 2003-2004
--
-- Maintainer : Isaac Jones <ijones@syntaxpolice.org>
-- Stability : alpha
-- Portability : portable
--
-- Explanation: Misc. Utilities, especially file-related utilities.
-- Stuff used by multiple modules that doesn't fit elsewhere.
{- All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.
* Neither the name of Isaac Jones nor the names of other
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -}
module Distribution.Simple.Utils (
die,
dieWithLocation,
warn,
rawSystemPath,
rawSystemVerbose,
rawSystemExit,
maybeExit,
xargs,
matchesDescFile,
rawSystemPathExit,
smartCopySources,
copyFileVerbose,
copyDirectoryRecursiveVerbose,
moduleToFilePath,
mkLibName,
mkProfLibName,
currentDir,
dirOf,
dotToSep,
withTempFile,
findFile,
defaultPackageDesc,
findPackageDesc,
defaultHookedPackageDesc,
findHookedPackageDesc,
distPref,
haddockPref,
srcPref,
) where
import Distribution.Compat.RawSystem (rawSystem)
import Distribution.Compat.Exception (finally)
import Control.Monad(when, filterM, unless)
import Data.List (nub, unfoldr)
import System.Environment (getProgName)
import System.IO (hPutStrLn, stderr, hFlush, stdout)
import System.IO.Error
import System.Exit
import System.Posix.Internals (c_getpid)
import Distribution.Compat.FilePath
(splitFileName, splitFileExt, joinFileName, joinFileExt, joinPaths,
pathSeparator,splitFilePath)
import System.Directory (getDirectoryContents, getCurrentDirectory
, doesDirectoryExist, doesFileExist, removeFile, getPermissions
, Permissions(executable))
import Distribution.Compat.Directory
(copyFile, findExecutable, createDirectoryIfMissing,
getDirectoryContentsWithoutSpecial)
-- ------------------------------------------------------------------------------- Utils for setup
dieWithLocation :: FilePath -> (Maybe Int) -> String -> IO a
dieWithLocation fname Nothing msg = die (fname ++ ": " ++ msg)
dieWithLocation fname (Just n) msg = die (fname ++ ":" ++ show n ++ ": " ++ msg)
die :: String -> IO a
die msg = do
hFlush stdout
pname <- getProgName
hPutStrLn stderr (pname ++ ": " ++ msg)
exitWith (ExitFailure 1)
warn :: String -> IO ()
warn msg = do
hFlush stdout
pname <- getProgName
hPutStrLn stderr (pname ++ ": Warning: " ++ msg)
-- -----------------------------------------------------------------------------
-- rawSystem variants
rawSystemPath :: Int -> String -> [String] -> IO ExitCode
rawSystemPath verbose prog args = do
r <- findExecutable prog
case r of
Nothing -> die ("Cannot find: " ++ prog)
Just path -> rawSystemVerbose verbose path args
rawSystemVerbose :: Int -> FilePath -> [String] -> IO ExitCode
rawSystemVerbose verbose prog args = do
when (verbose > 0) $
putStrLn (prog ++ concatMap (' ':) args)
e <- doesFileExist prog
if e
then do perms <- getPermissions prog
if (executable perms)
then rawSystem prog args
else die ("Error: file is not executable: " ++ show prog)
else die ("Error: file does not exist: " ++ show prog)
maybeExit :: IO ExitCode -> IO ()
maybeExit cmd = do
res <- cmd
if res /= ExitSuccess
then exitWith res
else return ()
-- Exit with the same exitcode if the subcommand fails
rawSystemExit :: Int -> FilePath -> [String] -> IO ()
rawSystemExit verbose path args = do
when (verbose > 0) $
putStrLn (path ++ concatMap (' ':) args)
maybeExit $ rawSystem path args
-- Exit with the same exitcode if the subcommand fails
rawSystemPathExit :: Int -> String -> [String] -> IO ()
rawSystemPathExit verbose prog args = do
maybeExit $ rawSystemPath verbose prog args
-- | Like the unix xargs program. Useful for when we've got very long command
-- lines that might overflow an OS limit on command line length and so you
-- need to invoke a command multiple times to get all the args in.
--
-- Use it with either of the rawSystem variants above. For example:
--
-- > xargs (32*1024) (rawSystemPath verbose) prog fixedArgs bigArgs
--
xargs :: Int -> (FilePath -> [String] -> IO ExitCode)
-> FilePath -> [String] -> [String] -> IO ExitCode
xargs maxSize rawSystem prog fixedArgs bigArgs =
let fixedArgSize = sum (map length fixedArgs) + length fixedArgs
chunkSize = maxSize - fixedArgSize
loop [] = return ExitSuccess
loop (args:remainingArgs) = do
status <- rawSystem prog (fixedArgs ++ args)
case status of
ExitSuccess -> loop remainingArgs
_ -> return status
in loop (chunks chunkSize bigArgs)
where chunks len = unfoldr $ \s ->
if null s then Nothing
else Just (chunk [] len s)
chunk acc len [] = (reverse acc,[])
chunk acc len (s:ss)
| len' < len = chunk (s:acc) (len-len'-1) ss
| otherwise = (reverse acc, s:ss)
where len' = length s
-- ------------------------------------------------------------
-- * File Utilities
-- ------------------------------------------------------------
-- |Get the file path for this particular module. In the IO monad
-- because it looks for the actual file. Might eventually interface
-- with preprocessor libraries in order to correctly locate more
-- filenames.
-- Returns empty list if no such files exist.
moduleToFilePath :: [FilePath] -- ^search locations
-> String -- ^Module Name
-> [String] -- ^possible suffixes
-> IO [FilePath]
moduleToFilePath pref s possibleSuffixes
= filterM doesFileExist $
concatMap (searchModuleToPossiblePaths s possibleSuffixes) pref
where searchModuleToPossiblePaths :: String -> [String] -> FilePath -> [FilePath]
searchModuleToPossiblePaths s' suffs searchP
= moduleToPossiblePaths searchP s' suffs
-- |Like 'moduleToFilePath', but return the location and the rest of
-- the path as separate results.
moduleToFilePath2
:: [FilePath] -- ^search locations
-> String -- ^Module Name
-> [String] -- ^possible suffixes
-> IO [(FilePath, FilePath)] -- ^locations and relative names
moduleToFilePath2 locs mname possibleSuffixes
= filterM exists $
[(loc, fname `joinFileExt` ext) | loc <- locs, ext <- possibleSuffixes]
where
fname = dotToSep mname
exists (loc, relname) = doesFileExist (loc `joinFileName` relname)
-- |Get the possible file paths based on this module name.
moduleToPossiblePaths :: FilePath -- ^search prefix
-> String -- ^module name
-> [String] -- ^possible suffixes
-> [FilePath]
moduleToPossiblePaths searchPref s possibleSuffixes =
let fname = searchPref `joinFileName` (dotToSep s)
in [fname `joinFileExt` ext | ext <- possibleSuffixes]
findFile :: [FilePath] -- ^search locations
-> FilePath -- ^File Name
-> IO FilePath
findFile prefPathsIn locPath = do
let prefPaths = nub prefPathsIn -- ignore dups
paths <- filterM doesFileExist [prefPath `joinFileName` locPath | prefPath <- prefPaths]
case nub paths of -- also ignore dups, though above nub should fix this.
[path] -> return path
[] -> die (locPath ++ " doesn't exist")
paths -> die (locPath ++ " is found in multiple places:" ++ unlines (map ((++) " ") paths))
dotToSep :: String -> String
dotToSep = map dts
where
dts '.' = pathSeparator
dts c = c
-- |Copy the source files into the right directory. Looks in the
-- build prefix for files that look like the input modules, based on
-- the input search suffixes. It copies the files into the target
-- directory.
smartCopySources :: Int -- ^verbose
-> [FilePath] -- ^build prefix (location of objects)
-> FilePath -- ^Target directory
-> [String] -- ^Modules
-> [String] -- ^search suffixes
-> Bool -- ^Exit if no such modules
-> Bool -- ^Preserve directory structure
-> IO ()
smartCopySources verbose srcDirs targetDir sources searchSuffixes exitIfNone preserveDirs
= do createDirectoryIfMissing True targetDir
allLocations <- mapM moduleToFPErr sources
let copies = [(srcDir `joinFileName` name,
if preserveDirs
then targetDir `joinFileName` srcDir `joinFileName` name
else targetDir `joinFileName` name) |
(srcDir, name) <- concat allLocations]
-- Create parent directories for everything:
mapM_ (createDirectoryIfMissing True) $ nub $
[fst (splitFileName targetFile) | (_, targetFile) <- copies]
-- Put sources into place:
sequence_ [copyFileVerbose verbose srcFile destFile |
(srcFile, destFile) <- copies]
where moduleToFPErr m
= do p <- moduleToFilePath2 srcDirs m searchSuffixes
when (null p && exitIfNone)
(die ("Error: Could not find module: " ++ m
++ " with any suffix: " ++ (show searchSuffixes)))
return p
copyFileVerbose :: Int -> FilePath -> FilePath -> IO ()
copyFileVerbose verbose src dest = do
when (verbose > 0) $
putStrLn ("copy " ++ src ++ " to " ++ dest)
copyFile src dest
-- adaptation of removeDirectoryRecursive
copyDirectoryRecursiveVerbose :: Int -> FilePath -> FilePath -> IO ()
copyDirectoryRecursiveVerbose verbose srcDir destDir = do
when (verbose > 0) $
putStrLn ("copy directory '" ++ srcDir ++ "' to '" ++ destDir ++ "'.")
let aux src dest =
let cp :: FilePath -> IO ()
cp f = let srcFile = joinPaths src f
destFile = joinPaths dest f
in do success <- try (copyFileVerbose verbose srcFile destFile)
case success of
Left e -> do isDir <- doesDirectoryExist srcFile
-- If f is not a directory, re-throw the error
unless isDir $ ioError e
aux srcFile destFile
Right _ -> return ()
in do createDirectoryIfMissing False dest
getDirectoryContentsWithoutSpecial src >>= mapM_ cp
in aux srcDir destDir
-- | The path name that represents the current directory.
-- In Unix, it's @\".\"@, but this is system-specific.
-- (E.g. AmigaOS uses the empty string @\"\"@ for the current directory.)
currentDir :: FilePath
currentDir = "."
dirOf :: FilePath -> FilePath
dirOf f = (\ (x, _, _) -> x) $ (splitFilePath f)
mkLibName :: FilePath -- ^file Prefix
-> String -- ^library name.
-> String
mkLibName pref lib = pref `joinFileName` ("libHS" ++ lib ++ ".a")
mkProfLibName :: FilePath -- ^file Prefix
-> String -- ^library name.
-> String
mkProfLibName pref lib = mkLibName pref (lib++"_p")
-- ------------------------------------------------------------
-- * Some Paths
-- ------------------------------------------------------------
distPref :: FilePath
distPref = "dist"
srcPref :: FilePath
srcPref = distPref `joinFileName` "src"
haddockPref :: FilePath
haddockPref = foldl1 joinPaths [distPref, "doc", "html"]
-- ------------------------------------------------------------
-- * temporary file names
-- ------------------------------------------------------------
-- use a temporary filename that doesn't already exist.
-- NB. *not* secure (we don't atomically lock the tmp file we get)
withTempFile :: FilePath -> String -> (FilePath -> IO a) -> IO a
withTempFile tmp_dir extn action
= do x <- getProcessID
findTempName x
where
findTempName x
= do let filename = ("tmp" ++ show x) `joinFileExt` extn
path = tmp_dir `joinFileName` filename
b <- doesFileExist path
if b then findTempName (x+1)
else action path `finally` try (removeFile path)
getProcessID :: IO Int
getProcessID = System.Posix.Internals.c_getpid >>= return . fromIntegral
-- ------------------------------------------------------------
-- * Finding the description file
-- ------------------------------------------------------------
oldDescFile :: String
oldDescFile = "Setup.description"
cabalExt :: String
cabalExt = "cabal"
buildInfoExt :: String
buildInfoExt = "buildinfo"
matchesDescFile :: FilePath -> Bool
matchesDescFile p = (snd $ splitFileExt p) == cabalExt
|| p == oldDescFile
noDesc :: IO a
noDesc = die $ "No description file found, please create a cabal-formatted description file with the name <pkgname>." ++ cabalExt
multiDesc :: [String] -> IO a
multiDesc l = die $ "Multiple description files found. Please use only one of : "
++ show (filter (/= oldDescFile) l)
-- |A list of possibly correct description files. Should be pre-filtered.
descriptionCheck :: [FilePath] -> IO FilePath
descriptionCheck [] = noDesc
descriptionCheck [x]
| x == oldDescFile
= do warn $ "The filename \"Setup.description\" is deprecated, please move to <pkgname>." ++ cabalExt
return x
| matchesDescFile x = return x
| otherwise = noDesc
descriptionCheck [x,y]
| x == oldDescFile
= do warn $ "The filename \"Setup.description\" is deprecated. Please move out of the way. Using \""
++ y ++ "\""
return y
| y == oldDescFile
= do warn $ "The filename \"Setup.description\" is deprecated. Please move out of the way. Using \""
++ x ++ "\""
return x
| otherwise = multiDesc [x,y]
descriptionCheck l = multiDesc l
-- |Package description file (/pkgname/@.cabal@)
defaultPackageDesc :: IO FilePath
defaultPackageDesc = getCurrentDirectory >>= findPackageDesc
-- |Find a package description file in the given directory. Looks for
-- @.cabal@ files.
findPackageDesc :: FilePath -- ^Where to look
-> IO FilePath -- <pkgname>.cabal
findPackageDesc p = do ls <- getDirectoryContents p
let descs = filter matchesDescFile ls
descriptionCheck descs
-- |Optional auxiliary package information file (/pkgname/@.buildinfo@)
defaultHookedPackageDesc :: IO (Maybe FilePath)
defaultHookedPackageDesc = getCurrentDirectory >>= findHookedPackageDesc
-- |Find auxiliary package information in the given directory.
-- Looks for @.buildinfo@ files.
findHookedPackageDesc
:: FilePath -- ^Directory to search
-> IO (Maybe FilePath) -- ^/dir/@\/@/pkgname/@.buildinfo@, if present
findHookedPackageDesc dir = do
ns <- getDirectoryContents dir
case [dir `joinFileName` n |
n <- ns, snd (splitFileExt n) == buildInfoExt] of
[] -> return Nothing
[f] -> return (Just f)
_ -> die ("Multiple files with extension " ++ buildInfoExt)
-- ------------------------------------------------------------
-- * Testing
-- ------------------------------------------------------------
|