FindGDAL.cmake 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. )
  49. if(UNIX)
  50. # Use gdal-config to obtain the library version (this should hopefully
  51. # allow us to -lgdal1.x.y where x.y are correct version)
  52. # For some reason, libgdal development packages do not contain
  53. # libgdal.so...
  54. find_program(GDAL_CONFIG gdal-config
  55. HINTS
  56. ENV GDAL_DIR
  57. ENV GDAL_ROOT
  58. )
  59. if(GDAL_CONFIG)
  60. exec_program(${GDAL_CONFIG} ARGS --libs OUTPUT_VARIABLE GDAL_CONFIG_LIBS)
  61. if(GDAL_CONFIG_LIBS)
  62. string(REGEX MATCHALL "-l[^ ]+" _gdal_dashl ${GDAL_CONFIG_LIBS})
  63. string(REPLACE "-l" "" _gdal_lib "${_gdal_dashl}")
  64. string(REGEX MATCHALL "-L[^ ]+" _gdal_dashL ${GDAL_CONFIG_LIBS})
  65. string(REPLACE "-L" "" _gdal_libpath "${_gdal_dashL}")
  66. endif()
  67. endif()
  68. endif()
  69. find_library(GDAL_LIBRARY
  70. NAMES ${_gdal_lib} gdal gdal_i gdal1.5.0 gdal1.4.0 gdal1.3.2 GDAL
  71. HINTS
  72. ENV GDAL_DIR
  73. ENV GDAL_ROOT
  74. ${_gdal_libpath}
  75. )
  76. include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
  77. FIND_PACKAGE_HANDLE_STANDARD_ARGS(GDAL DEFAULT_MSG GDAL_LIBRARY GDAL_INCLUDE_DIR)
  78. set(GDAL_LIBRARIES ${GDAL_LIBRARY})
  79. set(GDAL_INCLUDE_DIRS ${GDAL_INCLUDE_DIR})