FindNetCDF.cmake 3.5 KB

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