-
Notifications
You must be signed in to change notification settings - Fork 7
/
drift-ghc.hs
30 lines (28 loc) · 1.62 KB
/
drift-ghc.hs
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
import Data.List (isInfixOf)
import System.Cmd (rawSystem)
import System.Environment (getArgs)
import System.Exit (ExitCode(ExitSuccess))
import System.FilePath
import Paths_DrIFT (getBinDir)
main :: IO ExitCode
main = do args <- getArgs
case args of
(a:b:c:[]) -> conditional a b c
_ -> error "This is a driver script allowing DrIFT to be used seamlessly with ghc.\n \
\ in order to use it, pass '-pgmF drift-ghc -F' to ghc when compiling your programs."
conditional :: FilePath -> FilePath -> FilePath -> IO ExitCode
conditional orgnl inf outf = do prefix <- getBinDir
infile <- readFile inf
if "{-!" `isInfixOf` infile then do putStrLn (prefix </> "DrIFT " ++
inf ++ " -o " ++ outf)
rawSystem (prefix </> "DrIFT") [inf, "-o", outf]
else do writeFile outf ("{-# LINE 1 \"" ++ orgnl ++ " #-}")
readFile inf >>= appendFile outf
return ExitSuccess
{- GHC docs say: "-pgmF cmd
Use cmd as the pre-processor (with -F only).
Use -pgmF cmd to select the program to use as the preprocessor.
When invoked, the cmd pre-processor is given at least three arguments on its command-line:
1. the first argument is the name of the original source file,
2. the second is the name of the file holding the input
3. third is the name of the file where cmd should write its output to." -}