@@ -20,6 +20,8 @@ import System.Directory (createDirectoryIfMissing, canonicalizePat
2020import Distribution.Version (thisVersion, withinRange)
2121import Control.Exception (assert)
2222import Data.Set (empty)
23+import qualified Data.Map as Map
24+import qualified Data.Set as Set
2325
2426defaultBuildSettings :: BuildSettings
2527defaultBuildSettings = BuildSettings
@@ -76,7 +78,30 @@ build settings' = do
7678 | v `withinRange` vr -> return ()
7779 | otherwise -> error $ "Unsupported Cabal version: " ++ libVersion
7880
79− ph <- withBinaryFile "build.log" WriteMode $ \handle ->
81+ menv <- fmap Just $ getModifiedEnv settings
82+ let runCabal args handle = runProcess "cabal" args Nothing menv Nothing (Just handle) (Just handle)
83+
84+ -- First install build tools so they can be used below.
85+ case iiBuildTools ii of
86+ [] -> putStrLn "No build tools required"
87+ tools -> do
88+ putStrLn $ "Installing the following build tools: " ++ unwords tools
89+ ph1 <- withBinaryFile "build-tools.log" WriteMode $ \handle -> do
90+ let args = addCabalArgs settings
91+ $ "install"
92+ : ("--cabal-lib-version=" ++ libVersion)
93+ : "--build-log=logs-tools/$pkg.log"
94+ : "-j"
95+ : iiBuildTools ii
96+ hPutStrLn handle ("cabal " ++ unwords (map (\s -> "'" ++ s ++ "'") args))
97+ runCabal args handle
98+ ec1 <- waitForProcess ph1
99+ unless (ec1 == ExitSuccess) $ do
100+ putStrLn "Building of build tools failed, please see build-tools.log"
101+ exitWith ec1
102+ putStrLn "Build tools built"
103+
104+ ph <- withBinaryFile "build.log" WriteMode $ \handle -> do
80105 let args = addCabalArgs settings
81106 $ "install"
82107 : ("--cabal-lib-version=" ++ libVersion)
@@ -87,8 +112,8 @@ build settings' = do
87112 , extraArgs settings
88113 , iiPackageList ii
89114 ]
90− in do hPutStrLn handle ("cabal " ++ unwords (map (\s -> "'" ++ s ++ "'") args))
91− runProcess "cabal" args Nothing Nothing Nothing (Just handle) (Just handle)
115+ hPutStrLn handle ("cabal " ++ unwords (map (\s -> "'" ++ s ++ "'") args))
116+ runCabal args handle
92117 ec <- waitForProcess ph
93118 unless (ec == ExitSuccess) $ do
94119 putStrLn "Build failed, please see build.log"
@@ -99,3 +124,20 @@ build settings' = do
99124
100125 putStrLn "All test suites that were expected to pass did pass, building tarballs."
101126 makeTarballs ii
127+
128+-- | Get all of the build tools required.
129+iiBuildTools :: InstallInfo -> [String]
130+iiBuildTools InstallInfo { iiPackageDB = PackageDB m, iiPackages = packages } =
131+ -- FIXME possible improvement: track the dependencies between the build
132+ -- tools themselves, and install them in the correct order.
133+ map unPackageName
134+ $ filter (flip Map.member m)
135+ $ Set.toList
136+ $ Set.unions
137+ $ map piBuildTools
138+ $ Map.elems
139+ $ Map.filterWithKey isSelected m
140+ where
141+ unPackageName (PackageName pn) = pn
142+ isSelected name _ = name `Set.member` selected
143+ selected = Set.fromList $ Map.keys packages