From db7d059f8dab26d3a98661c6651d7f61f06c8033 Mon Sep 17 00:00:00 2001
From: Santiago Ospina De Los Rios <sospinar@gmail.com>
Date: Fri, 14 Apr 2023 14:53:35 +0200
Subject: [PATCH] Add deprecation warning on old exported names

---
 CMakeLists.txt                            |  2 +-
 cmake/modules/DuneAddLibrary.cmake        | 10 +++++++-
 cmake/modules/DuneAddPybind11Module.cmake |  2 +-
 cmake/modules/DuneProject.cmake           | 28 +++++++++++++++++++++++
 cmake/modules/DuneTestMacros.cmake        |  2 +-
 cmake/pkg/dune-common-config.cmake.in     |  8 ++++++-
 6 files changed, 47 insertions(+), 5 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index d90499006..9da21749a 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -14,7 +14,7 @@ include(DuneMacros)
 dune_project()
 
 # Create the module library for dune-common
-dune_add_library(dunecommon)
+dune_add_library(dunecommon EXPORT_NAME Common)
 
 # minimal c++ standard required
 target_compile_features(dunecommon PUBLIC cxx_std_17)
diff --git a/cmake/modules/DuneAddLibrary.cmake b/cmake/modules/DuneAddLibrary.cmake
index 627052804..d33461b21 100644
--- a/cmake/modules/DuneAddLibrary.cmake
+++ b/cmake/modules/DuneAddLibrary.cmake
@@ -186,9 +186,13 @@ function(dune_add_library_normal _name)
     set(${ProjectName}_EXPORT_SET ${ProjectName}-targets CACHE INTERNAL "")
   endif()
 
+  # Register library in global property <module>LIBRARIES
   if(NOT ARG_NO_MODULE_LIBRARY)
-    # Register library in global property <module>LIBRARIES
     set_property(GLOBAL APPEND PROPERTY ${ProjectName}_LIBRARIES Dune::${ARG_EXPORT_NAME})
+    if(NOT ARG_NO_EXPORT)
+      set_property(GLOBAL APPEND PROPERTY ${ProjectName}_LIBRARIES_ALIASES "${_name}:=Dune::${ARG_EXPORT_NAME}")
+      get_property(_library_aliases GLOBAL PROPERTY ${ProjectName}_LIBRARIES_ALIASES)
+    endif()
   endif()
 endfunction(dune_add_library_normal)
 
@@ -214,6 +218,7 @@ function(dune_add_library_interface _name)
   if(NOT ARG_EXPORT_NAME)
     set(ARG_EXPORT_NAME ${_name})
   endif()
+
   add_library(Dune::${ARG_EXPORT_NAME} ALIAS ${_name})
 
 
@@ -231,6 +236,9 @@ function(dune_add_library_interface _name)
   # Register library in global property <module>_LIBRARIES
   if(NOT ARG_NO_MODULE_LIBRARY)
     set_property(GLOBAL APPEND PROPERTY ${ProjectName}_LIBRARIES Dune::${ARG_EXPORT_NAME})
+    if(NOT ARG_NO_EXPORT)
+      set_property(GLOBAL APPEND PROPERTY ${ProjectName}_LIBRARIES_ALIASES "${_name}:=Dune::${ARG_EXPORT_NAME}")
+    endif()
   endif()
 endfunction(dune_add_library_interface)
 
diff --git a/cmake/modules/DuneAddPybind11Module.cmake b/cmake/modules/DuneAddPybind11Module.cmake
index 90c5273ea..8904987c5 100644
--- a/cmake/modules/DuneAddPybind11Module.cmake
+++ b/cmake/modules/DuneAddPybind11Module.cmake
@@ -86,7 +86,7 @@ function(dune_add_pybind11_module)
   endif()
 
   target_compile_definitions(${PYBIND11_MODULE_NAME} PRIVATE ${PYBIND11_MODULE_COMPILE_DEFINITIONS})
-  target_link_libraries(${PYBIND11_MODULE_NAME} PUBLIC dunecommon)
+  target_link_libraries(${PYBIND11_MODULE_NAME} PUBLIC Dune::Common)
   dune_target_enable_all_packages(${PYBIND11_MODULE_NAME})
 
   if(PYBIND11_MODULE_EXCLUDE_FROM_ALL)
diff --git a/cmake/modules/DuneProject.cmake b/cmake/modules/DuneProject.cmake
index ba85028c4..eef53f7a9 100644
--- a/cmake/modules/DuneProject.cmake
+++ b/cmake/modules/DuneProject.cmake
@@ -87,6 +87,11 @@ macro(dune_project)
   define_property(GLOBAL PROPERTY ${ProjectName}_LIBRARIES
         BRIEF_DOCS "List of libraries of the module. DO NOT EDIT!"
         FULL_DOCS "List of libraries of the module. Used to generate CMake's package configuration files. DO NOT EDIT!")
+
+  define_property(GLOBAL PROPERTY ${ProjectName}_LIBRARIES_ALIASES
+        BRIEF_DOCS "List of library aliases of the module. DO NOT EDIT!"
+        FULL_DOCS "List of libraries aliases of the module. Used to generate CMake's package configuration files. DO NOT EDIT!")
+
   dune_create_dependency_tree()
 
   # assert the project names matches
@@ -207,6 +212,7 @@ ${DUNE_CUSTOM_PKG_CONFIG_SECTION}
 if(${ProjectName}_LIBRARIES)
   get_filename_component(_dir \"\${CMAKE_CURRENT_LIST_FILE}\" PATH)
   include(\"\${_dir}/${ProjectName}-targets.cmake\")
+  @DUNE_DEPRECATED_LIBRARY_ALIASES@
 endif()
 
 endif()")
@@ -228,6 +234,28 @@ endif()")
     set(DUNE_INSTALL_LIBDIR ${DUNE_INSTALL_NONOBJECTLIBDIR})
   endif()
 
+  # setup deprecated aliases
+  get_property(_library_aliases GLOBAL PROPERTY ${ProjectName}_LIBRARIES_ALIASES)
+  set(DUNE_DEPRECATED_LIBRARY_ALIASES "")
+  foreach(_alias "${_library_aliases}")
+    string(FIND "${_alias}" ":=" _pos)
+    if(NOT _pos EQUAL "-1")
+      string(SUBSTRING "${_alias}" 0 ${_pos} _alias_name)
+      math(EXPR _pos "${_pos}+2")
+      string(SUBSTRING "${_alias}" ${_pos} -1 _export_name)
+      set(DUNE_DEPRECATED_LIBRARY_ALIASES "${DUNE_DEPRECATED_LIBRARY_ALIASES}
+add_library(${_alias_name} INTERFACE)
+target_link_libraries(${_alias_name} INTERFACE ${_export_name})
+if(CMAKE_VERSION VERSION_GREATER_EQUAL \"3.17\")
+  set_property(TARGET ${_alias_name} PROPERTY DEPRECATION \"Use ${_export_name} instead.\")
+endif()")
+      unset(_alias_name)
+      unset(_export_name)
+    endif()
+    unset(_pos)
+  endforeach(_alias)
+  unset(_library_aliases)
+
   # Set the location of the doc file source. Needed by custom package configuration
   # file section of dune-grid.
   set(DUNE_MODULE_SRC_DOCDIR "${PROJECT_SOURCE_DIR}/doc")
diff --git a/cmake/modules/DuneTestMacros.cmake b/cmake/modules/DuneTestMacros.cmake
index fd181726d..ccdf3a977 100644
--- a/cmake/modules/DuneTestMacros.cmake
+++ b/cmake/modules/DuneTestMacros.cmake
@@ -343,7 +343,7 @@ function(dune_add_test)
   endif()
 
   # add some default libraries to link against
-  list(APPEND ADDTEST_LINK_LIBRARIES dunecommon)
+  list(APPEND ADDTEST_LINK_LIBRARIES Dune::Common)
   list(REMOVE_DUPLICATES ADDTEST_LINK_LIBRARIES)
 
   # Add the executable if it is not already present
diff --git a/cmake/pkg/dune-common-config.cmake.in b/cmake/pkg/dune-common-config.cmake.in
index fcf2f310e..3493e0571 100644
--- a/cmake/pkg/dune-common-config.cmake.in
+++ b/cmake/pkg/dune-common-config.cmake.in
@@ -16,7 +16,7 @@ set(@DUNE_MOD_NAME@_CXX_FLAGS_DEBUG "@CMAKE_CXX_FLAGS_DEBUG@")
 set(@DUNE_MOD_NAME@_CXX_FLAGS_MINSIZEREL "@CMAKE_CXX_FLAGS_MINSIZEREL@")
 set(@DUNE_MOD_NAME@_CXX_FLAGS_RELEASE "@CMAKE_CXX_FLAGS_RELEASE@")
 set(@DUNE_MOD_NAME@_CXX_FLAGS_RELWITHDEBINFO "@CMAKE_CXX_FLAGS_RELWITHDEBINFO@")
-set(@DUNE_MOD_NAME@_LIBRARIES "dunecommon")
+set(@DUNE_MOD_NAME@_LIBRARIES Dune::Common)
 set_and_check(@DUNE_MOD_NAME@_SCRIPT_DIR "@PACKAGE_SCRIPT_DIR@")
 set_and_check(DOXYSTYLE_FILE "@PACKAGE_DOXYSTYLE_DIR@/Doxystyle")
 set_and_check(DOXYGENMACROS_FILE "@PACKAGE_DOXYSTYLE_DIR@/doxygen-macros")
@@ -26,3 +26,9 @@ set(@DUNE_MOD_NAME@_HASPYTHON 1)
 set(@DUNE_MOD_NAME@_PYTHONREQUIRES "@DUNE_MODULE_PYTHONREQUIRES@")
 set_and_check(@DUNE_MOD_NAME@_MODULE_PATH "@PACKAGE_DUNE_INSTALL_MODULEDIR@")
 endif(NOT @DUNE_MOD_NAME@_FOUND)
+
+add_library(dunecommon INTERFACE)
+target_link_libraries(dunecommon INTERFACE Dune::Common)
+if(CMAKE_VERSION VERSION_GREATER_EQUAL \"3.17\")
+  set_property(TARGET dunecommon PROPERTY DEPRECATION \"Use Dune::Common instead.\")
+endif()
-- 
GitLab