FindGDAL.cmake 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. The following variables may be set to modify the search strategy:
  36. ``FindGDAL_SKIP_GDAL_CONFIG``
  37. If set, ``gdal-config`` will not be used. This can be useful if there are
  38. GDAL libraries built with autotools (which provide the tool) and CMake (which
  39. do not) in the same environment.
  40. ``GDAL_ADDITIONAL_LIBRARY_VERSIONS``
  41. Extra versions of library names to search for.
  42. #]=======================================================================]
  43. # $GDALDIR is an environment variable that would
  44. # correspond to the ./configure --prefix=$GDAL_DIR
  45. # used in building gdal.
  46. #
  47. # Created by Eric Wing. I'm not a gdal user, but OpenSceneGraph uses it
  48. # for osgTerrain so I whipped this module together for completeness.
  49. # I actually don't know the conventions or where files are typically
  50. # placed in distros.
  51. # Any real gdal users are encouraged to correct this (but please don't
  52. # break the OS X framework stuff when doing so which is what usually seems
  53. # to happen).
  54. # This makes the presumption that you are include gdal.h like
  55. #
  56. #include "gdal.h"
  57. find_path(GDAL_INCLUDE_DIR gdal.h
  58. HINTS
  59. ENV GDAL_DIR
  60. ENV GDAL_ROOT
  61. PATH_SUFFIXES
  62. include/gdal
  63. include/GDAL
  64. include
  65. DOC "Path to the GDAL include directory"
  66. )
  67. mark_as_advanced(GDAL_INCLUDE_DIR)
  68. if(UNIX AND NOT FindGDAL_SKIP_GDAL_CONFIG)
  69. # Use gdal-config to obtain the library version (this should hopefully
  70. # allow us to -lgdal1.x.y where x.y are correct version)
  71. # For some reason, libgdal development packages do not contain
  72. # libgdal.so...
  73. find_program(GDAL_CONFIG gdal-config
  74. HINTS
  75. ENV GDAL_DIR
  76. ENV GDAL_ROOT
  77. PATH_SUFFIXES bin
  78. DOC "Path to the gdal-config tool"
  79. )
  80. mark_as_advanced(GDAL_CONFIG)
  81. if(GDAL_CONFIG)
  82. execute_process(COMMAND ${GDAL_CONFIG} --libs OUTPUT_VARIABLE GDAL_CONFIG_LIBS)
  83. if(GDAL_CONFIG_LIBS)
  84. # treat the output as a command line and split it up
  85. separate_arguments(args NATIVE_COMMAND "${GDAL_CONFIG_LIBS}")
  86. # only consider libraries whose name matches this pattern
  87. set(name_pattern "[gG][dD][aA][lL]")
  88. # consider each entry as a possible library path, name, or parent directory
  89. foreach(arg IN LISTS args)
  90. # library name
  91. if("${arg}" MATCHES "^-l(.*)$")
  92. set(lib "${CMAKE_MATCH_1}")
  93. # only consider libraries whose name matches the expected pattern
  94. if("${lib}" MATCHES "${name_pattern}")
  95. list(APPEND _gdal_lib "${lib}")
  96. endif()
  97. # library search path
  98. elseif("${arg}" MATCHES "^-L(.*)$")
  99. list(APPEND _gdal_libpath "${CMAKE_MATCH_1}")
  100. # assume this is a full path to a library
  101. elseif(IS_ABSOLUTE "${arg}" AND EXISTS "${arg}")
  102. # extract the file name
  103. get_filename_component(lib "${arg}" NAME)
  104. # only consider libraries whose name matches the expected pattern
  105. if(NOT "${lib}" MATCHES "${name_pattern}")
  106. continue()
  107. endif()
  108. # extract the file directory
  109. get_filename_component(dir "${arg}" DIRECTORY)
  110. # remove library prefixes/suffixes
  111. string(REGEX REPLACE "^(${CMAKE_SHARED_LIBRARY_PREFIX}|${CMAKE_STATIC_LIBRARY_PREFIX})" "" lib "${lib}")
  112. string(REGEX REPLACE "(${CMAKE_SHARED_LIBRARY_SUFFIX}|${CMAKE_STATIC_LIBRARY_SUFFIX})$" "" lib "${lib}")
  113. # use the file name and directory as hints
  114. list(APPEND _gdal_libpath "${dir}")
  115. list(APPEND _gdal_lib "${lib}")
  116. endif()
  117. endforeach()
  118. endif()
  119. endif()
  120. endif()
  121. # GDAL name its library when built with CMake as `gdal${major}${minor}`.
  122. set(_gdal_versions
  123. ${GDAL_ADDITIONAL_LIBRARY_VERSIONS} 3.0 2.4 2.3 2.2 2.1 2.0 1.11 1.10 1.9 1.8 1.7 1.6 1.5 1.4 1.3 1.2)
  124. set(_gdal_libnames)
  125. foreach (_gdal_version IN LISTS _gdal_versions)
  126. string(REPLACE "." "" _gdal_version "${_gdal_version}")
  127. list(APPEND _gdal_libnames "gdal${_gdal_version}" "GDAL${_gdal_version}")
  128. endforeach ()
  129. unset(_gdal_version)
  130. unset(_gdal_versions)
  131. find_library(GDAL_LIBRARY
  132. NAMES ${_gdal_lib} ${_gdal_libnames} gdal gdal_i gdal1.5.0 gdal1.4.0 gdal1.3.2 GDAL
  133. HINTS
  134. ENV GDAL_DIR
  135. ENV GDAL_ROOT
  136. ${_gdal_libpath}
  137. PATH_SUFFIXES lib
  138. DOC "Path to the GDAL library"
  139. )
  140. mark_as_advanced(GDAL_LIBRARY)
  141. unset(_gdal_libnames)
  142. unset(_gdal_lib)
  143. if (EXISTS "${GDAL_INCLUDE_DIR}/gdal_version.h")
  144. file(STRINGS "${GDAL_INCLUDE_DIR}/gdal_version.h" _gdal_version
  145. REGEX "GDAL_RELEASE_NAME")
  146. string(REGEX REPLACE ".*\"\(.*\)\"" "\\1" GDAL_VERSION "${_gdal_version}")
  147. unset(_gdal_version)
  148. else ()
  149. set(GDAL_VERSION GDAL_VERSION-NOTFOUND)
  150. endif ()
  151. include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
  152. FIND_PACKAGE_HANDLE_STANDARD_ARGS(GDAL
  153. VERSION_VAR GDAL_VERSION
  154. REQUIRED_VARS GDAL_LIBRARY GDAL_INCLUDE_DIR)
  155. if (GDAL_FOUND)
  156. set(GDAL_LIBRARIES ${GDAL_LIBRARY})
  157. set(GDAL_INCLUDE_DIRS ${GDAL_INCLUDE_DIR})
  158. if (NOT TARGET GDAL::GDAL)
  159. add_library(GDAL::GDAL UNKNOWN IMPORTED)
  160. set_target_properties(GDAL::GDAL PROPERTIES
  161. IMPORTED_LOCATION "${GDAL_LIBRARY}"
  162. INTERFACE_INCLUDE_DIRECTORIES "${GDAL_INCLUDE_DIR}")
  163. endif ()
  164. endif ()