Skip to content

Commit 8da7ea2

Browse files
committed
Allow fallback to find_library/find_path for netcdf
1 parent 025932e commit 8da7ea2

File tree

1 file changed

+23
-8
lines changed

1 file changed

+23
-8
lines changed

CMakeLists.txt

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,17 +88,32 @@ if (NetCDF_FOUND)
8888
set(CPRNC_NETCDF_FORTRAN_LIB netcdf_cprnc)
8989
endif()
9090
else ()
91-
message(STATUS "NetCDF was not found, falling back on pkg-config")
92-
find_package(PkgConfig REQUIRED)
91+
# If netcdf is not installed as a CMake pkg, the find_package above will fail,
92+
# but netcdf may still be installed in the <PackageName>_ROOT folders
93+
find_library(NetCDF_C_LIBRARIES netcdf QUIET HINTS ${NetCDF_C_ROOT} PATH_SUFFIXES lib lib64)
94+
find_library(NetCDF_Fortran_LIBRARIES netcdff QUIET HINTS ${NetCDF_Fortran_ROOT} PATH_SUFFIXES lib lib64)
95+
find_path(NetCDF_C_INCLUDE_DIRS netcdf.h QUIET HINTS ${NetCDF_C_ROOT} PATH_SUFFIXES include)
96+
find_path(NetCDF_Fortran_INCLUDE_DIRS netcdf.mod QUIET HINTS ${NetCDF_Fortran_ROOT} PATH_SUFFIXES include)
97+
98+
if (NetCDF_C_LIBRARIES AND NetCDF_Fortran_LIBRARIES AND
99+
NetCDF_C_INCLUDE_DIRS AND NetCDF_Fortran_INCLUDE_DIRS)
100+
add_library(netcdf_cprnc INTERFACE)
101+
target_link_libraries(netcdf_cprnc INTERFACE ${NetCDF_Fortran_LIBRARIES};${NetCDF_C_LIBRARIES})
102+
target_include_directories(netcdf_cprnc INTERFACE ${NetCDF_Fortran_INCLUDE_DIRS};${NetCDF_C_INCLUDE_DIRS})
103+
set(CPRNC_NETCDF_FORTRAN_LIB netcdf_cprnc)
104+
else()
105+
message(STATUS "NetCDF was not found, falling back on pkg-config")
106+
find_package(PkgConfig REQUIRED)
93107

94-
#===== NetCDF =====
95-
pkg_check_modules(NetCDF REQUIRED IMPORTED_TARGET netcdf)
108+
#===== NetCDF =====
109+
pkg_check_modules(NetCDF REQUIRED IMPORTED_TARGET netcdf)
96110

97-
#===== NetCDF-Fortran =====
98-
pkg_check_modules(NetCDF_Fortran REQUIRED IMPORTED_TARGET netcdf-fortran)
111+
#===== NetCDF-Fortran =====
112+
pkg_check_modules(NetCDF_Fortran REQUIRED IMPORTED_TARGET netcdf-fortran)
99113

100-
set(CPRNC_NETCDF_FORTRAN_LIB PkgConfig::NetCDF_Fortran)
101-
set(CPRNC_NETCDF_C_LIB PkgConfig::NetCDF)
114+
set(CPRNC_NETCDF_FORTRAN_LIB PkgConfig::NetCDF_Fortran)
115+
set(CPRNC_NETCDF_C_LIB PkgConfig::NetCDF)
116+
endif()
102117
endif()
103118

104119
add_executable (cprnc ${CPRNC_Fortran_SRCS} ${CPRNC_GenF90_SRCS})

0 commit comments

Comments
 (0)