FindGDAL.cmake 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. mark_as_advanced(GDAL_INCLUDE_DIR)
  60. if(UNIX)
  61. # Use gdal-config to obtain the library version (this should hopefully
  62. # allow us to -lgdal1.x.y where x.y are correct version)
  63. # For some reason, libgdal development packages do not contain
  64. # libgdal.so...
  65. find_program(GDAL_CONFIG gdal-config
  66. HINTS
  67. ENV GDAL_DIR
  68. ENV GDAL_ROOT
  69. PATH_SUFFIXES bin
  70. )
  71. mark_as_advanced(GDAL_CONFIG)
  72. if(GDAL_CONFIG)
  73. execute_process(COMMAND ${GDAL_CONFIG} --libs OUTPUT_VARIABLE GDAL_CONFIG_LIBS)
  74. if(GDAL_CONFIG_LIBS)
  75. # treat the output as a command line and split it up
  76. separate_arguments(args NATIVE_COMMAND "${GDAL_CONFIG_LIBS}")
  77. # only consider libraries whose name matches this pattern
  78. set(name_pattern "[gG][dD][aA][lL]")
  79. # consider each entry as a possible library path, name, or parent directory
  80. foreach(arg IN LISTS args)
  81. # library name
  82. if("${arg}" MATCHES "^-l(.*)$")
  83. set(lib "${CMAKE_MATCH_1}")
  84. # only consider libraries whose name matches the expected pattern
  85. if("${lib}" MATCHES "${name_pattern}")
  86. list(APPEND _gdal_lib "${lib}")
  87. endif()
  88. # library search path
  89. elseif("${arg}" MATCHES "^-L(.*)$")
  90. list(APPEND _gdal_libpath "${CMAKE_MATCH_1}")
  91. # assume this is a full path to a library
  92. elseif(IS_ABSOLUTE "${arg}" AND EXISTS "${arg}")
  93. # extract the file name
  94. get_filename_component(lib "${arg}" NAME)
  95. # only consider libraries whose name matches the expected pattern
  96. if(NOT "${lib}" MATCHES "${name_pattern}")
  97. continue()
  98. endif()
  99. # extract the file directory
  100. get_filename_component(dir "${arg}" DIRECTORY)
  101. # remove library prefixes/suffixes
  102. string(REGEX REPLACE "^(${CMAKE_SHARED_LIBRARY_PREFIX}|${CMAKE_STATIC_LIBRARY_PREFIX})" "" lib "${lib}")
  103. string(REGEX REPLACE "(${CMAKE_SHARED_LIBRARY_SUFFIX}|${CMAKE_STATIC_LIBRARY_SUFFIX})$" "" lib "${lib}")
  104. # use the file name and directory as hints
  105. list(APPEND _gdal_libpath "${dir}")
  106. list(APPEND _gdal_lib "${lib}")
  107. endif()
  108. endforeach()
  109. endif()
  110. endif()
  111. endif()
  112. find_library(GDAL_LIBRARY
  113. NAMES ${_gdal_lib} gdal gdal_i gdal1.5.0 gdal1.4.0 gdal1.3.2 GDAL
  114. HINTS
  115. ENV GDAL_DIR
  116. ENV GDAL_ROOT
  117. ${_gdal_libpath}
  118. PATH_SUFFIXES lib
  119. )
  120. mark_as_advanced(GDAL_LIBRARY)
  121. if (EXISTS "${GDAL_INCLUDE_DIR}/gdal_version.h")
  122. file(STRINGS "${GDAL_INCLUDE_DIR}/gdal_version.h" _gdal_version
  123. REGEX "GDAL_RELEASE_NAME")
  124. string(REGEX REPLACE ".*\"\(.*\)\"" "\\1" GDAL_VERSION "${_gdal_version}")
  125. unset(_gdal_version)
  126. else ()
  127. set(GDAL_VERSION GDAL_VERSION-NOTFOUND)
  128. endif ()
  129. include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
  130. FIND_PACKAGE_HANDLE_STANDARD_ARGS(GDAL
  131. VERSION_VAR GDAL_VERSION
  132. REQUIRED_VARS GDAL_LIBRARY GDAL_INCLUDE_DIR)
  133. if (GDAL_FOUND AND NOT TARGET GDAL::GDAL)
  134. add_library(GDAL::GDAL UNKNOWN IMPORTED)
  135. set_target_properties(GDAL::GDAL PROPERTIES
  136. IMPORTED_LOCATION "${GDAL_LIBRARY}"
  137. INTERFACE_INCLUDE_DIRECTORIES "${GDAL_INCLUDE_DIR}")
  138. endif ()
  139. set(GDAL_LIBRARIES ${GDAL_LIBRARY})
  140. set(GDAL_INCLUDE_DIRS ${GDAL_INCLUDE_DIR})