Support SBOM generation with CMake 4.5

CMake 4.5 provides SBOM generation without an experimental gate and
rejects the LICENSE argument of install(SBOM), which breaks the
configuration. It also fails to evaluate the link-only dependencies that
static libraries propagate to their consumers.

Generate the SBOM without the gate on CMake 4.5 and let the license of
each component follow its SPDX_LICENSE property. Skip the SBOM for static
Ceres builds on CMake 4.5 since their interface link libraries cannot be
processed.

Change-Id: I946b622f543c2f50ee7794004867e17d423fbff0
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 35890ab..7686e90 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -31,15 +31,18 @@
 
 cmake_minimum_required(VERSION 3.22...4.4)
 
-# Opt into the experimental software bill of materials (SBOM) generation.
-# CMake enables an experimental feature only if its gate variable is set to a
-# specific UUID. The UUID acknowledges that the feature is unstable and may
-# change with every CMake release. The values used here are documented in the
-# corresponding CMake sources:
+# Opt into the experimental software bill of materials (SBOM) generation of
+# CMake 4.3 and 4.4. CMake enables an experimental feature only if its gate
+# variable is set to a specific UUID. The UUID acknowledges that the feature is
+# unstable and may change with every CMake release. The values used here are
+# documented in the corresponding CMake sources:
 #
 # https://gitlab.kitware.com/cmake/cmake/-/blob/v4.3.5/Help/dev/experimental.rst
 # https://gitlab.kitware.com/cmake/cmake/-/blob/v4.4.4/Help/dev/experimental.rst
-if (CMAKE_VERSION VERSION_GREATER_EQUAL 4.3)
+#
+# CMake 4.5 provides SBOM generation without a gate.
+if (CMAKE_VERSION VERSION_GREATER_EQUAL 4.3
+    AND CMAKE_VERSION VERSION_LESS 4.5)
   if (CMAKE_VERSION VERSION_LESS 4.4)
     set(CMAKE_EXPERIMENTAL_GENERATE_SBOM
       ca494ed3-b261-4205-a01f-603c95e4cae0)
@@ -47,7 +50,8 @@
     set(CMAKE_EXPERIMENTAL_GENERATE_SBOM
       2d856d6d-53e8-488b-a17f-d486d2cac317)
   endif (CMAKE_VERSION VERSION_LESS 4.4)
-endif (CMAKE_VERSION VERSION_GREATER_EQUAL 4.3)
+endif (CMAKE_VERSION VERSION_GREATER_EQUAL 4.3
+  AND CMAKE_VERSION VERSION_LESS 4.5)
 
 # NOTE: The 'generic' CMake variables CMAKE_[SOURCE/BINARY]_DIR should not be
 #       used.  Always use the project-specific variants (generated by CMake):
@@ -755,45 +759,66 @@
 endif (TARGET ceres_cuda_kernels)
 
 # SBOM generation fails for targets whose link libraries contain generator
-# expressions, such as the whole archive linkage of a static Abseil.
+# expressions, such as the whole archive linkage of a static Abseil. CMake 4.5
+# additionally fails to evaluate the $<LINK_ONLY:...> entries which propagate
+# the private dependencies of static libraries through
+# INTERFACE_LINK_LIBRARIES.
 set(_Ceres_SBOM_SUPPORTED OFF)
 
-if (CMAKE_VERSION VERSION_GREATER_EQUAL 4.3
-    AND CMAKE_EXPERIMENTAL_GENERATE_SBOM)
+if (CMAKE_VERSION VERSION_GREATER_EQUAL 4.5
+    OR (CMAKE_VERSION VERSION_GREATER_EQUAL 4.3
+      AND CMAKE_EXPERIMENTAL_GENERATE_SBOM))
   set(_Ceres_SBOM_SUPPORTED ON)
+  set(_Ceres_SBOM_LINK_PROPERTIES LINK_LIBRARIES)
+
+  if (CMAKE_VERSION VERSION_GREATER_EQUAL 4.5)
+    list(APPEND _Ceres_SBOM_LINK_PROPERTIES INTERFACE_LINK_LIBRARIES)
+  endif (CMAKE_VERSION VERSION_GREATER_EQUAL 4.5)
 
   foreach (_Ceres_TARGET IN ITEMS ceres ceres_cuda_kernels)
     if (NOT TARGET ${_Ceres_TARGET})
       continue()
     endif (NOT TARGET ${_Ceres_TARGET})
 
-    get_target_property(_Ceres_LINK_LIBRARIES ${_Ceres_TARGET} LINK_LIBRARIES)
+    foreach (_Ceres_PROPERTY IN LISTS _Ceres_SBOM_LINK_PROPERTIES)
+      get_target_property(_Ceres_LINK_LIBRARIES ${_Ceres_TARGET}
+        ${_Ceres_PROPERTY})
 
-    if (_Ceres_LINK_LIBRARIES MATCHES "\\$<")
-      message(WARNING "Skipping SBOM generation because the link libraries "
-        "of target ${_Ceres_TARGET} contain generator expressions: "
-        "${_Ceres_LINK_LIBRARIES}")
-      set(_Ceres_SBOM_SUPPORTED OFF)
-    endif (_Ceres_LINK_LIBRARIES MATCHES "\\$<")
+      if (_Ceres_LINK_LIBRARIES MATCHES "\\$<")
+        message(WARNING "Skipping SBOM generation because the "
+          "${_Ceres_PROPERTY} of target ${_Ceres_TARGET} contain generator "
+          "expressions: ${_Ceres_LINK_LIBRARIES}")
+        set(_Ceres_SBOM_SUPPORTED OFF)
+      endif (_Ceres_LINK_LIBRARIES MATCHES "\\$<")
+    endforeach (_Ceres_PROPERTY IN LISTS _Ceres_SBOM_LINK_PROPERTIES)
   endforeach (_Ceres_TARGET IN ITEMS ceres ceres_cuda_kernels)
 
   unset(_Ceres_LINK_LIBRARIES)
+  unset(_Ceres_PROPERTY)
+  unset(_Ceres_SBOM_LINK_PROPERTIES)
   unset(_Ceres_TARGET)
-endif (CMAKE_VERSION VERSION_GREATER_EQUAL 4.3
-  AND CMAKE_EXPERIMENTAL_GENERATE_SBOM)
+endif (CMAKE_VERSION VERSION_GREATER_EQUAL 4.5
+  OR (CMAKE_VERSION VERSION_GREATER_EQUAL 4.3
+    AND CMAKE_EXPERIMENTAL_GENERATE_SBOM))
 
 if (_Ceres_SBOM_SUPPORTED)
   set(_Ceres_SBOM_ARGUMENTS
     PROJECT Ceres
     DESTINATION ${CMAKE_INSTALL_DATADIR}/sbom
     VERSION ${CERES_VERSION}
-    LICENSE ${_Ceres_SPDX_LICENSE}
     DESCRIPTION "${PROJECT_DESCRIPTION}"
     HOMEPAGE_URL ${PROJECT_HOMEPAGE_URL}
     FORMAT spdx-3.0+json
   )
 
-  if (CMAKE_VERSION VERSION_GREATER_EQUAL 4.4)
+  if (CMAKE_VERSION VERSION_GREATER_EQUAL 4.5)
+    # The license of each component is taken from its SPDX_LICENSE property.
+    install(SBOM Ceres
+      EXPORTS CeresExport
+      PACKAGE_URL https://github.com/ceres-solver/ceres-solver
+      ${_Ceres_SBOM_ARGUMENTS}
+    )
+  elseif (CMAKE_VERSION VERSION_GREATER_EQUAL 4.4)
     # The experimental SBOM support is enabled deliberately. Silence the
     # corresponding warning for this command only.
     block(SCOPE_FOR DIAGNOSTICS)
@@ -801,17 +826,19 @@
       install(SBOM Ceres
         EXPORTS CeresExport
         PACKAGE_URL https://github.com/ceres-solver/ceres-solver
+        LICENSE ${_Ceres_SPDX_LICENSE}
         ${_Ceres_SBOM_ARGUMENTS}
       )
     endblock()
-  else (CMAKE_VERSION VERSION_GREATER_EQUAL 4.4)
+  else (CMAKE_VERSION VERSION_GREATER_EQUAL 4.5)
     # CMake 4.3 provides no means to silence the warning from within the
     # project. Users can pass -Wno-dev instead.
     install(SBOM Ceres
       EXPORT CeresExport
+      LICENSE ${_Ceres_SPDX_LICENSE}
       ${_Ceres_SBOM_ARGUMENTS}
     )
-  endif (CMAKE_VERSION VERSION_GREATER_EQUAL 4.4)
+  endif (CMAKE_VERSION VERSION_GREATER_EQUAL 4.5)
 
   unset(_Ceres_SBOM_ARGUMENTS)
 endif (_Ceres_SBOM_SUPPORTED)