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
76 cmake_policy(SET CMP0159 NEW) # file(STRINGS) with REGEX updates CMAKE_MATCH_<n>
78 find_path(GDAL_INCLUDE_DIR gdal.h
86 DOC "Path to the GDAL include directory"
88 mark_as_advanced(GDAL_INCLUDE_DIR)
90 if(UNIX AND NOT FindGDAL_SKIP_GDAL_CONFIG)
91 # Use gdal-config to obtain the library version (this should hopefully
92 # allow us to -lgdal1.x.y where x.y are correct version)
93 # For some reason, libgdal development packages do not contain
95 find_program(GDAL_CONFIG gdal-config
100 DOC "Path to the gdal-config tool"
102 mark_as_advanced(GDAL_CONFIG)
105 execute_process(COMMAND ${GDAL_CONFIG} --libs OUTPUT_VARIABLE GDAL_CONFIG_LIBS)
108 # treat the output as a command line and split it up
109 separate_arguments(args NATIVE_COMMAND "${GDAL_CONFIG_LIBS}")
111 # only consider libraries whose name matches this pattern
112 set(name_pattern "[gG][dD][aA][lL]")
114 # consider each entry as a possible library path, name, or parent directory
115 foreach(arg IN LISTS args)
117 if("${arg}" MATCHES "^-l(.*)$")
118 set(lib "${CMAKE_MATCH_1}")
120 # only consider libraries whose name matches the expected pattern
121 if("${lib}" MATCHES "${name_pattern}")
122 list(APPEND _gdal_lib "${lib}")
124 # library search path
125 elseif("${arg}" MATCHES "^-L(.*)$")
126 list(APPEND _gdal_libpath "${CMAKE_MATCH_1}")
127 # assume this is a full path to a library
128 elseif(IS_ABSOLUTE "${arg}" AND EXISTS "${arg}")
129 # extract the file name
130 get_filename_component(lib "${arg}" NAME)
132 # only consider libraries whose name matches the expected pattern
133 if(NOT "${lib}" MATCHES "${name_pattern}")
137 # extract the file directory
138 get_filename_component(dir "${arg}" DIRECTORY)
140 # remove library prefixes/suffixes
141 string(REGEX REPLACE "^(${CMAKE_SHARED_LIBRARY_PREFIX}|${CMAKE_STATIC_LIBRARY_PREFIX})" "" lib "${lib}")
142 string(REGEX REPLACE "(${CMAKE_SHARED_LIBRARY_SUFFIX}|${CMAKE_STATIC_LIBRARY_SUFFIX})$" "" lib "${lib}")
144 # use the file name and directory as hints
145 list(APPEND _gdal_libpath "${dir}")
146 list(APPEND _gdal_lib "${lib}")
153 # GDAL name its library when built with CMake as `gdal${major}${minor}`.
155 ${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)
158 foreach (_gdal_version IN LISTS _gdal_versions)
159 string(REPLACE "." "" _gdal_version "${_gdal_version}")
160 list(APPEND _gdal_libnames "gdal${_gdal_version}" "GDAL${_gdal_version}")
163 unset(_gdal_versions)
165 find_library(GDAL_LIBRARY
166 NAMES ${_gdal_lib} ${_gdal_libnames} gdal gdald gdal_i gdal1.5.0 gdal1.4.0 gdal1.3.2 GDAL
172 DOC "Path to the GDAL library"
174 mark_as_advanced(GDAL_LIBRARY)
175 unset(_gdal_libnames)
178 if (EXISTS "${GDAL_INCLUDE_DIR}/gdal_version.h")
179 file(STRINGS "${GDAL_INCLUDE_DIR}/gdal_version.h" _gdal_version
180 REGEX "GDAL_RELEASE_NAME")
181 string(REGEX REPLACE ".*\"\(.*\)\"" "\\1" GDAL_VERSION "${_gdal_version}")
184 set(GDAL_VERSION GDAL_VERSION-NOTFOUND)
187 include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
188 FIND_PACKAGE_HANDLE_STANDARD_ARGS(GDAL
189 VERSION_VAR GDAL_VERSION
190 REQUIRED_VARS GDAL_LIBRARY GDAL_INCLUDE_DIR)
193 set(GDAL_LIBRARIES ${GDAL_LIBRARY})
194 set(GDAL_INCLUDE_DIRS ${GDAL_INCLUDE_DIR})
196 if (NOT TARGET GDAL::GDAL)
197 add_library(GDAL::GDAL UNKNOWN IMPORTED)
198 set_target_properties(GDAL::GDAL PROPERTIES
199 IMPORTED_LOCATION "${GDAL_LIBRARY}"
200 INTERFACE_INCLUDE_DIRECTORIES "${GDAL_INCLUDE_DIR}")