FindGDAL.cmake 5.2 KB

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