on be72e86 help sign in
elk/Setup.hs
159 lines 6.5 kB history raw
1import Distribution.Simple
2import Distribution.Simple.LocalBuildInfo
3import Distribution.Simple.Setup
4import Distribution.PackageDescription
6import System.Cmd
7import System.Directory
8import System.FilePath
9import System.IO
10import System.Process
12import Control.Monad
14-- Part 1
15-- ------
16-- Add a build callout
17-- We need to build elm-doc and run it because that generates the file "docs.json" needs by Libraries.hs
18-- which is part of the elm library and executable
19-- Unfort. there seems to be no way to tell cabal that:
20-- (a) elm-doc generates docs.json, and
21-- (b) elm (library) depends on docs.json
22-- Therefore, we either use make instead (or a script), or hack around in cabal
24-- Part 2
25-- ------
26-- Add a post-build callout.
27-- We need to build the runtime.js after we've built elm (because we use elm to generate some of the JavaScript),
28-- but before cabal does the install file copy step
30-- Assumptions
31-- Elm.cabal expects the generated files to end up in dist/data
32rtsDir lbi = (buildDir lbi) </> ".." </> "data" -- git won't look in dist + cabal will clean it
33jsDir lbi = (buildDir lbi) </> ".." </> "js"
35-- The runtime is called:
36rts lbi = (rtsDir lbi) </> "elm-runtime.js"
38-- The json file is called:
40-- The elm-docs executable is called:
41elmDoc = "elm-doc"
42elm_doc lbi = (buildDir lbi) </> elmDoc </> elmDoc
44types lbi = (rtsDir lbi) </> "docs.json"
46-- buildDir with LocalBuildInfo points to "dist/build" (usually)
47elm lbi = (buildDir lbi) </> "elm" </> "elm"
49-- Care! This appears to be based on an unstable API
50-- See: http://www.haskell.org/cabal/release/cabal-latest/doc/API/Cabal/Distribution-Simple.html#2
53main :: IO ()
54main = defaultMainWithHooks simpleUserHooks { {-- buildHook = myBuild, --} postBuild = myPostBuildWithTypes }
57-- Build
59-- Not currently used. buildTypes is in 'myPostBuildWithTypes'
60-- If using this again, change postBuild to 'myPostBuild'
61-- Purpose is to make sure docs.json was built before elm exec was (as elm exec depended on it)
62-- This is no longer true and the code below seems to affect cabal's build dependencies
63myBuild :: PackageDescription -> LocalBuildInfo -> UserHooks -> BuildFlags -> IO ()
64myBuild pd lbi uh bf = do
65 putStrLn $ "Custom build step started: compile " ++ elmDoc
66 withExe pd (\x -> putStrLn (exeName x))
67 buildHook simpleUserHooks (filterExe elmDoc pd) (filterLBI elmDoc lbi) uh bf
68 putStrLn "Custom build step started: build docs.json"
69 buildTypes lbi -- see note(1) below
70 putStrLn "Custom build step started: compile everything"
71 buildHook simpleUserHooks pd lbi uh bf
73-- note(1): We use to include docs.json directly into LoadLibraries at compile time
74-- If docs.json is used in other (template) haskell files, they should be copied
75-- and compiled in a separate directory (eg, dist/copiedSrc).
76-- This is to make sure they are re-compiled on docs.json changes.
77-- Copying is a better solution than 'touch'ing the source files
78-- (touch is non-portable and confusing wrt RCS).
80-- In the PackageDescription, the list of stuff to build is held in library (in a Maybe)
81-- and the executables list. We want a PackageDescription that only mentions the executable 'name'
82filterExe name pd = pd {
83 library = Nothing,
84 executables = filter (\x -> (exeName x == name)) (executables pd)
85 }
87-- It's not enough to fix the PackageDescription, we also have to fix the LocalBuildInfo.
88-- This includes the component build order (data ComponentName) which is horribly internal.
89filterLBI name lbi = lbi {
90 libraryConfig = Nothing,
91 compBuildOrder = [CExeName name],
92 executableConfigs = filter (\a -> (fst a == name)) (executableConfigs lbi)
93 }
95buildTypes lbi = do
96 createDirectoryIfMissing False (rtsDir lbi) -- dist should already exist
97 files <- getFiles ".elm" "libraries"
98 system (elm_doc lbi ++ " " ++ unwords files ++ " > " ++ (types lbi))
99 putStrLn $ "Custom build step completed: " ++ elmDoc
102-- Post Build
104myPostBuildWithTypes :: Args -> BuildFlags -> PackageDescription -> LocalBuildInfo -> IO ()
105myPostBuildWithTypes as bfs pd lbi = do
106 putStrLn "Custom build step started: build docs.json"
107 buildTypes lbi -- see note(1) below
108 myPostBuild as bfs pd lbi
110myPostBuild :: Args -> BuildFlags -> PackageDescription -> LocalBuildInfo -> IO ()
111myPostBuild as bfs pd lbi = do
112 putStrLn "Custom post build step started: build elm-runtime.js"
113 buildRuntime lbi
114 postBuild simpleUserHooks as bfs pd lbi
116getFiles ext dir = do
117 contents <- map (dir </>) `fmap` getDirectoryContents dir
118 let files = filter (\f -> takeExtension f == ext) contents
119 dirs = filter (not . hasExtension) contents
120 filess <- mapM (getFiles ext) dirs
121 return (files ++ concat filess)
123appendJS lbi file = do
124 putStrLn (dropExtension file)
125 str <- readFile file
126 length str `seq` return ()
127 appendFile (rts lbi) str
129appendElm lbi file = do
130 jsFile <- runElm lbi file
131 appendJS lbi jsFile
133-- replace 'system' call with 'runProcess' which handles args better and allows env variable
134-- "Elm_datadir" which is used by LoadLibraries to find docs.json
135runElm :: LocalBuildInfo -> String -> IO FilePath
136runElm lbi file = do
137 rts_c <- canonicalizePath (rts lbi) -- dist/data/elm-runtime.js
138 let js = jsDir lbi -- dist/js
139 let j = dropFileName (js </> file) -- dist/js/libraries/
140 createDirectoryIfMissing True j -- must do before any canonicalization
141 out_c <- canonicalizePath js -- dist/js (root folder)
142 elm_c <- canonicalizePath (elm lbi) -- dist/build/elm/elm
143 rtd_c <- canonicalizePath (rtsDir lbi) -- dist/data (for docs.json)
144 handle <- runProcess elm_c ["--only-js", "--no-prelude", "--runtime="++rts_c, "--output-directory="++out_c, file]
145 Nothing (Just [("Elm_datadir", rtd_c)]) Nothing Nothing Nothing
146 waitForProcess handle
147 return $ j </> replaceExtension (takeFileName file) ".js"
150buildRuntime lbi = do
151 createDirectoryIfMissing False (rtsDir lbi) -- dist should already exist
152 writeFile (rts lbi) "Elm = {}; Elm.Native = {}; Elm.Native.Graphics = {};\n\
153 \Elm.Graphics = {}; ElmRuntime = {}; ElmRuntime.Render = {}\n"
154 mapM_ (appendJS lbi) =<< getFiles ".js" "libraries"
155 mapM_ (appendElm lbi) =<< getFiles ".elm" "libraries"
156 mapM_ (appendJS lbi) =<< getFiles ".js" "runtime"
157 putStrLn "\n+------------------------------------------+\
158 \\n| Success building runtime and libraries! |\
159 \\n+------------------------------------------+\n"