on b2ed20c help sign in
stackage/Stackage/Build.hs
106 lines 4.2 kB history raw
1module Stackage.Build
2 ( build
3 , defaultBuildSettings
4 , BuildSettings (..)
5 ) where
7import Control.Monad (unless)
8import Prelude hiding (pi)
9import Stackage.CheckCabalVersion (checkCabalVersion)
10import Stackage.Config
11import Stackage.InstallInfo
12import Stackage.Types
13import Stackage.Util
14import System.Exit (ExitCode (ExitSuccess), exitWith)
15import System.IO (BufferMode (NoBuffering),
16 IOMode (WriteMode), hPutStrLn,
17 hSetBuffering, withBinaryFile)
18import System.Process (rawSystem, runProcess,
19 waitForProcess)
21defaultBuildSettings :: Maybe Int -- ^ argument to -j
22 -> GhcMajorVersion
23 -> BuildSettings
24defaultBuildSettings cores version = BuildSettings
25 { sandboxRoot = "sandbox"
26 , expectedFailuresBuild = defaultExpectedFailures version
27 , extraArgs = \bs -> "-fnetwork23" :
28 case bs of
29 BSTest -> []
30 _ ->
31 case cores of
32 Nothing -> ["-j"]
33 Just 1 -> []
34 Just j -> ["-j" ++ show j]
35 , testWorkerThreads = 4
36 , buildDocs = True
37 , tarballDir = "patching/tarballs"
38 , cabalFileDir = Nothing
39 }
41build :: BuildSettings -> BuildPlan -> IO ()
42build settings' bp = do
43 libVersion <- checkCabalVersion
45 putStrLn "Wiping out old sandbox folder"
46 rm_r $ sandboxRoot settings'
47 rm_r "logs"
48 settings <- fixBuildSettings settings'
50 putStrLn "Creating new package database"
51 ec1 <- rawSystem "ghc-pkg" ["init", packageDir settings]
52 unless (ec1 == ExitSuccess) $ do
53 putStrLn "Unable to create package database via ghc-pkg init"
54 exitWith ec1
56 menv <- fmap Just $ getModifiedEnv settings
57 let runCabal args handle = runProcess "cabal" args Nothing menv Nothing (Just handle) (Just handle)
59 -- First install build tools so they can be used below.
60 let installBuildTool tool = do
61 let toolsDir = packageDir settings ++ "-tools"
62 rm_r toolsDir
63 ecInit <- rawSystem "ghc-pkg" ["init", toolsDir]
64 unless (ecInit == ExitSuccess) $ do
65 putStrLn "Unable to create package database via ghc-pkg init"
66 exitWith ecInit
68 putStrLn $ "Installing build tool: " ++ tool
69 ec <- withBinaryFile "build-tools.log" WriteMode $ \handle -> do
70 hSetBuffering handle NoBuffering
72 let args = addCabalArgs settings BSTools
73 $ "install"
74 : ("--cabal-lib-version=" ++ libVersion)
75 : "--build-log=logs-tools/$pkg.log"
76 : [tool]
77 hPutStrLn handle ("cabal " ++ unwords (map (\s -> "'" ++ s ++ "'") args))
78 ph <- runCabal args handle
79 waitForProcess ph
80 unless (ec == ExitSuccess) $ do
81 putStrLn $ concat
82 [ "Building of "
83 , tool
84 , " failed, please see build-tools.log"
85 ]
86 exitWith ec
87 putStrLn $ tool ++ " built"
88 rm_r toolsDir
89 mapM_ installBuildTool $ bpTools bp
91 putStrLn "Beginning Stackage build"
92 ph <- withBinaryFile "build.log" WriteMode $ \handle -> do
93 packageList <- mapM (replaceTarball $ tarballDir settings) $ bpPackageList bp
94 let args = addCabalArgs settings BSBuild
95 $ "install"
96 : ("--cabal-lib-version=" ++ libVersion)
97 : "--build-log=logs/$pkg.log"
98 : "--max-backjumps=-1"
99 : "--reorder-goals"
100 : packageList
101 hPutStrLn handle ("cabal " ++ unwords (map (\s -> "'" ++ s ++ "'") args))
102 runCabal args handle
103 ec <- waitForProcess ph
104 unless (ec == ExitSuccess) $ do
105 putStrLn "Build failed, please see build.log"
106 exitWith ec