summaryrefslogtreecommitdiff
path: root/Build/EvilLinker.hs
blob: a3e838038919921761b077fa427b4a17411286f2 (plain)
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
{- Allows linking haskell programs too big for all the files to fit in a
 - command line.
 -
 -
 - Copyright 2013 Joey Hess <joey@kitenet.net>
 -
 - Licensed under the GNU GPL version 3 or higher.
 -}

module Main where

import Data.Maybe
import Data.Either
import Data.List
import Text.Parsec
import Text.Parsec.String
import Control.Applicative ((<$>))
import Control.Monad

import Utility.Monad
import Utility.Process

data CmdParams = CmdParams String String
	deriving (Show)

parseGhcLink :: Parser CmdParams
parseGhcLink = do
	many prelinklines
	linkheaderline
	char '"'
	gcccmd <- many1 (noneOf "\"")
	string "\" "
	gccparams <- restOfLine
	return $ CmdParams gcccmd gccparams
  where
	linkheaderline = do
		string "*** Linker"
		restOfLine
	prelinklines = do
		notFollowedBy linkheaderline
		restOfLine

restOfLine :: Parser String
restOfLine = newline `after` many (noneOf "\n")

getOutput :: String -> [String] -> IO String
getOutput cmd params = do
	putStrLn $ unwords [cmd, show params]
	(log, ok) <- processTranscript cmd params Nothing
	unless ok $
		error $ cmd ++ " failed:\n\n" ++ log
	return log

runParser' :: Parser a -> String -> a
runParser' p s = either (error . show) id (parse p "" s)

main = do
	ghcout <- getOutput "cabal"
		["build", "--ghc-options=-v -keep-tmp-files"]
	print $ runParser' parseGhcLink ghcout