forked from qgis/QGIS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFindGDAL.cmake
200 lines (167 loc) · 8.04 KB
/
FindGDAL.cmake
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# Find GDAL
# ~~~~~~~~~
# Copyright (c) 2007, Magnus Homann <magnus at homann dot se>
# Redistribution and use is allowed according to the terms of the BSD license.
# For details see the accompanying COPYING-CMAKE-SCRIPTS file.
#
#
# Once run this will define:
#
# GDAL_FOUND = system has GDAL lib
#
# GDAL_LIBRARY = full path to the library
#
# GDAL_INCLUDE_DIR = where to find headers
IF (POLICY CMP0053) # in CMake 3.1.0+
CMAKE_POLICY (SET CMP0053 OLD) # keep old-style @VAR@ expansion
ENDIF (POLICY CMP0053)
INCLUDE (@CMAKE_SOURCE_DIR@/cmake/MacPlistMacros.cmake)
IF(WIN32)
IF (MINGW)
FIND_PATH(GDAL_INCLUDE_DIR gdal.h /usr/local/include /usr/include c:/msys/local/include)
FIND_LIBRARY(GDAL_LIBRARY NAMES gdal PATHS /usr/local/lib /usr/lib c:/msys/local/lib)
ENDIF (MINGW)
IF (MSVC)
FIND_PATH(GDAL_INCLUDE_DIR gdal.h "$ENV{LIB_DIR}/include/gdal" $ENV{INCLUDE})
FIND_LIBRARY(GDAL_LIBRARY NAMES gdal gdal_i PATHS
"$ENV{LIB_DIR}/lib" $ENV{LIB} /usr/lib c:/msys/local/lib)
IF (GDAL_LIBRARY)
SET (
GDAL_LIBRARY;odbc32;odbccp32
CACHE STRING INTERNAL)
ENDIF (GDAL_LIBRARY)
ENDIF (MSVC)
ELSE(WIN32)
IF(UNIX)
# try to use framework on mac
# want clean framework path, not unix compatibility path
IF (APPLE)
IF (CMAKE_FIND_FRAMEWORK MATCHES "FIRST"
OR CMAKE_FRAMEWORK_PATH MATCHES "ONLY"
OR NOT CMAKE_FIND_FRAMEWORK)
SET (CMAKE_FIND_FRAMEWORK_save ${CMAKE_FIND_FRAMEWORK} CACHE STRING "" FORCE)
SET (CMAKE_FIND_FRAMEWORK "ONLY" CACHE STRING "" FORCE)
FIND_LIBRARY(GDAL_LIBRARY GDAL)
IF (GDAL_LIBRARY)
# they're all the same in a framework
SET (GDAL_INCLUDE_DIR ${GDAL_LIBRARY}/Headers CACHE PATH "Path to a file.")
# set GDAL_CONFIG to make later test happy, not used here, may not exist
SET (GDAL_CONFIG ${GDAL_LIBRARY}/unix/bin/gdal-config CACHE FILEPATH "Path to a program.")
# version in info.plist
GET_VERSION_PLIST (${GDAL_LIBRARY}/Resources/Info.plist GDAL_VERSION)
IF (NOT GDAL_VERSION)
MESSAGE (FATAL_ERROR "Could not determine GDAL version from framework.")
ENDIF (NOT GDAL_VERSION)
STRING(REGEX REPLACE "([0-9]+)\\.([0-9]+)\\.([0-9]+)" "\\1" GDAL_VERSION_MAJOR "${GDAL_VERSION}")
STRING(REGEX REPLACE "([0-9]+)\\.([0-9]+)\\.([0-9]+)" "\\2" GDAL_VERSION_MINOR "${GDAL_VERSION}")
IF (GDAL_VERSION_MAJOR LESS 2)
MESSAGE (FATAL_ERROR "GDAL version is too old (${GDAL_VERSION}). Use 2.1 or higher.")
ENDIF (GDAL_VERSION_MAJOR LESS 2)
IF ( (GDAL_VERSION_MAJOR EQUAL 2) AND (GDAL_VERSION_MINOR LESS 1) )
MESSAGE (FATAL_ERROR "GDAL version is too old (${GDAL_VERSION}). Use 2.1 or higher.")
ENDIF( (GDAL_VERSION_MAJOR EQUAL 2) AND (GDAL_VERSION_MINOR LESS 1) )
ENDIF (GDAL_LIBRARY)
SET (CMAKE_FIND_FRAMEWORK ${CMAKE_FIND_FRAMEWORK_save} CACHE STRING "" FORCE)
ENDIF ()
ENDIF (APPLE)
IF(CYGWIN)
FIND_LIBRARY(GDAL_LIBRARY NAMES gdal PATHS /usr/lib /usr/local/lib)
ENDIF(CYGWIN)
IF (NOT GDAL_INCLUDE_DIR OR NOT GDAL_LIBRARY OR NOT GDAL_CONFIG)
# didn't find OS X framework, and was not set by user
SET(GDAL_CONFIG_PREFER_PATH "$ENV{GDAL_HOME}/bin" CACHE STRING "preferred path to GDAL (gdal-config)")
SET(GDAL_CONFIG_PREFER_FWTOOLS_PATH "$ENV{FWTOOLS_HOME}/bin_safe" CACHE STRING "preferred path to GDAL (gdal-config) from FWTools")
FIND_PROGRAM(GDAL_CONFIG gdal-config
${GDAL_CONFIG_PREFER_PATH}
${GDAL_CONFIG_PREFER_FWTOOLS_PATH}
/usr/local/bin/
/usr/bin/
)
# MESSAGE("DBG GDAL_CONFIG ${GDAL_CONFIG}")
IF (GDAL_CONFIG)
## extract gdal version
EXEC_PROGRAM(${GDAL_CONFIG}
ARGS --version
OUTPUT_VARIABLE GDAL_VERSION )
STRING(REGEX REPLACE "([0-9]+)\\.([0-9]+)\\.([0-9]+)" "\\1" GDAL_VERSION_MAJOR "${GDAL_VERSION}")
STRING(REGEX REPLACE "([0-9]+)\\.([0-9]+)\\.([0-9]+)" "\\2" GDAL_VERSION_MINOR "${GDAL_VERSION}")
# MESSAGE("DBG GDAL_VERSION ${GDAL_VERSION}")
# MESSAGE("DBG GDAL_VERSION_MAJOR ${GDAL_VERSION_MAJOR}")
# MESSAGE("DBG GDAL_VERSION_MINOR ${GDAL_VERSION_MINOR}")
# check for gdal version
# version 1.2.5 is known NOT to be supported (missing CPL_STDCALL macro)
# According to INSTALL, 2.1+ is required
IF (GDAL_VERSION_MAJOR LESS 2)
MESSAGE (FATAL_ERROR "GDAL version is too old (${GDAL_VERSION}). Use 2.1 or higher.")
ENDIF (GDAL_VERSION_MAJOR LESS 2)
IF ( (GDAL_VERSION_MAJOR EQUAL 2) AND (GDAL_VERSION_MINOR LESS 1) )
MESSAGE (FATAL_ERROR "GDAL version is too old (${GDAL_VERSION}). Use 2.1 or higher.")
ENDIF( (GDAL_VERSION_MAJOR EQUAL 2) AND (GDAL_VERSION_MINOR LESS 1) )
# set INCLUDE_DIR to prefix+include
EXEC_PROGRAM(${GDAL_CONFIG}
ARGS --prefix
OUTPUT_VARIABLE GDAL_PREFIX)
#SET(GDAL_INCLUDE_DIR ${GDAL_PREFIX}/include CACHE STRING INTERNAL)
FIND_PATH(GDAL_INCLUDE_DIR
gdal.h
${GDAL_PREFIX}/include/gdal
${GDAL_PREFIX}/include
/usr/local/include
/usr/include
)
## extract link dirs for rpath
EXEC_PROGRAM(${GDAL_CONFIG}
ARGS --libs
OUTPUT_VARIABLE GDAL_CONFIG_LIBS )
## split off the link dirs (for rpath)
## use regular expression to match wildcard equivalent "-L*<endchar>"
## with <endchar> is a space or a semicolon
STRING(REGEX MATCHALL "[-][L]([^ ;])+"
GDAL_LINK_DIRECTORIES_WITH_PREFIX
"${GDAL_CONFIG_LIBS}" )
# MESSAGE("DBG GDAL_LINK_DIRECTORIES_WITH_PREFIX=${GDAL_LINK_DIRECTORIES_WITH_PREFIX}")
## remove prefix -L because we need the pure directory for LINK_DIRECTORIES
IF (GDAL_LINK_DIRECTORIES_WITH_PREFIX)
STRING(REGEX REPLACE "[-][L]" "" GDAL_LINK_DIRECTORIES ${GDAL_LINK_DIRECTORIES_WITH_PREFIX} )
ENDIF (GDAL_LINK_DIRECTORIES_WITH_PREFIX)
## split off the name
## use regular expression to match wildcard equivalent "-l*<endchar>"
## with <endchar> is a space or a semicolon
STRING(REGEX MATCHALL "[-][l]([^ ;])+"
GDAL_LIB_NAME_WITH_PREFIX
"${GDAL_CONFIG_LIBS}" )
# MESSAGE("DBG GDAL_LIB_NAME_WITH_PREFIX=${GDAL_LIB_NAME_WITH_PREFIX}")
## remove prefix -l because we need the pure name
IF (GDAL_LIB_NAME_WITH_PREFIX)
STRING(REGEX REPLACE "[-][l]" "" GDAL_LIB_NAME ${GDAL_LIB_NAME_WITH_PREFIX} )
ENDIF (GDAL_LIB_NAME_WITH_PREFIX)
IF (APPLE)
IF (NOT GDAL_LIBRARY)
# work around empty GDAL_LIBRARY left by framework check
# while still preserving user setting if given
# ***FIXME*** need to improve framework check so below not needed
SET(GDAL_LIBRARY ${GDAL_LINK_DIRECTORIES}/lib${GDAL_LIB_NAME}.dylib CACHE STRING INTERNAL FORCE)
ENDIF (NOT GDAL_LIBRARY)
ELSE (APPLE)
FIND_LIBRARY(GDAL_LIBRARY NAMES ${GDAL_LIB_NAME} PATHS ${GDAL_LINK_DIRECTORIES}/lib)
ENDIF (APPLE)
ELSE(GDAL_CONFIG)
MESSAGE("FindGDAL.cmake: gdal-config not found. Please set it manually. GDAL_CONFIG=${GDAL_CONFIG}")
ENDIF(GDAL_CONFIG)
ENDIF (NOT GDAL_INCLUDE_DIR OR NOT GDAL_LIBRARY OR NOT GDAL_CONFIG)
ENDIF(UNIX)
ENDIF(WIN32)
IF (GDAL_INCLUDE_DIR AND GDAL_LIBRARY)
SET(GDAL_FOUND TRUE)
ENDIF (GDAL_INCLUDE_DIR AND GDAL_LIBRARY)
IF (GDAL_FOUND)
IF (NOT GDAL_FIND_QUIETLY)
FILE(READ ${GDAL_INCLUDE_DIR}/gdal_version.h gdal_version)
STRING(REGEX REPLACE "^.*GDAL_RELEASE_NAME +\"([^\"]+)\".*$" "\\1" GDAL_RELEASE_NAME "${gdal_version}")
MESSAGE(STATUS "Found GDAL: ${GDAL_LIBRARY} (${GDAL_RELEASE_NAME})")
ENDIF (NOT GDAL_FIND_QUIETLY)
ELSE (GDAL_FOUND)
MESSAGE(GDAL_INCLUDE_DIR=${GDAL_INCLUDE_DIR})
MESSAGE(GDAL_LIBRARY=${GDAL_LIBRARY})
MESSAGE(FATAL_ERROR "Could not find GDAL")
ENDIF (GDAL_FOUND)