Debian Codegraph @

cmake/tribits/common_tpls/FindTPLNetcdf.cmake

raw
1 # @HEADER
2 # *****************************************************************************
3 # TriBITS: Tribal Build, Integrate, and Test System
4 #
5 # Copyright 2013-2016 NTESS and the TriBITS contributors.
6 # SPDX-License-Identifier: BSD-3-Clause
7 # *****************************************************************************
8 # @HEADER
9
10 cmake_policy(SET CMP0074 NEW)
11
12 set(Netcdf_ALLOW_MODERN FALSE CACHE BOOL "Allow finding Netcdf as a modern CMake config file with exported targets (and only this way)")
13
14 if (Netcdf_ALLOW_MODERN AND HDF5_FOUND_MODERN_CONFIG_FILE)
15
16 set(minimum_modern_netCDF_version 4.7.4)
17 print_var(Netcdf_ALLOW_MODERN)
18 message("-- Using find_package(netCDF ${minimum_modern_netCDF_version} CONFIG) ...")
19 find_package(netCDF ${minimum_modern_netCDF_version} CONFIG)
20 if (netCDF_FOUND)
21 message("-- Found netCDF_CONFIG=${netCDF_CONFIG}")
22 message("-- Generating Netcdf::all_libs and NetcdfConfig.cmake")
23 tribits_extpkg_create_imported_all_libs_target_and_config_file(
24 Netcdf
25 INNER_FIND_PACKAGE_NAME netCDF
26 IMPORTED_TARGETS_FOR_ALL_LIBS netCDF::netcdf)
27 set(TPL_Netcdf_NOT_FOUND FALSE)
28 endif()
29
30 endif()
31
32 if (NOT TARGET Netcdf::all_libs)
33
34 # First, set up the variables for the (backward-compatible) TriBITS way of
35 # finding Netcdf. These are used in case find_package(Netcdf ...) is not
36 # called or does not find HDF5. Also, these variables need to be non-null
37 # in order to trigger the right behavior in the function
38 # tribits_tpl_find_include_dirs_and_libraries().
39
40 set(REQUIRED_HEADERS netcdf.h)
41 set(REQUIRED_LIBS_NAMES netcdf)
42
43 if (TPL_ENABLE_MPI)
44 set(REQUIRED_LIBS_NAMES ${REQUIRED_LIBS_NAMES} pnetcdf)
45 endif()
46
47 #
48 # Second, search for Netcdf components (if allowed) using the standard
49 # find_package(NetCDF ...).
50 #
51 tribits_tpl_allow_pre_find_package(Netcdf Netcdf_ALLOW_PREFIND)
52 if (Netcdf_ALLOW_PREFIND)
53
54 message("-- Using find_package(Netcdf ...) ...")
55
56 set(CMAKE_MODULE_PATH
57 "${CMAKE_MODULE_PATH}"
58 "${CMAKE_CURRENT_LIST_DIR}/find_modules"
59 "${CMAKE_CURRENT_LIST_DIR}/utils"
60 )
61
62 find_package(NetCDF)
63
64 if (NetCDF_FOUND)
65 set(DOCSTR "List of semi-colon separated paths to look for the TPL Netcdf")
66 set(TPL_Netcdf_Enables_Netcdf4 ${NetCDF_NEEDS_HDF5} CACHE BOOL
67 "True if netcdf enables netcdf-4")
68 set(TPL_Netcdf_Enables_PNetcdf ${NetCDF_NEEDS_PNetCDF} CACHE BOOL
69 "True if netcdf enables pnetcdf")
70 set(TPL_Netcdf_PARALLEL ${NetCDF_PARALLEL} CACHE INTERNAL
71 "True if netcdf compiled with parallel enabled")
72 set(TPL_Netcdf_LIBRARY_DIRS ${_hdf5_LIBRARY_SEARCH_DIRS} CACHE PATH
73 "${DOCSTR} library files")
74 set(TPL_Netcdf_LIBRARIES ${NetCDF_LIBRARIES} CACHE PATH
75 "List of semi-colon separated library names (not 'lib' or extension).")
76 set(TPL_Netcdf_INCLUDE_DIRS ${NetCDF_INCLUDE_DIRS} CACHE PATH
77 "${DOCSTR} header files.")
78 endif()
79 else()
80 # Curl library is only required if DAP is enabled; should detect inside
81 # FindNetCDF.cmake, but that is not being called... SEMS has DAP enabled;
82 # many HPC systems don't, but they override these settings...
83 find_program(NC_CONFIG "nc-config")
84 if (NC_CONFIG)
85 execute_process(COMMAND "nc-config --has-dap2"
86 OUTPUT_VARIABLE NETCDF_HAS_DAP2)
87 execute_process(COMMAND "nc-config --has-dap4"
88 OUTPUT_VARIABLE NETCDF_HAS_DAP4)
89 endif()
90 if ((NOT NC_CONFIG) OR NETCDF_HAS_DAP2 OR NETCDF_HAS_DAP4)
91 set(REQUIRED_LIBS_NAMES ${REQUIRED_LIBS_NAMES} curl)
92 endif()
93 endif()
94
95 #
96 # Third, call tribits_tpl_find_include_dirs_and_libraries()
97 #
98 tribits_tpl_find_include_dirs_and_libraries( Netcdf
99 REQUIRED_HEADERS ${REQUIRED_HEADERS}
100 REQUIRED_LIBS_NAMES ${REQUIRED_LIBS_NAMES}
101 )
102 # NOTE: If find_package(Netcdf ...) was called and successfully found Netcdf,
103 # then tribits_tpl_find_include_dirs_and_libraries() will use the already-set
104 # variables TPL_Netcdf_INCLUDE_DIRS and TPL_Netcdf_LIBRARIES and then print
105 # them out (and set some other standard variables as well). This is the final
106 # "hook" into the TriBITS TPL system.
107
108 # If the `find_package(NetCDF)` is not run, then this may not be set
109 # Need to determine how this is set in the library that is being used...
110
111 if ("${TPL_Netcdf_PARALLEL}" STREQUAL "")
112 assert_defined(TPL_Netcdf_INCLUDE_DIRS)
113 find_path(meta_path
114 NAMES "netcdf_meta.h"
115 HINTS ${TPL_Netcdf_INCLUDE_DIRS}
116 NO_DEFAULT_PATH)
117
118 if (meta_path)
119 # Search meta for NC_HAS_PARALLEL setting...
120 # Note that there is both NC_HAS_PARALLEL and NC_HAS_PARALLEL4, only want first...
121 file(STRINGS "${meta_path}/netcdf_meta.h" netcdf_par_string REGEX "NC_HAS_PARALLEL ")
122 string(REGEX MATCH "[01]" netcdf_par_val "${netcdf_par_string}")
123 if (netcdf_par_val EQUAL 1)
124 set(TPL_Netcdf_PARALLEL True CACHE INTERNAL
125 "True if netcdf compiled with parallel enabled")
126 endif()
127 endif()
128 if ("${TPL_Netcdf_PARALLEL}" STREQUAL "")
129 set(TPL_Netcdf_PARALLEL False CACHE INTERNAL
130 "True if netcdf compiled with parallel enabled")
131 endif()
132 endif()
133 message(STATUS "TPL_Netcdf_PARALLEL is ${TPL_Netcdf_PARALLEL}")
134 endif()