Skip to content

Commit 07c671f

Browse files
committed
Try to guess boost library names (fix #12)
1 parent da1d9dd commit 07c671f

File tree

1 file changed

+93
-8
lines changed

1 file changed

+93
-8
lines changed

setup.py

Lines changed: 93 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
#! /usr/bin/env python
22

33
import os
4+
import os.path
5+
import platform
46
import re
57
import shutil
68
import subprocess
79
import sys
810
from distutils import sysconfig
911

10-
from setuptools import Extension, setup
12+
from setuptools import Command, Extension, setup
1113

1214
PYTHON3 = sys.version_info[0] == 3
1315

@@ -21,6 +23,92 @@ def check_output(args):
2123
return output.rstrip('\n')
2224

2325

26+
def clean_boost_name(name):
27+
name = name.split('.')[0]
28+
if name.startswith('lib'):
29+
name = name[3:]
30+
return name
31+
32+
33+
def find_boost_library(_dir, _id):
34+
if not os.path.exists(_dir):
35+
return
36+
for name in os.listdir(_dir):
37+
if _id in name:
38+
# Special case for boost_python, as it could contain python version
39+
# number.
40+
if "python" in _id:
41+
if PYTHON3:
42+
if "3" not in name:
43+
continue
44+
else:
45+
if "3" in name:
46+
continue
47+
return clean_boost_name(name)
48+
49+
50+
def get_boost_library_names():
51+
# A few examples:
52+
# - Ubuntu 15.04 Multiarch or Debian sid:
53+
# /usr/lib/x86_64-linux-gnu/libboost_python.a -> libboost_python-py27.a
54+
# /usr/lib/x86_64-linux-gnu/libboost_python-py27.a
55+
# /usr/lib/x86_64-linux-gnu/libboost_python-py34.a
56+
# /usr/lib/x86_64-linux-gnu/libboost_system.a
57+
# /usr/lib/x86_64-linux-gnu/libboost_thread.a
58+
# - Fedora 64 bits:
59+
# /usr/lib64/libboost_python.so
60+
# /usr/lib64/libboost_python3.so
61+
# /usr/lib64/libboost_system.so
62+
# /usr/lib64/libboost_thread.so
63+
# - OSX with homebrew
64+
# /usr/local/lib/libboost_thread-mt.a -> ../Cellar/boost/1.57.0/lib/libboost_thread-mt.a # noqa
65+
# - Debian Wheezy
66+
# /usr/lib/libboost_python-py27.so
67+
# /usr/lib/libboost_python-mt-py27.so
68+
names = {
69+
"boost_python": os.environ.get("BOOST_PYTHON_LIB"),
70+
"boost_system": os.environ.get("BOOST_SYSTEM_LIB"),
71+
"boost_thread": os.environ.get("BOOST_THREAD_LIB")
72+
}
73+
if all(names.values()):
74+
return names.values()
75+
if os.name == 'posix': # Unix system (Linux, MacOS)
76+
libdirs = ['/lib', '/lib64', '/usr/lib', '/usr/lib64']
77+
multiarch = sysconfig.get_config_var("MULTIARCH")
78+
if multiarch:
79+
libdirs.extend(['/lib/%s' % multiarch, '/usr/lib/%s' % multiarch])
80+
if platform.system() == "Darwin":
81+
libdirs.extend(['/opt/local/lib/'])
82+
if os.environ.get('BOOST_ROOT'):
83+
libdirs.append(os.environ.get('BOOST_ROOT'))
84+
for _dir in libdirs:
85+
for key, value in names.items():
86+
if not value:
87+
value = find_boost_library(_dir, key)
88+
if value:
89+
names[key] = value
90+
if all(names.values()):
91+
break
92+
for key, value in names.items():
93+
if not value:
94+
names[key] = key # Set default.
95+
return names.values()
96+
97+
98+
class WhichBoostCommand(Command):
99+
description = 'Output found boost names. Useful for debug.'
100+
user_options = []
101+
102+
def initialize_options(self):
103+
pass
104+
105+
def finalize_options(self):
106+
pass
107+
108+
def run(self):
109+
print("\n".join(list(get_boost_library_names())))
110+
111+
24112
cflags = sysconfig.get_config_var('CFLAGS')
25113
sysconfig._config_vars['CFLAGS'] = re.sub(
26114
' +', ' ', cflags.replace('-g', '').replace('-Os', '').replace('-arch i386', ''))
@@ -48,9 +136,6 @@ def check_output(args):
48136
mapnik_config = 'mapnik-config'
49137
mason_build = False
50138

51-
boost_python_lib = os.environ.get("BOOST_PYTHON_LIB", 'boost_python-mt')
52-
boost_system_lib = os.environ.get("BOOST_SYSTEM_LIB", 'boost_system-mt')
53-
boost_thread_lib = os.environ.get("BOOST_THREAD_LIB", 'boost_thread-mt')
54139

55140
try:
56141
linkflags = check_output([mapnik_config, '--libs']).split(' ')
@@ -204,6 +289,9 @@ def check_output(args):
204289
'mapnik': ['libmapnik.*', 'plugins/*/*'],
205290
},
206291
test_suite='nose.collector',
292+
cmdclass={
293+
'whichboost': WhichBoostCommand,
294+
},
207295
ext_modules=[
208296
Extension('mapnik._mapnik', [
209297
'src/mapnik_color.cpp',
@@ -247,10 +335,7 @@ def check_output(args):
247335
'mapnik',
248336
'mapnik-wkt',
249337
'mapnik-json',
250-
boost_python_lib,
251-
boost_thread_lib,
252-
boost_system_lib
253-
],
338+
] + list(get_boost_library_names()),
254339
extra_compile_args=extra_comp_args,
255340
extra_link_args=linkflags,
256341
)

0 commit comments

Comments
 (0)