-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathsetup.py
260 lines (201 loc) · 7.14 KB
/
setup.py
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# -*- coding: utf-8 -*-
import os
import sys
from subprocess import check_output, CalledProcessError
from setuptools import setup, Extension, find_packages
from setuptools.command.build_ext import build_ext
from distutils.errors import CompileError
GDAL_VERSION = open('GDAL_VERSION', 'r').read().strip()
PKG_VERSION = '12'
ENV_GDALHOME = 'GDALHOME'
PY2 = sys.version_info[0] == 2
GVP = tuple(map(int, GDAL_VERSION.split('.')))
MULTIPY = GVP < (3, 3)
PACKAGE_DIR = ('py' + sys.version[0]) if MULTIPY else '.'
python_requires = '>=3.6' if GVP >= (3, 3) else None
class GDALConfigError(Exception):
pass
def set_builtin(name, value):
if isinstance(__builtins__, dict):
__builtins__[name] = value
else:
setattr(__builtins__, name, value)
def get_numpy_include():
# Fix numpy installation using setuptools
set_builtin('__NUMPY_SETUP__', False)
import numpy
return numpy.get_include()
def fetch_config(option, gdal_config='gdal-config'):
try:
return check_output([gdal_config, '--%s' % option]).decode('utf-8').strip()
except CalledProcessError as e:
raise GDALConfigError(e)
def supports_cxx11(compiler, compiler_flag=None):
ret = False
with open('gdal_python_cxx11_test.cpp', 'wt') as f:
f.write("""
#if __cplusplus < 201103L
#error "C++11 required"
#endif
int main () { return 0; }""")
f.close()
extra_postargs = None
if compiler_flag:
extra_postargs = [compiler_flag]
if os.name == 'posix':
# Redirect stderr to /dev/null to hide any error messages
# from the compiler.
devnull = open(os.devnull, 'w')
oldstderr = os.dup(sys.stderr.fileno())
os.dup2(devnull.fileno(), sys.stderr.fileno())
try:
compiler.compile([f.name], extra_postargs=extra_postargs)
ret = True
except CompileError:
pass
os.dup2(oldstderr, sys.stderr.fileno())
devnull.close()
else:
try:
compiler.compile([f.name], extra_postargs=extra_postargs)
ret = True
except CompileError:
pass
os.unlink('gdal_python_cxx11_test.cpp')
if os.path.exists('gdal_python_cxx11_test.o'):
os.unlink('gdal_python_cxx11_test.o')
return ret
class gdal_ext(build_ext):
GDAL_CONFIG = 'gdal-config'
def run(self):
inst_gdal_version = self.get_gdal_config('version')
if inst_gdal_version != GDAL_VERSION:
raise GDALConfigError('Version mismatch %s != %s' % (
inst_gdal_version, GDAL_VERSION))
build_ext.run(self)
def initialize_options(self):
build_ext.initialize_options(self)
self.gdaldir = None
self.gdal_config = self.GDAL_CONFIG
self.extra_cflags = []
def get_gdal_config(self, option):
return fetch_config(option, gdal_config=self.gdal_config)
def build_extensions(self):
# Add a -std=c++11 or similar flag if needed
ct = self.compiler.compiler_type
if ct == 'unix' and not supports_cxx11(self.compiler):
cxx11_flag = None
if supports_cxx11(self.compiler, '-std=c++11'):
cxx11_flag = '-std=c++11'
if cxx11_flag:
for ext in self.extensions:
# gdalconst builds as a .c file
if ext.name != 'osgeo._gdalconst':
ext.extra_compile_args += [cxx11_flag]
build_ext.build_extensions(self)
def finalize_options(self):
if self.libraries is None:
self.libraries = ['gdal', ]
build_ext.finalize_options(self)
if ENV_GDALHOME in os.environ:
print('GDAL prefix from environment variable %s' % ENV_GDALHOME)
self.gdaldir = os.environ[ENV_GDALHOME]
else:
self.gdaldir = self.get_gdal_config('prefix')
self.include_dirs.append(get_numpy_include())
self.library_dirs.append(os.path.join(self.gdaldir, 'lib'))
self.include_dirs.append(os.path.join(self.gdaldir, 'include', 'gdal'))
self.include_dirs.append(os.path.join(self.gdaldir, 'include'))
cflags = self.get_gdal_config('cflags')
if cflags:
self.extra_cflags = cflags.split()
def build_extension(self, ext):
ext.extra_compile_args.extend(self.extra_cflags)
return build_ext.build_extension(self, ext)
extra_link_args = []
extra_compile_args = []
gdal_module = Extension(
'osgeo._gdal',
sources=['extensions/gdal_wrap.cpp'],
extra_compile_args=extra_compile_args,
extra_link_args=extra_link_args)
gdalconst_module = Extension(
'osgeo._gdalconst',
sources=['extensions/gdalconst_wrap.c'],
extra_compile_args=extra_compile_args,
extra_link_args=extra_link_args)
osr_module = Extension(
'osgeo._osr',
sources=['extensions/osr_wrap.cpp'],
extra_compile_args=extra_compile_args,
extra_link_args=extra_link_args)
ogr_module = Extension(
'osgeo._ogr',
sources=['extensions/ogr_wrap.cpp'],
extra_compile_args=extra_compile_args,
extra_link_args=extra_link_args)
gdal_array_module = Extension(
'osgeo._gdal_array',
sources=['extensions/gdal_array_wrap.cpp'],
extra_compile_args=extra_compile_args,
extra_link_args=extra_link_args)
ext_modules = [
gdal_module,
gdalconst_module,
osr_module,
ogr_module,
gdal_array_module,
]
packages = find_packages(where=PACKAGE_DIR)
# raise Exception(packages)
name = 'pygdal'
version = GDAL_VERSION + '.' + PKG_VERSION
author = "Frank Warmerdam"
author_email = "[email protected]"
maintainer = "Aleksandr Dezhin"
maintainer_email = "[email protected]"
description = "Virtualenv and setuptools friendly " \
+ "version of standard GDAL python bindings"
long_description = str(open('README.md', 'r').read())
url = "https://github.com/nextgis/pygdal"
license = "MIT"
classifiers = [
'Development Status :: 5 - Production/Stable',
'Intended Audience :: Developers',
'Intended Audience :: Science/Research',
'License :: OSI Approved :: MIT License',
'Operating System :: OS Independent',
'Programming Language :: Python :: 2',
'Programming Language :: Python :: 3',
'Programming Language :: C',
'Programming Language :: C++',
'Topic :: Scientific/Engineering :: GIS',
'Topic :: Scientific/Engineering :: Information Analysis',
]
# NumPy doesn't support Python 2.x since 1.17
requires = ['numpy>=1.0.0' + (',<1.17' if PY2 else ''), ]
extra = dict()
setup(
name=name,
version=version,
author=author,
author_email=author_email,
maintainer=maintainer,
maintainer_email=maintainer_email,
description=description,
long_description=long_description,
long_description_content_type='text/markdown',
url=url,
license=license,
classifiers=classifiers,
setup_requires=requires,
install_requires=requires,
python_requires=python_requires,
include_package_data=True,
packages=packages,
package_dir={"": PACKAGE_DIR},
ext_modules=ext_modules,
zip_safe=False,
cmdclass=dict(build_ext=gdal_ext),
**extra
)