-
Notifications
You must be signed in to change notification settings - Fork 199
Expand file tree
/
Copy pathCompile.hs
More file actions
288 lines (260 loc) · 9.66 KB
/
Compile.hs
File metadata and controls
288 lines (260 loc) · 9.66 KB
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE PatternGuards #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE ViewPatterns #-}
{-
Copyright 2019 The CodeWorld Authors. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-}
module CodeWorld.Compile
( compileSource
, Stage(..)
, CompileStatus(..)
) where
import CodeWorld.Compile.Framework
import CodeWorld.Compile.Stages
import Control.Applicative
import Control.Concurrent
import Control.Exception
import Control.Monad
import Control.Monad.IO.Class
import Control.Monad.State
import Data.ByteString (ByteString)
import qualified Data.ByteString as B
import Data.List
import Data.Maybe
import Data.Monoid
import Data.Text (Text)
import qualified Data.Text as T
import Data.Text.Encoding (decodeUtf8, encodeUtf8)
import ErrorSanitizer
import Language.Haskell.Exts.SrcLoc
import System.Exit (ExitCode(..))
import System.Directory
import System.FilePath
import System.IO
import System.IO.Temp (withSystemTempDirectory)
import System.Process
import Text.Read (readMaybe)
import Text.Regex.TDFA
import Text.Regex.TDFA.Text
formatDiagnostics :: [Diagnostic] -> Text
formatDiagnostics diags =
T.intercalate "\n\n" (map formatDiagnostic diags)
formatDiagnostic :: Diagnostic -> Text
formatDiagnostic (loc, _, msg) =
T.pack (formatLocation loc ++ ": " ++ msg)
readUtf8 :: FilePath -> IO Text
readUtf8 f = decodeUtf8 <$> B.readFile f
writeUtf8 :: FilePath -> Text -> IO ()
writeUtf8 f = B.writeFile f . encodeUtf8
compileSource
:: Stage -> FilePath -> FilePath -> String -> Bool -> IO CompileStatus
compileSource stage src err mode verbose = fromMaybe CompileAborted <$>
withTimeout timeout (compileStatus <$> execStateT build initialState)
where
initialState = CompileState {
compileMode = mode,
compileStage = stage,
compileSourcePath = src,
compileOutputPath = err,
compileVerbose = verbose,
compileTimeout = timeout,
compileStatus = CompileSuccess,
compileErrors = [],
compileReadSource = Nothing,
compileParsedSource = Nothing
}
timeout = case stage of
GenBase _ _ _ _ -> maxBound :: Int
_ -> userCompileMicros
userCompileMicros :: Int
userCompileMicros = 30 * 1000000
build :: MonadCompile m => m ()
build = do
checkDangerousSource
ifSucceeding checkCodeConventions
ifSucceeding compileCode
errPath <- gets compileOutputPath
diags <- sort <$> gets compileErrors
liftIO $ createDirectoryIfMissing True (takeDirectory errPath)
liftIO $ B.writeFile errPath $ encodeUtf8 $ formatDiagnostics diags
compileCode :: MonadCompile m => m ()
compileCode = ifSucceeding $ withSystemTempDirectory "build" $ \tmpdir -> do
ghcjsArgs <- prepareCompile tmpdir
timeout <- gets compileTimeout
verbose <- gets compileVerbose
(exitCode, output) <- liftIO $ runCompiler tmpdir timeout ghcjsArgs verbose
when (exitCode /= ExitSuccess) failCompile
mode <- gets compileMode
if mode == "codeworld"
then addParsedDiagnostics (rewriteErrors output)
else addParsedDiagnostics output
ifSucceeding $ copyOutputFrom (tmpdir </> "program.jsexe")
prepareCompile :: MonadCompile m => FilePath -> m [String]
prepareCompile dir = do
src <- gets compileSourcePath
liftIO $ copyFile src (dir </> "program.hs")
mode <- gets compileMode
stage <- gets compileStage
linkArgs <- case stage of
ErrorCheck -> return ["-fno-code"]
FullBuild _ -> return ["-dedupe"]
GenBase mod base _ _ -> do
liftIO $ copyFile base (dir </> mod <.> "hs")
return [mod <.> "hs", "-generate-base", mod]
UseBase _ syms _ -> do
liftIO $ copyFile syms (dir </> "out.base.symbs")
return ["-dedupe", "-use-base", "out.base.symbs"]
return $ ["program.hs"] ++ buildArgs mode ++ linkArgs
buildArgs :: SourceMode -> [String]
buildArgs "codeworld" =
[ "-DGHCJS_BROWSER"
, "-ferror-spans"
, "-fno-diagnostics-show-caret"
, "-hide-package"
, "base"
, "-package"
, "codeworld-base"
, "-fplugin"
, "CodeWorld.Requirements.RequirementsChecker"
, "-Wall"
, "-Wdeferred-type-errors"
, "-Wdeferred-out-of-scope-variables"
, "-fno-warn-deprecated-flags"
, "-fno-warn-amp"
, "-fno-warn-missing-signatures"
, "-fno-warn-incomplete-patterns"
, "-fno-warn-unused-matches"
, "-fdefer-type-errors"
, "-fdefer-out-of-scope-variables"
, "-Wno-partial-type-signatures"
, "-XBangPatterns"
, "-XDisambiguateRecordFields"
, "-XEmptyDataDecls"
, "-XExistentialQuantification"
, "-XForeignFunctionInterface"
, "-XGADTs"
, "-XJavaScriptFFI"
, "-XKindSignatures"
, "-XLiberalTypeSynonyms"
, "-XNamedFieldPuns"
, "-XNoMonomorphismRestriction"
, "-XNoQuasiQuotes"
, "-XNoTemplateHaskell"
, "-XNoUndecidableInstances"
, "-XOverloadedStrings"
, "-XPackageImports"
, "-XPatternGuards"
, "-XParallelListComp"
, "-XPartialTypeSignatures"
, "-XRankNTypes"
, "-XRebindableSyntax"
, "-XRecordWildCards"
, "-XScopedTypeVariables"
, "-XTypeOperators"
, "-XViewPatterns"
, "-XImplicitPrelude" -- MUST come after RebindableSyntax.
, "-main-is"
, "Main.program"
]
buildArgs "haskell" =
[ "-DGHCJS_BROWSER"
, "-ferror-spans"
, "-fno-diagnostics-show-caret"
, "-package"
, "codeworld-api"
, "-package"
, "QuickCheck"
, "-fplugin"
, "CodeWorld.Requirements.RequirementsChecker"
]
runCompiler :: FilePath -> Int -> [String] -> Bool -> IO (ExitCode, Text)
runCompiler dir timeout args verbose =
withTimeout firstAttemptTimeout (attempt ("-O" : args))
>>= maybe (attempt args) return
where
firstAttemptTimeout = timeout `div` 2
attempt :: [String] -> IO (ExitCode, Text)
attempt currentArgs = do
when verbose $ hPutStrLn stderr $
"COMMAND: ghcjs " ++ intercalate " " args
(exitCode, result) <- runSync dir "ghcjs" args
when verbose $ hPutStrLn stderr $ "RESULT: " ++ show exitCode
return (exitCode, result)
addParsedDiagnostics :: MonadCompile m => Text -> m ()
addParsedDiagnostics output = addDiagnostics newDiags
where messages = filter (/= "") $ T.splitOn "\n\n" (T.strip output)
newDiags = [ parseDiagnostic msg | msg <- messages ]
parseDiagnostic :: Text -> Diagnostic
parseDiagnostic msg
| ((_ : (readT -> Just ln)
: (readT -> Just col)
: body : _) : _) <-
msg =~ ("^program.hs:([0-9]+):([0-9]+): ((.|\n)*)$" :: Text)
= (srcSpanFrom ln ln col (col + 1), CompileSuccess, T.unpack body)
| ((_ : (readT -> Just ln)
: (readT -> Just col1)
: (readT -> Just col2)
: body : _) : _) <-
msg =~ ("^program.hs:([0-9]+):([0-9]+)-([0-9]+): ((.|\n)*)$" :: Text)
= (srcSpanFrom ln ln col1 (col2 + 1), CompileSuccess, T.unpack body)
| ((_ : (readT -> Just ln1)
: (readT -> Just col1)
: (readT -> Just ln2)
: (readT -> Just col2)
: body : _) : _) <-
msg =~ ("^program.hs:[(]([0-9]+),([0-9]+)[)]" <>
"-[(]([0-9]+),([0-9]+)[)]: ((.|\n)*)$" :: Text)
= (srcSpanFrom ln1 ln2 col1 (col2 + 1), CompileSuccess, T.unpack body)
| otherwise = (noSrcSpan, CompileSuccess, T.unpack msg ++ " (no loc)")
where readT = readMaybe . T.unpack
srcSpanFrom :: Int -> Int -> Int -> Int -> SrcSpanInfo
srcSpanFrom l1 l2 c1 c2 =
SrcSpanInfo (SrcSpan "program.hs" l1 c1 l2 c2) []
copyOutputFrom :: MonadCompile m => FilePath -> m ()
copyOutputFrom target = gets compileStage >>= \stage -> case stage of
GenBase _ _ out syms -> liftIO $ do
rtsCode <- readUtf8 (target </> "rts.js")
libCode <- readUtf8 (target </> "lib.base.js")
outCode <- readUtf8 (target </> "out.base.js")
createDirectoryIfMissing True (takeDirectory out)
writeUtf8 out (rtsCode <> libCode <> outCode)
createDirectoryIfMissing True (takeDirectory syms)
copyFile (target </> "out.base.symbs") syms
UseBase out _ baseURL -> liftIO $ do
let prefix = T.pack $
"var el = document.createElement('script');" ++
"el.type = 'text/javascript';" ++
"el.src = '" ++ baseURL ++ "';" ++
"el.async = false;" ++
"el.onload = function() {"
let suffix = T.pack $
"window.h$mainZCZCMainzimain = h$mainZCZCMainzimain;" ++
"start();" ++
"start = function() {};" ++
"};" ++
"document.body.appendChild(el);"
libCode <- readUtf8 (target </> "lib.js")
outCode <- readUtf8 (target </> "out.js")
createDirectoryIfMissing True (takeDirectory out)
writeUtf8 out (prefix <> libCode <> outCode <> suffix)
FullBuild out -> liftIO $ do
rtsCode <- readUtf8 (target </> "rts.js")
libCode <- readUtf8 (target </> "lib.js")
outCode <- readUtf8 (target </> "out.js")
createDirectoryIfMissing True (takeDirectory out)
writeUtf8 out (rtsCode <> libCode <> outCode)
ErrorCheck -> return ()