1 # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
2 # file Copyright.txt or https://cmake.org/licensing for details.
4 #[=======================================================================[.rst:
8 Find Geospatial Data Abstraction Library (GDAL).
13 .. versionadded:: 3.14
15 This module defines :prop_tgt:`IMPORTED` target ``GDAL::GDAL``
16 if GDAL has been found.
21 This module will set the following variables in your project:
24 True if GDAL is found.
26 Include directories for GDAL headers.
28 Libraries to link to GDAL.
30 .. versionadded:: 3.14
31 The version of GDAL found.
36 The following cache variables may also be set:
39 The libgdal library file.
41 The directory containing ``gdal.h``.
46 Set ``GDAL_DIR`` or ``GDAL_ROOT`` in the environment to specify the
47 GDAL installation prefix.
49 The following variables may be set to modify the search strategy:
51 ``FindGDAL_SKIP_GDAL_CONFIG``
52 If set, ``gdal-config`` will not be used. This can be useful if there are
53 GDAL libraries built with autotools (which provide the tool) and CMake (which
54 do not) in the same environment.
55 ``GDAL_ADDITIONAL_LIBRARY_VERSIONS``
56 Extra versions of library names to search for.
57 #]=======================================================================]
59 # $GDALDIR is an environment variable that would
60 # correspond to the ./configure --prefix=$GDAL_DIR
61 # used in building gdal.
63 # Created by Eric Wing. I'm not a gdal user, but OpenSceneGraph uses it
64 # for osgTerrain so I whipped this module together for completeness.
65 # I actually don't know the conventions or where files are typically
67 # Any real gdal users are encouraged to correct this (but please don't
68 # break the OS X framework stuff when doing so which is what usually seems
71 # This makes the presumption that you are include gdal.h like
75 find_path(GDAL_INCLUDE_DIR gdal.h
83 DOC "Path to the GDAL include directory"
85 mark_as_advanced(GDAL_INCLUDE_DIR)
87 if(UNIX AND NOT FindGDAL_SKIP_GDAL_CONFIG)
88 # Use gdal-config to obtain the library version (this should hopefully
89 # allow us to -lgdal1.x.y where x.y are correct version)
90 # For some reason, libgdal development packages do not contain
92 find_program(GDAL_CONFIG gdal-config
97 DOC "Path to the gdal-config tool"
99 mark_as_advanced(GDAL_CONFIG)
102 execute_process(COMMAND ${GDAL_CONFIG} --libs OUTPUT_VARIABLE GDAL_CONFIG_LIBS)
105 # treat the output as a command line and split it up
106 separate_arguments(args NATIVE_COMMAND "${GDAL_CONFIG_LIBS}")
108 # only consider libraries whose name matches this pattern
109 set(name_pattern "[gG][dD][aA][lL]")
111 # consider each entry as a possible library path, name, or parent directory
112 foreach(arg IN LISTS args)
114 if("${arg}" MATCHES "^-l(.*)$")
115 set(lib "${CMAKE_MATCH_1}")
117 # only consider libraries whose name matches the expected pattern
118 if("${lib}" MATCHES "${name_pattern}")
119 list(APPEND _gdal_lib "${lib}")
121 # library search path
122 elseif("${arg}" MATCHES "^-L(.*)$")
123 list(APPEND _gdal_libpath "${CMAKE_MATCH_1}")
124 # assume this is a full path to a library
125 elseif(IS_ABSOLUTE "${arg}" AND EXISTS "${arg}")
126 # extract the file name
127 get_filename_component(lib "${arg}" NAME)
129 # only consider libraries whose name matches the expected pattern
130 if(NOT "${lib}" MATCHES "${name_pattern}")
134 # extract the file directory
135 get_filename_component(dir "${arg}" DIRECTORY)
137 # remove library prefixes/suffixes
138 string(REGEX REPLACE "^(${CMAKE_SHARED_LIBRARY_PREFIX}|${CMAKE_STATIC_LIBRARY_PREFIX})" "" lib "${lib}")
139 string(REGEX REPLACE "(${CMAKE_SHARED_LIBRARY_SUFFIX}|${CMAKE_STATIC_LIBRARY_SUFFIX})$" "" lib "${lib}")
141 # use the file name and directory as hints
142 list(APPEND _gdal_libpath "${dir}")
143 list(APPEND _gdal_lib "${lib}")
150 # GDAL name its library when built with CMake as `gdal${major}${minor}`.
152 ${GDAL_ADDITIONAL_LIBRARY_VERSIONS} 3.0 2.4 2.3 2.2 2.1 2.0 1.11 1.10 1.9 1.8 1.7 1.6 1.5 1.4 1.3 1.2)
155 foreach (_gdal_version IN LISTS _gdal_versions)
156 string(REPLACE "." "" _gdal_version "${_gdal_version}")
157 list(APPEND _gdal_libnames "gdal${_gdal_version}" "GDAL${_gdal_version}")
160 unset(_gdal_versions)
162 find_library(GDAL_LIBRARY
163 NAMES ${_gdal_lib} ${_gdal_libnames} gdal gdald gdal_i gdal1.5.0 gdal1.4.0 gdal1.3.2 GDAL
169 DOC "Path to the GDAL library"
171 mark_as_advanced(GDAL_LIBRARY)
172 unset(_gdal_libnames)
175 if (EXISTS "${GDAL_INCLUDE_DIR}/gdal_version.h")
176 file(STRINGS "${GDAL_INCLUDE_DIR}/gdal_version.h" _gdal_version
177 REGEX "GDAL_RELEASE_NAME")
178 string(REGEX REPLACE ".*\"\(.*\)\"" "\\1" GDAL_VERSION "${_gdal_version}")
181 set(GDAL_VERSION GDAL_VERSION-NOTFOUND)
184 include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
185 FIND_PACKAGE_HANDLE_STANDARD_ARGS(GDAL
186 VERSION_VAR GDAL_VERSION
187 REQUIRED_VARS GDAL_LIBRARY GDAL_INCLUDE_DIR)
190 set(GDAL_LIBRARIES ${GDAL_LIBRARY})
191 set(GDAL_INCLUDE_DIRS ${GDAL_INCLUDE_DIR})
193 if (NOT TARGET GDAL::GDAL)
194 add_library(GDAL::GDAL UNKNOWN IMPORTED)
195 set_target_properties(GDAL::GDAL PROPERTIES
196 IMPORTED_LOCATION "${GDAL_LIBRARY}"
197 INTERFACE_INCLUDE_DIRECTORIES "${GDAL_INCLUDE_DIR}")