From 2e46c8ad96292acc9426a2b94c1e452ede5812ed Mon Sep 17 00:00:00 2001
From: Matthew Pickering <matthewtpickering@gmail.com>
Date: Wed, 28 Feb 2024 10:23:55 +0000
Subject: [PATCH] hadrian: Improve parallelism in binary-dist-dir rule

I noticed that the "docs" target was needed after the libraries and
executables were built. We can improve the parallelism by needing
everything at once so that documentation can be built immediately after
a library is built for example.
---
 hadrian/src/Rules/BinaryDist.hs | 26 +++++++++++++++++---------
 1 file changed, 17 insertions(+), 9 deletions(-)

diff --git a/hadrian/src/Rules/BinaryDist.hs b/hadrian/src/Rules/BinaryDist.hs
index 16b4dac06dde..6daaad6b7ecc 100644
--- a/hadrian/src/Rules/BinaryDist.hs
+++ b/hadrian/src/Rules/BinaryDist.hs
@@ -145,12 +145,7 @@ bindistRules = do
         installTo NotRelocatable installPrefix
 
     phony "binary-dist-dir" $ do
-        -- We 'need' all binaries and libraries
-        all_pkgs <- stagePackages Stage1
-        (lib_targets, bin_targets) <- partitionEithers <$> mapM pkgTarget all_pkgs
-        cross <- flag CrossCompiling
-        iserv_targets <- if cross then pure [] else iservBins
-        need (lib_targets ++ (map (\(_, p) -> p) (bin_targets ++ iserv_targets)))
+
 
         version        <- setting ProjectVersion
         targetPlatform <- setting TargetPlatformFull
@@ -164,6 +159,22 @@ bindistRules = do
             rtsIncludeDir    = ghcBuildDir -/- "lib" -/- distDir -/- rtsDir
                                -/- "include"
 
+        -- We 'need' all binaries and libraries
+        all_pkgs <- stagePackages Stage1
+        (lib_targets, bin_targets) <- partitionEithers <$> mapM pkgTarget all_pkgs
+        cross <- flag CrossCompiling
+        iserv_targets <- if cross then pure [] else iservBins
+
+        let lib_exe_targets = (lib_targets ++ (map (\(_, p) -> p) (bin_targets ++ iserv_targets)))
+
+        let doc_target = if cross then [] else ["docs"]
+
+        let other_targets = map (bindistFilesDir -/-) (["configure", "Makefile"] ++ bindistInstallFiles)
+        let all_targets = lib_exe_targets ++ doc_target ++ other_targets
+
+        -- Better parallelism if everything is needed together.
+        need all_targets
+
         -- We create the bindist directory at <root>/bindist/ghc-X.Y.Z-platform/
         -- and populate it with Stage2 build results
         createDirectory bindistFilesDir
@@ -232,7 +243,6 @@ bindistRules = do
         cmd_ (bindistFilesDir -/- "bin" -/- ghcPkgName) ["recache"]
 
 
-        unless cross $ need ["docs"]
 
         -- TODO: we should only embed the docs that have been generated
         -- depending on the current settings (flavours' "ghcDocs" field and
@@ -274,8 +284,6 @@ bindistRules = do
         -- We then 'need' all the files necessary to configure and install
         -- (as in, './configure [...] && make install') this build on some
         -- other machine.
-        need $ map (bindistFilesDir -/-)
-                  (["configure", "Makefile"] ++ bindistInstallFiles)
         copyFile ("hadrian" -/- "bindist" -/- "config.mk.in") (bindistFilesDir -/- "config.mk.in")
         generateBuildMk >>= writeFile' (bindistFilesDir -/- "build.mk")
         copyFile ("hadrian" -/- "cfg" -/- "default.target.in") (bindistFilesDir -/- "default.target.in")
-- 
GitLab