Skip to content

Commit cb89b56

Browse files
committed
Add options to codeworld-compiler to do incremental building.
This is a step toward #540
1 parent be6aa24 commit cb89b56

3 files changed

Lines changed: 59 additions & 28 deletions

File tree

codeworld-compiler/exec/Main.hs

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,51 +18,69 @@
1818
-}
1919

2020
import Compile
21+
import Control.Applicative (optional)
2122
import Control.Monad (join)
2223
import Data.Monoid ((<>))
2324
import Options.Applicative
2425
import System.Environment
2526
import System.Directory
2627
import System.IO
2728

28-
data Options = Options { source :: String,
29-
output :: String,
30-
err :: String,
31-
mode :: String
29+
data Options = Options { source :: String,
30+
output :: String,
31+
err :: String,
32+
mode :: String,
33+
baseModule :: Maybe String,
34+
baseSymbols :: Maybe String
3235
} deriving (Show)
3336

3437
main = execParser opts >>= runWithOptions
3538
where
36-
parser = Options <$> argument str ( metavar "SourceFile"
37-
<> help "Location of source file" )
38-
<*> strOption ( long "output"
39-
<> short 'o'
40-
<> metavar "OutputFile"
41-
<> help "Location of output file" )
42-
<*> strOption ( long "error"
43-
<> short 'e'
44-
<> metavar "ErrorFile"
45-
<> help "Location of error file" )
46-
<*> strOption ( long "mode"
47-
<> short 'm'
48-
<> metavar "BuildMode"
49-
<> help "Enter the mode of compilation" )
39+
parser = Options <$> argument str ( metavar "SourceFile"
40+
<> help "Location of source file" )
41+
<*> strOption ( long "output"
42+
<> short 'o'
43+
<> metavar "OutputFile"
44+
<> help "Location of output file" )
45+
<*> strOption ( long "error"
46+
<> short 'e'
47+
<> metavar "ErrorFile"
48+
<> help "Location of error file" )
49+
<*> strOption ( long "mode"
50+
<> short 'm'
51+
<> metavar "BuildMode"
52+
<> help "Enter the mode of compilation" )
53+
<*> optional (strOption ( long "base-module"
54+
<> short 'b'
55+
<> metavar "BaseModule"
56+
<> help "Base module to build dependencies" ))
57+
<*> optional (strOption ( long "base-syms"
58+
<> short 's'
59+
<> metavar "BaseSyms"
60+
<> help "Location of base symbol file" ))
5061
opts = info parser mempty
5162

63+
optionsToStage :: Options -> Stage
64+
optionsToStage Options{..} = case (baseModule, baseSymbols) of
65+
(Just mod, Just syms) -> GenBase mod syms
66+
(Nothing, Just syms) -> UseBase syms
67+
(Nothing, Nothing) -> FullBuild
68+
_ -> error "--base-module must be used with --base-syms"
69+
5270
runWithOptions :: Options -> IO ()
53-
runWithOptions Options{..} = do
71+
runWithOptions opts@Options{..} = do
5472
fileExists <- doesFileExist source
5573
if fileExists
5674
then do
57-
compileOutput <- extractSource source output err mode
75+
compileOutput <- extractSource (optionsToStage opts) source output err mode
5876
return ()
5977
else
6078
putStrLn $ "File not found:" ++ (show source)
6179

62-
extractSource :: String -> String -> String -> String -> IO Bool
63-
extractSource source out err mode = do
64-
res <- compileSource source out err mode
65-
case res of
80+
extractSource :: Stage -> String -> String -> String -> String -> IO Bool
81+
extractSource stage source out err mode = do
82+
res <- compileSource stage source out err mode
83+
case res of
6684
True -> return True
6785
False -> do
6886
errMsg <- readFile err

codeworld-compiler/src/Compile.hs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
limitations under the License.
1818
-}
1919

20-
module Compile ( compileSource ) where
20+
module Compile ( compileSource, Stage(..) ) where
2121

2222
import Control.Concurrent
2323
import Control.Monad
@@ -35,8 +35,12 @@ import Text.Regex.TDFA
3535

3636
import ParseCode
3737

38-
compileSource :: FilePath -> FilePath -> FilePath -> String -> IO Bool
39-
compileSource src out err mode = checkDangerousSource src >>= \case
38+
data Stage = FullBuild
39+
| GenBase String FilePath
40+
| UseBase FilePath
41+
42+
compileSource :: Stage -> FilePath -> FilePath -> FilePath -> String -> IO Bool
43+
compileSource stage src out err mode = checkDangerousSource src >>= \case
4044
True -> do
4145
B.writeFile err
4246
"Sorry, but your program refers to forbidden language features."
@@ -49,7 +53,11 @@ compileSource src out err mode = checkDangerousSource src >>= \case
4953
baseArgs <- case mode of
5054
"haskell" -> return haskellCompatibleBuildArgs
5155
"codeworld" -> standardBuildArgs <$> hasOldStyleMain src
52-
let ghcjsArgs = baseArgs ++ [ "program.hs" ]
56+
let linkArgs = case stage of
57+
FullBuild -> []
58+
GenBase name _ -> ["-generate-base", name]
59+
UseBase path -> ["-use-base", path]
60+
let ghcjsArgs = baseArgs ++ linkArgs ++ [ "program.hs" ]
5361
runCompiler tmpdir userCompileMicros ghcjsArgs >>= \case
5462
Nothing -> return False
5563
Just output -> do
@@ -67,6 +75,10 @@ compileSource src out err mode = checkDangerousSource src >>= \case
6775
libCode <- B.readFile $ target </> "lib.js"
6876
outCode <- B.readFile $ target </> "out.js"
6977
B.writeFile out (rtsCode <> libCode <> outCode)
78+
case stage of
79+
GenBase _ sympath ->
80+
copyFile (target </> "out.base.symbs") sympath
81+
_ -> return ()
7082
return hasTarget
7183

7284
userCompileMicros :: Int

codeworld-server/src/Main.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,7 @@ compileIfNeeded mode programId = do
380380
if hasResult
381381
then return hasTarget
382382
else compileSource
383+
FullBuild
383384
(buildRootDir mode </> sourceFile programId)
384385
(buildRootDir mode </> targetFile programId)
385386
(buildRootDir mode </> resultFile programId)

0 commit comments

Comments
 (0)