on 77fa7fa help sign in
stackage/Stackage/Build.hs
115 lines 4.6 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.ModuleNameConflict
13import Stackage.Types
14import Stackage.Util
15import System.Exit (ExitCode (ExitSuccess), exitWith)
16import System.IO (BufferMode (NoBuffering),
17 IOMode (WriteMode), hPutStrLn,
18 hSetBuffering, withBinaryFile)
19import qualified System.IO.UTF8
20import System.Process (rawSystem, runProcess,
21 waitForProcess)
22import qualified Data.ByteString.Lazy.Char8 as L8
24defaultBuildSettings :: Maybe Int -- ^ argument to -j
25 -> GhcMajorVersion
26 -> BuildSettings
27defaultBuildSettings cores version = BuildSettings
28 { sandboxRoot = "sandbox"
29 , extraArgs = \bs -> "-fnetwork23" : "-fhttps" :
30 case bs of
31 BSTest -> []
32 _ ->
33 case cores of
34 Nothing -> ["-j"]
35 Just 1 -> []
36 Just j -> ["-j" ++ show j]
37 , testWorkerThreads = 4
38 , buildDocs = True
39 , tarballDir = "patching/tarballs"
40 , cabalFileDir = Nothing
41 , underlayPackageDirs = []
42 }
44build :: BuildSettings -> BuildPlan -> IO ()
45build settings' bp = do
46 libVersion <- checkCabalVersion
48 putStrLn "Wiping out old sandbox folder"
49 rm_r $ sandboxRoot settings'
50 rm_r "logs"
51 settings <- fixBuildSettings settings'
53 putStrLn "Creating new package database"
54 ec1 <- rawSystem "ghc-pkg" ["init", packageDir settings]
55 unless (ec1 == ExitSuccess) $ do
56 putStrLn "Unable to create package database via ghc-pkg init"
57 exitWith ec1
59 menv <- fmap Just $ getModifiedEnv settings
60 let runCabal args handle = runProcess "cabal" args Nothing menv Nothing (Just handle) (Just handle)
62 -- First install build tools so they can be used below.
63 let installBuildTool tool = do
64 let toolsDir = packageDir settings ++ "-tools"
65 rm_r toolsDir
66 ecInit <- rawSystem "ghc-pkg" ["init", toolsDir]
67 unless (ecInit == ExitSuccess) $ do
68 putStrLn "Unable to create package database via ghc-pkg init"
69 exitWith ecInit
71 putStrLn $ "Installing build tool: " ++ tool
72 ec <- withBinaryFile "build-tools.log" WriteMode $ \handle -> do
73 hSetBuffering handle NoBuffering
75 let args = addCabalArgs settings BSTools
76 $ "install"
77 : ("--cabal-lib-version=" ++ libVersion)
78 : "--build-log=logs-tools/$pkg.log"
79 : [tool]
80 hPutStrLn handle ("cabal " ++ unwords (map (\s -> "'" ++ s ++ "'") args))
81 ph <- runCabal args handle
82 waitForProcess ph
83 unless (ec == ExitSuccess) $ do
84 putStrLn $ concat
85 [ "Building of "
86 , tool
87 , " failed, please see build-tools.log"
88 ]
89 exitWith ec
90 putStrLn $ tool ++ " built"
91 rm_r toolsDir
92 mapM_ installBuildTool $ bpTools bp
94 putStrLn "Beginning Stackage build"
95 ph <- withBinaryFile "build.log" WriteMode $ \handle -> do
96 packageList <- mapM (replaceTarball $ tarballDir settings) $ bpPackageList bp
97 let args = addCabalArgs settings BSBuild
98 $ "install"
99 : ("--cabal-lib-version=" ++ libVersion)
100 : "--build-log=logs/$pkg.log"
101 : "--max-backjumps=-1"
102 : "--reorder-goals"
103 : packageList
104 hPutStrLn handle ("cabal " ++ unwords (map (\s -> "'" ++ s ++ "'") args))
105 runCabal args handle
106 ec <- waitForProcess ph
107 unless (ec == ExitSuccess) $ do
108 putStrLn "Build failed, please see build.log"
109 L8.readFile "build.log" >>= L8.putStr
110 exitWith ec
112 putStrLn "Build completed successfully, checking for module name conflicts"
113 conflicts <- getModuleNameConflicts $ packageDir settings
114 System.IO.UTF8.writeFile "module-name-conflicts.txt"
115 $ renderModuleNameConflicts conflicts