forked from mapnik/python-mapnik
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.py
executable file
·379 lines (331 loc) · 13.1 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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
#!/usr/bin/env python3
import os
import os.path
import re
import shutil
import subprocess
import sys
import glob
from distutils import sysconfig
from ctypes.util import find_library
from setuptools import Command, Extension, setup
NEEDED_BOOST_LIBS = ['python', 'thread', 'system']
# Utils
def check_output(args, shell=False):
output = subprocess.check_output(args, shell=shell)
output = output.decode()
return output.rstrip('\n')
def boost_suffixes():
suffixes = []
# boost-python
# Debian naming convention for versions installed in parallel
suffixes.append("-py%d%d" % (sys.version_info.major,
sys.version_info.minor))
# standard suffix for Python3
suffixes.append(sys.version_info.major)
# Gentoo
suffixes.append("-{}.{}".format(sys.version_info.major, sys.version_info.minor))
# Other
suffixes.extend([
"", # standard naming
"-mt" # former naming schema for multithreading build
])
return suffixes
def find_boost_library(_id):
for suf in boost_suffixes():
name = "%s%s" % (_id, suf)
lib = find_library(name)
if lib is not None:
return name
def get_boost_library_names():
found = []
missing = []
for _id in NEEDED_BOOST_LIBS:
_id = 'boost_' + _id
name = os.environ.get("%s_LIB" % _id.upper(), find_boost_library(_id))
if name:
found.append(name)
else:
missing.append(_id)
if missing:
msg = ""
for name in missing:
msg += ("\nMissing {} boost library, try to add its name with "
"{}_LIB environment var.").format(name, name.upper())
raise EnvironmentError(msg)
return found
def get_boost_library_static_paths():
"""
Paths for static linking
"""
paths = []
boost_path = os.environ.get("BOOST_LIBS")
if not boost_path:
boost_system_path = check_output(['ld -o /dev/null -lboost_system --verbose 2>/dev/null | grep succeeded | grep -o "/[^ ]*"'], shell=True)
if boost_system_path:
boost_path = os.path.dirname(boost_system_path)
if not boost_path:
raise Exception("Failed to find boost libs")
for name in NEEDED_BOOST_LIBS:
for suf in boost_suffixes():
lib_path = os.path.join(boost_path,
'libboost_{}{}.a'.format(name, suf))
if os.path.exists(lib_path):
paths.append(lib_path)
break
return paths
class WhichBoostCommand(Command):
description = 'Output found boost names. Useful for debug.'
user_options = []
def initialize_options(self):
pass
def finalize_options(self):
pass
def run(self):
print("Static Boost libs:")
print("\n".join(get_boost_library_static_paths()))
print("Dynamic Boost libs:")
print("\n".join(get_boost_library_names()))
cflags = sysconfig.get_config_var('CFLAGS')
sysconfig._config_vars['CFLAGS'] = re.sub(
' +', ' ', cflags.replace('-g ', '').replace('-Os', '').replace('-arch i386', ''))
opt = sysconfig.get_config_var('OPT')
sysconfig._config_vars['OPT'] = re.sub(
' +', ' ', opt.replace('-g ', '').replace('-Os', ''))
ldshared = sysconfig.get_config_var('LDSHARED')
sysconfig._config_vars['LDSHARED'] = re.sub(
' +', ' ', ldshared.replace('-g ', '').replace('-Os', '').replace('-arch i386', ''))
ldflags = sysconfig.get_config_var('LDFLAGS')
if ldflags:
sysconfig._config_vars['LDFLAGS'] = re.sub(
' +', ' ', ldflags.replace('-g ', '').replace('-Os', '').replace('-arch i386', ''))
pycflags = sysconfig.get_config_var('PY_CFLAGS')
sysconfig._config_vars['PY_CFLAGS'] = re.sub(
' +', ' ', pycflags.replace('-g ', '').replace('-Os', '').replace('-arch i386', ''))
sysconfig._config_vars['CFLAGSFORSHARED'] = ''
os.environ['ARCHFLAGS'] = ''
if os.environ.get("MASON_BUILD", "false") == "true":
# run bootstrap.sh to get mason builds
subprocess.call(['./bootstrap.sh'])
mapnik_config = 'mason_packages/.link/bin/mapnik-config'
mason_build = True
else:
mapnik_config = 'mapnik-config'
mason_build = False
linkflags = []
lib_path = os.path.join(check_output([mapnik_config, '--prefix']),'lib')
linkflags.extend(check_output([mapnik_config, '--libs']).split(' '))
linkflags.extend(check_output([mapnik_config, '--ldflags']).split(' '))
linkflags.extend([ '-lmapnik-wkt', '-lmapnik-json'])
linkflags.append('-lprotobuf-lite')
# For ConnectionManager
linkflags.append('-lpq')
# Dynamically make the mapnik/paths.py file if it doesn't exist.
if os.path.isfile('mapnik/paths.py'):
create_paths = False
else:
create_paths = True
f_paths = open('mapnik/paths.py', 'w')
f_paths.write('import os\n')
f_paths.write('\n')
input_plugin_path = check_output([mapnik_config, '--input-plugins'])
font_path = check_output([mapnik_config, '--fonts'])
if mason_build:
try:
if sys.platform == 'darwin':
base_f = 'libmapnik.dylib'
else:
base_f = 'libmapnik.so.3.0'
f = os.path.join(lib_path, base_f)
if not os.path.exists(os.path.join('mapnik', 'lib')):
os.makedirs(os.path.join('mapnik', 'lib'))
shutil.copyfile(f, os.path.join('mapnik', 'lib', base_f))
except shutil.Error:
pass
input_plugin_files = os.listdir(input_plugin_path)
input_plugin_files = [os.path.join(
input_plugin_path, f) for f in input_plugin_files]
if not os.path.exists(os.path.join('mapnik', 'plugins', 'input')):
os.makedirs(os.path.join('mapnik', 'plugins', 'input'))
for f in input_plugin_files:
try:
shutil.copyfile(f, os.path.join(
'mapnik', 'plugins', 'input', os.path.basename(f)))
except shutil.Error:
pass
font_files = os.listdir(font_path)
font_files = [os.path.join(font_path, f) for f in font_files]
if not os.path.exists(os.path.join('mapnik', 'plugins', 'fonts')):
os.makedirs(os.path.join('mapnik', 'plugins', 'fonts'))
for f in font_files:
try:
shutil.copyfile(f, os.path.join(
'mapnik', 'plugins', 'fonts', os.path.basename(f)))
except shutil.Error:
pass
if create_paths:
f_paths.write(
'mapniklibpath = os.path.join(os.path.dirname(os.path.realpath(__file__)), "plugins")\n')
f_paths.write("inputpluginspath = os.path.join(mapniklibpath,'input')\n")
f_paths.write("fontscollectionpath = os.path.join(mapniklibpath,'fonts')\n")
elif create_paths:
if os.environ.get('LIB_DIR_NAME'):
mapnik_lib_path = lib_path + os.environ.get('LIB_DIR_NAME')
else:
mapnik_lib_path = lib_path + "/mapnik"
f_paths.write("mapniklibpath = '{path}'\n".format(path=mapnik_lib_path))
f_paths.write('mapniklibpath = os.path.normpath(mapniklibpath)\n')
f_paths.write(
"inputpluginspath = '{path}'\n".format(path=input_plugin_path))
f_paths.write(
"fontscollectionpath = '{path}'\n".format(path=font_path))
if create_paths:
f_paths.write(
"__all__ = [mapniklibpath,inputpluginspath,fontscollectionpath]\n")
f_paths.close()
if mason_build:
share_dir = 'share'
for dep in ['icu','gdal','proj']:
share_path = os.path.join('mapnik', share_dir, dep)
if not os.path.exists(share_path):
os.makedirs(share_path)
icu_path = 'mason_packages/.link/share/icu/*/*.dat'
icu_files = glob.glob(icu_path)
if len(icu_files) != 1:
raise Exception("Failed to find icu dat file at "+ icu_path)
for f in icu_files:
shutil.copyfile(f, os.path.join(
'mapnik', share_dir, 'icu', os.path.basename(f)))
gdal_path = 'mason_packages/.link/share/gdal/'
gdal_files = os.listdir(gdal_path)
gdal_files = [os.path.join(gdal_path, f) for f in gdal_files]
for f in gdal_files:
try:
shutil.copyfile(f, os.path.join(
'mapnik', share_dir, 'gdal', os.path.basename(f)))
except shutil.Error:
pass
proj_path = 'mason_packages/.link/share/proj/'
proj_files = os.listdir(proj_path)
proj_files = [os.path.join(proj_path, f) for f in proj_files]
for f in proj_files:
try:
shutil.copyfile(f, os.path.join(
'mapnik', share_dir, 'proj', os.path.basename(f)))
except shutil.Error:
pass
extra_comp_args = check_output([mapnik_config, '--cflags']).split(' ')
extra_comp_args.append('-I/usr/src/mapbox/mapnik-vector-tile')
if os.environ.get("PYCAIRO", "true") == "true":
try:
extra_comp_args.append('-DHAVE_PYCAIRO')
print("-I%s/include/pycairo".format(sys.exec_prefix))
extra_comp_args.append("-I{0}/include/pycairo".format(sys.exec_prefix))
#extra_comp_args.extend(check_output(["pkg-config", '--cflags', 'pycairo']).strip().split(' '))
#linkflags.extend(check_output(["pkg-config", '--libs', 'pycairo']).strip().split(' '))
except:
raise Exception("Failed to find compiler options for pycairo")
if sys.platform == 'darwin':
extra_comp_args.append('-mmacosx-version-min=10.11')
# silence warning coming from boost python macros which
# would is hard to silence via pragma
extra_comp_args.append('-Wno-parentheses-equality')
linkflags.append('-mmacosx-version-min=10.11')
else:
linkflags.append('-lrt')
linkflags.append('-Wl,-z,origin')
linkflags.append('-Wl,-rpath=$ORIGIN/lib')
static_boost = bool(os.environ.get("STATIC_BOOST", False))
if not static_boost:
linkflags.extend(['-l%s' % i for i in get_boost_library_names()])
if os.environ.get("CC", False) == False:
os.environ["CC"] = check_output([mapnik_config, '--cxx'])
if os.environ.get("CXX", False) == False:
os.environ["CXX"] = check_output([mapnik_config, '--cxx'])
# monkey-patch for parallel compilation
def parallelCCompile(self, sources, output_dir=None, macros=None, include_dirs=None, debug=0, extra_preargs=None, extra_postargs=None, depends=None):
# those lines are copied from distutils.ccompiler.CCompiler directly
macros, objects, extra_postargs, pp_opts, build = self._setup_compile(output_dir, macros, include_dirs, sources, depends, extra_postargs)
cc_args = self._get_cc_args(pp_opts, debug, extra_preargs)
# parallel code
N=int(os.environ.get("JOBS", 2)) # number of parallel compilations
import multiprocessing.pool
def _single_compile(obj):
try: src, ext = build[obj]
except KeyError: return
self._compile(obj, src, ext, cc_args, extra_postargs, pp_opts)
# convert to list, imap is evaluated on-demand
list(multiprocessing.pool.ThreadPool(N).imap(_single_compile,objects))
return objects
if int(os.environ.get("JOBS", 1)) > 1:
import distutils.ccompiler
distutils.ccompiler.CCompiler.compile=parallelCCompile
setup(
name="mapnik",
version="0.1",
packages=['mapnik'],
author="Blake Thompson",
author_email="[email protected]",
description="Python bindings for Mapnik",
license="GNU LESSER GENERAL PUBLIC LICENSE",
keywords="mapnik mapbox mapping cartography",
url="http://mapnik.org/",
tests_require=[
'nose',
],
package_data={
'mapnik': ['lib/*', 'plugins/*/*', 'share/*/*'],
},
test_suite='nose.collector',
cmdclass={
'whichboost': WhichBoostCommand,
},
ext_modules=[
Extension('mapnik._mapnik', [
'src/mapnik_color.cpp',
'src/mapnik_coord.cpp',
'src/mapnik_datasource.cpp',
'src/mapnik_datasource_cache.cpp',
'src/mapnik_envelope.cpp',
'src/mapnik_expression.cpp',
'src/mapnik_feature.cpp',
'src/mapnik_featureset.cpp',
'src/mapnik_font_engine.cpp',
'src/mapnik_fontset.cpp',
'src/mapnik_gamma_method.cpp',
'src/mapnik_geometry.cpp',
'src/mapnik_image.cpp',
'src/mapnik_image_view.cpp',
'src/mapnik_label_collision_detector.cpp',
'src/mapnik_layer.cpp',
'src/mapnik_logger.cpp',
'src/mapnik_map.cpp',
'src/mapnik_palette.cpp',
'src/mapnik_parameters.cpp',
'src/mapnik_proj_transform.cpp',
'src/mapnik_projection.cpp',
'src/mapnik_python.cpp',
'src/mapnik_query.cpp',
'src/mapnik_raster_colorizer.cpp',
'src/mapnik_rule.cpp',
'src/mapnik_scaling_method.cpp',
'src/mapnik_style.cpp',
'src/mapnik_svg_generator_grammar.cpp',
'src/mapnik_symbolizer.cpp',
'src/mapnik_text_placement.cpp',
'src/mapnik_view_transform.cpp',
'src/mapnik_connection_manager.cpp',
'src/mapnik_vector_tile.cpp',
'src/mapnik_vector_tile_render.cpp',
'src/mapnik_vector_tile_preview.cpp',
'src/mapnik_vector_tile_info.cpp',
'/usr/src/mapbox/mapnik-vector-tile/vector_tile.pb.cc',
'src/parallel_encoding.cpp',
],
language='c++',
extra_compile_args=extra_comp_args,
extra_link_args=linkflags,
extra_objects=(get_boost_library_static_paths() if static_boost else [])
)
]
)