-
Notifications
You must be signed in to change notification settings - Fork 0
/
Build.hs
241 lines (207 loc) · 7.74 KB
/
Build.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
{-# LANGUAGE QuasiQuotes #-}
module Build where
import Control.Concurrent
import Control.Lens
import Control.Monad.RWS
import Control.Monad.Trans.Either
import Control.Monad.Trans.Maybe
import Data.Char
import Data.ConfigFile hiding (get)
import qualified Data.ConfigFile as CF
import Data.Either.Combinators
import Data.List
import Data.Maybe
import Data.String.Lines
import System.Directory
import System.Exit
import System.FilePath
import System.IO.Unsafe
import System.Process
import Text.Printf.TH
import Utilities.Config
data CompileMode = Make | CompileFile
data CompileFlags = CompileFlags
{ mode :: CompileMode
, profile :: Bool }
type Build = RWST FilePath () (FilePath,[FilePath]) (MaybeT IO)
data BuildTool = Cabal | Stack
deriving (Eq,Show,Read)
inf :: FilePath
inf = "interface"
bin :: FilePath
bin = "bin"
gatherConfig :: [FilePath] -> IO (Either CPError ConfigParser)
gatherConfig fs = do
fs' <- filterM doesFileExist fs
runEitherT $ foldM (fmap EitherT . readfile) emptyCP fs'
option :: Get_C a
=> Either CPError ConfigParser
-> a -> String -> a
option cp x name = fromRight x $ do
cp <- cp
CF.get cp "DEFAULT" name
tool :: BuildTool
tool = unsafePerformIO $ do
homeSetting <- homeSettingPath
cp <- gatherConfig
[ homeSetting </> "build.setting"
, "./build.setting" ]
return $ option cp Cabal "buildTool"
build :: FilePath -> Build a -> IO (Maybe (a, FilePath, [FilePath]))
build path cmd = fmap f <$> runMaybeT (runRWST cmd path ("ghc",[]))
where
f (x,(y,z),()) = (x,y,z)
args :: CompileFlags -> FilePath -> Build ()
args opt file = do
path <- ask
liftIO $ createDirectoryIfMissing True $
path </> "bin" </> takeDirectory file
let flag = case mode opt of
CompileFile -> ["-c",file]
Make
| profile opt -> ["--make",file,"-o",path </> "bin" </> dropExtension file ++ "_p"]
| otherwise -> ["--make",file,"-o",path </> "bin" </> dropExtension file]
_2 .= flag ++
[ "-j8"
, "-odir" ++ bin
, "-i" ++ intercalate ":" [inf,"suite","src","utils","latex","logic"
,"libs/axiomatic-classes","libs/data-compressed"
,"libs/data-map-class","libs/data-packaged"
,"libs/existential","libs/generic-instances"
,"libs/invariants","libs/partial-order"
,"libs/th-printf","libs/reactive-banana-transformers"
,"libs/string-lenses","libs/unfoldable"
,"libs/show-with","libs/typelist-functor"
,"libs/file-system-mockup","libs/transformer-lenses"
,"libs/bipartite-graph","libs/classy-lens-hierarchy"
,"libs/co-applicative", "libs/unitb-testing"
,"libs/lens-extra", "libs/pretty-printable"
]
, "-hidir" ++ inf
, "-W", "-O0"
, "-XTupleSections"
, "-XDeriveFunctor"
, "-XDeriveGeneric"
, "-XQuasiQuotes"
, "-XDeriveFoldable"
, "-XRankNTypes"
, "-XMultiParamTypeClasses"
, "-XGeneralizedNewtypeDeriving"
, "-XTemplateHaskell"
, "-XFlexibleContexts"
, "-XFlexibleInstances"
, "-XDeriveDataTypeable"
, "-XTypeSynonymInstances"
, "-XDefaultSignatures"
, "-XDeriveTraversable"
, "-XFunctionalDependencies"
, "-XImplicitParams"
--, "-D__HASHED_KEYS__"
-- , "-D__HASH_MAP__"
-- , "-D__HASH_MAP_LAZY__"
-- , "-D__PACKAGED_NAMES__"
, "-fwarn-missing-signatures"
, "-fwarn-incomplete-uni-patterns"
, "-fwarn-missing-methods"
-- , "-fwarn-orphans"
, "-threaded", "-fno-ignore-asserts"
, "-fwarn-tabs", "-Werror"
, "-ignore-package", "literate-unitb"
-- , "-package", "either-4.3"
-- , "-package", "mtl-2.1.3.1"
-- , "-package", "QuickCheck"
-- , "-package", "transformers-0.3.0.0"
-- , "-package", "exceptions-0.6.1"
-- , "-ddump-splices"
-- , "-fforce-recomp"
-- , "-O2"
] ++ if profile opt
then [ "-osufp_o", "-hisufp_hi"
, "-prof", "-O2"
, "-fhpc"
, "-caf-all"
-- , "-fforce-recomp"
, "-auto-all"
, "-rtsopts" ]
else [ "-dynamic-too" ]
-- , "-v"
showBuildCommand :: FilePath -> Build () -> IO (Maybe String)
showBuildCommand fp cmd = fmap (fmap $ view _1) $ build fp $ do
cmd
(ghc,args) <- get
return $ showCommandForUser ghc args
compile :: Bool -> Build () -> Build ()
compile silent argsM = do
argsM
(ghc,args) <- get
lift $ MaybeT $ do
r <- if silent then do
(r,_out,err) <- readProcessWithExitCode ghc args []
-- putStrLn $ unlines $ filter (not . ("[" `isPrefixOf`)) $ lines _out
putStrLn $ removeAtLineNumber err
return r
else rawSystem ghc args
threadDelay 10
case r of
ExitSuccess -> return $ Just ()
_ -> return Nothing
removeAtLineNumber :: String -> String
removeAtLineNumber = traverseLines.spanIso isSpace._2 %~ dropKW "at "
where
dropKW kw xs | kw `isPrefixOf` xs = drop (length kw) xs
| otherwise = xs
newtype CabalTarget = CabalTarget FilePath
returnIf :: a -> ExitCode -> Build a
returnIf x ExitSuccess = return x
returnIf _ (ExitFailure _) = mzero
liftIOWithExit :: IO ExitCode -> Build ()
liftIOWithExit cmd = do
r <- liftIO cmd
returnIf () r
toolBuild :: (String -> [String] -> r) -> [String] -> r
toolBuild f args = case tool of
Cabal -> f "cabal" $ "build" : args
Stack -> f "stack" $ "build" : args
toolExec :: (String -> [String] -> r) -> [String] -> r
toolExec f args = case tool of
Cabal -> f "cabal" $ "run" : args
Stack -> f "stack" $ "exec" : args
execCommand :: String -> String -> String
execCommand command outfile = toolExec (\c args -> [s|%s %s %s > %s|] c (intercalate " " args) command outfile) []
cabal_run :: CabalTarget -> Build ()
cabal_run (CabalTarget target) = do
liftIOWithExit $ do
(r,_out,err) <- toolExec readProcessWithExitCode [target] []
-- putStrLn $ unlines $ filter (not . ("[" `isPrefixOf`)) $ lines _out
putStrLn $ removeAtLineNumber err
return r
cabal_build :: Maybe String -> Build CabalTarget
cabal_build target = do
liftIOWithExit $ do
(r,_out,err) <- toolBuild readProcessWithExitCode (maybeToList target) []
-- putStrLn $ unlines $ filter (not . ("[" `isPrefixOf`)) $ lines _out
putStrLn $ removeAtLineNumber err
return r
return $ CabalTarget $ fromMaybe "" target
enableProfiling :: FilePath -> Build FilePath
enableProfiling target = do
compile False (args (CompileFlags Make True) target)
return $ "bin" </> dropExtension target ++ "_p"
make :: FilePath -> Build FilePath
make target = do
compile False (args (CompileFlags Make False) target)
return $ "bin" </> dropExtension target
compile_test :: Build FilePath
compile_test = make "suite/test_tmp.hs"
profile_test :: Build FilePath
profile_test = do
_ <- make "suite/test_tmp.hs"
enableProfiling "suite/test_tmp.hs"
compile_all :: Build FilePath
compile_all = make "suite/test.hs"
compile_app :: Build FilePath
compile_app = make "app/Continuous.hs"
profile_app :: Build FilePath
profile_app = do
_ <- make "app/Continuous.hs"
enableProfiling "app/Continuous.hs"