FindGDAL.cmake 5.9 KB

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