on master help sign in
Simplify setup code, trim lots of extra stuff.
commit33d4934e696e9d56328056af2b168c994c7e9906 authorEvan Czaplicki <info@elm-lang.org> dateMon 25 Aug 2014, 18:38 (12 years ago) parente35caf6
1 file changed+61−94
Setup.hs+61−94
Setup.hs+61−94
@@ -1,55 +1,40 @@
1+{-# OPTIONS_GHC -W #-}
12import Distribution.Simple
23import Distribution.Simple.LocalBuildInfo
34import Distribution.Simple.Setup
45import Distribution.PackageDescription
56
6−import System.Cmd
77import System.Directory
88import System.FilePath
99import System.IO
1010import System.Process
1111
12−import Control.Monad
1312import qualified Data.Binary as Binary
1413import qualified Data.ByteString.Lazy as BS
1514import qualified Data.List as List
1615
17−-- Part 1
18−-- ------
19−-- Add a build callout
20−-- We need to build elm-doc and run it because that generates the file "docs.json"
21−-- needs by Libraries.hs which is part of the elm library and executable
22−-- Unfort. there seems to be no way to tell cabal that:
23−-- (a) elm-doc generates docs.json, and
24−-- (b) elm (library) depends on docs.json
25−-- Therefore, we either use make instead (or a script), or hack around in cabal
26−
27−-- Part 2
28−-- ------
29−-- Add a post-build callout.
30−-- We need to build the runtime.js after we've built elm (because we use elm to generate some of the JavaScript),
31−-- but before cabal does the install file copy step
32−
33−-- Assumptions
34−-- Elm.cabal expects the generated files to end up in dist/data
35−-- git won't look in dist + cabal will clean it
36−rtsDir :: LocalBuildInfo -> FilePath
37−rtsDir lbi = "data"
38−
39−tempDir :: LocalBuildInfo -> FilePath
40−tempDir lbi = "temp"
16+-- OVERVIEW
17+-- --------
18+-- Add a post-build callout that compiles the standard libraries with the new
19+-- compiler before cabal copies static files to actually install things.
20+
21+assetsDir :: FilePath
22+assetsDir = "data"
23+
24+tempDir :: FilePath
25+tempDir = "temp"
4126
4227-- The runtime is called:
43−rts :: LocalBuildInfo -> FilePath
44−rts lbi = rtsDir lbi </> "elm-runtime.js"
28+runtime :: FilePath
29+runtime = assetsDir </> "elm-runtime.js"
4530
4631-- The runtime is called:
47−docs :: LocalBuildInfo -> FilePath
48−docs lbi = rtsDir lbi </> "docs.json"
32+docs :: FilePath
33+docs = assetsDir </> "docs.json"
4934
5035-- The interfaces for the Standard Libraries live in:
51−interfaces :: LocalBuildInfo -> FilePath
52−interfaces lbi = rtsDir lbi </> "interfaces.data"
36+interfaces :: FilePath
37+interfaces = assetsDir </> "interfaces.data"
5338
5439elm :: LocalBuildInfo -> FilePath
5540elm lbi = buildDir lbi </> "elm" </> "elm"
@@ -65,63 +50,47 @@ main :: IO ()
6550main = defaultMainWithHooks simpleUserHooks { postBuild = myPostBuild }
6651
6752
68−-- Build
69−
70−-- note(1): We use to include docs.json directly into LoadLibraries at compile time
71−-- If docs.json is used in other (template) haskell files, they should be copied
72−-- and compiled in a separate directory (eg, dist/copiedSrc).
73−-- This is to make sure they are re-compiled on docs.json changes.
74−-- Copying is a better solution than 'touch'ing the source files
75−-- (touch is non-portable and confusing wrt RCS).
76−
77−-- In the PackageDescription, the list of stuff to build is held in library
78−-- (in a Maybe) and the executables list. We want a PackageDescription that
79−-- only mentions the executable 'name'
80−filterExe :: String -> PackageDescription -> PackageDescription
81−filterExe name pd = pd {
82− library = Nothing,
83− executables = filter (\x -> (exeName x == name)) (executables pd)
84− }
85−
8653
8754-- Post Build
8855
8956myPostBuild :: Args -> BuildFlags -> PackageDescription -> LocalBuildInfo -> IO ()
90−myPostBuild as bfs pd lbi = do
57+myPostBuild args flags packageDescription lbi = do
9158 putStrLn "Custom build step: compiling standard libraries"
9259 (elmos, elmis) <- compileLibraries lbi
9360 putStrLn "Custom build step: build interfaces.data"
94− buildInterfaces lbi elmis
61+ buildInterfaces elmis
9562 putStrLn "Custom build step: build elm-runtime.js"
96− buildRuntime lbi elmos
63+ buildRuntime elmos
9764 putStrLn "Custom build step: build docs.json"
98− buildDocs lbi
99− removeDirectoryRecursive (tempDir lbi)
65+ buildDocs
66+ removeDirectoryRecursive tempDir
10067 removeDirectoryRecursive ("libraries" </> "docs")
101− postBuild simpleUserHooks as bfs pd lbi
68+ postBuild simpleUserHooks args flags packageDescription lbi
10269
10370
10471compileLibraries lbi = do
105− let temp = tempDir lbi -- temp
106− rts = rtsDir lbi -- data
107− createDirectoryIfMissing True temp
108− createDirectoryIfMissing True rts
109− out_c <- canonicalizePath temp -- temp (root folder)
72+ createDirectoryIfMissing True tempDir
73+ createDirectoryIfMissing True assetsDir
74+ out_c <- canonicalizePath tempDir -- temp (root folder)
11075 elm_c <- canonicalizePath (elm lbi) -- dist/build/elm/elm
11176 doc_c <- canonicalizePath (document lbi) -- dist/build/elm-doc/elm-doc
112− rtd_c <- canonicalizePath rts -- data
77+ rtd_c <- canonicalizePath assetsDir -- data
11378
11479 let make file = do
115− -- replace 'system' call with 'runProcess' which handles args better
116− -- and allows env variable "Elm_datadir" which is used by LoadLibraries
117− -- to find docs.json
118− let args = [ "--only-js", "--make", "--no-prelude"
119− , "--cache-dir="++out_c, "--build-dir="++out_c, file ]
120− arg = Just [("Elm_datadir", rtd_c)]
80+ let args =
81+ [ "--only-js"
82+ , "--make"
83+ , "--no-prelude"
84+ , "--cache-dir=" ++ out_c
85+ , "--build-dir=" ++ out_c
86+ , file
87+ ]
88+ let arg = Just [("Elm_datadir", rtd_c)]
12189 handle <- runProcess elm_c args Nothing arg Nothing Nothing Nothing
122− exitCode <- waitForProcess handle
90+ _exitCode <- waitForProcess handle
12391 return ( out_c </> replaceExtension file "elmo"
124− , out_c </> replaceExtension file "elmi")
92+ , out_c </> replaceExtension file "elmi"
93+ )
12594
12695 setCurrentDirectory "libraries"
12796 paths <- getFiles ".elm" "."
@@ -130,11 +99,10 @@ compileLibraries lbi = do
13099 setCurrentDirectory ".."
131100 return files
132101
133−buildInterfaces :: LocalBuildInfo -> [FilePath] -> IO ()
134−buildInterfaces lbi elmis = do
135− createDirectoryIfMissing True (rtsDir lbi)
136− let ifaces = interfaces lbi
137− ifaceHandle <- openBinaryFile ifaces WriteMode
102+buildInterfaces :: [FilePath] -> IO ()
103+buildInterfaces elmis = do
104+ createDirectoryIfMissing True assetsDir
105+ ifaceHandle <- openBinaryFile interfaces WriteMode
138106 BS.hPut ifaceHandle (Binary.encode (length elmis))
139107 let append file = do
140108 handle <- openBinaryFile file ReadMode
@@ -144,27 +112,26 @@ buildInterfaces lbi elmis = do
144112 mapM_ append elmis
145113 hClose ifaceHandle
146114
147−buildRuntime :: LocalBuildInfo -> [FilePath] -> IO ()
148−buildRuntime lbi elmos = do
149− createDirectoryIfMissing True (rtsDir lbi)
150− let rts' = rts lbi
151− writeFile rts' "'use strict';\n\
152− \var Elm = {}; Elm.Native = {}; Elm.Native.Graphics = {};\n\
153− \var ElmRuntime = {}; ElmRuntime.Render = {};\n"
154− mapM_ (appendTo rts') =<< getFiles ".js" "libraries"
155− mapM_ (appendTo rts') elmos
156− mapM_ (appendTo rts') =<< getFiles ".js" "runtime"
157−
158−buildDocs :: LocalBuildInfo -> IO ()
159−buildDocs lbi = do
160− createDirectoryIfMissing True (rtsDir lbi)
161− let docs' = docs lbi
162− writeFile docs' "[\n"
115+buildRuntime :: [FilePath] -> IO ()
116+buildRuntime elmos = do
117+ createDirectoryIfMissing True assetsDir
118+ writeFile runtime
119+ "'use strict';\n\
120+ \var Elm = {}; Elm.Native = {}; Elm.Native.Graphics = {};\n\
121+ \var ElmRuntime = {}; ElmRuntime.Render = {};\n"
122+ mapM_ (appendTo runtime) =<< getFiles ".js" "libraries"
123+ mapM_ (appendTo runtime) elmos
124+ mapM_ (appendTo runtime) =<< getFiles ".js" "runtime"
125+
126+buildDocs :: IO ()
127+buildDocs = do
128+ createDirectoryIfMissing True assetsDir
129+ writeFile docs "[\n"
163130 json <- getFiles ".json" ("libraries" </> "docs")
164− let appends = map (appendTo docs') json
165− addCommas = List.intersperse (appendFile docs' ",\n")
131+ let appends = map (appendTo docs) json
132+ addCommas = List.intersperse (appendFile docs ",\n")
166133 sequence_ (addCommas appends)
167− appendFile docs' "\n]"
134+ appendFile docs "\n]"
168135
169136getFiles ext dir = do
170137 contents <- map (dir </>) `fmap` getDirectoryContents dir