EvilLinker stage 2

This commit is contained in:
Joey Hess 2013-12-06 12:43:30 -04:00
parent 01799cafc3
commit 639808658d

View file

@ -19,10 +19,12 @@ import Control.Monad
import Utility.Monad
import Utility.Process
import System.Directory
data CmdParams = CmdParams String String
data CmdParams = CmdParams { cmd :: String, opts :: String }
deriving (Show)
{- Find where ghc calls gcc to link the executable. -}
parseGhcLink :: Parser CmdParams
parseGhcLink = do
many prelinklines
@ -40,6 +42,14 @@ parseGhcLink = do
notFollowedBy linkheaderline
restOfLine
{- Find where gcc calls collect1. -}
parseCollect1 :: Parser CmdParams
parseCollect1 = error "TODO"
{- Find where collect1 calls ld. -}
parseLd :: Parser CmdParams
parseLd = error "TODO"
restOfLine :: Parser String
restOfLine = newline `after` many (noneOf "\n")
@ -54,7 +64,21 @@ getOutput cmd params = do
runParser' :: Parser a -> String -> a
runParser' p s = either (error . show) id (parse p "" s)
atFile :: FilePath -> String
atFile f = '@':f
runAtFile :: Parser CmdParams -> String -> FilePath -> [String] -> IO String
runAtFile p s f extraparams = do
writeFile f (opts c)
out <- getOutput (cmd c) (atFile f:extraparams)
removeFile f
return out
where
c = runParser' p s
main = do
ghcout <- getOutput "cabal"
["build", "--ghc-options=-v -keep-tmp-files"]
print $ runParser' parseGhcLink ghcout
gccout <- runAtFile parseGhcLink ghcout "gcc.opt" ["-v"]
collect1out <- runAtFile parseCollect1 gccout "collect1.opt" ["-v"]
void $ runAtFile parseLd collect1out "ld.opt" []