|
| 1 | +#[==[ |
| 2 | +Provides the following variables: |
| 3 | +
|
| 4 | + * `NetCDF_FOUND`: Whether NetCDF was found or not. |
| 5 | + * `NetCDF_INCLUDE_DIRS`: Include directories necessary to use NetCDF. |
| 6 | + * `NetCDF_LIBRARIES`: Libraries necessary to use NetCDF. |
| 7 | + * `NetCDF_VERSION`: The version of NetCDF found. |
| 8 | + * `NetCDF::NetCDF`: A target to use with `target_link_libraries`. |
| 9 | +Adapted from https://github.com/Kitware/VTK/blob/master/CMake/FindNetCDF.cmake |
| 10 | +Downloaded 10/29/2019 |
| 11 | +#]==] |
| 12 | + |
| 13 | +# Try to find a CMake-built NetCDF. |
| 14 | +message(STATUS "Searching for NetCDF...") |
| 15 | +find_package(netCDF CONFIG QUIET) |
| 16 | +if (netCDF_FOUND) |
| 17 | + # Forward the variables in a consistent way. |
| 18 | + set(NetCDF_FOUND "${netCDF_FOUND}") |
| 19 | + set(NetCDF_INCLUDE_DIRS "${netCDF_INCLUDE_DIR}") |
| 20 | + set(NetCDF_LIBRARIES "${netCDF_LIBRARIES}") |
| 21 | + set(NetCDF_VERSION "${NetCDFVersion}") |
| 22 | + if (NOT TARGET NetCDF::NetCDF) |
| 23 | + add_library(NetCDF::NetCDF INTERFACE IMPORTED) |
| 24 | + set_target_properties(NetCDF::NetCDF PROPERTIES |
| 25 | + INTERFACE_LINK_LIBRARIES "${NetCDF_LIBRARIES}") |
| 26 | + endif () |
| 27 | + # Skip the rest of the logic in this file. |
| 28 | + return () |
| 29 | +endif () |
| 30 | + |
| 31 | +message(STATUS "Searching for PkgConfig...") |
| 32 | +find_package(PkgConfig QUIET) |
| 33 | +if (PkgConfig_FOUND) |
| 34 | + pkg_check_modules(_NetCDF QUIET netcdf IMPORTED_TARGET) |
| 35 | + if (_NetCDF_FOUND) |
| 36 | + # Forward the variables in a consistent way. |
| 37 | + set(NetCDF_FOUND "${_NetCDF_FOUND}") |
| 38 | + set(NetCDF_INCLUDE_DIRS "${_NetCDF_INCLUDE_DIRS}") |
| 39 | + set(NetCDF_LIBRARIES "${_NetCDF_LIBRARIES}") |
| 40 | + set(NetCDF_VERSION "${_NetCDF_VERSION}") |
| 41 | + if (NOT TARGET NetCDF::NetCDF) |
| 42 | + add_library(NetCDF::NetCDF INTERFACE IMPORTED) |
| 43 | + set_target_properties(NetCDF::NetCDF PROPERTIES |
| 44 | + INTERFACE_LINK_LIBRARIES "PkgConfig::_NetCDF") |
| 45 | + endif () |
| 46 | + message(STATUS "NetCDF_LIBRARIES: ${NetCDF_LIBRARIES}") |
| 47 | + # Skip the rest of the logic in this file. |
| 48 | + return () |
| 49 | + endif () |
| 50 | +endif () |
| 51 | + |
| 52 | +message(STATUS "Searching for path to NetCDF include...") |
| 53 | +find_path(NetCDF_INCLUDE_DIR |
| 54 | + NAMES netcdf.h |
| 55 | + DOC "netcdf include directories") |
| 56 | +mark_as_advanced(NetCDF_INCLUDE_DIR) |
| 57 | + |
| 58 | +find_library(NetCDF_LIBRARY |
| 59 | + NAMES netcdf |
| 60 | + DOC "netcdf library") |
| 61 | +mark_as_advanced(NetCDF_LIBRARY) |
| 62 | + |
| 63 | +if (NetCDF_INCLUDE_DIR) |
| 64 | + file(STRINGS "${NetCDF_INCLUDE_DIR}/netcdf_meta.h" _netcdf_version_lines |
| 65 | + REGEX "#define[ \t]+NC_VERSION_(MAJOR|MINOR|PATCH|NOTE)") |
| 66 | + string(REGEX REPLACE ".*NC_VERSION_MAJOR *\([0-9]*\).*" "\\1" _netcdf_version_major "${_netcdf_version_lines}") |
| 67 | + string(REGEX REPLACE ".*NC_VERSION_MINOR *\([0-9]*\).*" "\\1" _netcdf_version_minor "${_netcdf_version_lines}") |
| 68 | + string(REGEX REPLACE ".*NC_VERSION_PATCH *\([0-9]*\).*" "\\1" _netcdf_version_patch "${_netcdf_version_lines}") |
| 69 | + string(REGEX REPLACE ".*NC_VERSION_NOTE *\"\([^\"]*\)\".*" "\\1" _netcdf_version_note "${_netcdf_version_lines}") |
| 70 | + set(NetCDF_VERSION "${_netcdf_version_major}.${_netcdf_version_minor}.${_netcdf_version_patch}${_netcdf_version_note}") |
| 71 | + unset(_netcdf_version_major) |
| 72 | + unset(_netcdf_version_minor) |
| 73 | + unset(_netcdf_version_patch) |
| 74 | + unset(_netcdf_version_note) |
| 75 | + unset(_netcdf_version_lines) |
| 76 | +endif () |
| 77 | + |
| 78 | +include(FindPackageHandleStandardArgs) |
| 79 | +find_package_handle_standard_args(NetCDF |
| 80 | + REQUIRED_VARS NetCDF_LIBRARY NetCDF_INCLUDE_DIR |
| 81 | + VERSION_VAR NetCDF_VERSION) |
| 82 | + |
| 83 | +if (NetCDF_FOUND) |
| 84 | + set(NetCDF_INCLUDE_DIRS "${NetCDF_INCLUDE_DIR}") |
| 85 | + set(NetCDF_LIBRARIES "${NetCDF_LIBRARY}") |
| 86 | + message(STATUS "NetCDF libraries: ${NetCDF_LIBRARIES}") |
| 87 | + if (NOT TARGET NetCDF::NetCDF) |
| 88 | + add_library(NetCDF::NetCDF UNKNOWN IMPORTED) |
| 89 | + set_target_properties(NetCDF::NetCDF PROPERTIES |
| 90 | + IMPORTED_LOCATION "${NetCDF_LIBRARY}" |
| 91 | + INTERFACE_INCLUDE_DIRECTORIES "${NetCDF_INCLUDE_DIR}") |
| 92 | + endif () |
| 93 | +endif () |
0 commit comments