Merge topic 'cxx-checks-tolerate-unused-arguments'
[kiteware-cmake.git] / Modules / FindGDAL.cmake
bloba9c57408de0dabd394100ff0fdd8fdc919c2d7e1
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:
5 FindGDAL
6 --------
8 Find Geospatial Data Abstraction Library (GDAL).
10 IMPORTED Targets
11 ^^^^^^^^^^^^^^^^
13 .. versionadded:: 3.14
15 This module defines :prop_tgt:`IMPORTED` target ``GDAL::GDAL``
16 if GDAL has been found.
18 Result Variables
19 ^^^^^^^^^^^^^^^^
21 This module will set the following variables in your project:
23 ``GDAL_FOUND``
24   True if GDAL is found.
25 ``GDAL_INCLUDE_DIRS``
26   Include directories for GDAL headers.
27 ``GDAL_LIBRARIES``
28   Libraries to link to GDAL.
29 ``GDAL_VERSION``
30   .. versionadded:: 3.14
31     The version of GDAL found.
33 Cache variables
34 ^^^^^^^^^^^^^^^
36 The following cache variables may also be set:
38 ``GDAL_LIBRARY``
39   The libgdal library file.
40 ``GDAL_INCLUDE_DIR``
41   The directory containing ``gdal.h``.
43 Hints
44 ^^^^^
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
66 # placed in distros.
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
69 # to happen).
71 # This makes the presumption that you are include gdal.h like
73 #include "gdal.h"
75 cmake_policy(PUSH)
76 cmake_policy(SET CMP0159 NEW) # file(STRINGS) with REGEX updates CMAKE_MATCH_<n>
78 find_path(GDAL_INCLUDE_DIR gdal.h
79   HINTS
80     ENV GDAL_DIR
81     ENV GDAL_ROOT
82   PATH_SUFFIXES
83     include/gdal
84     include/GDAL
85     include
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
94     # libgdal.so...
95     find_program(GDAL_CONFIG gdal-config
96         HINTS
97           ENV GDAL_DIR
98           ENV GDAL_ROOT
99         PATH_SUFFIXES bin
100         DOC "Path to the gdal-config tool"
101     )
102     mark_as_advanced(GDAL_CONFIG)
104     if(GDAL_CONFIG)
105         execute_process(COMMAND ${GDAL_CONFIG} --libs OUTPUT_VARIABLE GDAL_CONFIG_LIBS)
107         if(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)
116                 # library name
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}")
123                     endif()
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}")
134                         continue()
135                     endif()
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}")
147                 endif()
148             endforeach()
149         endif()
150     endif()
151 endif()
153 # GDAL name its library when built with CMake as `gdal${major}${minor}`.
154 set(_gdal_versions
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)
157 set(_gdal_libnames)
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}")
161 endforeach ()
162 unset(_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
167   HINTS
168      ENV GDAL_DIR
169      ENV GDAL_ROOT
170      ${_gdal_libpath}
171   PATH_SUFFIXES lib
172   DOC "Path to the GDAL library"
174 mark_as_advanced(GDAL_LIBRARY)
175 unset(_gdal_libnames)
176 unset(_gdal_lib)
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}")
182     unset(_gdal_version)
183 else ()
184     set(GDAL_VERSION GDAL_VERSION-NOTFOUND)
185 endif ()
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)
192 if (GDAL_FOUND)
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}")
201     endif ()
202 endif ()
204 cmake_policy(POP)