-
Notifications
You must be signed in to change notification settings - Fork 0
/
Setup.hs
61 lines (52 loc) · 2.04 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
52
53
54
55
56
57
58
59
60
61
{-# LANGUAGE CPP #-}
{-# LANGUAGE QuasiQuotes #-}
-- Source: https://github.com/haskell/cabal/issues/6726#issuecomment-918663262
-- | Custom Setup that runs bnfc to generate the language sub-libraries
-- for the parsers included in Ogma.
module Main (main) where
import Data.List (intercalate)
import Distribution.Simple (defaultMainWithHooks, hookedPrograms, postConf, preBuild, simpleUserHooks)
import Distribution.Simple.Program (Program (..), findProgramVersion, simpleProgram)
import PyF (fmt)
import System.Exit (ExitCode (..))
import System.Process (callCommand)
-- | Run BNFC, happy, and alex on the grammar before the actual build step.
--
-- All options for bnfc are hard-coded here.
main :: IO ()
main =
defaultMainWithHooks $
simpleUserHooks
{ hookedPrograms = [bnfcProgram]
, postConf = \args flags packageDesc localBuildInfo -> do
let
isWindows =
#ifdef mingw32_HOST_OS
True
#else
False
#endif
-- See the details on the command in https://github.com/objectionary/normalizer/issues/347#issuecomment-2117097070
command = intercalate "; " $
[ "set -ex" ] <>
[ "chcp.com" | isWindows ] <>
[ "chcp.com 65001" | isWindows ] <>
[ "cp grammar/hindley-milner.cf grammar/Parser.cf" -- Workaround to customize generated package name
, "bnfc --haskell -d -p HM --generic -o src grammar/Parser.cf"
, "rm grammar/Parser.cf"
, "cd src/HM/Parser"
, "alex Lex.x"
, "happy Par.y"
, "true"
]
fullCommand = [fmt|bash -c ' {command} '|]
putStrLn fullCommand
_ <- callCommand fullCommand
postConf simpleUserHooks args flags packageDesc localBuildInfo
}
-- | NOTE: This should be in Cabal.Distribution.Simple.Program.Builtin.
bnfcProgram :: Program
bnfcProgram =
(simpleProgram "bnfc")
{ programFindVersion = findProgramVersion "--version" id
}