-
Notifications
You must be signed in to change notification settings - Fork 0
/
Setup.hs
52 lines (47 loc) · 2.48 KB
/
Setup.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import Distribution.PackageDescription (BuildInfo)
import Distribution.Simple (UserHooks(..),defaultMainWithHooks,simpleUserHooks)
import Distribution.Simple.BuildPaths (autogenComponentModulesDir)
import Distribution.Simple.LocalBuildInfo (LocalBuildInfo(..),ComponentLocalBuildInfo)
import Distribution.Simple.PreProcess (PreProcessor(..),knownSuffixHandlers,mkSimplePreProcessor)
import Distribution.Simple.Program (Program(..),findProgramVersion,runDbProgram,simpleProgram)
import Distribution.Simple.Utils (info)
import System.FilePath (takeBaseName,takeDirectory)
main :: IO ()
main =
defaultMainWithHooks simpleUserHooks{hookedPreProcessors = ("cf",ppBnfc):knownSuffixHandlers}
ppBnfc :: BuildInfo -> LocalBuildInfo -> ComponentLocalBuildInfo -> PreProcessor
ppBnfc _ lbi clbi =
PreProcessor {platformIndependent = True
,runPreProcessor = mkSimplePreProcessor $ \inFile outFile verbosity ->
do info verbosity ("Running "++programName bnfcProgram
++" --outputdir="++autogenDir
++" --haskell " ++ inFile
)
runDbProgram verbosity bnfcProgram (withPrograms lbi)
["--outputdir="++autogenDir, "--haskell", inFile]
info verbosity ("Creating "++programName bnfcProgram++" output file '"++outFile++"'"
++" from input file '"++inFile++"'")
outFile `makeFrom` inFile
}
where autogenDir = autogenComponentModulesDir lbi clbi
bnfcProgram :: Program
bnfcProgram = (simpleProgram "bnfc") {
-- Invoking "bnfc --version" gives a string like "2.8.2"
programFindVersion = findProgramVersion "--version" id
}
makeFrom :: String -> String -> IO ()
makeFrom outFile inFile = do inFileStr <- readFile inFile
writeFile outFile (outFileStrFrom inFileStr)
where outFileStrFrom inFileStr =
"{- |\n"
++"This is the SciDB AFL BNFC file:\n"
++"\n"
++quote inFileStr++"\n"
++"-}\n"
++"\n"
++"module "++takeBaseName inFile++" where\n"
++"\n"
++"-- | BNFC configuration file as a string, for the purposes of documentation.\n"
++"bnfcFile :: String\n"
++"bnfcFile = "++show inFileStr++"\n"
quote inFileStr = unlines $ fmap ("> "++) $ lines inFileStr