E56B Don't parse the flags from pkg-config, just pass everything through. · matplotlib/matplotlib@462891e · GitHub
[go: up one dir, main page]

Skip to content

Commit 462891e

Browse files
committed
Don't parse the flags from pkg-config, just pass everything through.
1 parent acde188 commit 462891e

File tree

1 file changed

+34
-64
lines changed

1 file changed

+34
-64
lines changed

setupext.py

Lines changed: 34 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import os
1010
import pathlib
1111
import platform
12+
import shlex
1213
import shutil
1314
import subprocess
1415
import sys
@@ -209,20 +210,17 @@ def get_file_hash(filename):
209210

210211

211212
class PkgConfig(object):
212-
"""
213-
This is a class for communicating with pkg-config.
214-
"""
213+
"""This is a class for communicating with pkg-config."""
214+
215215
def __init__(self):
216-
"""
217-
Determines whether pkg-config exists on this machine.
218-
"""
219-
if sys.platform == 'win32':
220-
self.has_pkgconfig = False
221-
else:
222-
self.pkg_config = os.environ.get('PKG_CONFIG', 'pkg-config')
223-
self.set_pkgconfig_path()
224-
self.has_pkgconfig = shutil.which(self.pkg_config) is not None
225-
if not self.has_pkgconfig:
216+
"""Determines whether pkg-config exists on this machine."""
217+
self.pkg_config = None
218+
if sys.platform != 'win32':
219+
pkg_config = os.environ.get('PKG_CONFIG', 'pkg-config')
220+
if shutil.which(pkg_config) is not None:
221+
self.pkg_config = pkg_config
222+
self.set_pkgconfig_path()
223+
else:
226224
print("IMPORTANT WARNING:\n"
227225
" pkg-config is not installed.\n"
228226
" matplotlib may not be able to find some of its dependencies")
@@ -241,52 +239,28 @@ def set_pkgconfig_path(self):
241239
except KeyError:
242240
os.environ['PKG_CONFIG_PATH'] = pkgconfig_path
243241

244-
def setup_extension(self, ext, package, default_include_dirs=[],
245-
default_library_dirs=[], default_libraries=[],
246-
alt_exec=None):
247-
"""
248-
Add parameters to the given `ext` for the given `package`.
249-
"""
250-
flag_map = {
251-
'-I': 'include_dirs', '-L': 'library_dirs', '-l': 'libraries'}
252-
253-
executable = alt_exec
254-
if self.has_pkgconfig:
255-
executable = (self.pkg_config + ' {0}').format(package)
256-
257-
use_defaults = True
258-
259-
if executable is not None:
260-
command = "{0} --libs --cflags ".format(executable)
261-
242+
def setup_extension(self, ext, package,
243+
alt_exec=None, default_libraries=()):
244+
"""Add parameters to the given *ext* for the given *package*."""
245+
cmd = ([self.pkg_config, package] if self.pkg_config
246+
else alt_exec)
247+
if cmd is not None:
262248
try:
263-
output = subprocess.check_output(
264-
command, shell=True, stderr=subprocess.STDOUT)
265-
except subprocess.CalledProcessError:
249+
flags = shlex.split(subprocess.check_output(
250+
[*cmd, "--cflags", "--libs"], universal_newlines=True))
251+
except (OSError, subprocess.CalledProcessError):
266252
pass
267253
else:
268-
output = output.decode(sys.getfilesystemencoding())
269-
use_defaults = False
270-
for token in output.split():
271-
attr = flag_map.get(token[:2])
272-
if attr is not None:
273-
getattr(ext, attr).insert(0, token[2:])
274-
275-
if use_defaults:
276-
basedirs = get_base_dirs()
277-
for base in basedirs:
278-
for include in default_include_dirs:
279-
dir = os.path.join(base, include)
280-
if os.path.exists(dir):
281-
ext.include_dirs.append(dir)
282-
for lib in default_library_dirs:
283-
dir = os.path.join(base, lib)
284-
if os.path.exists(dir):
285-
802E ext.library_dirs.append(dir)
286-
ext.libraries.extend(default_libraries)
287-
return True
288-
289-
return False
254+
# In theory, one could call pkg-config separately with --cflags
255+
# and --libs and use them to fill extra_compile_args and
256+
# extra_link_args respectively, but keeping all the flags
257+
# together works as well and makes it simpler to handle
258+
# libpng-config, which has --ldflags instead of --libs.
259+
ext.extra_compile_args.extend(flags)
260+
ext.extra_link_args.extend(flags)
261+
return
262+
# Else, fall back on the defaults.
263+
ext.libraries.extend(default_libraries)
290264

291265
def get_version(self, package):
292266
"""
@@ -719,12 +693,7 @@ def add_flags(self, ext):
719693
else:
720694
pkg_config.setup_extension(
721695
ext, 'freetype2',
722-
default_include_dirs=[
723-
'include/freetype2', 'freetype2',
724-
'lib/freetype2/include',
725-
'lib/freetype2/include/freetype2'],
726-
default_library_dirs=[
727-
'freetype2/lib'],
696+
alt_exec=['freetype-config', '--cflags', '--libs'],
728697
default_libraries=['freetype', 'z'])
729698
ext.define_macros.append(('FREETYPE_BUILD_TYPE', 'system'))
730699

@@ -891,8 +860,9 @@ def get_extension(self):
891860
]
892861
ext = make_extension('matplotlib._png', sources)
893862
pkg_config.setup_extension(
894-
ext, 'libpng', default_libraries=['png', 'z'],
895-
alt_exec='libpng-config --ldflags')
863+
ext, 'libpng',
864+
alt_exec=['libpng-config', '--cflags', '--ldflags'],
865+
default_libraries=['png', 'z'])
896866
Numpy().add_flags(ext)
897867
return ext
898868

0 commit comments

Comments
 (0)
0