on a665487 help sign in
stackage/Stackage/Build.hs
116 lines 4.7 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 , expectedFailuresBuild = defaultExpectedFailures version
30 , extraArgs = \bs -> "-fnetwork23" : "-fhttps" :
31 case bs of
32 BSTest -> []
33 _ ->
34 case cores of
35 Nothing -> ["-j"]
36 Just 1 -> []
37 Just j -> ["-j" ++ show j]
38 , testWorkerThreads = 4
39 , buildDocs = True
40 , tarballDir = "patching/tarballs"
41 , cabalFileDir = Nothing
42 , underlayPackageDirs = []
43 }
45build :: BuildSettings -> BuildPlan -> IO ()
46build settings' bp = do
47 libVersion <- checkCabalVersion
49 putStrLn "Wiping out old sandbox folder"
50 rm_r $ sandboxRoot settings'
51 rm_r "logs"
52 settings <- fixBuildSettings settings'
54 putStrLn "Creating new package database"
55 ec1 <- rawSystem "ghc-pkg" ["init", packageDir settings]
56 unless (ec1 == ExitSuccess) $ do
57 putStrLn "Unable to create package database via ghc-pkg init"
58 exitWith ec1
60 menv <- fmap Just $ getModifiedEnv settings
61 let runCabal args handle = runProcess "cabal" args Nothing menv Nothing (Just handle) (Just handle)
63 -- First install build tools so they can be used below.
64 let installBuildTool tool = do
65 let toolsDir = packageDir settings ++ "-tools"
66 rm_r toolsDir
67 ecInit <- rawSystem "ghc-pkg" ["init", toolsDir]
68 unless (ecInit == ExitSuccess) $ do
69 putStrLn "Unable to create package database via ghc-pkg init"
70 exitWith ecInit
72 putStrLn $ "Installing build tool: " ++ tool
73 ec <- withBinaryFile "build-tools.log" WriteMode $ \handle -> do
74 hSetBuffering handle NoBuffering
76 let args = addCabalArgs settings BSTools
77 $ "install"
78 : ("--cabal-lib-version=" ++ libVersion)
79 : "--build-log=logs-tools/$pkg.log"
80 : [tool]
81 hPutStrLn handle ("cabal " ++ unwords (map (\s -> "'" ++ s ++ "'") args))
82 ph <- runCabal args handle
83 waitForProcess ph
84 unless (ec == ExitSuccess) $ do
85 putStrLn $ concat
86 [ "Building of "
87 , tool
88 , " failed, please see build-tools.log"
89 ]
90 exitWith ec
91 putStrLn $ tool ++ " built"
92 rm_r toolsDir
93 mapM_ installBuildTool $ bpTools bp
95 putStrLn "Beginning Stackage build"
96 ph <- withBinaryFile "build.log" WriteMode $ \handle -> do
97 packageList <- mapM (replaceTarball $ tarballDir settings) $ bpPackageList bp
98 let args = addCabalArgs settings BSBuild
99 $ "install"
100 : ("--cabal-lib-version=" ++ libVersion)
101 : "--build-log=logs/$pkg.log"
102 : "--max-backjumps=-1"
103 : "--reorder-goals"
104 : packageList
105 hPutStrLn handle ("cabal " ++ unwords (map (\s -> "'" ++ s ++ "'") args))
106 runCabal args handle
107 ec <- waitForProcess ph
108 unless (ec == ExitSuccess) $ do
109 putStrLn "Build failed, please see build.log"
110 L8.readFile "build.log" >>= L8.putStr
111 exitWith ec
113 putStrLn "Build completed successfully, checking for module name conflicts"
114 conflicts <- getModuleNameConflicts $ packageDir settings
115 System.IO.UTF8.writeFile "module-name-conflicts.txt"
116 $ renderModuleNameConflicts conflicts