From fc9828448d54a63b7fc09ee044b6b0246632e011 Mon Sep 17 00:00:00 2001
From: Kris Thielemans <kris.f.thielemans@hotmail.com>
Date: Tue, 18 Jul 2017 01:11:23 +0100
Subject: FindHDF5: fix hdf5-config case with more than one configuration

Select a configuration as the first of the following available:

* MAP_IMPORTED_CONFIG_<CONFIG>
* ${CMAKE_BUILD_TYPE}, RELWITHDEBINFO, RELEASE, DEBUG
* First entry in IMPORTED_CONFIGURATIONS

Fixes: #17039
---
 Modules/FindHDF5.cmake | 46 +++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 43 insertions(+), 3 deletions(-)

diff --git a/Modules/FindHDF5.cmake b/Modules/FindHDF5.cmake
index 48d5de4..d3fd0ee 100644
--- a/Modules/FindHDF5.cmake
+++ b/Modules/FindHDF5.cmake
@@ -396,6 +396,45 @@ macro( _HDF5_parse_compile_line
   endforeach()
 endmacro()
 
+# Select a preferred imported configuration from a target
+function(_HDF5_select_imported_config target imported_conf)
+    # We will first assign the value to a local variable _imported_conf, then assign
+    # it to the function argument at the end.
+    get_target_property(_imported_conf ${target} MAP_IMPORTED_CONFIG_${CMAKE_BUILD_TYPE})
+    if (NOT _imported_conf)
+        # Get available imported configurations by examining target properties
+        get_target_property(_imported_conf ${target} IMPORTED_CONFIGURATIONS)
+        if(HDF5_FIND_DEBUG)
+            message(STATUS "Found imported configurations: ${_imported_conf}")
+        endif()
+        # Find the imported configuration that we prefer.
+        # We do this by making list of configurations in order of preference,
+        # starting with ${CMAKE_BUILD_TYPE} and ending with the first imported_conf
+        set(_preferred_confs ${CMAKE_BUILD_TYPE})
+        list(GET _imported_conf 0 _fallback_conf)
+        list(APPEND _preferred_confs RELWITHDEBINFO RELEASE DEBUG ${_fallback_conf})
+        if(HDF5_FIND_DEBUG)
+            message(STATUS "Start search through imported configurations in the following order: ${_preferred_confs}")
+        endif()
+        # Now find the first of these that is present in imported_conf
+        cmake_policy(PUSH)
+        cmake_policy(SET CMP0057 NEW) # support IN_LISTS
+        foreach (_conf IN LISTS _preferred_confs)
+            if (${_conf} IN_LIST _imported_conf)
+               set(_imported_conf ${_conf})
+               break()
+            endif()
+        endforeach()
+        cmake_policy(POP)
+    endif()
+    if(HDF5_FIND_DEBUG)
+        message(STATUS "Selected imported configuration: ${_imported_conf}")
+    endif()
+    # assign value to function argument
+    set(${imported_conf} ${_imported_conf} PARENT_SCOPE)
+endfunction()
+
+
 if(NOT HDF5_ROOT)
     set(HDF5_ROOT $ENV{HDF5_ROOT})
 endif()
@@ -452,8 +491,8 @@ if(NOT HDF5_FOUND AND NOT HDF5_NO_FIND_PACKAGE_CONFIG_FILE)
                 message(STATUS "Trying to get properties of target ${HDF5_${_lang}_TARGET}${_suffix}")
             endif()
             # Find library for this target. Complicated as on Windows with a DLL, we need to search for the import-lib.
-            get_target_property(_imported_conf ${HDF5_${_lang}_TARGET}${_suffix} IMPORTED_CONFIGURATIONS)
-            get_target_property(_lang_location ${HDF5_${_lang}_TARGET}${_suffix} IMPORTED_IMPLIB_${_imported_conf} )
+            _HDF5_select_imported_config(${HDF5_${_lang}_TARGET}${_suffix} _hdf5_imported_conf)
+            get_target_property(_lang_location ${HDF5_${_lang}_TARGET}${_suffix} IMPORTED_IMPLIB_${_hdf5_imported_conf} )
             if (NOT _lang_location)
                 # no import lib, just try LOCATION
                 get_target_property(_lang_location ${HDF5_${_lang}_TARGET}${_suffix} LOCATION)
@@ -465,7 +504,7 @@ if(NOT HDF5_FOUND AND NOT HDF5_NO_FIND_PACKAGE_CONFIG_FILE)
                 set(HDF5_${_lang}_FOUND True)
             endif()
             if(FIND_HL)
-                get_target_property(__lang_hl_location ${HDF5_${_lang}_HL_TARGET}${_suffix} IMPORTED_IMPLIB_${_imported_conf} )
+                get_target_property(__lang_hl_location ${HDF5_${_lang}_HL_TARGET}${_suffix} IMPORTED_IMPLIB_${_hdf5_imported_conf} )
                 if (NOT _lang_hl_location)
                     get_target_property(_lang_hl_location ${HDF5_${_lang}_HL_TARGET}${_suffix} LOCATION)
                 endif()
@@ -476,6 +515,7 @@ if(NOT HDF5_FOUND AND NOT HDF5_NO_FIND_PACKAGE_CONFIG_FILE)
                     set(HDF5_HL_FOUND True)
                 endif()
             endif()
+            unset( _hdf5_imported_conf)
         endforeach()
     endif()
 endif()
-- 
cgit v0.12


From 9310d00822378e24dbd43849f9892d2f775ca3ff Mon Sep 17 00:00:00 2001
From: Kris Thielemans <kris.f.thielemans@hotmail.com>
Date: Mon, 24 Jul 2017 09:16:29 +0200
Subject: FindHDF5: for a static imported config, use CONFIG-specific library

first check the LOCATION_<CONFIG> propertiy to find the library, then
LOCATION
---
 Modules/FindHDF5.cmake | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/Modules/FindHDF5.cmake b/Modules/FindHDF5.cmake
index d3fd0ee..bbcfe75 100644
--- a/Modules/FindHDF5.cmake
+++ b/Modules/FindHDF5.cmake
@@ -495,7 +495,10 @@ if(NOT HDF5_FOUND AND NOT HDF5_NO_FIND_PACKAGE_CONFIG_FILE)
             get_target_property(_lang_location ${HDF5_${_lang}_TARGET}${_suffix} IMPORTED_IMPLIB_${_hdf5_imported_conf} )
             if (NOT _lang_location)
                 # no import lib, just try LOCATION
-                get_target_property(_lang_location ${HDF5_${_lang}_TARGET}${_suffix} LOCATION)
+                get_target_property(_lang_location ${HDF5_${_lang}_TARGET}${_suffix} LOCATION_${_hdf5_imported_conf})
+                if (NOT _lang_location)
+                    get_target_property(_lang_location ${HDF5_${_lang}_TARGET}${_suffix} LOCATION)
+                endif()
             endif()
             if( _lang_location )
                 set(HDF5_${_lang}_LIBRARY ${_lang_location})
-- 
cgit v0.12


From 4150048a3b8147da3a3fc130a39634ac651c64fe Mon Sep 17 00:00:00 2001
From: Kris Thielemans <kris.f.thielemans@hotmail.com>
Date: Mon, 24 Jul 2017 09:30:06 +0200
Subject: FindHDF5: unset some variables

prefixed some local variables with _hdf5 and unset them at the end
(still more of these to do)
---
 Modules/FindHDF5.cmake | 26 ++++++++++++++------------
 1 file changed, 14 insertions(+), 12 deletions(-)

diff --git a/Modules/FindHDF5.cmake b/Modules/FindHDF5.cmake
index bbcfe75..b1bf097 100644
--- a/Modules/FindHDF5.cmake
+++ b/Modules/FindHDF5.cmake
@@ -492,33 +492,35 @@ if(NOT HDF5_FOUND AND NOT HDF5_NO_FIND_PACKAGE_CONFIG_FILE)
             endif()
             # Find library for this target. Complicated as on Windows with a DLL, we need to search for the import-lib.
             _HDF5_select_imported_config(${HDF5_${_lang}_TARGET}${_suffix} _hdf5_imported_conf)
-            get_target_property(_lang_location ${HDF5_${_lang}_TARGET}${_suffix} IMPORTED_IMPLIB_${_hdf5_imported_conf} )
-            if (NOT _lang_location)
+            get_target_property(_hdf5_lang_location ${HDF5_${_lang}_TARGET}${_suffix} IMPORTED_IMPLIB_${_hdf5_imported_conf} )
+            if (NOT _hdf5_lang_location)
                 # no import lib, just try LOCATION
-                get_target_property(_lang_location ${HDF5_${_lang}_TARGET}${_suffix} LOCATION_${_hdf5_imported_conf})
-                if (NOT _lang_location)
-                    get_target_property(_lang_location ${HDF5_${_lang}_TARGET}${_suffix} LOCATION)
+                get_target_property(_hdf5_lang_location ${HDF5_${_lang}_TARGET}${_suffix} LOCATION_${_hdf5_imported_conf})
+                if (NOT _hdf5_lang_location)
+                    get_target_property(_hdf5_lang_location ${HDF5_${_lang}_TARGET}${_suffix} LOCATION)
                 endif()
             endif()
-            if( _lang_location )
-                set(HDF5_${_lang}_LIBRARY ${_lang_location})
+            if( _hdf5_lang_location )
+                set(HDF5_${_lang}_LIBRARY ${_hdf5_lang_location})
                 list(APPEND HDF5_LIBRARIES ${HDF5_${_lang}_TARGET}${_suffix})
                 set(HDF5_${_lang}_LIBRARIES ${HDF5_${_lang}_TARGET}${_suffix})
                 set(HDF5_${_lang}_FOUND True)
             endif()
             if(FIND_HL)
                 get_target_property(__lang_hl_location ${HDF5_${_lang}_HL_TARGET}${_suffix} IMPORTED_IMPLIB_${_hdf5_imported_conf} )
-                if (NOT _lang_hl_location)
-                    get_target_property(_lang_hl_location ${HDF5_${_lang}_HL_TARGET}${_suffix} LOCATION)
+                if (NOT _hdf5_lang_hl_location)
+                    get_target_property(_hdf5_lang_hl_location ${HDF5_${_lang}_HL_TARGET}${_suffix} LOCATION)
                 endif()
-                if( _lang_hl_location )
-                    set(HDF5_${_lang}_HL_LIBRARY ${_lang_hl_location})
+                if( _hdf5_lang_hl_location )
+                    set(HDF5_${_lang}_HL_LIBRARY ${_hdf5_lang_hl_location})
                     list(APPEND HDF5_HL_LIBRARIES ${HDF5_${_lang}_TARGET}${_suffix})
                     set(HDF5_${_lang}_HL_LIBRARIES ${HDF5_${_lang}_TARGET}${_suffix})
                     set(HDF5_HL_FOUND True)
                 endif()
+                unset(_hdf5_lang_hl_location)
             endif()
-            unset( _hdf5_imported_conf)
+            unset(_hdf5_imported_conf)
+            unset(_hdf5_lang_location)
         endforeach()
     endif()
 endif()
-- 
cgit v0.12


From 4956fc26b5bcf1cc9a0c7f885909a0bebbffaba4 Mon Sep 17 00:00:00 2001
From: Kris Thielemans <kris.f.thielemans@hotmail.com>
Date: Mon, 24 Jul 2017 09:46:30 +0200
Subject: FindHDF5: fixes for HL when using an imported config

- some variables did not have the appropriate HL infix
- some logic was not yet implemented for HL
---
 Modules/FindHDF5.cmake | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/Modules/FindHDF5.cmake b/Modules/FindHDF5.cmake
index b1bf097..de20049 100644
--- a/Modules/FindHDF5.cmake
+++ b/Modules/FindHDF5.cmake
@@ -509,12 +509,15 @@ if(NOT HDF5_FOUND AND NOT HDF5_NO_FIND_PACKAGE_CONFIG_FILE)
             if(FIND_HL)
                 get_target_property(__lang_hl_location ${HDF5_${_lang}_HL_TARGET}${_suffix} IMPORTED_IMPLIB_${_hdf5_imported_conf} )
                 if (NOT _hdf5_lang_hl_location)
-                    get_target_property(_hdf5_lang_hl_location ${HDF5_${_lang}_HL_TARGET}${_suffix} LOCATION)
+                    get_target_property(_hdf5_lang_hl_location ${HDF5_${_lang}_HL_TARGET}${_suffix} LOCATION_${_hdf5_imported_conf})
+                    if (NOT _hdf5_hl_lang_location)
+                        get_target_property(_hdf5_hl_lang_location ${HDF5_${_lang}_HL_TARGET}${_suffix} LOCATION)
+                    endif()
                 endif()
                 if( _hdf5_lang_hl_location )
                     set(HDF5_${_lang}_HL_LIBRARY ${_hdf5_lang_hl_location})
-                    list(APPEND HDF5_HL_LIBRARIES ${HDF5_${_lang}_TARGET}${_suffix})
-                    set(HDF5_${_lang}_HL_LIBRARIES ${HDF5_${_lang}_TARGET}${_suffix})
+                    list(APPEND HDF5_HL_LIBRARIES ${HDF5_${_lang}_HL_TARGET}${_suffix})
+                    set(HDF5_${_lang}_HL_LIBRARIES ${HDF5_${_lang}_HL_TARGET}${_suffix})
                     set(HDF5_HL_FOUND True)
                 endif()
                 unset(_hdf5_lang_hl_location)
@@ -918,11 +921,14 @@ if (HDF5_FIND_DEBUG)
   message(STATUS "HDF5_DEFINITIONS: ${HDF5_DEFINITIONS}")
   message(STATUS "HDF5_INCLUDE_DIRS: ${HDF5_INCLUDE_DIRS}")
   message(STATUS "HDF5_LIBRARIES: ${HDF5_LIBRARIES}")
+  message(STATUS "HDF5_HL_LIBRARIES: ${HDF5_HL_LIBRARIES}")
   foreach(__lang IN LISTS HDF5_LANGUAGE_BINDINGS)
     message(STATUS "HDF5_${__lang}_DEFINITIONS: ${HDF5_${__lang}_DEFINITIONS}")
     message(STATUS "HDF5_${__lang}_INCLUDE_DIR: ${HDF5_${__lang}_INCLUDE_DIR}")
     message(STATUS "HDF5_${__lang}_INCLUDE_DIRS: ${HDF5_${__lang}_INCLUDE_DIRS}")
     message(STATUS "HDF5_${__lang}_LIBRARY: ${HDF5_${__lang}_LIBRARY}")
     message(STATUS "HDF5_${__lang}_LIBRARIES: ${HDF5_${__lang}_LIBRARIES}")
+    message(STATUS "HDF5_${__lang}_HL_LIBRARY: ${HDF5_${__lang}_HL_LIBRARY}")
+    message(STATUS "HDF5_${__lang}_HL_LIBRARIES: ${HDF5_${__lang}_HL_LIBRARIES}")
   endforeach()
 endif()
-- 
cgit v0.12