use Cabal to determine program locations

This commit is contained in:
Luite Stegeman 2012-04-04 02:27:41 +02:00
parent 0c60da3472
commit c097d289f8
2 changed files with 38 additions and 17 deletions

View File

@ -5,37 +5,55 @@
{-# LANGUAGE CPP #-}
module Main where
import System.Process (rawSystem, readProcess)
import System.Environment (getArgs)
import Data.Maybe (fromMaybe)
import Control.Monad (when)
import System.Exit (exitWith)
import Data.Maybe (fromMaybe)
import Distribution.Compiler (CompilerFlavor(..))
import Distribution.Simple.Configure (configCompiler)
import Distribution.Simple.Program (defaultProgramConfiguration, programPath, ghcProgram,
ldProgram, arProgram)
import Distribution.Simple.Program.Db (lookupProgram, configureProgram)
import Distribution.Simple.Program.Types (Program(..))
import Distribution.Verbosity (silent)
import System.Directory (doesDirectoryExist)
import System.Environment (getArgs)
import System.Exit (exitWith, ExitCode(..))
import System.IO (hPutStrLn, stderr)
import System.Process (rawSystem, readProcess)
#ifdef LDCMD
cmd = lookupGhcInfo "ld command" "ld"
cmd :: Program
cmd = ldProgram
outFile = "dist/ldargs.txt"
#else
#ifdef ARCMD
cmd = lookupGhcInfo "ar command" "ar"
cmd :: Program
cmd = arProgram
outFile ="dist/arargs.txt"
#else
cmd = return "ghc"
cmd :: Program
cmd = ghcProgram
outFile = "dist/ghcargs.txt"
#endif
#endif
lookupGhcInfo :: String -> String -> IO String
lookupGhcInfo xs d = fmap (fromMaybe d . lookup xs . read) (readProcess "ghc" ["--info"] "")
passthrough args = do
c <- cmd
rawSystem c args
runProgram :: Program -> [String] -> IO ExitCode
runProgram pgm args = do
(comp, pgmc) <- configCompiler (Just GHC) Nothing Nothing defaultProgramConfiguration silent
pgmc' <- configureProgram silent pgm pgmc
case lookupProgram pgm pgmc' of
Nothing -> do
hPutStrLn stderr ("cannot find program '" ++ programName pgm ++ "'")
return (ExitFailure 1)
Just p -> rawSystem (programPath p) args
main = do
args <- getArgs
e <- doesDirectoryExist "dist"
when e $ writeFile outFile (show args ++ "\n")
ex <- passthrough args
ex <- runProgram cmd args
exitWith ex

View File

@ -94,18 +94,21 @@ library
executable yesod-ghc-wrapper
main-is: ghcwrapper.hs
build-depends:
base >= 4 && < 5
base >= 4 && < 5
, Cabal >= 1.10 && < 1.16
executable yesod-ld-wrapper
main-is: ghcwrapper.hs
cpp-options: -DLDCMD
build-depends:
base >= 4 && < 5
base >= 4 && < 5
, Cabal >= 1.10 && < 1.16
executable yesod-ar-wrapper
main-is: ghcwrapper.hs
cpp-options: -DARCMD
build-depends:
base >= 4 && < 5
base >= 4 && < 5
, Cabal >= 1.10 && < 1.16
executable yesod
if flag(ghc7)