Salome HOME
Fix detection of HDF5.
[tools/configuration.git] / cmake / FindSalomeHDF5.cmake
1 # Copyright (C) 2013-2021  CEA/DEN, EDF R&D, OPEN CASCADE
2 #
3 # This library is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU Lesser General Public
5 # License as published by the Free Software Foundation; either
6 # version 2.1 of the License, or (at your option) any later version.
7 #
8 # This library is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 # Lesser General Public License for more details.
12 #
13 # You should have received a copy of the GNU Lesser General Public
14 # License along with this library; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 #
17 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 #
19 # Author: Adrien Bruneton
20 #
21
22 # HDF5 detection for Salome
23 #
24 #  !! Please read the generic detection procedure in SalomeMacros.cmake !!
25 #
26 # --- HDF5 specificities ----
27 #  MPI root directory used for HDF5 compilation is exposed into MPI_ROOT_DIR_EXP
28 #
29
30 SET(HDF5_ROOT_DIR $ENV{HDF5_ROOT_DIR} CACHE PATH "Path to the HDF5.")
31
32 IF(HDF5_ROOT_DIR)
33   SET (HDF5_ROOT ${HDF5_ROOT_DIR})
34   IF(WIN32)
35     SET(HDF5_DIR ${HDF5_ROOT_DIR}/cmake/hdf5)
36   ENDIF()
37 ENDIF()
38
39 FIND_PACKAGE(HDF5)
40 MARK_AS_ADVANCED(FORCE HDF5_INCLUDE_DIR HDF5_LIB HDF5_DIR)
41
42 # Stupidly enough, CONFIG mode and MODULE mode for HDF5 do not return the same thing ...!
43 SET(HDF5_INCLUDE_DIRS "${HDF5_INCLUDE_DIRS};${HDF5_INCLUDE_DIR}")
44 # Same story with libraries - if in CONFIG mode, HDF5_LIBRARIES is not defined:
45 IF(NOT DEFINED HDF5_LIBRARIES)
46     IF(TARGET hdf5)
47       SET(HDF5_C_LIBRARIES hdf5)
48     ELSEIF(TARGET hdf5::hdf5-shared)
49       SET(HDF5_C_LIBRARIES hdf5::hdf5-shared)
50     ENDIF()
51     IF(TARGET hdf5_cpp)
52       SET(HDF5_CXX_LIBRARIES hdf5_cpp)
53     ELSEIF(TARGET hdf5::hdf5_cpp-shared)
54       SET(HDF5_CXX_LIBRARIES hdf5::hdf5_cpp-shared)
55     ENDIF()
56     IF(TARGET hdf5_hl)
57       SET(HDF5_C_HL_LIBRARIES hdf5_hl)
58     ELSEIF(TARGET hdf5::hdf5_hl-shared)
59       SET(HDF5_C_HL_LIBRARIES hdf5::hdf5_hl-shared)
60     ENDIF()
61     IF(TARGET hdf5_hl_cpp)
62       SET(HDF5_CXX_HL_LIBRARIES hdf5_hl_cpp)
63     ELSEIF(TARGET hdf5::hdf5_hl_cpp-shared)
64       SET(HDF5_CXX_HL_LIBRARIES hdf5::hdf5_hl_cpp-shared)
65     ENDIF()
66     # Note: now we only set HDF5_LIBRARIES to CXX libraries as it's enough for SALOME.
67     # In future, we probably must list all libraries from requested components.
68     SET(HDF5_LIBRARIES ${HDF5_CXX_LIBRARIES})
69 ENDIF()
70
71 ##
72 ## 7. Specific to HDF5 only:
73 ## Expose MPI configuration to the rest of the world
74 ##
75 IF(HDF5_ENABLE_PARALLEL OR HDF5_IS_PARALLEL)
76   # Set only one reference boolean variable:
77   # (unfortunately what is found in /usr/share/cmake/Modules/FindHDF5.cmake
78   #  and in the native HDF5-config.cmake differ!)
79   SET(HDF5_IS_PARALLEL TRUE)
80
81   # HDF5 was compiled with MPI support
82   # Unfortunately HDF5 doesn't expose its MPI configuration easily ...
83   # We sniff the properties of the HDF5 target which should also be there:
84   IF(NOT DEFINED HDF5_LIBRARIES)
85     SET(HDF5_LIBRARIES "hdf5")
86     IF(NOT TARGET hdf5 AND NOT TARGET hdf5-static AND NOT TARGET hdf5-shared)
87       # Some HDF5 versions (e.g. 1.8.18) used hdf5::hdf5 etc
88       SET(_target_prefix "hdf5::")
89     ENDIF()
90     SET(_suffix "-shared")
91     SET(HDF5_LIBRARIES "${_target_prefix}hdf5${_suffix}")
92   ENDIF()
93   GET_PROPERTY(_lib_lst SOURCE ${HDF5_LIBRARIES} PROPERTY IMPORTED_LINK_INTERFACE_LIBRARIES_NOCONFIG)
94   FOREACH(s ${_lib_lst})
95     STRING(FIND "${s}" "mpi." _res)   # should cover WIN(?) and LINUX
96     IF(_res GREATER -1)
97       GET_FILENAME_COMPONENT(_tmp "${s}" PATH)     # go up to levels
98       GET_FILENAME_COMPONENT(MPI_ROOT_DIR_EXP "${_tmp}" PATH)
99       BREAK()
100     ENDIF()
101   ENDFOREACH()
102   IF(NOT SalomeHDF5_FIND_QUIETLY)
103     MESSAGE(STATUS "HDF5 was compiled with MPI: ${MPI_ROOT_DIR_EXP}")
104   ENDIF()
105 ENDIF()
106
107 ## Add definitions
108 ADD_DEFINITIONS(-DH5_USE_16_API)
109 IF(WIN32)
110   ADD_DEFINITIONS(-D_HDF5USEDLL_)
111 ENDIF()
112
113 ## Ensure SALOME uses MPI if HDF5 was parallel:
114 IF(HDF5_IS_PARALLEL AND NOT SALOME_USE_MPI)
115    MESSAGE(FATAL_ERROR "HDF5 is compiled with MPI, you have to set SALOME_USE_MPI to ON")
116 ENDIF()
117
118 IF(HDF5_FOUND)
119   SALOME_ACCUMULATE_HEADERS(HDF5_INCLUDE_DIRS)
120   SALOME_ACCUMULATE_ENVIRONMENT(LD_LIBRARY_PATH ${HDF5_LIBRARIES})
121 ENDIF()