Skip to content

Commit dea8909

Browse files
committed
Cleanup NetCDF dependency handling
1 parent 78c0be7 commit dea8909

File tree

5 files changed

+190
-99
lines changed

5 files changed

+190
-99
lines changed

CMakeLists.txt

+2-3
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ link_directories( ${SAMRAI_DIR}/lib )
4040
link_directories( ${HYPRE_DIR}/lib )
4141
link_directories( ${SUNDIALS_DIR}/lib )
4242

43-
find_package(netCDF REQUIRED)
4443
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake_modules")
44+
find_package(NetCDF REQUIRED COMPONENTS CXX)
4545

4646
set( PROJECT_LINK_LIBS1 libSAMRAI_tbox.a )
4747
set( PROJECT_LINK_LIBS2 libSAMRAI_math.a )
@@ -67,8 +67,7 @@ set( PROJECT_LINK_LIBS_HYPRE libHYPRE.so )
6767
set( PROJECT_LINK_LIBS_CVODE libsundials_cvode.a )
6868
set( PROJECT_LINK_LIBS_CPODES libsundials_cpodes.a )
6969

70-
set( PROJECT_LINK_LIBS_NETCDFCXX NetCDF::NetCDF )
71-
set( PROJECT_LINK_LIBS_NETCDF NetCDF::NetCDF )
70+
set( PROJECT_LINK_LIBS_NETCDF NetCDF::NetCDF_CXX )
7271

7372
include_directories( ${SAMRAI_DIR}/include )
7473
include_directories( ${HYPRE_DIR}/include )

cmake_modules/FindNetCDF.cmake

+187
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
#[==[
2+
Adapted from
3+
* https://github.com/Kitware/VTK/blob/master/CMake/FindNetCDF.cmake
4+
- Downloaded 10/29/2019
5+
* https://github.com/lammps/lammps/blob/master/cmake/Modules/FindNetCDF.cmake
6+
- Downloaded 11/06/2019
7+
8+
Provides the following variables:
9+
10+
* `NetCDF_FOUND`: Whether NetCDF was found or not.
11+
* `NetCDF_INCLUDE_DIRS`: Include directories necessary to use NetCDF.
12+
* `NetCDF_LIBRARIES`: Libraries necessary to use NetCDF.
13+
* `NetCDF_VERSION`: The version of NetCDF found.
14+
* `NetCDF::NetCDF`: A target to use with `target_link_libraries`.
15+
16+
You can require certain interfaces to be FOUND by passing a COMPONENTS argument
17+
to find_package containing one or more of
18+
19+
* CXX
20+
* CXX_LEGACY
21+
* F77
22+
* F90
23+
24+
When interfaces are requested the user has access to interface specific hints:
25+
26+
* 'NetCDF_${LANG}_INCLUDE_DIR': where to search for interface header files
27+
* 'NetCDF_${LANG}_LIBRARY': interface library
28+
29+
and the following target(s) will be created
30+
31+
* 'NetCDF::NetCDF_${LANG}'
32+
33+
#]==]
34+
35+
include(FindPackageHandleStandardArgs)
36+
37+
if(NOT NetCDF_FOUND)
38+
# Try to find a CMake-built NetCDF.
39+
message(STATUS "Searching for NetCDF...")
40+
find_package(netCDF CONFIG QUIET)
41+
if (netCDF_FOUND)
42+
# Forward the variables in a consistent way.
43+
set(NetCDF_FOUND "${netCDF_FOUND}")
44+
set(NetCDF_INCLUDE_DIRS "${netCDF_INCLUDE_DIR}")
45+
set(NetCDF_LIBRARIES "${netCDF_LIBRARIES}")
46+
set(NetCDF_LIBRARY_DIRS "${netCDF_LIB_DIR}")
47+
set(NetCDF_VERSION "${NetCDFVersion}")
48+
if (NOT TARGET NetCDF::NetCDF)
49+
add_library(NetCDF::NetCDF INTERFACE IMPORTED)
50+
set_target_properties(NetCDF::NetCDF PROPERTIES
51+
INTERFACE_LINK_LIBRARIES "${NetCDF_LIBRARIES}")
52+
endif ()
53+
endif ()
54+
endif()
55+
56+
if(NOT NetCDF_FOUND)
57+
message(STATUS "Searching for PkgConfig...")
58+
find_package(PkgConfig QUIET)
59+
if (PkgConfig_FOUND)
60+
pkg_check_modules(_NetCDF QUIET netcdf IMPORTED_TARGET)
61+
if (_NetCDF_FOUND)
62+
# Forward the variables in a consistent way.
63+
set(NetCDF_FOUND "${_NetCDF_FOUND}")
64+
set(NetCDF_INCLUDE_DIRS "${_NetCDF_INCLUDE_DIRS}")
65+
set(NetCDF_LIBRARIES "${_NetCDF_LIBRARIES}")
66+
set(NetCDF_VERSION "${_NetCDF_VERSION}")
67+
if (NOT TARGET NetCDF::NetCDF)
68+
add_library(NetCDF::NetCDF INTERFACE IMPORTED)
69+
set_target_properties(NetCDF::NetCDF PROPERTIES
70+
INTERFACE_LINK_LIBRARIES "PkgConfig::_NetCDF")
71+
endif ()
72+
message(STATUS "NetCDF_LIBRARIES: ${NetCDF_LIBRARIES}")
73+
endif ()
74+
endif ()
75+
endif()
76+
77+
if(NOT NetCDF_FOUND)
78+
message(STATUS "Searching for path to NetCDF include...")
79+
find_path(NetCDF_INCLUDE_DIR
80+
NAMES netcdf.h
81+
DOC "netcdf include directories")
82+
mark_as_advanced(NetCDF_INCLUDE_DIR)
83+
84+
find_library(NetCDF_LIBRARY
85+
NAMES netcdf
86+
DOC "netcdf library")
87+
mark_as_advanced(NetCDF_LIBRARY)
88+
89+
if (NetCDF_INCLUDE_DIR)
90+
file(STRINGS "${NetCDF_INCLUDE_DIR}/netcdf_meta.h" _netcdf_version_lines
91+
REGEX "#define[ \t]+NC_VERSION_(MAJOR|MINOR|PATCH|NOTE)")
92+
string(REGEX REPLACE ".*NC_VERSION_MAJOR *\([0-9]*\).*" "\\1"
93+
_netcdf_version_major "${_netcdf_version_lines}")
94+
string(REGEX REPLACE ".*NC_VERSION_MINOR *\([0-9]*\).*" "\\1"
95+
_netcdf_version_minor "${_netcdf_version_lines}")
96+
string(REGEX REPLACE ".*NC_VERSION_PATCH *\([0-9]*\).*" "\\1"
97+
_netcdf_version_patch "${_netcdf_version_lines}")
98+
string(REGEX REPLACE ".*NC_VERSION_NOTE *\"\([^\"]*\)\".*" "\\1"
99+
_netcdf_version_note "${_netcdf_version_lines}")
100+
set(NetCDF_VERSION
101+
"${_netcdf_version_major}.${_netcdf_version_minor}.${_netcdf_version_patch}${_netcdf_version_note}")
102+
unset(_netcdf_version_major)
103+
unset(_netcdf_version_minor)
104+
unset(_netcdf_version_patch)
105+
unset(_netcdf_version_note)
106+
unset(_netcdf_version_lines)
107+
endif ()
108+
109+
find_package_handle_standard_args(NetCDF
110+
REQUIRED_VARS NetCDF_LIBRARY NetCDF_INCLUDE_DIR
111+
VERSION_VAR NetCDF_VERSION)
112+
113+
if (NetCDF_FOUND)
114+
set(NetCDF_INCLUDE_DIRS "${NetCDF_INCLUDE_DIR}")
115+
set(NetCDF_LIBRARIES "${NetCDF_LIBRARY}")
116+
get_filename_component(NetCDF_LIBRARY_DIRS "${NetCDF_LIBRARY}" DIRECTORY)
117+
message(STATUS "NetCDF libraries: ${NetCDF_LIBRARIES}")
118+
if (NOT TARGET NetCDF::NetCDF)
119+
add_library(NetCDF::NetCDF UNKNOWN IMPORTED)
120+
set_target_properties(NetCDF::NetCDF PROPERTIES
121+
IMPORTED_LOCATION "${NetCDF_LIBRARY}"
122+
INTERFACE_INCLUDE_DIRECTORIES "${NetCDF_INCLUDE_DIR}")
123+
endif ()
124+
endif ()
125+
endif()
126+
127+
128+
# Find requested language components
129+
130+
macro(NetCDF_check_interface lang header libs)
131+
if(NetCDF_${lang})
132+
#search starting from user modifiable cache var
133+
find_path(NetCDF_${lang}_INCLUDE_DIR NAMES ${header}
134+
HINTS "${NetCDF_INCLUDE_DIRS}"
135+
HINTS "${NetCDF_${lang}_ROOT}/include"
136+
${USE_DEFAULT_PATHS})
137+
138+
find_library(NetCDF_${lang}_LIBRARY NAMES ${libs}
139+
HINTS "${NetCDF_lib_dirs}"
140+
HINTS "${NetCDF_${lang}_ROOT}/lib"
141+
${USE_DEFAULT_PATHS})
142+
143+
mark_as_advanced(NetCDF_${lang}_INCLUDE_DIR NetCDF_${lang}_LIBRARY)
144+
145+
#export to internal varS that rest of project can use directly
146+
set(NetCDF_${lang}_LIBRARIES ${NetCDF_${lang}_LIBRARY})
147+
set(NetCDF_${lang}_INCLUDE_DIRS ${NetCDF_${lang}_INCLUDE_DIR})
148+
149+
find_package_handle_standard_args(NetCDF
150+
REQUIRED_VARS NetCDF_${lang}_LIBRARY NetCDF_${lang}_INCLUDE_DIR)
151+
152+
list(APPEND NetCDF_LIBRARIES ${NetCDF_${lang}_LIBRARY})
153+
list(APPEND NetCDF_INCLUDE_DIRS ${NetCDF_${lang}_INCLUDE_DIR})
154+
155+
if(NOT TARGET NetCDF::NetCDF_${lang})
156+
add_library(NetCDF::NetCDF_${lang} INTERFACE IMPORTED)
157+
set_property(TARGET NetCDF::NetCDF_${lang} PROPERTY
158+
INTERFACE_INCLUDE_DIRECTORIES "${NetCDF_${lang}_INCLUDE_DIR}")
159+
set_property(TARGET NetCDF::NetCDF_${lang} APPEND PROPERTY
160+
INTERFACE_LINK_LIBRARIES "${NetCDF_${lang}_LIBRARY}" NetCDF::NetCDF)
161+
endif()
162+
endif()
163+
endmacro()
164+
165+
list(FIND NetCDF_FIND_COMPONENTS "CXX" _nextcomp)
166+
if(_nextcomp GREATER -1)
167+
set(NetCDF_CXX 1)
168+
endif()
169+
list(FIND NetCDF_FIND_COMPONENTS "CXX_LEGACY" _nextcomp)
170+
if(_nextcomp GREATER -1)
171+
set(NetCDF_CXX_LEGACY 1)
172+
endif()
173+
list(FIND NetCDF_FIND_COMPONENTS "F77" _nextcomp)
174+
if(_nextcomp GREATER -1)
175+
set(NetCDF_F77 1)
176+
endif()
177+
list(FIND NetCDF_FIND_COMPONENTS "F90" _nextcomp)
178+
if(_nextcomp GREATER -1)
179+
set(NetCDF_F90 1)
180+
endif()
181+
NetCDF_check_interface(CXX netcdf netcdf_c++4)
182+
NetCDF_check_interface(CXX_LEGACY netcdfcpp.h netcdf_c++)
183+
NetCDF_check_interface(F77 netcdf.inc netcdff)
184+
NetCDF_check_interface(F90 netcdf.mod netcdff)
185+
186+
list(REMOVE_DUPLICATES NetCDF_INCLUDE_DIRS)
187+
list(REMOVE_DUPLICATES NetCDF_LIBRARIES)

cmake_modules/FindnetCDF.cmake

-93
This file was deleted.

source/CMakeLists.txt

-1
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,6 @@ target_link_libraries(${MAIN} ${PROJECT_LINK_LIBS_SAMRAI}
183183
${PROJECT_LINK_LIBS_HYPRE}
184184
${PROJECT_LINK_LIBS_CVODE}
185185
${PROJECT_LINK_LIBS_CPODES}
186-
${PROJECT_LINK_LIBS_NETCDFCXX}
187186
${PROJECT_LINK_LIBS_NETCDF}
188187
${PROJECT_LINK_LIBS_HDF5}
189188
)

tests/CMakeLists.txt

+1-2
Original file line numberDiff line numberDiff line change
@@ -236,8 +236,7 @@ add_test(NAME AlCu
236236
${CMAKE_CURRENT_SOURCE_DIR}/AlCu/${NDIM}d.input
237237
)
238238

239-
target_link_libraries(testNetCDF ${PROJECT_LINK_LIBS_NETCDFCXX}
240-
${PROJECT_LINK_LIBS_NETCDF}
239+
target_link_libraries(testNetCDF ${PROJECT_LINK_LIBS_NETCDF}
241240
${PROJECT_LINK_LIBS_HDF5})
242241
target_link_libraries(testMPI ${PROJECT_LINK_LIBS1})
243242
target_link_libraries(testHyprePoisson ${PROJECT_LINK_LIBS_SAMRAI}

0 commit comments

Comments
 (0)