Imported Upstream version 3.25.0
[platform/upstream/cmake.git] / Modules / FindGDAL.cmake
1 # Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
2 # file Copyright.txt or https://cmake.org/licensing for details.
3
4 #[=======================================================================[.rst:
5 FindGDAL
6 --------
7
8 Find Geospatial Data Abstraction Library (GDAL).
9
10 IMPORTED Targets
11 ^^^^^^^^^^^^^^^^
12
13 .. versionadded:: 3.14
14
15 This module defines :prop_tgt:`IMPORTED` target ``GDAL::GDAL``
16 if GDAL has been found.
17
18 Result Variables
19 ^^^^^^^^^^^^^^^^
20
21 This module will set the following variables in your project:
22
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.
32
33 Cache variables
34 ^^^^^^^^^^^^^^^
35
36 The following cache variables may also be set:
37
38 ``GDAL_LIBRARY``
39   The libgdal library file.
40 ``GDAL_INCLUDE_DIR``
41   The directory containing ``gdal.h``.
42
43 Hints
44 ^^^^^
45
46 Set ``GDAL_DIR`` or ``GDAL_ROOT`` in the environment to specify the
47 GDAL installation prefix.
48
49 The following variables may be set to modify the search strategy:
50
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 #]=======================================================================]
58
59 # $GDALDIR is an environment variable that would
60 # correspond to the ./configure --prefix=$GDAL_DIR
61 # used in building gdal.
62 #
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).
70
71 # This makes the presumption that you are include gdal.h like
72 #
73 #include "gdal.h"
74
75 find_path(GDAL_INCLUDE_DIR gdal.h
76   HINTS
77     ENV GDAL_DIR
78     ENV GDAL_ROOT
79   PATH_SUFFIXES
80     include/gdal
81     include/GDAL
82     include
83   DOC "Path to the GDAL include directory"
84 )
85 mark_as_advanced(GDAL_INCLUDE_DIR)
86
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
91     # libgdal.so...
92     find_program(GDAL_CONFIG gdal-config
93         HINTS
94           ENV GDAL_DIR
95           ENV GDAL_ROOT
96         PATH_SUFFIXES bin
97         DOC "Path to the gdal-config tool"
98     )
99     mark_as_advanced(GDAL_CONFIG)
100
101     if(GDAL_CONFIG)
102         execute_process(COMMAND ${GDAL_CONFIG} --libs OUTPUT_VARIABLE GDAL_CONFIG_LIBS)
103
104         if(GDAL_CONFIG_LIBS)
105             # treat the output as a command line and split it up
106             separate_arguments(args NATIVE_COMMAND "${GDAL_CONFIG_LIBS}")
107
108             # only consider libraries whose name matches this pattern
109             set(name_pattern "[gG][dD][aA][lL]")
110
111             # consider each entry as a possible library path, name, or parent directory
112             foreach(arg IN LISTS args)
113                 # library name
114                 if("${arg}" MATCHES "^-l(.*)$")
115                     set(lib "${CMAKE_MATCH_1}")
116
117                     # only consider libraries whose name matches the expected pattern
118                     if("${lib}" MATCHES "${name_pattern}")
119                         list(APPEND _gdal_lib "${lib}")
120                     endif()
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)
128
129                     # only consider libraries whose name matches the expected pattern
130                     if(NOT "${lib}" MATCHES "${name_pattern}")
131                         continue()
132                     endif()
133
134                     # extract the file directory
135                     get_filename_component(dir "${arg}" DIRECTORY)
136
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}")
140
141                     # use the file name and directory as hints
142                     list(APPEND _gdal_libpath "${dir}")
143                     list(APPEND _gdal_lib "${lib}")
144                 endif()
145             endforeach()
146         endif()
147     endif()
148 endif()
149
150 # GDAL name its library when built with CMake as `gdal${major}${minor}`.
151 set(_gdal_versions
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)
153
154 set(_gdal_libnames)
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}")
158 endforeach ()
159 unset(_gdal_version)
160 unset(_gdal_versions)
161
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
164   HINTS
165      ENV GDAL_DIR
166      ENV GDAL_ROOT
167      ${_gdal_libpath}
168   PATH_SUFFIXES lib
169   DOC "Path to the GDAL library"
170 )
171 mark_as_advanced(GDAL_LIBRARY)
172 unset(_gdal_libnames)
173 unset(_gdal_lib)
174
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}")
179     unset(_gdal_version)
180 else ()
181     set(GDAL_VERSION GDAL_VERSION-NOTFOUND)
182 endif ()
183
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)
188
189 if (GDAL_FOUND)
190     set(GDAL_LIBRARIES ${GDAL_LIBRARY})
191     set(GDAL_INCLUDE_DIRS ${GDAL_INCLUDE_DIR})
192
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}")
198     endif ()
199 endif ()