Debian Codegraph @

cmake/tribits/common_tpls/find_modules/FindNetCDF.cmake

raw
1 # -*- mode: cmake -*-
2 # @HEADER
3 # *****************************************************************************
4 # TriBITS: Tribal Build, Integrate, and Test System
5 #
6 # Copyright 2013-2016 NTESS and the TriBITS contributors.
7 # SPDX-License-Identifier: BSD-3-Clause
8 # *****************************************************************************
9 # @HEADER
10 #
11 # Based on the MSTK NetCDF Find Module which is from Amanzi open
12 # source code https://software.lanl.gov/ascem/trac #
13 # Usage:
14 # Control the search through NetCDF_DIR or setting environment variable
15 # NetCDF_ROOT to the NetCDF installation prefix.
16 #
17 # This module does not search default paths!
18 #
19 # Following variables are set:
20 # NetCDF_FOUND (BOOL) Flag indicating if NetCDF was found
21 # NetCDF_INCLUDE_DIR (PATH) Path to the NetCDF include file
22 # NetCDF_INCLUDE_DIRS (LIST) List of all required include files
23 # NetCDF_LIBRARY_DIR (PATH) Path to the NetCDF library
24 # NetCDF_LIBRARY (FILE) NetCDF library
25 # NetCDF_LIBRARIES (LIST) List of all required NetCDF libraries
26 #
27 # Additional variables set
28 # NetCDF_C_LIBRARY (FILE) NetCDF C library
29 # NetCDF_CXX_LIBRARY (FILE) NetCDF C++ library
30 # NetCDF_LARGE_DIMS (BOOL) Checks the header files for size of
31 # NC_MAX_DIMS and NC_MAX_VARS
32 # Returns TRUE if
33 # NC_MAX_DIMS >= 655363
34 # NC_MAX_VARS >= 524288
35 #
36 # #############################################################################
37
38 # Standard CMake modules see CMAKE_ROOT/Modules
39 include(FindPackageHandleStandardArgs)
40
41 # MSTK CMake functions see <root>/cmake/modules for source
42 include(AddPackageDependency)
43
44 if ( NetCDF_LIBRARIES AND NetCDF_INCLUDE_DIRS )
45
46 # Do nothing. Variables are set. No need to search again
47
48 else(NetCDF_LIBRARIES AND NetCDF_INCLUDE_DIRS)
49
50 # If NetCDF_ROOT was defined in the environment, use it.
51 # Definition from the command line will take precedence.
52 if (NOT NetCDF_ROOT AND NOT $ENV{NetCDF_ROOT} STREQUAL "")
53 set(NetCDF_ROOT $ENV{NetCDF_ROOT})
54 endif()
55
56 # NetCDF_DIR is DEPRECATED WARN THE USER if it is set
57 if (NOT NetCDF_ROOT AND NetCDF_DIR )
58 message(WARNING "The configuration parameter NetCDF_DIR is deprecated."
59 " Please use NetCDF_ROOT instead to define the NetCDF installation")
60 set(NetCDF_ROOT ${NetCDF_DIR})
61 endif()
62
63
64 # Cache variables
65 if(NetCDF_ROOT)
66 set(NetCDF_ROOT "${NetCDF_ROOT}" CACHE PATH "Path to search for NetCDF include and library files")
67 endif()
68
69 if(NetCDF_INCLUDE_DIR)
70 set(NetCDF_INCLUDE_DIR "${NetCDF_INCLUDE_DIR}" CACHE PATH "Path to search for NetCDF include files")
71 endif()
72
73 if(NetCDF_LIBRARY_DIR)
74 set(NetCDF_LIBRARY_DIR "${NetCDF_LIBRARY_DIR}" CACHE PATH "Path to search for NetCDF library files")
75 endif()
76
77 # Search for include files
78 # Search order preference:
79 # (1) NetCDF_INCLUDE_DIR - check existence of path AND if the include files exist
80 # (2) NetCDF_ROOT/<include>
81 # (3) Default CMake paths See cmake --html-help=out.html file for more information.
82 #
83 set(netcdf_inc_names "netcdf.h")
84 if (NetCDF_INCLUDE_DIR)
85
86 if (EXISTS "${NetCDF_INCLUDE_DIR}")
87
88 find_path(cdf_test_include_path
89 NAMES ${netcdf_inc_names}
90 HINTS ${NetCDF_INCLUDE_DIR}
91 NO_DEFAULT_PATH)
92 if(NOT cdf_test_include_path)
93 message(SEND_ERROR "Can not locate ${netcdf_inc_names} in ${NetCDF_INCLUDE_DIR}")
94 endif()
95 set(NetCDF_INCLUDE_DIR "${cdf_test_include_path}")
96
97 else()
98 message(SEND_ERROR "NetCDF_INCLUDE_DIR=${NetCDF_INCLUDE_DIR} does not exist")
99 set(NetCDF_INCLUDE_DIR "NetCDF_INCLUDE_DIR-NOTFOUND")
100 endif()
101
102 else()
103
104 set(netcdf_inc_suffixes "include")
105 if(NetCDF_ROOT)
106
107 if (EXISTS "${NetCDF_ROOT}" )
108
109 find_path(NetCDF_INCLUDE_DIR
110 NAMES ${netcdf_inc_names}
111 HINTS ${NetCDF_ROOT}/include
112 PATH_SUFFIXES ${netcdf_inc_suffixes}
113 NO_DEFAULT_PATH)
114
115 else()
116 message(SEND_ERROR "NetCDF_ROOT=${NetCDF_ROOT} does not exist")
117 set(NetCDF_INCLUDE_DIR "NetCDF_INCLUDE_DIR-NOTFOUND")
118 endif()
119
120
121 else()
122
123 find_path(NetCDF_INCLUDE_DIR
124 NAMES ${netcdf_inc_names}
125 PATH_SUFFIXES ${netcdf_inc_suffixes})
126
127 endif()
128
129 endif()
130
131
132 if ( NOT NetCDF_INCLUDE_DIR )
133 message(SEND_ERROR "Can not locate NetCDF include directory")
134 endif()
135
136 # Large dimension and parallel check here
137 if ( NetCDF_INCLUDE_DIR )
138
139 set(netcdf_h "${NetCDF_INCLUDE_DIR}/netcdf.h" )
140 message(STATUS "NetCDF include file ${netcdf_h} will be searched for define values")
141
142 file(STRINGS "${netcdf_h}" netcdf_max_dims_string REGEX "^#define NC_MAX_DIMS")
143 string(REGEX REPLACE "[^0-9]" "" netcdf_max_dims "${netcdf_max_dims_string}")
144
145 file(STRINGS "${netcdf_h}" netcdf_max_vars_string REGEX "^#define NC_MAX_VARS")
146 string(REGEX REPLACE "[^0-9]" "" netcdf_max_vars "${netcdf_max_vars_string}")
147
148 if (
149 ( (netcdf_max_dims EQUAL 65536) OR (netcdf_max_dims GREATER 65536) ) AND
150 ( (netcdf_max_vars EQUAL 524288) OR (netcdf_max_vars GREATER 524288) )
151 )
152 set(NetCDF_LARGE_DIMS TRUE)
153 else()
154 message(WARNING "WARNING: The NetCDF found in ${NetCDF_ROOT} does not have the correct NC_MAX_DIMS and NC_MAX_VARS. "
155 "It may not be compatible with Exodus. See NetCDF-Mapping.md for details\n" )
156 set(NetCDF_LARGE_DIMS FALSE)
157 endif()
158
159 set(NetCDF_PARALLEL False)
160 find_path(meta_path
161 NAMES "netcdf_meta.h"
162 HINTS ${NetCDF_INCLUDE_DIR}
163 NO_DEFAULT_PATH)
164 if(meta_path)
165 # Search meta for NC_HAS_PARALLEL setting...
166 # Note that there is both NC_HAS_PARALLEL4 and NC_HAS_PARALLEL, only want NC_HAS_PARALLEL
167 # so add a space to end to avoid getting NC_HAS_PARALLEL4
168 file(STRINGS "${meta_path}/netcdf_meta.h" netcdf_par_string REGEX "NC_HAS_PARALLEL ")
169 string(REGEX REPLACE "[^0-9]" "" netcdf_par_val "${netcdf_par_string}")
170 # NOTE: The line for NC_HAS_PARALLEL has an hdf5 string in it which results
171 # netcdf_par_val being set to 05 or 15 above...
172 if (netcdf_par_val EQUAL 15)
173 set(NetCDF_PARALLEL True)
174 endif()
175 endif()
176
177 endif()
178
179 # Search for libraries
180 # Search order preference:
181 # (1) NetCDF_LIBRARY_DIR - check existence of path AND if the include files exist
182 # (2) NetCDF_ROOT/<lib,Lib>
183 # (3) Default CMake paths See cmake --html-help=out.html file for more information.
184 #
185 if (NetCDF_LIBRARY_DIR)
186
187 if (EXISTS "${NetCDF_LIBRARY_DIR}")
188
189 find_library(NetCDF_C_LIBRARY
190 NAMES netcdf
191 HINTS ${NetCDF_LIBRARY_DIR}
192 NO_DEFAULT_PATH)
193
194 # find_library(NetCDF_CXX_LIBRARY
195 # NAMES netcdf_c++
196 # HINTS ${NetCDF_LIBRARY_DIR}
197 # NO_DEFAULT_PATH)
198
199 else()
200 message(SEND_ERROR "NetCDF_LIBRARY_DIR=${NetCDF_LIBRARY_DIR} does not exist")
201 set(NetCDF_LIBRARY "NetCDF_C_LIBRARY-NOTFOUND")
202 # set(NetCDF_LIBRARY "NetCDF_CXX_LIBRARY-NOTFOUND")
203 endif()
204
205 else()
206
207 if(NetCDF_ROOT)
208
209 if (EXISTS "${NetCDF_ROOT}" )
210
211 find_library(NetCDF_C_LIBRARY
212 NAMES netcdf
213 HINTS ${NetCDF_ROOT}
214 PATH_SUFFIXES "lib" "Lib"
215 NO_DEFAULT_PATH)
216
217 # find_library(NetCDF_CXX_LIBRARY
218 # NAMES netcdf_c++
219 # HINTS ${NetCDF_ROOT}
220 # PATH_SUFFIXES "lib" "Lib"
221 # NO_DEFAULT_PATH)
222
223 else()
224 message(SEND_ERROR "NetCDF_ROOT=${NetCDF_ROOT} does not exist")
225 set(NetCDF_LIBRARY "NetCDF_C_LIBRARY-NOTFOUND")
226 # set(NetCDF_LIBRARY "NetCDF_CXX_LIBRARY-NOTFOUND")
227 endif()
228
229
230 else()
231
232 find_library(NetCDF_C_LIBRARY
233 NAMES netcdf
234 PATH_SUFFIXES ${netcdf_lib_suffixes})
235
236 # find_library(NetCDF_CXX_LIBRARY
237 # NAMES netcdf_c++
238 # PATH_SUFFIXES ${netcdf_lib_suffixes})
239
240
241 endif()
242
243 endif()
244
245 if ( NOT NetCDF_C_LIBRARY )
246 message(SEND_ERROR "Can not locate NetCDF C library")
247 endif()
248
249 # if ( NOT NetCDF_CXX_LIBRARY )
250 # message(SEND_ERROR "Can not locate NetCDF CXX library")
251 # endif()
252
253
254
255 # Define the LIBRARIES and INCLUDE_DORS
256 set(NetCDF_INCLUDE_DIRS ${NetCDF_INCLUDE_DIR})
257 set(NetCDF_LIBRARIES ${NetCDF_CXX_LIBRARY} ${NetCDF_C_LIBRARY})
258
259 # Need to find the NetCDF config script to check for HDF5
260 if ( NetCDF_ROOT OR NetCDF_BIN_DIR )
261 message(STATUS "\tNetCDF_ROOT is ${NetCDF_ROOT}")
262 find_program(netcdf_config nc-config
263 PATHS ${NetCDF_ROOT}/bin ${NetCDF_BIN_DIR}
264 NO_DEFAULT_PATH
265 NO_CMAKE_SYSTEM_PATH
266 DOC "NetCDF configuration script")
267
268 if (netcdf_config)
269 message(STATUS "Found NetCDF configuration script: ${netcdf_config}")
270 execute_process(COMMAND "${netcdf_config}" "--has-hdf5"
271 RESULT_VARIABLE _ret_code
272 OUTPUT_VARIABLE _stdout
273 ERROR_VARIABLE _stderr
274 )
275 string(REGEX REPLACE "[\n\r ]" "" _hdf5_answer ${_stdout})
276 message(STATUS "${netcdf_config} --has-hdf5 returned '${_hdf5_answer}'")
277 string(COMPARE EQUAL "${_hdf5_answer}" "yes" _has_hdf5)
278 if (${_has_hdf5} )
279 set(NetCDF_NEEDS_HDF5 True)
280 else()
281 set(NetCDF_NEEDS_HDF5 False)
282 endif()
283
284 execute_process(COMMAND "${netcdf_config}" "--version"
285 RESULT_VARIABLE _ret_code
286 OUTPUT_VARIABLE _stdout
287 ERROR_VARIABLE _stderr
288 )
289 string(REGEX REPLACE "[\n\r]" "" NetCDF_VERSION ${_stdout})
290
291 # If --has-pnetcdf returns true, then add pnetcdf as dependent library.
292 execute_process(COMMAND "${netcdf_config}" "--has-pnetcdf"
293 RESULT_VARIABLE _ret_code
294 OUTPUT_VARIABLE _stdout
295 ERROR_VARIABLE _stderr
296 )
297 string(REGEX REPLACE "[\n\r ]" "" _pnetcdf_answer ${_stdout})
298 message(STATUS "${netcdf_config} --has-pnetcdf returned '${_pnetcdf_answer}'")
299 string(COMPARE EQUAL "${_pnetcdf_answer}" "yes" _has_pnetcdf)
300 if (${_has_pnetcdf} )
301 set(NetCDF_NEEDS_PNetCDF True)
302 else()
303 set(NetCDF_NEEDS_PNetCDF False)
304 endif()
305
306
307 endif()
308 endif()
309
310 if(NetCDF_NEEDS_HDF5)
311 message(STATUS "NetCDF requires HDF5")
312 add_package_dependency(NetCDF DEPENDS_ON HDF5)
313 else()
314 message(STATUS "NetCDF does not require HDF5")
315 endif()
316
317 if(NetCDF_NEEDS_PNetCDF)
318 message(STATUS "NetCDF requires PNetCDF")
319 add_package_dependency(NetCDF DEPENDS_ON PNetCDF)
320 else()
321 message(STATUS "NetCDF does not require PNetCDF")
322 endif()
323
324 endif(NetCDF_LIBRARIES AND NetCDF_INCLUDE_DIRS )
325
326 # --- Search for NetCDF tools
327 if ( NetCDF_BINARY_DIR )
328 # Do nothing. Variables are set. No need to search again
329 else()
330 if(NetCDF_BINARY_DIR)
331 set(NetCDF_BINARY_DIR "${NetCDF_BINARY_DIR}" CACHE PATH "Path to search for NetCDF tools")
332 endif()
333
334 set(netcdf_bin_names "ncdump")
335 if (NetCDF_BINARY_DIR)
336
337 if (EXISTS "${NetCDF_BINARY_DIR}")
338
339 find_path(cdf_test_bin_path
340 NAMES ${netcdf_bin_names}
341 HINTS ${NetCDF_BINARY_DIR}
342 NO_DEFAULT_PATH)
343 if(NOT cdf_test_bin_path)
344 message(SEND_ERROR "Can not locate ${netcdf_bin_names} in ${NetCDF_BINARY_DIR}")
345 endif()
346 set(NetCDF_BINARY_DIR "${cdf_test_bin_path}")
347
348 else()
349 message(SEND_ERROR "NetCDF_BINARY_DIR=${NetCDF_BINARY_DIR} does not exist")
350 set(NetCDF_BINARY_DIR "NetCDF_BINARY_DIR-NOTFOUND")
351 endif()
352
353 else()
354
355 set(netcdf_bin_suffixes "bin")
356 if(NetCDF_ROOT)
357
358 if (EXISTS "${NetCDF_ROOT}" )
359
360 find_path(NetCDF_BINARY_DIR
361 NAMES ${netcdf_bin_names}
362 HINTS ${NetCDF_ROOT}/bin
363 PATH_SUFFIXES ${netcdf_bin_suffixes}
364 NO_DEFAULT_PATH)
365
366 else()
367 message(SEND_ERROR "NetCDF_ROOT=${NetCDF_ROOT} does not exist")
368 set(NetCDF_BINARY_DIR "NetCDF_BINARY_DIR-NOTFOUND")
369 endif()
370
371
372 else()
373
374 find_path(NetCDF_BINARY_DIR
375 NAMES ${netcdf_bin_names}
376 PATH_SUFFIXES ${netcdf_bin_suffixes})
377
378 endif()
379
380 endif()
381
382
383 if ( NOT NetCDF_BINARY_DIR )
384 message(STATUS "Can not locate NetCDF bin directory")
385 endif()
386 endif()
387
388 set(_netcdf_TOOLS ncdump ncgen nccopy)
389 set(NETCDF_TOOLS_FOUND)
390 foreach( tool ${_netcdf_TOOLS})
391 string(TOUPPER "${tool}" tool_uc)
392 set(_netcdf_VAR_NAME NETCDF_${tool_uc}_BINARY)
393 find_program(${_netcdf_VAR_NAME}
394 ${tool}
395 HINTS ${NetCDF_BINARY_DIR})
396 if (${_netcdf_VAR_NAME})
397 list(APPEND NETCDF_TOOLS_FOUND ${tool})
398 endif()
399 endforeach()
400
401 # Send useful message if everything is found
402 find_package_handle_standard_args(NetCDF DEFAULT_MSG
403 NetCDF_LIBRARIES
404 NetCDF_INCLUDE_DIRS)
405
406 # find_package)handle)standard_args should set NetCDF_FOUND but it does not!
407 if ( NetCDF_LIBRARIES AND NetCDF_INCLUDE_DIRS)
408 set(NetCDF_FOUND TRUE)
409 else()
410 set(NetCDF_FOUND FALSE)
411 endif()
412
413 # --- Provide a summary of what the module found
414 if ( NOT NetCDF_FIND_QUIETLY )
415
416 # Create a not found list
417
418 message(STATUS "NetCDF Version: ${NetCDF_VERSION}")
419 message(STATUS "\tNetCDF_NEEDS_HDF5 = ${NetCDF_NEEDS_HDF5}")
420 message(STATUS "\tNetCDF_NEEDS_PNetCDF = ${NetCDF_NEEDS_PNetCDF}")
421 message(STATUS "\tNetCDF_PARALLEL = ${NetCDF_PARALLEL}")
422 message(STATUS "\tNetCDF_INCLUDE_DIRS = ${NetCDF_INCLUDE_DIRS}")
423 message(STATUS "\tNetCDF_LIBRARIES = ${NetCDF_LIBRARIES}")
424 message(STATUS "\tNetCDF_BINARIES = ${NETCDF_TOOLS_FOUND}")
425
426 endif()
427
428 mark_as_advanced(
429 NetCDF_INCLUDE_DIR
430 NetCDF_INCLUDE_DIRS
431 NetCDF_C_LIBRARY
432 NetCDF_CXX_LIBRARY
433 NetCDF_LIBRARIES
434 NetCDF_LIBRARY_DIR
435 )