Commit 50278144 authored by André Anjos's avatar André Anjos 💬
Browse files

Auto-add sys.executable prefix to search paths all over the place

parent 40df88ad
......@@ -6,8 +6,8 @@
"""A custom build class for Pkg-config based extensions
"""
import sys
import os
import sys
import platform
import pkg_resources
from setuptools.extension import Extension as DistutilsExtension
......@@ -17,7 +17,7 @@ from pkg_resources import resource_filename
from .pkgconfig import pkgconfig
from .boost import boost
from .utils import uniq, find_executable, find_library
from .utils import uniq, uniq_paths, find_executable, find_library
from .cmake import CMakeListsGenerator
__version__ = pkg_resources.require(__name__)[0].version
......@@ -118,7 +118,7 @@ def reorganize_isystem(args):
else:
remainder.append(args[i])
includes = uniq(includes[::-1])[::-1]
includes = uniq_paths(includes[::-1])[::-1]
# sort includes so that the shortest system includes go last
# this algorithm will ensure '/usr/include' comes after other
......@@ -406,7 +406,7 @@ class Extension(DistutilsExtension):
kwargs['include_dirs'] = user_includes + bob_includes + kwargs['include_dirs']
# Uniq'fy parameters that are not on our parameter list
kwargs['include_dirs'] = uniq(kwargs['include_dirs'])
kwargs['include_dirs'] = uniq_paths(kwargs['include_dirs'])
# Stream-line '-isystem' includes
kwargs['extra_compile_args'] = reorganize_isystem(kwargs['extra_compile_args'])
......@@ -418,11 +418,14 @@ class Extension(DistutilsExtension):
if platform.system() == 'Linux':
kwargs.setdefault('runtime_library_dirs', [])
kwargs['runtime_library_dirs'] += kwargs['library_dirs']
kwargs['runtime_library_dirs'] = uniq(kwargs['runtime_library_dirs'])
kwargs['runtime_library_dirs'] = uniq_paths(kwargs['runtime_library_dirs'])
# .. except for the bob libraries
kwargs['library_dirs'] += bob_library_dirs
# Uniq'fy library directories
kwargs['library_dirs'] = uniq_paths(kwargs['library_dirs'])
# Run the constructor for the base class
DistutilsExtension.__init__(self, name, sources, **kwargs)
......@@ -535,10 +538,10 @@ class Library (Extension):
sources = self.c_sources,
target_directory = self.c_target_directory,
version = self.c_version,
include_directories = uniq(self.c_include_directories),
system_include_directories = uniq(self.c_system_include_directories),
include_directories = uniq_paths(self.c_include_directories),
system_include_directories = uniq_paths(self.c_system_include_directories),
libraries = uniq(self.c_libraries),
library_directories = uniq(self.c_library_directories),
library_directories = uniq_paths(self.c_library_directories),
macros = uniq(self.c_define_macros)
)
generator.generate(self.c_package_directory)
......
......@@ -4,26 +4,10 @@
# Wed 16 Oct 10:08:42 2013 CEST
import os
import sys
import subprocess
import logging
def uniq(seq, idfun=None):
"""Very fast, order preserving uniq function"""
# order preserving
if idfun is None:
def idfun(x): return x
seen = {}
result = []
for item in seq:
marker = idfun(item)
# in old Python versions:
# if seen.has_key(marker)
# but in new ones:
if marker in seen: continue
seen[marker] = 1
result.append(item)
return result
from .utils import uniq_paths
def call_pkgconfig(cmd, paths=None):
"""Runs a command as a subprocess and raises if that does not work
......@@ -31,15 +15,32 @@ def call_pkgconfig(cmd, paths=None):
Returns the exit status, stdout and stderr.
"""
# if the user has passed their own paths, add it to the environment
env = os.environ
if paths is not None:
env = os.environ.copy()
var = os.pathsep.join(paths)
old = env.get('PKG_CONFIG_PATH', False)
env['PKG_CONFIG_PATH'] = os.pathsep.join([var, old]) if old else var
# sets the PKG_CONFIG_PATH taking into consideration:
# 1. BOB_PREFIX_PATH, if set
# 2. the paths set by the user
# 3. the current python executable prefix
# 1. BOB_PREFIX_PATH
bob_prefix = os.environ.get('BOB_PREFIX_PATH', False)
if bob_prefix:
bob_prefix = bob_prefix.split(os.pathsep)
pkg_path += [os.path.join(k, 'lib', 'pkgconfig') for k in bob_prefix]
# 2. user path
pkg_path = paths if paths else []
# 3. adds the current python executable prefix
pkg_path.append(os.path.join(os.path.dirname(os.path.dirname(sys.executable)), 'lib', 'pkgconfig'))
# Make unique to avoid searching twice
pkg_path = uniq_paths(pkg_path)
env = os.environ.copy()
var = os.pathsep.join(pkg_path)
old = env.get('PKG_CONFIG_PATH', False)
env['PKG_CONFIG_PATH'] = os.pathsep.join([var, old]) if old else var
# calls the lua creation script using the parameters
# calls the program
cmd = ['pkg-config'] + [str(k) for k in cmd]
subproc = subprocess.Popen(
cmd,
......@@ -105,7 +106,7 @@ class pkgconfig:
The name of the package of interest, as you would pass on the command
line
extra_paths
paths
Search paths to be added to the environment's PKG_CONFIG_PATH to search
for packages.
......
......@@ -12,9 +12,9 @@ import glob
import platform
DEFAULT_PREFIXES = [
"/usr",
"/usr/local",
"/opt/local",
"/usr/local",
"/usr",
]
def find_file(name, subpaths=None, prefixes=None):
......@@ -50,19 +50,22 @@ def find_file(name, subpaths=None, prefixes=None):
search = []
# Priority 1
# Priority 1: the environment
if 'BOB_PREFIX_PATH' in os.environ:
search += os.environ['BOB_PREFIX_PATH'].split(os.pathsep)
# Priority 2
# Priority 2: user passed paths
if prefixes:
search += prefixes
# Priority 3
# Priority 3: the current system executable
search.append(os.path.dirname(os.path.dirname(sys.executable)))
# Priority 4: the default search prefixes
search += DEFAULT_PREFIXES
# Make unique to avoid searching twice
search = uniq(search)
search = uniq_paths(search)
# Exhaustive combination of paths and subpaths
if subpaths:
......@@ -277,13 +280,27 @@ def find_executable(name, subpaths=None, prefixes=None):
# The module names can be set with or without version number
return find_file(name, my_subpaths, prefixes)
def uniq(seq):
"""Uniqu-fy preserving order"""
seen = set()
seen_add = seen.add
return [x for x in seq if x not in seen and not seen_add(x)]
def uniq(seq, idfun=None):
"""Very fast, order preserving uniq function"""
# order preserving
if idfun is None:
def idfun(x): return x
seen = {}
result = []
for item in seq:
marker = idfun(item)
# in old Python versions:
# if seen.has_key(marker)
# but in new ones:
if marker in seen: continue
seen[marker] = 1
result.append(item)
return result
def uniq_paths(seq):
"""Uniq'fy a list of paths taking into consideration their real paths"""
return uniq([os.path.realpath(k) for k in seq if os.path.exists(k)])
def egrep(filename, expression):
"""Runs grep for a given expression on each line of the file
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment