Repository: AMReX-Codes/amrex — pull request #1468 (cpp)
Title: CMake: improve HDF5 support

## Summary

Fix #1463

## Additional background

## Checklist

The proposed changes:
- [ ] fix a bug or incorrect behavior in AMReX
- [ ] add new capabilities to AMReX
- [ ] changes answers in the test suite to more than roundoff level
- [ ] are likely to significantly affect the results of downstream AMReX users
- [ ] are described in the proposed changes to the AMReX documentation, if appropriate


Resolved issues:
title: Build Question - Lib folders
body: Hello there, I'm trying to build AMReX with the following libraries and versions:
```
1) gcc/9.1.1
4) cmake/3.16.7
2) mpich/3.3.1
3) petsc/3.11.3
5) hdf5/1.10.5
```
I'm on tag `20.10`, created a `build` folder and using the following options with cmake:
```
-DCMAKE_INSTALL_PREFIX=/opt/apps/amrex/20.10-mpich-3.3.1-petsc-3.11.3-hdf5-1.10.5
-DDIM=3 
-DENABLE_FORTRAN=yes 
-DENABLE_FORTRAN_INTERFACES=yes 
-DENABLE_MPI=yes 
-DENABLE_HYPRE=yes 
-DENABLE_HDF5=yes 
-DENABLE_PETSC=yes
```

How can I set the paths for those libraries I want to use? I coudn't find in the docs flags to set the path of the libraries ...
None of my libs are instaled in the system path, allways in `/opt/apps`.


Hints:

Fix patch:
diff --git a/Tools/CMake/AMReXThirdPartyLibraries.cmake b/Tools/CMake/AMReXThirdPartyLibraries.cmake
index 52ad1ff27ce..82193da3c2f 100644
--- a/Tools/CMake/AMReXThirdPartyLibraries.cmake
+++ b/Tools/CMake/AMReXThirdPartyLibraries.cmake
@@ -3,13 +3,19 @@
 #
 if (ENABLE_HDF5)
     set(HDF5_PREFER_PARALLEL TRUE)
-    find_package(HDF5 1.10.4 REQUIRED COMPONENTS CXX)
+    find_package(HDF5 1.10.4 REQUIRED)
     if (NOT HDF5_IS_PARALLEL)
         message(FATAL_ERROR "\nHDF5 library does not support parallel I/O")
+     endif ()
+
+    if (TARGET hdf5::hdf5)  # CMake >= 3.19
+       target_link_libraries(amrex PUBLIC hdf5::hdf5)
+    else ()  # CMake < 3.19 -- Remove when minimum cmake version is bumped up
+       target_include_directories(amrex PUBLIC ${HDF5_INCLUDE_DIRS})
+       target_compile_definitions(amrex PUBLIC ${HDF5_DEFINITIONS})
+       target_link_libraries(amrex PUBLIC ${HDF5_LIBRARIES})
     endif ()
-    target_include_directories(amrex PUBLIC ${HDF5_CXX_INCLUDE_DIRS})
-    target_compile_definitions(amrex PUBLIC ${HDF5_CXX_DEFINES})
-    target_link_libraries(amrex PUBLIC ${HDF5_CXX_LIBRARIES})
+
 endif ()
 
 


Test patch:
diff --git a/Tests/CMakeLists.txt b/Tests/CMakeLists.txt
index 29cd7d32e83..f97a625c78c 100644
--- a/Tests/CMakeLists.txt
+++ b/Tests/CMakeLists.txt
@@ -7,6 +7,10 @@ if (ENABLE_PARTICLES)
    list(APPEND AMREX_TESTS_SUBDIRS Particles)
 endif ()
 
+if (ENABLE_HDF5)
+   list(APPEND AMREX_TESTS_SUBDIRS HDF5Benchmark)
+endif ()
+
 list(TRANSFORM AMREX_TESTS_SUBDIRS PREPEND "${CMAKE_CURRENT_LIST_DIR}/")
 
 #
diff --git a/Tests/HDF5Benchmark/CMakeLists.txt b/Tests/HDF5Benchmark/CMakeLists.txt
new file mode 100644
index 00000000000..718a622058a
--- /dev/null
+++ b/Tests/HDF5Benchmark/CMakeLists.txt
@@ -0,0 +1,12 @@
+# This tests requires particle support
+if (NOT ENABLE_PARTICLES)
+   return()
+endif ()
+
+set(_sources     main.cpp)
+set(_input_files inputs  )
+
+setup_test(_sources _input_files)
+
+unset(_sources)
+unset(_input_files)