Debian Codegraph @

CMakeModules/FindGDAL.cmake

raw
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 This module defines :prop_tgt:`IMPORTED` target ``GDAL::GDAL``
14 if GDAL has been found.
15
16 Result Variables
17 ^^^^^^^^^^^^^^^^
18
19 This module will set the following variables in your project:
20
21 ``GDAL_FOUND``
22 True if GDAL is found.
23 ``GDAL_INCLUDE_DIRS``
24 Include directories for GDAL headers.
25 ``GDAL_LIBRARIES``
26 Libraries to link to GDAL.
27 ``GDAL_VERSION``
28 The version of GDAL found.
29
30 Cache variables
31 ^^^^^^^^^^^^^^^
32
33 The following cache variables may also be set:
34
35 ``GDAL_LIBRARY``
36 The libgdal library file.
37 ``GDAL_INCLUDE_DIR``
38 The directory containing ``gdal.h``.
39
40 Hints
41 ^^^^^
42
43 Set ``GDAL_DIR`` or ``GDAL_ROOT`` in the environment to specify the
44 GDAL installation prefix.
45 #]=======================================================================]
46
47 # $GDALDIR is an environment variable that would
48 # correspond to the ./configure --prefix=$GDAL_DIR
49 # used in building gdal.
50 #
51 # Created by Eric Wing. I'm not a gdal user, but OpenSceneGraph uses it
52 # for osgTerrain so I whipped this module together for completeness.
53 # I actually don't know the conventions or where files are typically
54 # placed in distros.
55 # Any real gdal users are encouraged to correct this (but please don't
56 # break the OS X framework stuff when doing so which is what usually seems
57 # to happen).
58
59 # This makes the presumption that you are include gdal.h like
60 #
61 #include "gdal.h"
62
63 include(FindPackageHandleStandardArgs)
64
65 find_path(GDAL_INCLUDE_DIR gdal.h
66 HINTS
67 ENV GDAL_DIR
68 ENV GDAL_ROOT
69 PATH_SUFFIXES
70 include/gdal
71 include/GDAL
72 include
73 )
74
75 if(UNIX)
76 # Use gdal-config to obtain the library version (this should hopefully
77 # allow us to -lgdal1.x.y where x.y are correct version)
78 # For some reason, libgdal development packages do not contain
79 # libgdal.so...
80 find_program(GDAL_CONFIG gdal-config
81 HINTS
82 ENV GDAL_DIR
83 ENV GDAL_ROOT
84 PATH_SUFFIXES bin
85 )
86
87 if(GDAL_CONFIG)
88 exec_program(${GDAL_CONFIG} ARGS --libs OUTPUT_VARIABLE GDAL_CONFIG_LIBS)
89
90 if(GDAL_CONFIG_LIBS)
91 # treat the output as a command line and split it up
92 separate_arguments(args NATIVE_COMMAND "${GDAL_CONFIG_LIBS}")
93
94 # only consider libraries whose name matches this pattern
95 set(name_pattern "[gG][dD][aA][lL]")
96
97 # consider each entry as a possible library path, name, or parent directory
98 foreach(arg IN LISTS args)
99 # library name
100 if("${arg}" MATCHES "^-l(.*)$")
101 set(lib "${CMAKE_MATCH_1}")
102
103 # only consider libraries whose name matches the expected pattern
104 if("${lib}" MATCHES "${name_pattern}")
105 list(APPEND _gdal_lib "${lib}")
106 endif()
107 # library search path
108 elseif("${arg}" MATCHES "^-L(.*)$")
109 list(APPEND _gdal_libpath "${CMAKE_MATCH_1}")
110 # assume this is a full path to a library
111 elseif(IS_ABSOLUTE "${arg}" AND EXISTS "${arg}")
112 # extract the file name
113 get_filename_component(lib "${arg}" NAME)
114
115 # only consider libraries whose name matches the expected pattern
116 if(NOT "${lib}" MATCHES "${name_pattern}")
117 continue()
118 endif()
119
120 # extract the file directory
121 get_filename_component(dir "${arg}" DIRECTORY)
122
123 # remove library prefixes/suffixes
124 string(REGEX REPLACE "^(${CMAKE_SHARED_LIBRARY_PREFIX}|${CMAKE_STATIC_LIBRARY_PREFIX})" "" lib "${lib}")
125 string(REGEX REPLACE "(${CMAKE_SHARED_LIBRARY_SUFFIX}|${CMAKE_STATIC_LIBRARY_SUFFIX})$" "" lib "${lib}")
126
127 # use the file name and directory as hints
128 list(APPEND _gdal_libpath "${dir}")
129 list(APPEND _gdal_lib "${lib}")
130 endif()
131 endforeach()
132 endif()
133 endif()
134 endif()
135
136 find_library(GDAL_LIBRARY
137 NAMES ${_gdal_lib} gdal gdal_i gdal1.5.0 gdal1.4.0 gdal1.3.2 GDAL
138 HINTS
139 ENV GDAL_DIR
140 ENV GDAL_ROOT
141 ${_gdal_libpath}
142 PATH_SUFFIXES lib
143 )
144
145 if (EXISTS "${GDAL_INCLUDE_DIR}/gdal_version.h")
146 file(STRINGS "${GDAL_INCLUDE_DIR}/gdal_version.h" _gdal_version
147 REGEX "GDAL_RELEASE_NAME")
148 string(REGEX REPLACE ".*\"\(.*\)\"" "\\1" GDAL_VERSION "${_gdal_version}")
149 unset(_gdal_version)
150 else ()
151 set(GDAL_VERSION GDAL_VERSION-NOTFOUND)
152 endif ()
153
154 FIND_PACKAGE_HANDLE_STANDARD_ARGS(GDAL
155 VERSION_VAR GDAL_VERSION
156 REQUIRED_VARS GDAL_LIBRARY GDAL_INCLUDE_DIR)
157
158 if (GDAL_FOUND AND NOT TARGET GDAL::GDAL)
159 add_library(GDAL::GDAL UNKNOWN IMPORTED)
160 set_target_properties(GDAL::GDAL PROPERTIES
161 IMPORTED_LOCATION "${GDAL_LIBRARY}"
162 INTERFACE_INCLUDE_DIRECTORIES "${GDAL_INCLUDE_DIR}")
163 endif ()
164
165 set(GDAL_LIBRARIES ${GDAL_LIBRARY})
166 set(GDAL_INCLUDE_DIRS ${GDAL_INCLUDE_DIR})