From 79993bba432284aa31ef7caba9967946e058c4dc Mon Sep 17 00:00:00 2001
From: Mark Olesen <Mark.Olesen@esi-group.com>
Date: Fri, 15 Dec 2023 09:37:51 +0100
Subject: [PATCH] CONFIG: support explicit selection of 'ld' linker

- for clang-based compilers the default linker may be lld or simply ld.
  Support '+link-ld' to explicitly select use of the ld linker.

- consolidate linker rules into single files

STYLE: adjust SPDX Identifier
---
 bin/tools/openfoam.in                   |  4 +-
 etc/bashrc                              | 11 +++---
 etc/cshrc                               | 11 +++---
 modules/list-modules                    |  2 +-
 wmake/rules/General/Amd/link-c++        |  1 +
 wmake/rules/General/Clang/link-c++      | 50 +++++++++++++++++++++++--
 wmake/rules/General/Clang/link-gold-c++ | 16 --------
 wmake/rules/General/Clang/link-lld-c++  | 16 --------
 wmake/rules/General/Clang/link-mold-c++ | 16 --------
 wmake/rules/General/Gcc/link-c++        | 34 ++++++++++++++---
 wmake/rules/General/Gcc/link-gold-c++   | 16 --------
 wmake/rules/linux64Clang/c++            | 15 +-------
 wmake/rules/linux64Gcc/c++              |  6 +--
 wmake/rules/linuxClang/c++              | 15 +-------
 wmake/rules/linuxGcc/c++                |  6 +--
 15 files changed, 95 insertions(+), 124 deletions(-)
 delete mode 100644 wmake/rules/General/Clang/link-gold-c++
 delete mode 100644 wmake/rules/General/Clang/link-lld-c++
 delete mode 100644 wmake/rules/General/Clang/link-mold-c++
 delete mode 100644 wmake/rules/General/Gcc/link-gold-c++

diff --git a/bin/tools/openfoam.in b/bin/tools/openfoam.in
index 5676d240d71..a3c43e1c99e 100644
--- a/bin/tools/openfoam.in
+++ b/bin/tools/openfoam.in
@@ -1,10 +1,10 @@
 #!/bin/sh
 exec "@PROJECT_DIR@"/etc/openfoam "$@"
 #------------------------------------------------------------------------------
-# OpenFOAM file (www.openfoam.com)
+# This file is part of OpenFOAM (www.openfoam.com)
 #
 # Copyright (C) 2020-2021 OpenCFD Ltd.
-# SPDX-License-Identifier: (GPL-3.0+)
+# SPDX-License-Identifier: (GPL-3.0-or-later)
 #
 # Description
 #     Forward to OpenFOAM etc/openfoam bash session script.
diff --git a/etc/bashrc b/etc/bashrc
index 77175bf41ec..11230ffdd77 100644
--- a/etc/bashrc
+++ b/etc/bashrc
@@ -83,9 +83,10 @@ export WM_LABEL_SIZE=32
 export WM_COMPILE_OPTION=Opt
 
 # [WM_COMPILE_CONTROL] - additional control for compilation/linking
-#   +gold   : with gold linker
-#   +lld    : with  lld linker (with clang)
-#   +mold   : with mold linker (with clang)
+#   +gold   : use gold linker
+#   +link-ld: use   ld linker [clang]
+#   +lld    : use  lld linker [clang]
+#   +mold   : use mold linker [clang]
 #   ~libz   : without libz compression
 #   ~rpath  : without rpath handling [MacOS]
 #   +openmp : with openmp
@@ -94,8 +95,8 @@ export WM_COMPILE_OPTION=Opt
 #   +xcrun  : use xcrun and native compilers [MacOS]
 #   +strict : more deprecation warnings (may generate *many* warnings)
 #   ccache=... : ccache command (unquoted, single/double or <> quoted)
-#   version=... : compiler suffix (eg, "11" for gcc-11)
-#export WM_COMPILE_CONTROL="+gold"
+#   version=... : compiler suffix (eg, version=11 -> gcc-11)
+#export WM_COMPILE_CONTROL="+strict"
 
 # [WM_MPLIB] - MPI implementation:
 # = SYSTEMOPENMPI | OPENMPI | SYSTEMMPI | MPI | MPICH | MPICH-GM |
diff --git a/etc/cshrc b/etc/cshrc
index d8d51379818..2a312e42b9c 100644
--- a/etc/cshrc
+++ b/etc/cshrc
@@ -83,9 +83,10 @@ setenv WM_LABEL_SIZE 32
 setenv WM_COMPILE_OPTION Opt
 
 # [WM_COMPILE_CONTROL] - additional control for compilation/linking
-#   +gold   : with gold linker
-#   +lld    : with  lld linker (with clang)
-#   +mold   : with mold linker (with clang)
+#   +gold   : use gold linker
+#   +link-ld: use   ld linker [clang]
+#   +lld    : use  lld linker [clang]
+#   +mold   : use mold linker [clang]
 #   ~libz   : without libz compression
 #   ~rpath  : without rpath handling [MacOS]
 #   +openmp : with openmp
@@ -94,8 +95,8 @@ setenv WM_COMPILE_OPTION Opt
 #   +xcrun  : use xcrun and native compilers [MacOS]
 #   +strict : more deprecation warnings (may generate *many* warnings)
 #   ccache=... : ccache command (unquoted, single/double or <> quoted)
-#   version=... : compiler suffix (eg, "11" for gcc-11)
-#setenv WM_COMPILE_CONTROL "+gold"
+#   version=... : compiler suffix (eg, version=11 -> gcc-11)
+#setenv WM_COMPILE_CONTROL "+strict"
 
 # [WM_MPLIB] - MPI implementation:
 # = SYSTEMOPENMPI | OPENMPI | SYSTEMMPI | MPI | MPICH | MPICH-GM |
diff --git a/modules/list-modules b/modules/list-modules
index c87750b97f6..27dba5068f9 100755
--- a/modules/list-modules
+++ b/modules/list-modules
@@ -8,7 +8,7 @@
 #------------------------------------------------------------------------------
 #     Copyright (C) 2020 OpenCFD Ltd.
 #------------------------------------------------------------------------------
-# SPDX-License-Identifier: (GPL-3.0+)
+# SPDX-License-Identifier: (GPL-3.0-or-later)
 #
 # Script
 #     list-modules
diff --git a/wmake/rules/General/Amd/link-c++ b/wmake/rules/General/Amd/link-c++
index e048a3dd1bb..615d3c508b5 100644
--- a/wmake/rules/General/Amd/link-c++
+++ b/wmake/rules/General/Amd/link-c++
@@ -6,6 +6,7 @@ LINKLIBSO   = $(CC) $(c++FLAGS) -shared
 
 LINKEXE     = $(CC) $(c++FLAGS)
 
+# ---------------
 # Link dummy stub to resolve libPstream symbols used by libOpenFOAM
 ifneq (,$(findstring -lOpenFOAM,$(PROJECT_LIBS)))
     LINKEXE += -L$(FOAM_LIBBIN)/dummy -lPstream
diff --git a/wmake/rules/General/Clang/link-c++ b/wmake/rules/General/Clang/link-c++
index b4581dc9bf6..56d1da110cb 100644
--- a/wmake/rules/General/Clang/link-c++
+++ b/wmake/rules/General/Clang/link-c++
@@ -1,10 +1,54 @@
 #------------------------------------------------------------------------------
+# Common linker settings
 
 LINK_LIBS   = $(c++DBUG)
-
 LINKLIBSO   = $(CC) $(c++FLAGS) -shared
+LINKEXE     = $(CC) $(c++FLAGS)
+
+undefine LINKEXE_STUB
+
+#------------------------------------------------------------------------------
+
+# Use gold linker
+ifneq (,$(findstring +gold,$(WM_COMPILE_CONTROL)))
+
+    LINKLIBSO  += -fuse-ld=gold
+    LINKEXE    += -fuse-ld=gold
+    LINKEXE_STUB := true
+
+# Use mold linker
+else ifneq (,$(findstring +mold,$(WM_COMPILE_CONTROL)))
+
+    LINKLIBSO  += -fuse-ld=mold
+    LINKEXE    += -fuse-ld=mold
+    LINKEXE_STUB := true
+
+# Use lld linker
+else ifneq (,$(findstring +lld,$(WM_COMPILE_CONTROL)))
+
+    LINKLIBSO  += -fuse-ld=lld
+    LINKEXE    += -fuse-ld=lld
+    LINKEXE_STUB := true
+
+# Use ld linker - no dummy stub ('+link-ld' not '+ld' to avoid false matches)
+else ifneq (,$(findstring +link-ld,$(WM_COMPILE_CONTROL)))
+
+    LINKLIBSO  += -fuse-ld=ld
+    LINKEXE    += -fuse-ld=ld -Xlinker --add-needed
+
+# Default linker, assume ld - no dummy stub
+else
+
+    LINKEXE    += -Xlinker --add-needed
+
+endif
 
-LINKEXE     = $(CC) $(c++FLAGS) \
-    -Xlinker --add-needed
+# ---------------
+# Link dummy stub to resolve libPstream symbols used by libOpenFOAM
+ifneq (,$(LINKEXE_STUB))
+ifneq (,$(findstring -lOpenFOAM,$(PROJECT_LIBS)))
+    LINKEXE += -L$(FOAM_LIBBIN)/dummy -lPstream
+endif
+endif
 
 #------------------------------------------------------------------------------
diff --git a/wmake/rules/General/Clang/link-gold-c++ b/wmake/rules/General/Clang/link-gold-c++
deleted file mode 100644
index 6bf424fa3c3..00000000000
--- a/wmake/rules/General/Clang/link-gold-c++
+++ /dev/null
@@ -1,16 +0,0 @@
-#------------------------------------------------------------------------------
-
-LINK_LIBS   = $(c++DBUG)
-
-LINKLIBSO   = $(CC) $(c++FLAGS) -shared \
-    -fuse-ld=gold
-
-LINKEXE     = $(CC) $(c++FLAGS) \
-    -fuse-ld=gold
-
-# Link dummy stub to resolve libPstream symbols used by libOpenFOAM
-ifneq (,$(findstring -lOpenFOAM,$(PROJECT_LIBS)))
-    LINKEXE += -L$(FOAM_LIBBIN)/dummy -lPstream
-endif
-
-#------------------------------------------------------------------------------
diff --git a/wmake/rules/General/Clang/link-lld-c++ b/wmake/rules/General/Clang/link-lld-c++
deleted file mode 100644
index 4af882e95f3..00000000000
--- a/wmake/rules/General/Clang/link-lld-c++
+++ /dev/null
@@ -1,16 +0,0 @@
-#------------------------------------------------------------------------------
-
-LINK_LIBS   = $(c++DBUG)
-
-LINKLIBSO   = $(CC) $(c++FLAGS) -shared \
-    -fuse-ld=lld
-
-LINKEXE     = $(CC) $(c++FLAGS) \
-    -fuse-ld=lld
-
-# Link dummy stub to resolve libPstream symbols used by libOpenFOAM
-ifneq (,$(findstring -lOpenFOAM,$(PROJECT_LIBS)))
-    LINKEXE += -L$(FOAM_LIBBIN)/dummy -lPstream
-endif
-
-#------------------------------------------------------------------------------
diff --git a/wmake/rules/General/Clang/link-mold-c++ b/wmake/rules/General/Clang/link-mold-c++
deleted file mode 100644
index f84f20cd935..00000000000
--- a/wmake/rules/General/Clang/link-mold-c++
+++ /dev/null
@@ -1,16 +0,0 @@
-#------------------------------------------------------------------------------
-
-LINK_LIBS   = $(c++DBUG)
-
-LINKLIBSO   = $(CC) $(c++FLAGS) -shared \
-    -fuse-ld=mold
-
-LINKEXE     = $(CC) $(c++FLAGS) \
-    -fuse-ld=mold
-
-# Link dummy stub to resolve libPstream symbols used by libOpenFOAM
-ifneq (,$(findstring -lOpenFOAM,$(PROJECT_LIBS)))
-    LINKEXE += -L$(FOAM_LIBBIN)/dummy -lPstream
-endif
-
-#------------------------------------------------------------------------------
diff --git a/wmake/rules/General/Gcc/link-c++ b/wmake/rules/General/Gcc/link-c++
index ced650574c5..eab8f17299b 100644
--- a/wmake/rules/General/Gcc/link-c++
+++ b/wmake/rules/General/Gcc/link-c++
@@ -1,13 +1,35 @@
 #------------------------------------------------------------------------------
+# Common linker settings
 
 LINK_LIBS   = $(c++DBUG)
+LINKLIBSO   = $(CC) $(c++FLAGS) -shared
+LINKEXE     = $(CC) $(c++FLAGS)
 
-LINKLIBSO   = $(CC) $(c++FLAGS) -shared \
-    -Xlinker --add-needed \
-    -Xlinker --no-as-needed
+undefine LINKEXE_STUB
 
-LINKEXE     = $(CC) $(c++FLAGS) \
-    -Xlinker --add-needed \
-    -Xlinker --no-as-needed
+#------------------------------------------------------------------------------
+
+# Use gold linker
+ifneq (,$(findstring +gold,$(WM_COMPILE_CONTROL)))
+
+    LINKLIBSO  += -fuse-ld=gold
+    LINKEXE    += -fuse-ld=gold
+    LINKEXE_STUB := true
+
+# Default linker, assume ld - no dummy stub
+else
+
+    LINKLIBSO  += -Xlinker --add-needed -Xlinker --no-as-needed
+    LINKEXE    += -Xlinker --add-needed -Xlinker --no-as-needed
+
+endif
+
+
+# Link dummy stub to resolve libPstream symbols used by libOpenFOAM
+ifneq (,$(LINKEXE_STUB))
+ifneq (,$(findstring -lOpenFOAM,$(PROJECT_LIBS)))
+    LINKEXE += -L$(FOAM_LIBBIN)/dummy -lPstream
+endif
+endif
 
 #------------------------------------------------------------------------------
diff --git a/wmake/rules/General/Gcc/link-gold-c++ b/wmake/rules/General/Gcc/link-gold-c++
deleted file mode 100644
index 6bf424fa3c3..00000000000
--- a/wmake/rules/General/Gcc/link-gold-c++
+++ /dev/null
@@ -1,16 +0,0 @@
-#------------------------------------------------------------------------------
-
-LINK_LIBS   = $(c++DBUG)
-
-LINKLIBSO   = $(CC) $(c++FLAGS) -shared \
-    -fuse-ld=gold
-
-LINKEXE     = $(CC) $(c++FLAGS) \
-    -fuse-ld=gold
-
-# Link dummy stub to resolve libPstream symbols used by libOpenFOAM
-ifneq (,$(findstring -lOpenFOAM,$(PROJECT_LIBS)))
-    LINKEXE += -L$(FOAM_LIBBIN)/dummy -lPstream
-endif
-
-#------------------------------------------------------------------------------
diff --git a/wmake/rules/linux64Clang/c++ b/wmake/rules/linux64Clang/c++
index cc23c07134c..e78e087981d 100644
--- a/wmake/rules/linux64Clang/c++
+++ b/wmake/rules/linux64Clang/c++
@@ -16,19 +16,6 @@ cctoo       = $(Ctoo)
 cpptoo      = $(Ctoo)
 cxxtoo      = $(Ctoo)
 
-# Linking:
-
-ifneq (,$(findstring +gold,$(WM_COMPILE_CONTROL)))
-    include $(GENERAL_RULES)/Clang/link-gold-c++
-
-else ifneq (,$(findstring +mold,$(WM_COMPILE_CONTROL)))
-    include $(GENERAL_RULES)/Clang/link-mold-c++
-
-else ifneq (,$(findstring +lld,$(WM_COMPILE_CONTROL)))
-    include $(GENERAL_RULES)/Clang/link-lld-c++
-
-else
-    include $(GENERAL_RULES)/Clang/link-c++
-endif
+include $(GENERAL_RULES)/Clang/link-c++
 
 #------------------------------------------------------------------------------
diff --git a/wmake/rules/linux64Gcc/c++ b/wmake/rules/linux64Gcc/c++
index c13780b2a2e..21221d168dc 100644
--- a/wmake/rules/linux64Gcc/c++
+++ b/wmake/rules/linux64Gcc/c++
@@ -16,10 +16,6 @@ cctoo       = $(Ctoo)
 cpptoo      = $(Ctoo)
 cxxtoo      = $(Ctoo)
 
-ifneq (,$(findstring +gold,$(WM_COMPILE_CONTROL)))
-    include $(GENERAL_RULES)/Gcc/link-gold-c++
-else
-    include $(GENERAL_RULES)/Gcc/link-c++
-endif
+include $(GENERAL_RULES)/Gcc/link-c++
 
 #------------------------------------------------------------------------------
diff --git a/wmake/rules/linuxClang/c++ b/wmake/rules/linuxClang/c++
index b3e7c1ac84b..f86720d022b 100644
--- a/wmake/rules/linuxClang/c++
+++ b/wmake/rules/linuxClang/c++
@@ -16,19 +16,6 @@ cctoo       = $(Ctoo)
 cpptoo      = $(Ctoo)
 cxxtoo      = $(Ctoo)
 
-# Linking:
-
-ifneq (,$(findstring +gold,$(WM_COMPILE_CONTROL)))
-    include $(GENERAL_RULES)/Clang/link-gold-c++
-
-else ifneq (,$(findstring +mold,$(WM_COMPILE_CONTROL)))
-    include $(GENERAL_RULES)/Clang/link-mold-c++
-
-else ifneq (,$(findstring +lld,$(WM_COMPILE_CONTROL)))
-    include $(GENERAL_RULES)/Clang/link-lld-c++
-
-else
-    include $(GENERAL_RULES)/Clang/link-c++
-endif
+include $(GENERAL_RULES)/Clang/link-c++
 
 #------------------------------------------------------------------------------
diff --git a/wmake/rules/linuxGcc/c++ b/wmake/rules/linuxGcc/c++
index a850fb8ea3a..091f2e509e4 100644
--- a/wmake/rules/linuxGcc/c++
+++ b/wmake/rules/linuxGcc/c++
@@ -16,10 +16,6 @@ cctoo       = $(Ctoo)
 cpptoo      = $(Ctoo)
 cxxtoo      = $(Ctoo)
 
-ifneq (,$(findstring +gold,$(WM_COMPILE_CONTROL)))
-    include $(GENERAL_RULES)/Gcc/link-gold-c++
-else
-    include $(GENERAL_RULES)/Gcc/link-c++
-endif
+include $(GENERAL_RULES)/Gcc/link-c++
 
 #------------------------------------------------------------------------------
-- 
GitLab