7
7
project (cprnc Fortran C)
8
8
include (CheckFunctionExists)
9
9
include (ExternalProject)
10
- find_package (PkgConfig REQUIRED)
11
10
12
11
#===== Local modules =====
13
12
list (APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR} /cmake)
@@ -75,11 +74,33 @@ foreach (SRC_FILE IN LISTS CPRNC_GenF90_SRCS)
75
74
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR} /${SRC_FILE} .in genf90)
76
75
endforeach ()
77
76
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 ()
80
103
81
- #===== NetCDF-Fortran =====
82
- pkg_check_modules(NetCDF_Fortran REQUIRED IMPORTED_TARGET netcdf-fortran)
83
104
add_executable (cprnc ${CPRNC_Fortran_SRCS} ${CPRNC_GenF90_SRCS} )
84
105
target_include_directories (cprnc PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} )
85
106
@@ -88,7 +109,7 @@ add_dependencies (cprnc genf90)
88
109
# Always use -fPIC
89
110
set_property (TARGET cprnc PROPERTY POSITION_INDEPENDENT_CODE ON )
90
111
target_link_libraries (cprnc
91
- PUBLIC PkgConfig::NetCDF_Fortran PkgConfig::NetCDF )
112
+ PUBLIC ${CPRNC_NETCDF_FORTRAN_LIB} ${CPRNC_NETCDF_C_LIB} )
92
113
93
114
# We do not want cprnc injecting ctests into parent projects
94
115
if (CPRNC_STANDALONE)
0 commit comments