FindGDAL.cmake 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file Copyright.txt or https://cmake.org/licensing for details.
  3. #[=======================================================================[.rst:
  4. FindGDAL
  5. --------
  6. Find Geospatial Data Abstraction Library (GDAL).
  7. IMPORTED Targets
  8. ^^^^^^^^^^^^^^^^
  9. .. versionadded:: 3.14
  10. This module defines :prop_tgt:`IMPORTED` target ``GDAL::GDAL``
  11. if GDAL has been found.
  12. Result Variables
  13. ^^^^^^^^^^^^^^^^
  14. This module will set the following variables in your project:
  15. ``GDAL_FOUND``
  16. True if GDAL is found.
  17. ``GDAL_INCLUDE_DIRS``
  18. Include directories for GDAL headers.
  19. ``GDAL_LIBRARIES``
  20. Libraries to link to GDAL.
  21. ``GDAL_VERSION``
  22. .. versionadded:: 3.14
  23. The version of GDAL found.
  24. Cache variables
  25. ^^^^^^^^^^^^^^^
  26. The following cache variables may also be set:
  27. ``GDAL_LIBRARY``
  28. The libgdal library file.
  29. ``GDAL_INCLUDE_DIR``
  30. The directory containing ``gdal.h``.
  31. Hints
  32. ^^^^^
  33. Set ``GDAL_DIR`` or ``GDAL_ROOT`` in the environment to specify the
  34. GDAL installation prefix.
  35. #]=======================================================================]
  36. # $GDALDIR is an environment variable that would
  37. # correspond to the ./configure --prefix=$GDAL_DIR
  38. # used in building gdal.
  39. #
  40. # Created by Eric Wing. I'm not a gdal user, but OpenSceneGraph uses it
  41. # for osgTerrain so I whipped this module together for completeness.
  42. # I actually don't know the conventions or where files are typically
  43. # placed in distros.
  44. # Any real gdal users are encouraged to correct this (but please don't
  45. # break the OS X framework stuff when doing so which is what usually seems
  46. # to happen).
  47. # This makes the presumption that you are include gdal.h like
  48. #
  49. #include "gdal.h"
  50. find_path(GDAL_INCLUDE_DIR gdal.h
  51. HINTS
  52. ENV GDAL_DIR
  53. ENV GDAL_ROOT
  54. PATH_SUFFIXES
  55. include/gdal
  56. include/GDAL
  57. include
  58. )
  59. if(UNIX)
  60. # Use gdal-config to obtain the library version (this should hopefully
  61. # allow us to -lgdal1.x.y where x.y are correct version)
  62. # For some reason, libgdal development packages do not contain
  63. # libgdal.so...
  64. find_program(GDAL_CONFIG gdal-config
  65. HINTS
  66. ENV GDAL_DIR
  67. ENV GDAL_ROOT
  68. PATH_SUFFIXES bin
  69. )
  70. if(GDAL_CONFIG)
  71. exec_program(${GDAL_CONFIG} ARGS --libs OUTPUT_VARIABLE GDAL_CONFIG_LIBS)
  72. if(GDAL_CONFIG_LIBS)
  73. # treat the output as a command line and split it up
  74. separate_arguments(args NATIVE_COMMAND "${GDAL_CONFIG_LIBS}")
  75. # only consider libraries whose name matches this pattern
  76. set(name_pattern "[gG][dD][aA][lL]")
  77. # consider each entry as a possible library path, name, or parent directory
  78. foreach(arg IN LISTS args)
  79. # library name
  80. if("${arg}" MATCHES "^-l(.*)$")
  81. set(lib "${CMAKE_MATCH_1}")
  82. # only consider libraries whose name matches the expected pattern
  83. if("${lib}" MATCHES "${name_pattern}")
  84. list(APPEND _gdal_lib "${lib}")
  85. endif()
  86. # library search path
  87. elseif("${arg}" MATCHES "^-L(.*)$")
  88. list(APPEND _gdal_libpath "${CMAKE_MATCH_1}")
  89. # assume this is a full path to a library
  90. elseif(IS_ABSOLUTE "${arg}" AND EXISTS "${arg}")
  91. # extract the file name
  92. get_filename_component(lib "${arg}" NAME)
  93. # only consider libraries whose name matches the expected pattern
  94. if(NOT "${lib}" MATCHES "${name_pattern}")
  95. continue()
  96. endif()
  97. # extract the file directory
  98. get_filename_component(dir "${arg}" DIRECTORY)
  99. # remove library prefixes/suffixes
  100. string(REGEX REPLACE "^(${CMAKE_SHARED_LIBRARY_PREFIX}|${CMAKE_STATIC_LIBRARY_PREFIX})" "" lib "${lib}")
  101. string(REGEX REPLACE "(${CMAKE_SHARED_LIBRARY_SUFFIX}|${CMAKE_STATIC_LIBRARY_SUFFIX})$" "" lib "${lib}")
  102. # use the file name and directory as hints
  103. list(APPEND _gdal_libpath "${dir}")
  104. list(APPEND _gdal_lib "${lib}")
  105. endif()
  106. endforeach()
  107. endif()
  108. endif()
  109. endif()
  110. find_library(GDAL_LIBRARY
  111. NAMES ${_gdal_lib} gdal gdal_i gdal1.5.0 gdal1.4.0 gdal1.3.2 GDAL
  112. HINTS
  113. ENV GDAL_DIR
  114. ENV GDAL_ROOT
  115. ${_gdal_libpath}
  116. PATH_SUFFIXES lib
  117. )
  118. if (EXISTS "${GDAL_INCLUDE_DIR}/gdal_version.h")
  119. file(STRINGS "${GDAL_INCLUDE_DIR}/gdal_version.h" _gdal_version
  120. REGEX "GDAL_RELEASE_NAME")
  121. string(REGEX REPLACE ".*\"\(.*\)\"" "\\1" GDAL_VERSION "${_gdal_version}")
  122. unset(_gdal_version)
  123. else ()
  124. set(GDAL_VERSION GDAL_VERSION-NOTFOUND)
  125. endif ()
  126. include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
  127. FIND_PACKAGE_HANDLE_STANDARD_ARGS(GDAL
  128. VERSION_VAR GDAL_VERSION
  129. REQUIRED_VARS GDAL_LIBRARY GDAL_INCLUDE_DIR)
  130. if (GDAL_FOUND AND NOT TARGET GDAL::GDAL)
  131. add_library(GDAL::GDAL UNKNOWN IMPORTED)
  132. set_target_properties(GDAL::GDAL PROPERTIES
  133. IMPORTED_LOCATION "${GDAL_LIBRARY}"
  134. INTERFACE_INCLUDE_DIRECTORIES "${GDAL_INCLUDE_DIR}")
  135. endif ()
  136. set(GDAL_LIBRARIES ${GDAL_LIBRARY})
  137. set(GDAL_INCLUDE_DIRS ${GDAL_INCLUDE_DIR})