HPCW 3.0
Loading...
Searching...
No Matches
FindNetCDF.cmake
Go to the documentation of this file.
1# Copyright (c) 2019 - 2023 Ralf Mueller
2# Copyright (c) 2019 - 2024 David Guibert
3# All rights reserved.
4#
5# SPDX-License-Identifier: Apache-2.0
6
7find_package(PkgConfig)
8if(PkgConfig_FOUND AND NOT TARGET PkgConfig::NetCDF)
9 set(ENV{PKG_CONFIG_ALLOW_SYSTEM_CFLAGS} 1)
10 pkg_check_modules(NetCDF_C netcdf REQUIRED IMPORTED_TARGET GLOBAL)
11 list(FIND NetCDF_STATIC_LIBRARIES "hdf5_hl" req_hdf5_hl)
12 list(FIND NetCDF_STATIC_LIBRARIES "hdf5" req_hdf5)
13 # message(STATUS "NetCDF_C_FOUND=${NetCDF_C_FOUND}") #FOUND set to 1 if
14 # module(s) exist message(STATUS "NetCDF_C_LIBRARIES=${NetCDF_C_LIBRARIES}")
15 # #LIBRARIES only the libraries (without the '-l') message(STATUS
16 # "NetCDF_C_LINK_LIBRARIES=${NetCDF_C_LINK_LIBRARIES}") #LINK_LIBRARIES the
17 # libraries and their absolute paths message(STATUS
18 # "NetCDF_C_LIBRARY_DIRS=${NetCDF_C_LIBRARY_DIRS}") #LIBRARY_DIRS the paths of
19 # the libraries (without the '-L') message(STATUS
20 # "NetCDF_C_LDFLAGS=${NetCDF_C_LDFLAGS}") #LDFLAGS all required linker flags
21 # message(STATUS "NetCDF_C_LDFLAGS_OTHER=${NetCDF_C_LDFLAGS_OTHER}")
22 # #LDFLAGS_OTHER all other linker flags message(STATUS
23 # "NetCDF_C_INCLUDE_DIRS=${NetCDF_C_INCLUDE_DIRS}") #INCLUDE_DIRS the '-I'
24 # preprocessor flags (without the '-I') message(STATUS
25 # "NetCDF_C_CFLAGS=${NetCDF_C_CFLAGS}") #CFLAGS all required cflags
26 # message(STATUS "NetCDF_C_CFLAGS_OTHER=${NetCDF_C_CFLAGS_OTHER}")
27 # #CFLAGS_OTHER the other compiler flags
28
29 # message(STATUS "NetCDF_C_STATIC_LIBRARIES=${NetCDF_C_STATIC_LIBRARIES}")
30 # #LIBRARIES only the libraries (without the '-l') message(STATUS
31 # "NetCDF_C_STATIC_LINK_LIBRARIES=${NetCDF_C_STATIC_LINK_LIBRARIES}")
32 # #LINK_LIBRARIES the libraries and their absolute paths message(STATUS
33 # "NetCDF_C_STATIC_LIBRARY_DIRS=${NetCDF_C_STATIC_LIBRARY_DIRS}")
34 # #LIBRARY_DIRS the paths of the libraries (without the '-L') message(STATUS
35 # "NetCDF_C_STATIC_LDFLAGS=${NetCDF_C_STATIC_LDFLAGS}") #LDFLAGS all required
36 # linker flags message(STATUS
37 # "NetCDF_C_STATIC_LDFLAGS_OTHER=${NetCDF_C_STATIC_LDFLAGS_OTHER}")
38 # #LDFLAGS_OTHER all other linker flags message(STATUS
39 # "NetCDF_C_STATIC_INCLUDE_DIRS=${NetCDF_C_STATIC_INCLUDE_DIRS}")
40 # #INCLUDE_DIRS the '-I' preprocessor flags (without the '-I') message(STATUS
41 # "NetCDF_C_STATIC_CFLAGS=${NetCDF_C_STATIC_CFLAGS}") #CFLAGS all required
42 # cflags message(STATUS
43 # "NetCDF_C_STATIC_CFLAGS_OTHER=${NetCDF_C_STATIC_CFLAGS_OTHER}")
44 # #CFLAGS_OTHER the other compiler flags
45
46 list(GET NetCDF_C_LIBDIR 0 NetCDF_C_LIBRARY_DIRS)
47 get_filename_component(NETCDF_DIR "${NetCDF_C_LIBDIR}" DIRECTORY)
48 set_property(GLOBAL PROPERTY NETCDF_DIR "${NETCDF_DIR}")
49 message(STATUS "NETCDF_DIR: ${NETCDF_DIR}")
50
51 set(hdf5_components "")
52 if(req_hdf5)
53 list(APPEND hdf5_components C)
54 endif()
55 if(req_hdf5_hl)
56 list(APPEND hdf5_components HL)
57 endif()
58 if(hdf5_components)
59 if(NOT TARGET hdf5::hdf5) # and hdf5::hdf5_hl
60 set(HDF5_FIND_DEBUG ON)
61 find_package(
62 HDF5
63 COMPONENTS ${hdf5_components}
64 REQUIRED)
65
66 if(NOT TARGET hdf5::hdf5) # recent CMake define this
67 add_library(hdf5::hdf5 UNKNOWN IMPORTED GLOBAL)
68 list(GET HDF5_C_LIBRARIES 0 HDF5_C_LIBRARY)
69 set_target_properties(
70 hdf5::hdf5
71 PROPERTIES IMPORTED_LOCATION "${HDF5_C_LIBRARY}"
72 COMPILE_DEFINITIONS "${HDF5_C_DEFINITIONS}"
73 INTERFACE_INCLUDE_DIRECTORIES "${HDF5_C_INCLUDE_DIRS}"
74 LINK_LIBRARIES "${HDF5_C_LIBRARIES};curl" # FIXME missing
75 # dependency
76 # found
77 )
78 endif()
79
80 if(NOT TARGET hdf5::hdf5_hl) # recent CMake define this in FindHDF5.cmake
81 add_library(hdf5::hdf5_hl UNKNOWN IMPORTED GLOBAL)
82 set_target_properties(
83 hdf5::hdf5_hl PROPERTIES LINK_LIBRARIES
84 "${HDF5_C_HL_LIBRARIES};hdf5::hdf5")
85 endif()
86 if(NOT HDF5_C_LIBRARY)
87 list(GET HDF5_C_LIBRARIES 0 HDF5_C_LIBRARY)
88 endif()
89 get_filename_component(HDF5_DIR "${HDF5_C_LIBRARY}" DIRECTORY)
90 get_filename_component(HDF5_DIR "${HDF5_DIR}" DIRECTORY)
91 message(STATUS "-> HDF5_DIR: ${HDF5_DIR}")
92 endif()
93
94 target_link_libraries(PkgConfig::NetCDF_C INTERFACE hdf5::hdf5
95 hdf5::hdf5_hl)
96 endif()
97 add_library(NetCDF::NetCDF_C ALIAS PkgConfig::NetCDF_C)
98 add_library(netcdf::C ALIAS PkgConfig::NetCDF_C)
99
100 set(CMAKE_REQUIRED_FLAGS)
101 set(CMAKE_REQUIRED_INCLUDES)
102 set(CMAKE_REQUIRED_LIBRARIES PkgConfig::NetCDF_C)
103
104 include(CheckCSourceCompiles)
105 check_c_source_compiles(
106 "
107 #include <netcdf.h>
108 #include <stdio.h>
109
110 int main(void){
111 printf(\"%s\", nc_inq_libvers());
112 return 0;
113 }
114 "
115 NetCDF_C_links)
116 if(NOT NetCDF_C_links)
117 message(FATAL_ERROR "NetCDF::NetCDF_C does not compiles")
118 endif()
119endif()
120
121if(PkgConfig_FOUND AND NOT TARGET PkgConfig::NetCDF_Fortran)
122 set(ENV{PKG_CONFIG_ALLOW_SYSTEM_CFLAGS} 1)
123 pkg_check_modules(NetCDF_Fortran netcdf-fortran REQUIRED IMPORTED_TARGET
124 GLOBAL)
125 target_link_libraries(PkgConfig::NetCDF_Fortran INTERFACE PkgConfig::NetCDF_C)
126 add_library(NetCDF::NetCDF_Fortran ALIAS PkgConfig::NetCDF_Fortran)
127 add_library(netcdf::Fortran ALIAS PkgConfig::NetCDF_Fortran)
128
129 list(GET NetCDF_Fortran_LIBDIR 0 NetCDF_Fortran_LIBRARY_DIRS)
130 get_filename_component(NETCDFF_DIR "${NetCDF_Fortran_LIBDIR}" DIRECTORY)
131 set_property(GLOBAL PROPERTY NETCDFF_DIR "${NETCDFF_DIR}")
132 message(STATUS "NETCDFF_DIR: ${NETCDFF_DIR}")
133
134 set(CMAKE_REQUIRED_FLAGS)
135 set(CMAKE_REQUIRED_INCLUDES)
136 set(CMAKE_REQUIRED_LIBRARIES PkgConfig::NetCDF_Fortran)
137 include(CheckFortranSourceCompiles)
138 CHECK_Fortran_SOURCE_COMPILES("use netcdf; end" NetCDF_Fortran_links SRC_EXT
139 f90)
140 # if(NOT NetCDF_Fortran_links)
141 # message(FATAL_ERROR "NetCDF::NetCDF_Fortran does not compiles")
142 # endif()
143endif()