Skip to content

Commit 025932e

Browse files
authored
Merge pull request #23 from ESMCI/jgfouca/better_find_netcdf
Switch to a more robust method of finding netcdf
2 parents f4979ab + d80f63a commit 025932e

File tree

1 file changed

+27
-6
lines changed

1 file changed

+27
-6
lines changed

CMakeLists.txt

+27-6
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ endif()
77
project (cprnc Fortran C)
88
include (CheckFunctionExists)
99
include (ExternalProject)
10-
find_package(PkgConfig REQUIRED)
1110

1211
#===== Local modules =====
1312
list (APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
@@ -75,11 +74,33 @@ foreach (SRC_FILE IN LISTS CPRNC_GenF90_SRCS)
7574
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${SRC_FILE}.in genf90)
7675
endforeach ()
7776

78-
#===== NetCDF =====
79-
pkg_check_modules(NetCDF REQUIRED IMPORTED_TARGET netcdf)
77+
# Try to find NetCDF dependency. Try standard find_package first.
78+
find_package(NetCDF QUIET COMPONENTS C Fortran)
79+
if (NetCDF_FOUND)
80+
if(TARGET NetCDF::NetCDF_Fortran)
81+
set(CPRNC_NETCDF_FORTRAN_LIB NetCDF::NetCDF_Fortran)
82+
set(CPRNC_NETCDF_C_LIB NetCDF::NetCDF_C)
83+
else()
84+
# We used an old-school find netcdf module. Make the target here
85+
add_library(netcdf_cprnc INTERFACE)
86+
target_link_libraries(netcdf_cprnc INTERFACE ${NetCDF_Fortran_LIBRARIES};${NetCDF_C_LIBRARIES})
87+
target_include_directories(netcdf_cprnc INTERFACE ${NetCDF_Fortran_INCLUDE_DIRS};${NetCDF_C_INCLUDE_DIRS})
88+
set(CPRNC_NETCDF_FORTRAN_LIB netcdf_cprnc)
89+
endif()
90+
else ()
91+
message(STATUS "NetCDF was not found, falling back on pkg-config")
92+
find_package(PkgConfig REQUIRED)
93+
94+
#===== NetCDF =====
95+
pkg_check_modules(NetCDF REQUIRED IMPORTED_TARGET netcdf)
96+
97+
#===== NetCDF-Fortran =====
98+
pkg_check_modules(NetCDF_Fortran REQUIRED IMPORTED_TARGET netcdf-fortran)
99+
100+
set(CPRNC_NETCDF_FORTRAN_LIB PkgConfig::NetCDF_Fortran)
101+
set(CPRNC_NETCDF_C_LIB PkgConfig::NetCDF)
102+
endif()
80103

81-
#===== NetCDF-Fortran =====
82-
pkg_check_modules(NetCDF_Fortran REQUIRED IMPORTED_TARGET netcdf-fortran)
83104
add_executable (cprnc ${CPRNC_Fortran_SRCS} ${CPRNC_GenF90_SRCS})
84105
target_include_directories(cprnc PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
85106

@@ -88,7 +109,7 @@ add_dependencies (cprnc genf90)
88109
# Always use -fPIC
89110
set_property(TARGET cprnc PROPERTY POSITION_INDEPENDENT_CODE ON)
90111
target_link_libraries (cprnc
91-
PUBLIC PkgConfig::NetCDF_Fortran PkgConfig::NetCDF)
112+
PUBLIC ${CPRNC_NETCDF_FORTRAN_LIB} ${CPRNC_NETCDF_C_LIB})
92113

93114
# We do not want cprnc injecting ctests into parent projects
94115
if (CPRNC_STANDALONE)

0 commit comments

Comments
 (0)