FindGDAL.cmake 4.2 KB

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