Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2ec27bd

Browse files
committed
Don't parse the flags from pkg-config, just pass everything through.
1 parent 0166abe commit 2ec27bd

1 file changed

Lines changed: 34 additions & 64 deletions

File tree

setupext.py

Lines changed: 34 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import pathlib
1212
import platform
1313
import setuptools
14+
import shlex
1415
import shutil
1516
import subprocess
1617
import sys
@@ -321,20 +322,17 @@ def get_buffer_hash(fd):
321322

322323

323324
class PkgConfig(object):
324-
"""
325-
This is a class for communicating with pkg-config.
326-
"""
325+
"""This is a class for communicating with pkg-config."""
326+
327327
def __init__(self):
328-
"""
329-
Determines whether pkg-config exists on this machine.
330-
"""
331-
if sys.platform == 'win32':
332-
self.has_pkgconfig = False
333-
else:
334-
self.pkg_config = os.environ.get('PKG_CONFIG', 'pkg-config')
335-
self.set_pkgconfig_path()
336-
self.has_pkgconfig = shutil.which(self.pkg_config) is not None
337-
if not self.has_pkgconfig:
328+
"""Determines whether pkg-config exists on this machine."""
329+
self.pkg_config = None
330+
if sys.platform != 'win32':
331+
pkg_config = os.environ.get('PKG_CONFIG', 'pkg-config')
332+
if shutil.which(pkg_config) is not None:
333+
self.pkg_config = pkg_config
334+
self.set_pkgconfig_path()
335+
else:
338336
print("IMPORTANT WARNING:\n"
339337
" pkg-config is not installed.\n"
340338
" matplotlib may not be able to find some of its dependencies")
@@ -353,52 +351,28 @@ def set_pkgconfig_path(self):
353351
except KeyError:
354352
os.environ['PKG_CONFIG_PATH'] = pkgconfig_path
355353

356-
def setup_extension(self, ext, package, default_include_dirs=[],
357-
default_library_dirs=[], default_libraries=[],
358-
alt_exec=None):
359-
"""
360-
Add parameters to the given `ext` for the given `package`.
361-
"""
362-
flag_map = {
363-
'-I': 'include_dirs', '-L': 'library_dirs', '-l': 'libraries'}
364-
365-
executable = alt_exec
366-
if self.has_pkgconfig:
367-
executable = (self.pkg_config + ' {0}').format(package)
368-
369-
use_defaults = True
370-
371-
if executable is not None:
372-
command = "{0} --libs --cflags ".format(executable)
373-
354+
def setup_extension(self, ext, package,
355+
alt_exec=None, default_libraries=()):
356+
"""Add parameters to the given *ext* for the given *package*."""
357+
cmd = ([self.pkg_config, package] if self.pkg_config
358+
else alt_exec)
359+
if cmd is not None:
374360
try:
375-
output = subprocess.check_output(
376-
command, shell=True, stderr=subprocess.STDOUT)
377-
except subprocess.CalledProcessError:
361+
flags = shlex.split(subprocess.check_output(
362+
[*cmd, "--cflags", "--libs"], universal_newlines=True))
363+
except (OSError, subprocess.CalledProcessError):
378364
pass
379365
else:
380-
output = output.decode(sys.getfilesystemencoding())
381-
use_defaults = False
382-
for token in output.split():
383-
attr = flag_map.get(token[:2])
384-
if attr is not None:
385-
getattr(ext, attr).insert(0, token[2:])
386-
387-
if use_defaults:
388-
basedirs = get_base_dirs()
389-
for base in basedirs:
390-
for include in default_include_dirs:
391-
dir = os.path.join(base, include)
392-
if os.path.exists(dir):
393-
ext.include_dirs.append(dir)
394-
for lib in default_library_dirs:
395-
dir = os.path.join(base, lib)
396-
if os.path.exists(dir):
397-
ext.library_dirs.append(dir)
398-
ext.libraries.extend(default_libraries)
399-
return True
400-
401-
return False
366+
# In theory, one could call pkg-config separately with --cflags
367+
# and --libs and use them to fill extra_compile_args and
368+
# extra_link_args respectively, but keeping all the flags
369+
# together works as well and makes it simpler to handle
370+
# libpng-config, which has --ldflags instead of --libs.
371+
ext.extra_compile_args.extend(flags)
372+
ext.extra_link_args.extend(flags)
373+
return
374+
# Else, fall back on the defaults.
375+
ext.libraries.extend(default_libraries)
402376

403377
def get_version(self, package):
404378
"""
@@ -806,12 +780,7 @@ def add_flags(self, ext):
806780
else:
807781
pkg_config.setup_extension(
808782
ext, 'freetype2',
809-
default_include_dirs=[
810-
'include/freetype2', 'freetype2',
811-
'lib/freetype2/include',
812-
'lib/freetype2/include/freetype2'],
813-
default_library_dirs=[
814-
'freetype2/lib'],
783+
alt_exec=['freetype-config', '--cflags', '--libs'],
815784
default_libraries=['freetype', 'z'])
816785
ext.define_macros.append(('FREETYPE_BUILD_TYPE', 'system'))
817786

@@ -954,8 +923,9 @@ def get_extension(self):
954923
]
955924
ext = make_extension('matplotlib._png', sources)
956925
pkg_config.setup_extension(
957-
ext, 'libpng', default_libraries=['png', 'z'],
958-
alt_exec='libpng-config --ldflags')
926+
ext, 'libpng',
927+
alt_exec=['libpng-config', '--cflags', '--ldflags'],
928+
default_libraries=['png', 'z'])
959929
Numpy().add_flags(ext)
960930
return ext
961931

0 commit comments

Comments
 (0)