Skip to content

Commit 85ae4fe

Browse files
committed
Simpler pattern for finding boost library names
1 parent 07c671f commit 85ae4fe

File tree

1 file changed

+36
-64
lines changed

1 file changed

+36
-64
lines changed

setup.py

Lines changed: 36 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22

33
import os
44
import os.path
5-
import platform
65
import re
76
import shutil
87
import subprocess
98
import sys
109
from distutils import sysconfig
10+
from ctypes.util import find_library
1111

1212
from setuptools import Command, Extension, setup
1313

14-
PYTHON3 = sys.version_info[0] == 3
14+
PYTHON3 = sys.version_info.major == 3
1515

1616

1717
# Utils
@@ -30,69 +30,41 @@ def clean_boost_name(name):
3030
return name
3131

3232

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)
33+
def find_boost_library(_id):
34+
suffixes = [
35+
"", # standard naming
36+
"-mt" # former naming schema for multithreading build
37+
]
38+
if "python" in _id:
39+
# Debian naming convention for versions installed in parallel
40+
suffixes.insert(0, "-py%d%d" % (sys.version_info.major,
41+
sys.version_info.minor))
42+
# standard suffix for Python3
43+
suffixes.insert(1, sys.version_info.major)
44+
for suf in suffixes:
45+
name = "%s%s" % (_id, suf)
46+
lib = find_library(name)
47+
if lib is not None:
48+
return name
4849

4950

5051
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()
52+
wanted = ['boost_python', 'boost_system', 'boost_thread']
53+
found = []
54+
missing = []
55+
for _id in wanted:
56+
name = os.environ.get("%s_LIB" % _id.upper(), find_boost_library(_id))
57+
if name:
58+
found.append(name)
59+
else:
60+
missing.append(_id)
61+
if missing:
62+
msg = ""
63+
for name in missing:
64+
msg += ("\nMissing {} boost library, try to add its name with "
65+
"{}_LIB environment var.").format(name, name.upper())
66+
raise EnvironmentError(msg)
67+
return found
9668

9769

9870
class WhichBoostCommand(Command):
@@ -106,7 +78,7 @@ def finalize_options(self):
10678
pass
10779

10880
def run(self):
109-
print("\n".join(list(get_boost_library_names())))
81+
print("\n".join(get_boost_library_names()))
11082

11183

11284
cflags = sysconfig.get_config_var('CFLAGS')
@@ -335,7 +307,7 @@ def run(self):
335307
'mapnik',
336308
'mapnik-wkt',
337309
'mapnik-json',
338-
] + list(get_boost_library_names()),
310+
] + get_boost_library_names(),
339311
extra_compile_args=extra_comp_args,
340312
extra_link_args=linkflags,
341313
)

0 commit comments

Comments
 (0)