forked from gipit/gippy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.py
executable file
·278 lines (240 loc) · 9.52 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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#!/usr/bin/env python
################################################################################
# GIPPY: Geospatial Image Processing library for Python
#
# AUTHOR: Matthew Hanson
# EMAIL: [email protected]
#
# Copyright (C) 2015 Applied Geosolutions
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
################################################################################
"""
setup for GIP and gippy
"""
import os
import sys
import glob
import re
import subprocess
import logging
import shutil
from numpy import get_include as numpy_get_include
from imp import load_source
# setup imports
from setuptools import setup, Extension
from setuptools.command.build_ext import build_ext
from setuptools.command.install import install
from setuptools.command.develop import develop
#from wheel.bdist_wheel import bdist_wheel
from distutils import sysconfig
__version__ = load_source('gippy.version', 'gippy/version.py').__version__
# get the dependencies and installs
with open('requirements.txt') as fid:
install_requires = [l.strip() for l in fid.readlines() if l]
with open('requirements-dev.txt') as fid:
test_requires = [l.strip() for l in fid.readlines() if l]
# logging
logging.basicConfig(level=logging.DEBUG)
# logging.basicConfig()
log = logging.getLogger(os.path.basename(__file__))
class CConfig(object):
"""Interface to config options from any utility"""
def __init__(self, cmd):
self.cmd = cmd
self.get_include()
self.get_libs()
def get(self, option):
try:
stdout, stderr = subprocess.Popen(
[self.cmd, option],
stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
except OSError:
# e.g., [Errno 2] No such file or directory
raise OSError("Could not find script")
if stderr and not stdout:
raise ValueError(stderr.strip())
if sys.version_info[0] >= 3:
result = stdout.decode('ascii').strip()
else:
result = stdout.strip()
# log.info('%s %s: %r', self.cmd, option, result)
return result
def get_include(self):
self.include = []
for item in self.get('--cflags').split():
if item.startswith("-I"):
self.include.extend(item[2:].split(":"))
return self.include
def get_libs(self):
self.libs = []
self.lib_dirs = []
self.extra_link_args = []
for item in self.get('--libs').split():
if item.startswith("-L"):
self.lib_dirs.extend(item[2:].split(":"))
elif item.startswith("-l"):
self.libs.append(item[2:])
else:
# e.g. -framework GEOS
self.extra_link_args.append(item)
def version(self):
match = re.match(r'(\d+)\.(\d+)\.(\d+)', self.get('--version').strip())
return tuple(map(int, match.groups()))
class _build_ext(build_ext):
# builds the external modules: libgip.so, _gippy.so, _algorithms.so
def run(self):
log.debug('_build_ext run')
for m in swig_modules:
# know where to find libgip for linking
m.library_dirs.append(os.path.join(self.build_lib, 'gippy'))
# on linux add to rpath
if sys.platform != 'darwin':
m.runtime_library_dirs.append('$ORIGIN')
# in python3 the created .so files have names tagged to version, see PEP 3147, 3149
if sysconfig.get_config_var('SOABI') is not None:
# create link
libpath = os.path.join(self.build_lib, 'gippy')
if not os.path.exists(libpath):
os.makedirs(libpath)
link = os.path.join(self.build_lib, gip_module._full_name + '.so')
os.symlink(os.path.basename(gip_module._file_name), link)
# build the extensionss
build_ext.run(self)
# for mac update runtime library location. Use otool -L to see shared libs in a .so
if sys.platform == 'darwin':
old_name = os.path.join(self.build_lib, gip_module._file_name)
new_name = '@loader_path/libgip.so' # %s' % os.path.basename(gip_module._file_name)
cmd0 = 'install_name_tool -change %s %s' % (old_name, new_name)
for m in swig_modules:
cmd = '%s %s' % (cmd0, os.path.join(os.path.abspath(self.build_lib), m._file_name))
log.debug(cmd)
out = subprocess.check_output(cmd.split(' '))
# also update top level .so files 'develop' likes to create (still not sure why)
if os.path.exists(os.path.basename(m._file_name)):
cmd = '%s %s' % (cmd0, os.path.basename(m._file_name))
log.debug(cmd)
out = subprocess.check_output(cmd.split(' '))
log.debug(out)
class _develop(develop):
def run(self):
log.debug('_develop run')
develop.run(self)
# move lib files into gippy directory
[shutil.move(f, 'gippy/') for f in glob.glob('*.so')]
if sysconfig.get_config_var('SOABI') is not None:
# rename libgip if Python 3.2+
os.rename(gip_module._file_name, 'gippy/libgip.so')
class _install(install):
def run(self):
log.debug('_install run')
# ensure extension built before packaging
self.run_command('build_ext')
install.run(self)
#class _bdist_wheel(bdist_wheel):
# binary wheel
# def run(self):
# log.debug('_bdist_wheel run')
# self.distribution.ext_modules = [gip_module] + swig_modules
# self.run_command('build_ext')
# bdist_wheel.run(self)
# GDAL config parameters
gdal_config = CConfig(os.environ.get('GDAL_CONFIG', 'gdal-config'))
extra_compile_args = ['-fPIC', '-O3', '-std=c++11']
if gdal_config.version()[0] == 2:
extra_compile_args.append('-D GDAL2')
extra_link_args = gdal_config.extra_link_args
# not sure if current directory is necessary here
lib_dirs = gdal_config.lib_dirs + ['./']
if sys.platform == 'darwin':
extra_compile_args.append('-stdlib=libc++')
extra_link_args.append('-stdlib=libc++')
ldshared = sysconfig.get_config_var('LDSHARED')
sysconfig._config_vars['LDSHARED'] = re.sub(
' +', ' ',
ldshared.replace('-bundle', '-dynamiclib')
)
extra_compile_args.append('-mmacosx-version-min=10.8')
extra_link_args.append('-mmacosx-version-min=10.8')
# silence various warnings
extra_compile_args.append('-Wno-absolute-value')
extra_compile_args.append('-Wno-shift-negative-value')
extra_compile_args.append('-Wno-parentheses-equality')
extra_compile_args.append('-Wno-deprecated-declarations')
else:
# Remove the "-Wstrict-prototypes" compiler option that swig adds, which isn't valid for C++.
cfg_vars = sysconfig.get_config_vars()
for key, value in cfg_vars.items():
if type(value) == str:
cfg_vars[key] = value.replace("-Wstrict-prototypes", "")
extra_compile_args.append('-Wno-maybe-uninitialized')
# the libgip.so module containing all the C++ code
gip_module = Extension(
name=os.path.join("gippy", "libgip"),
sources=glob.glob('GIP/*.cpp'),
include_dirs=['GIP', numpy_get_include()] + gdal_config.include,
library_dirs=lib_dirs,
libraries=[
'pthread'
] + gdal_config.libs,
extra_compile_args=extra_compile_args,
extra_link_args=extra_link_args
)
# the swig .so modules containing the C++ code that wraps libgip.so
swig_modules = []
for n in ['gippy', 'algorithms']:
src = os.path.join('gippy', n + '.i')
cppsrc = os.path.join('gippy', n + '_wrap.cpp')
src = cppsrc if os.path.exists(cppsrc) else src
swig_modules.append(
Extension(
name=os.path.join('gippy', '_' + n),
sources=[src],
swig_opts=['-c++', '-w509', '-w511', '-w315', '-IGIP', '-fcompact', '-fvirtual', '-keyword'],
include_dirs=['GIP', numpy_get_include()] + gdal_config.include,
library_dirs=lib_dirs,
libraries=[
'gip', 'pthread'
] + gdal_config.libs,
extra_compile_args=extra_compile_args,
extra_link_args=extra_link_args
)
)
setup(
name='gippy',
version=__version__,
description='Geospatial Image Processing for Python',
author='Matthew Hanson',
author_email='[email protected]',
license='Apache v2.0',
# platform_tag='linux_x86_64',
classifiers=[
'License :: OSI Approved :: Apache Software License',
'Programming Language :: C++',
'Programming Language :: Python',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5'
],
ext_modules=[gip_module] + swig_modules,
packages=['gippy'],
install_requires=install_requires,
test_suite='nose.collector',
tests_require=test_requires,
cmdclass={
"build_ext": _build_ext,
"develop": _develop,
"install": _install,
#"bdist_wheel": _bdist_wheel,
}
)