-
Notifications
You must be signed in to change notification settings - Fork 38
/
Shake.hs
229 lines (195 loc) · 8.04 KB
/
Shake.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
{-#
LANGUAGE
MultiParamTypeClasses,
GeneralizedNewtypeDeriving,
StandaloneDeriving,
DeriveDataTypeable,
FlexibleInstances,
ViewPatterns
#-}
import Prelude hiding ((*>))
import Control.Exception
import Control.Monad
import Control.DeepSeq
import Data.Typeable
import Data.Functor
import Data.List
import Data.Hashable
import Data.Binary
import Development.Shake hiding (command_)
import qualified Development.Shake as S (command_)
import Development.Shake.FilePath
import System.FilePath.Posix hiding ((</>), doesDirectoryExist)
import System.Posix.Directory
import System.Posix.Env hiding (getEnv)
import System.Directory hiding (doesDirectoryExist, doesFileExist)
import System.Environment (getArgs)
import System.Exit
import System.IO
import Distribution.Verbosity
import Distribution.Text (simpleParse)
import Distribution.Compiler
import Distribution.Package hiding (pkgName)
import Distribution.PackageDescription
import Distribution.PackageDescription.Parse
import Distribution.PackageDescription.PrettyPrint
import Distribution.PackageDescription.Configuration
import Text.Parsec
import Text.Parsec.String
import Data.Version
llvmVersion = "3.5.2"
llvmDir = "out" </> ("llvm-" ++ llvmVersion)
pkgName = "llvm-general"
wipedir :: FilePath -> Action ()
wipedir d = do
present <- doesDirectoryExist d
when present $ command_ [] "rm" [ "-r", d ]
mkdir :: FilePath -> Action ()
mkdir d = command_ [] "mkdir" [ "-p", d ]
untar :: FilePath -> FilePath -> Action ()
untar d tb = command_ [] "tar" [ "xCf", d, tb ]
needRecursive :: FilePath -> Action ()
needRecursive p = do
subdirs <- getDirectoryDirs p
forM [ s | s <- subdirs, s /= ".git" ] $ needRecursive . (p </>)
files <- getDirectoryFiles p ["*"]
need $ map (p </>) files
touch f = do
let d = dropFileName f
dExists <- doesDirectoryExist d
unless dExists $ command_ [] "mkdir" ["-p", d]
command_ [] "touch" [ f ]
command_ :: [CmdOption] -> String -> [String] -> Action ()
command_ opts c args = foldr x (S.command_ opts c args) opts
where x (Cwd p') rest = do
p <- liftIO $ canonicalizePath p'
putQuiet $ "Entering directory `" ++ p ++ "'"
rest
putQuiet $ "Leaving directory `" ++ p ++ "'"
x _ rest = rest
newtype BuildRoot = BuildRoot () deriving (Eq, Ord, Read, Show, Binary, Hashable, NFData, Typeable)
newtype LLVMConfig = LLVMConfig () deriving (Eq, Ord, Read, Show, Binary, Hashable, NFData, Typeable)
newtype CabalVersion = CabalVersion String deriving (Eq, Ord, Read, Show, Binary, Hashable, NFData, Typeable)
stamp :: ((String, String)) -> String
stamp (stage, pkg) = pkg </> "dist/shake/stamps" </> stage
parseStamp :: String -> (String, String)
parseStamp s = (takeFileName s, takeDirectory1 s)
needStamps :: [(String,String)] -> Action ()
needStamps ls = need (map stamp ls)
main = shake shakeOptions {
shakeVersion = "2",
shakeVerbosity = Normal
} $ do
action $ do
liftIO $ hSetBuffering stdout NoBuffering
liftIO $ hSetBuffering stderr NoBuffering
getBuildRoot <- addOracle $ \(BuildRoot _) -> do
liftIO $ getWorkingDirectory
getCabalVersion <- addOracle $ \(CabalVersion pkg) -> do
liftIO $ liftM (showVersion . pkgVersion . package . packageDescription) $ readPackageDescription silent (pkg </> pkg ++ ".cabal")
getLlvmConfig <- addOracle $ \(LLVMConfig _) -> do
Exit exitCode <- command [] "which" ["llvm-config"]
let x = exitCode /= ExitSuccess
when x $ do
done <- doesFileExist (llvmDir </> "install/bin/llvm-config")
unless done $ need [ (llvmDir </> "install/bin/llvm-config") ]
return x
ghPagesLock <- newResource "gh-pages lock" 1
action $ do
args <- liftIO getArgs
need (if null args then [ stamp ("tested", "llvm-general") ] else args)
phony "env" $ do
command_ [] "env" []
let shared = [ "--enable-shared" | True ]
let ghPages = "out" </> "gh-pages"
sandbox = "out" </> "sandbox"
sandboxConfigFile = "out" </> "cabal.sandbox.config"
let getCabalStep = do
buildRoot <- getBuildRoot (BuildRoot ())
ownLLVM <- getLlvmConfig (LLVMConfig ())
pathOpt <- if ownLLVM
then do
opt <- addPath [buildRoot </> llvmDir </> "install/bin"] []
return [opt]
else
return []
return $ \pkg args -> command_ ([Cwd pkg] ++ pathOpt) "cabal" $ [ "--sandbox-config-file=" ++ (buildRoot </> sandboxConfigFile) ] ++ args
let localPackageDeps "llvm-general" = [ "llvm-general-pure" ]
localPackageDeps _ = []
allPkgs = [ "llvm-general-pure", "llvm-general"]
needStage stage pkgs = needStamps [ (stage, pkg) | pkg <- pkgs ]
let cabal args = do
command_ [] "cabal" $ [ "--sandbox-config-file=" ++ sandboxConfigFile ] ++ args
let ensureSandbox = do
present <- doesFileExist sandboxConfigFile
unless present $ do
cabal [ "sandbox", "init", "--sandbox=" ++ sandbox ]
cabal $ [ "sandbox", "add-source" ] ++ allPkgs
phony "build" $ needStage "built" allPkgs
phony "test" $ needStage "tested" allPkgs
phony "doc" $ needStage "documented" allPkgs
phony "pubdoc" $ needStage "docPublished" allPkgs
stamp ("*","*") *> \(stmp@(parseStamp -> (stage, pkg))) -> (>> touch stmp) $ do
ensureSandbox
cabalStep <- getCabalStep
tag <- getCabalVersion (CabalVersion pkg)
buildRoot <- getBuildRoot (BuildRoot ())
case stage of
"configured" -> do
needStage "installed" (localPackageDeps pkg)
need [ pkg </> "Setup.hs" ]
need [ pkg </> pkg ++ ".cabal" ]
cabalStep pkg $ [ "install", "--only-dependencies", "--enable-tests" ] ++ shared
cabalStep pkg $ [ "configure", "--enable-tests", "-fshared-llvm" ] ++ shared
"built" -> do
needStage "configured" [pkg]
needRecursive $ pkg </> "src"
needRecursive $ pkg </> "test"
cabalStep pkg [ "build" ]
"installed" -> do
needStage "built" [pkg]
cabalStep pkg $ [ "install", "--reinstall", "--force-reinstalls" ] ++ shared
"tested" -> do
needStage "built" [pkg]
cabalStep pkg [ "test" ]
"documented" -> do
needStage "built" [pkg]
needStage "documented" (localPackageDeps pkg)
cabalStep pkg $ [
"haddock",
"--html-location=http://hackage.haskell.org/packages/archive/$pkg/$version/doc/html"
] ++ [
"--haddock-options=--read-interface="
++ ("/llvm-general" </> tag </> "doc/html" </> dpkg)
++ ","
++ (buildRoot </> dpkg </> "dist/doc/html" </> dpkg </> dpkg <.> "haddock")
| dpkg <- localPackageDeps pkg
]
"docPublished" -> do
needStage "documented" [pkg]
withResource ghPagesLock 1 $ do
ghPagesExists <- doesDirectoryExist ghPages
unless ghPagesExists $ command_ [Cwd "out"] "git" ["clone", buildRoot, "-b", "gh-pages", "gh-pages"]
command_ [] "rm" [ "-rf", ghPages </> tag </> "doc" </> "html" </> pkg ]
command_ [] "mkdir" [ "-p", ghPages </> tag </> "doc" </> "html" ]
command_ [] "cp" [ "-r", pkg </> "dist/doc/html" </> pkg, ghPages </> tag </> "doc" </> "html" ]
command_ [Cwd ghPages] "git" [ "add", "-A", "." ]
command_ [Cwd ghPages] "git" [ "commit", "-m", show ("update " ++ tag ++ " " ++ pkg ++ " doc") ]
command_ [Cwd ghPages] "git" [ "push" ]
llvmDir </> "install/bin/llvm-config" *> \out -> do
[tarball] <- getDirectoryFiles "." [ "downloads/llvm-" ++ llvmVersion ++ ".src.tar.*" ]
buildRoot <- askOracle (BuildRoot ())
need [ tarball ]
let buildDir = llvmDir </> "build"
wipedir buildDir
mkdir buildDir
untar buildDir tarball
(".":"..":[srcDir']) <- liftM sort $ liftIO $ System.Directory.getDirectoryContents buildDir
let srcDir = buildDir </> srcDir'
command_ [Cwd srcDir] "sh" [
"./configure",
"--prefix=" ++ buildRoot </> llvmDir </> "install",
"--enable-shared"
]
command_ [Cwd srcDir] "make" [ "-j", "8", "install" ]
wipedir buildDir