From c1a3ecde720b3bddc2c8616daaa06ee324e602ab Mon Sep 17 00:00:00 2001
From: Ben Gamari <ben@smart-cactus.org>
Date: Wed, 20 Sep 2023 11:56:44 -0400
Subject: [PATCH] users-guide: Refactor handling of :base-ref: et al.

---
 configure.ac                      | 14 --------------
 docs/users_guide/ghc_config.py.in | 16 ++++++++--------
 hadrian/src/Rules/Generate.hs     | 17 ++++++++++++++++-
 m4/library_version.m4             | 10 ----------
 4 files changed, 24 insertions(+), 33 deletions(-)
 delete mode 100644 m4/library_version.m4

diff --git a/configure.ac b/configure.ac
index 31c7f2fc4456..10c39fae8294 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1145,20 +1145,6 @@ AC_SUBST(BUILD_MAN)
 AC_SUBST(BUILD_SPHINX_HTML)
 AC_SUBST(BUILD_SPHINX_PDF)
 
-dnl ** Determine library versions
-dnl The packages below should include all packages needed by
-dnl doc/users_guide/ghc_config.py.in.
-LIBRARY_VERSION(base)
-LIBRARY_VERSION(Cabal, Cabal/Cabal/Cabal.cabal)
-dnl template-haskell.cabal and ghc-prim.cabal are generated later
-dnl by Hadrian but the .in files already have the version
-LIBRARY_VERSION(template-haskell, template-haskell/template-haskell.cabal.in)
-LIBRARY_VERSION(array)
-LIBRARY_VERSION(ghc-prim, ghc-prim/ghc-prim.cabal.in)
-LIBRARY_VERSION(ghc-compact)
-LIBRARY_ghc_VERSION="$ProjectVersion"
-AC_SUBST(LIBRARY_ghc_VERSION)
-
 if grep '	' compiler/ghc.cabal.in 2>&1 >/dev/null; then
    AC_MSG_ERROR([compiler/ghc.cabal.in contains tab characters; please remove them])
 fi
diff --git a/docs/users_guide/ghc_config.py.in b/docs/users_guide/ghc_config.py.in
index 4a89ff2c7a79..b5887d38c1ee 100644
--- a/docs/users_guide/ghc_config.py.in
+++ b/docs/users_guide/ghc_config.py.in
@@ -18,14 +18,14 @@ libs_base_uri = '../libraries'
 # N.B. If you add a package to this list be sure to also add a corresponding
 # LIBRARY_VERSION macro call to configure.ac.
 lib_versions = {
-    'base': '@LIBRARY_base_VERSION@',
-    'ghc-prim': '@LIBRARY_ghc_prim_VERSION@',
-    'template-haskell': '@LIBRARY_template_haskell_VERSION@',
-    'ghc-compact': '@LIBRARY_ghc_compact_VERSION@',
-    'ghc': '@LIBRARY_ghc_VERSION@',
-    'parallel': '@LIBRARY_parallel_VERSION@',
-    'Cabal': '@LIBRARY_Cabal_VERSION@',
-    'array': '@LIBRARY_array_VERSION@',
+    'base': '@LIBRARY_base_UNIT_ID@',
+    'ghc-prim': '@LIBRARY_ghc_prim_UNIT_ID@',
+    'template-haskell': '@LIBRARY_template_haskell_UNIT_ID@',
+    'ghc-compact': '@LIBRARY_ghc_compact_UNIT_ID@',
+    'ghc': '@LIBRARY_ghc_UNIT_ID@',
+    'parallel': '@LIBRARY_parallel_UNIT_ID@',
+    'Cabal': '@LIBRARY_Cabal_UNIT_ID@',
+    'array': '@LIBRARY_array_UNIT_ID@',
 }
 
 version = '@ProjectVersion@'
diff --git a/hadrian/src/Rules/Generate.hs b/hadrian/src/Rules/Generate.hs
index f6fb706f2d18..d774fd9b6ac2 100644
--- a/hadrian/src/Rules/Generate.hs
+++ b/hadrian/src/Rules/Generate.hs
@@ -320,7 +320,21 @@ packageVersions = foldMap f [ base, ghcPrim, compiler, ghc, cabal, templateHaske
   where
     f :: Package -> Interpolations
     f pkg = interpolateVar var $ version <$> readPackageData pkg
-      where var = "LIBRARY_" <> pkgName pkg <> "_VERSION"
+      where var = "LIBRARY_" <> escapedPkgName pkg <> "_VERSION"
+
+packageUnitIds :: Stage -> Interpolations
+packageUnitIds stage =
+    foldMap f [ base, ghcPrim, compiler, ghc, cabal, templateHaskell, ghcCompact, array ]
+  where
+    f :: Package -> Interpolations
+    f pkg = interpolateVar var $ pkgUnitId stage pkg
+      where var = "LIBRARY_" <> escapedPkgName pkg <> "_UNIT_ID"
+
+escapedPkgName :: Package -> String
+escapedPkgName = map f . pkgName
+  where
+    f '-'   = '_'
+    f other = other
 
 templateRule :: FilePath -> Interpolations -> Rules ()
 templateRule outPath interps = do
@@ -348,6 +362,7 @@ templateRules = do
   templateRule "libraries/template-haskell/template-haskell.cabal" $ projectVersion
   templateRule "libraries/prologue.txt" $ packageVersions
   templateRule "docs/index.html" $ packageVersions
+  templateRule "docs/users_guide/ghc_config.py" $ packageUnitIds Stage1
 
 
 -- Generators
diff --git a/m4/library_version.m4 b/m4/library_version.m4
deleted file mode 100644
index f0a9e8219ca8..000000000000
--- a/m4/library_version.m4
+++ /dev/null
@@ -1,10 +0,0 @@
-# LIBRARY_VERSION(lib, [cabal_file])
-# --------------------------------
-# Gets the version number of a library.
-# If $1 is ghc-prim, then we define LIBRARY_ghc_prim_VERSION as 1.2.3
-# $2 points to the directory under libraries/
-AC_DEFUN([LIBRARY_VERSION],[
-cabal_file=m4_default([$2],[$1/$1.cabal])
-LIBRARY_[]translit([$1], [-], [_])[]_VERSION=`grep -i "^version:" libraries/${cabal_file} | sed "s/.* //"`
-AC_SUBST(LIBRARY_[]translit([$1], [-], [_])[]_VERSION)
-])
-- 
GitLab