FindGDAL.cmake 4.1 KB

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