From 62d632c47181d2c8df21c1375a73fb27f54ba4cc Mon Sep 17 00:00:00 2001
From: "Roscoe A. Bartlett" <rabartl@sandia.gov>
Date: Thu, 3 Aug 2023 17:50:41 -0600
Subject: [PATCH] Remove special raw CMake package logic for lib install dir
 (#582)

This avoids the special logic in the file package1/CMakeLists.txt by having
TribitsExampleProject2/CMakeLists.txt set the var
${PROJECT_NAME}_USE_GNUINSTALLDIRS=ON.  This resulted in everything being
installed under <prefix>/lib64/ on my current machine automatically.

However, to get this to work I had to set:

  -DCMAKE_INSTALL_LIBDIR:STRING=lib

or the find_package(...) command with CMake 3.23.1 would no longer find
<Package>Config.cmake files under:

  <prefix>/lib64/

This seems inconsistent with CMake documentation for find_package() as of
CMake 3.23.1 that suggests that it will look under the location:

  <prefix>/(lib/<arch>|lib*|share)/cmake/<name>*/

Well that should pick up the directory:

  <prefix>/lib*/cmake/<name>*/

But that was not the case :-(

Perhaps this is fixed in the latest CMake?  In any case, we get around this by
by setting CMAKE_INSTALL_LIBDIR=lib.

Other changes made in this commit:

* Fixed the 'TribitsExampleApp2' tests to correctly switch between searching
  for the top project-level package-config file or the individual
  package-config files.  (This required passing in
  -DTribitsExApp2_FIND_INDIVIDUAL_PACKAGES=ON and updating the regex.)
---
 .../TribitsExampleApp2_Tests.cmake                |  5 ++++-
 .../TribitsExampleProject2_Tests.cmake            | 12 ++++++++++++
 .../TribitsExampleApp2/AppHelperFuncs.cmake       |  5 +++--
 .../TribitsExampleProject2/CMakeLists.txt         |  1 +
 .../packages/package1/CMakeLists.txt              | 15 +--------------
 5 files changed, 21 insertions(+), 17 deletions(-)

diff --git a/test/core/ExamplesUnitTests/TribitsExampleApp2_Tests.cmake b/test/core/ExamplesUnitTests/TribitsExampleApp2_Tests.cmake
index bccc0e18b..d57cd9f61 100644
--- a/test/core/ExamplesUnitTests/TribitsExampleApp2_Tests.cmake
+++ b/test/core/ExamplesUnitTests/TribitsExampleApp2_Tests.cmake
@@ -115,9 +115,12 @@ function(TribitsExampleApp2  tribitsExProj2TestNameBaseBase
 
   if (fullOrComponents STREQUAL "FULL")
     set(tribitsExProjUseComponentsArg "")
+    set(packageListRegex "TribitsExProj2_PACKAGE_LIST = Package3[;]Package2[;]Package1")
   elseif (fullOrComponents STREQUAL "COMPONENTS")
     set(tribitsExProjUseComponentsArg
+      -DTribitsExApp2_FIND_INDIVIDUAL_PACKAGES=ON
       -DTribitsExApp2_USE_COMPONENTS="Package1,Package2,Package3")
+    set(packageListRegex "Found Package1;Found Package2;Found Package3")
   else()
     message(FATAL_ERROR "Invalid value of fullOrComponents='${fullOrComponents}'!")
   endif()
@@ -146,7 +149,7 @@ function(TribitsExampleApp2  tribitsExProj2TestNameBaseBase
         ${tribitsExProjUseComponentsArg}
         ${${PROJECT_NAME}_TRIBITS_DIR}/examples/TribitsExampleApp2
       PASS_REGULAR_EXPRESSION_ALL
-        "TribitsExProj2_PACKAGE_LIST = Package3[;]Package2[;]Package1"
+        "${packageListRegex}"
         "-- Configuring done"
         "-- Generating done"
         "-- Build files have been written to: .*/${testName}/app_build"
diff --git a/test/core/ExamplesUnitTests/TribitsExampleProject2_Tests.cmake b/test/core/ExamplesUnitTests/TribitsExampleProject2_Tests.cmake
index 37cd49565..bee834b32 100644
--- a/test/core/ExamplesUnitTests/TribitsExampleProject2_Tests.cmake
+++ b/test/core/ExamplesUnitTests/TribitsExampleProject2_Tests.cmake
@@ -187,6 +187,7 @@ function(TribitsExampleProject2_find_tpl_parts  sharedOrStatic  findingTplsMetho
         ${tplLibAndIncDirsArgs}
         ${cmakePrefixPathCacheArg}
         -DTribitsExProj2_ENABLE_TESTS=ON
+        -DCMAKE_INSTALL_LIBDIR:STRING=lib
         -DCMAKE_INSTALL_PREFIX=${testDir}/install
         -DTribitsExProj2_ENABLE_ALL_PACKAGES=ON
         ../TribitsExampleProject2
@@ -352,6 +353,7 @@ function(TribitsExampleProject2_find_tpl_parts_no_optional_packages_tpls  shared
         -DTribitsExProj2_ENABLE_ALL_OPTIONAL_PACKAGES=OFF
         -DPackage3_ENABLE_Package2=OFF
         -DTribitsExProj2_ENABLE_TESTS=ON
+        -DCMAKE_INSTALL_LIBDIR:STRING=lib
         -DCMAKE_INSTALL_PREFIX=install
         -DTribitsExProj2_ENABLE_ALL_PACKAGES=ON
         ${${PROJECT_NAME}_TRIBITS_DIR}/examples/TribitsExampleProject2
@@ -469,6 +471,7 @@ function(TribitsExampleProject2_explicit_tpl_vars  sharedOrStatic)
         -DCMAKE_BUILD_TYPE=DEBUG
         -DTribitsExProj2_ENABLE_ALL_PACKAGES=ON
         -DTribitsExProj2_ENABLE_TESTS=ON
+        -DCMAKE_INSTALL_LIBDIR:STRING=lib
         -DCMAKE_INSTALL_PREFIX=install
         ${${PROJECT_NAME}_TRIBITS_DIR}/examples/TribitsExampleProject2
       PASS_REGULAR_EXPRESSION_ALL
@@ -596,6 +599,7 @@ function(TribitsExampleProject2_find_package  sharedOrStatic  package1TribitsOrR
         ${package1UseRawCMakeArgs}
         -DTribitsExProj2_ENABLE_ALL_PACKAGES=ON
         -DTribitsExProj2_ENABLE_TESTS=ON
+        -DCMAKE_INSTALL_LIBDIR:STRING=lib
         -DCMAKE_INSTALL_PREFIX=${testDir}/install
         -D CMAKE_PREFIX_PATH="${tplInstallBaseDir}/install_tpl1<semicolon>${tplInstallBaseDir}/install_tpl2<semicolon>${tplInstallBaseDir}/install_tpl3<semicolon>${tplInstallBaseDir}/install_tpl4"
         ../TribitsExampleProject2
@@ -702,6 +706,7 @@ tribits_add_advanced_test( ${testNameBase}
       -DCMAKE_BUILD_TYPE=DEBUG
       -DTpl1_EXTRACT_INFO_AFTER_FIND_PACKAGE=ON
       -DTribitsExProj2_ENABLE_TESTS=ON
+      -DCMAKE_INSTALL_LIBDIR:STRING=lib
       -DCMAKE_INSTALL_PREFIX=install
       -DTribitsExProj2_ENABLE_Package1=ON
       ${${PROJECT_NAME}_TRIBITS_DIR}/examples/TribitsExampleProject2
@@ -748,6 +753,7 @@ tribits_add_advanced_test( ${testNameBase}
       -DTpl1_EXTRACT_INFO_AFTER_FIND_PACKAGE=ON
       -DTribitsExProj2_ENABLE_TESTS=ON
       -DCMAKE_PREFIX_PATH="${testDir}/install"
+      -DCMAKE_INSTALL_LIBDIR:STRING=lib
       -DCMAKE_INSTALL_PREFIX=install
       -DTribitsExProj2_ENABLE_Package1=ON
       ${${PROJECT_NAME}_TRIBITS_DIR}/examples/TribitsExampleProject2
@@ -905,6 +911,7 @@ function(TribitsExampleProject2_External_Package_by_Package
         -DTribitsExProj2_TRIBITS_DIR=${${PROJECT_NAME}_TRIBITS_DIR}
         -DTribitsExProj2_ENABLE_SECONDARY_TESTED_CODE=ON
         -DTribitsExProj2_ENABLE_Package1=ON
+        -DCMAKE_INSTALL_LIBDIR:STRING=lib
         -DCMAKE_INSTALL_PREFIX=../install_package1
         -DTribitsExProj2_SKIP_INSTALL_PROJECT_CMAKE_CONFIG_FILES=TRUE
         -DTPL_ENABLE_Tpl1=ON
@@ -941,6 +948,7 @@ function(TribitsExampleProject2_External_Package_by_Package
         -DTribitsExProj2_TRIBITS_DIR=${${PROJECT_NAME}_TRIBITS_DIR}
         -DTribitsExProj2_ENABLE_SECONDARY_TESTED_CODE=ON
         -DTribitsExProj2_ENABLE_Package2=ON
+        -DCMAKE_INSTALL_LIBDIR:STRING=lib
         -DCMAKE_INSTALL_PREFIX=../install_package2
         -DTribitsExProj2_SKIP_INSTALL_PROJECT_CMAKE_CONFIG_FILES=TRUE
         -DTPL_ENABLE_Package1=ON  # Pull in already installed Package!
@@ -1002,6 +1010,7 @@ function(TribitsExampleProject2_External_Package_by_Package
         -DTribitsExProj2_ENABLE_SECONDARY_TESTED_CODE=ON
         -DTribitsExProj2_ENABLE_ALL_PACKAGES=ON
         -DTribitsExProj2_ENABLE_TESTS=ON
+        -DCMAKE_INSTALL_LIBDIR:STRING=lib
         -DCMAKE_INSTALL_PREFIX=../install
         -DTribitsExProj2_SKIP_INSTALL_PROJECT_CMAKE_CONFIG_FILES=TRUE
         -DTPL_ENABLE_Package2=ON  # Pull in already installed Package!
@@ -1200,6 +1209,7 @@ function(TribitsExampleProject2_External_RawPackage1_then_Package_by_Package
       ARGS
         ${TribitsExampleProject2_COMMON_CONFIG_ARGS}
         -DCMAKE_PREFIX_PATH=${tplInstallBaseDir}/install_tpl1
+        -DCMAKE_INSTALL_LIBDIR:STRING=lib
         -DCMAKE_INSTALL_PREFIX=../install_package1
         ../TribitsExampleProject2/packages/package1
       PASS_REGULAR_EXPRESSION_ALL
@@ -1226,6 +1236,7 @@ function(TribitsExampleProject2_External_RawPackage1_then_Package_by_Package
         -DTribitsExProj2_ENABLE_SECONDARY_TESTED_CODE=ON
         -DTribitsExProj2_ENABLE_TESTS=ON
         -DTribitsExProj2_ENABLE_Package2=ON
+        -DCMAKE_INSTALL_LIBDIR:STRING=lib
         -DCMAKE_INSTALL_PREFIX=../install_package2
         -DTribitsExProj2_SKIP_INSTALL_PROJECT_CMAKE_CONFIG_FILES=TRUE
         -DTPL_ENABLE_Package1=ON  # Pull in already installed Package1!
@@ -1296,6 +1307,7 @@ function(TribitsExampleProject2_External_RawPackage1_then_Package_by_Package
         -DTribitsExProj2_ENABLE_SECONDARY_TESTED_CODE=ON
         -DTribitsExProj2_ENABLE_ALL_PACKAGES=ON
         -DTribitsExProj2_ENABLE_TESTS=ON
+        -DCMAKE_INSTALL_LIBDIR:STRING=lib
         -DCMAKE_INSTALL_PREFIX=../install
         -DTribitsExProj2_SKIP_INSTALL_PROJECT_CMAKE_CONFIG_FILES=TRUE
         -DTPL_ENABLE_Package2=ON  # Pull in already installed Package!
diff --git a/tribits/examples/TribitsExampleApp2/AppHelperFuncs.cmake b/tribits/examples/TribitsExampleApp2/AppHelperFuncs.cmake
index cbed66bcd..ebb23e178 100644
--- a/tribits/examples/TribitsExampleApp2/AppHelperFuncs.cmake
+++ b/tribits/examples/TribitsExampleApp2/AppHelperFuncs.cmake
@@ -37,7 +37,7 @@ macro(getTribitsExProj2StuffForAppByPackage)
   # Find each package and gather up all the <Package>::all_libs targets
   set(APP_DEPS_LIB_TARGETS "")
   foreach (packageName IN LISTS ${PROJECT_NAME}_USE_COMPONENTS)
-    find_package(${packageName} REQUIRED)
+    find_package(${packageName} CONFIG REQUIRED)
     message("Found ${packageName}!")
     list(APPEND APP_DEPS_LIB_TARGETS ${packageName}::all_libs)
   endforeach()
@@ -59,7 +59,8 @@ endmacro()
 #
 macro(getTribitsExProj2StuffForAppByProject)
 
-  find_package(TribitsExProj2 REQUIRED COMPONENTS ${${PROJECT_NAME}_USE_COMPONENTS})
+  find_package(TribitsExProj2 CONFIG REQUIRED
+    COMPONENTS ${${PROJECT_NAME}_USE_COMPONENTS})
 
   message("\nFound TribitsExProj2!  Here are the details: ")
   message("   TribitsExProj2_DIR = ${TribitsExProj2_DIR}")
diff --git a/tribits/examples/TribitsExampleProject2/CMakeLists.txt b/tribits/examples/TribitsExampleProject2/CMakeLists.txt
index 7ba3163d9..1c249d2b2 100644
--- a/tribits/examples/TribitsExampleProject2/CMakeLists.txt
+++ b/tribits/examples/TribitsExampleProject2/CMakeLists.txt
@@ -8,6 +8,7 @@ cmake_minimum_required(VERSION 3.23.0 FATAL_ERROR)
 include("${CMAKE_CURRENT_SOURCE_DIR}/ProjectName.cmake")
 project(${PROJECT_NAME} LANGUAGES NONE)
 set(TRIBITS_HIDE_DEPRECATED_INCLUDE_DIRECTORIES_OVERRIDE  TRUE)
+set(${PROJECT_NAME}_USE_GNUINSTALLDIRS ON)
 set(${PROJECT_NAME}_TRIBITS_DIR
    "${CMAKE_CURRENT_LIST_DIR}/../.."  CACHE  STRING
   "TriBITS base directory (default assumes in TriBITS source tree)")
diff --git a/tribits/examples/TribitsExampleProject2/packages/package1/CMakeLists.txt b/tribits/examples/TribitsExampleProject2/packages/package1/CMakeLists.txt
index 1211cdda6..4bc4f7ae9 100644
--- a/tribits/examples/TribitsExampleProject2/packages/package1/CMakeLists.txt
+++ b/tribits/examples/TribitsExampleProject2/packages/package1/CMakeLists.txt
@@ -55,12 +55,7 @@ else()
     @ONLY )
 
   # Generate and install the Package1Config.cmake file for the install tree
-  if (COMMAND tribits_package)
-    set(pkgConfigInstallDir
-      "${${CMAKE_PROJECT_NAME}_INSTALL_LIB_DIR}/cmake/${PACKAGE_NAME}")
-  else()
-    set(pkgConfigInstallDir "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}")
-  endif()
+  set(pkgConfigInstallDir "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}")
   install(EXPORT ${PROJECT_NAME}
     DESTINATION "${pkgConfigInstallDir}"
     NAMESPACE ${PROJECT_NAME}::
@@ -75,11 +70,3 @@ else()
     DESTINATION "${pkgConfigInstallDir}" )
 
 endif()
-
-
-# NOTE: Above, the variable CMAKE_INSTALL_LIBDIR set by
-# include(GNUInstallDirs) is different that what is used by TriBITS by default
-# in at least some cases.  That is very irritating and this is something that
-# needs to be fixed (see TriBITSPub/TriBITS#411).  But note that the location
-# of the libraries does not have to be the same as the installed
-# `<Package>Config.cmake`.
\ No newline at end of file