/ lib / pkg_config.py
pkg_config.py
 1  from __future__ import print_function
 2  
 3  import shlex
 4  import subprocess
 5  import sys
 6  
 7  from .config import Configuration
 8  
 9  
10  class PkgConfig(object):
11      class Error(Exception):
12          """Raised when information could not be obtained from pkg-config."""
13  
14      def __init__(self, package_name):
15          """Query pkg-config for information about a package.
16  
17          :type package_name: str
18          :param package_name: The name of the package to query.
19          :raises PkgConfig.Error: When a call to pkg-config fails.
20          """
21          self.package_name = package_name
22          self._cflags = self._call("--cflags")
23          self._cflags_only_I = self._call("--cflags-only-I")
24          self._cflags_only_other = self._call("--cflags-only-other")
25          self._libs = self._call("--libs")
26          self._libs_only_l = self._call("--libs-only-l")
27          self._libs_only_L = self._call("--libs-only-L")
28          self._libs_only_other = self._call("--libs-only-other")
29  
30      def _call(self, *pkg_config_args):
31          try:
32              cmd = [Configuration.current.pkg_config] + list(pkg_config_args) + [self.package_name]
33              print("Executing command '{}'".format(cmd), file=sys.stderr)
34              return shlex.split(subprocess.check_output(cmd).decode('utf-8'))
35          except subprocess.CalledProcessError as e:
36              raise self.Error("pkg-config exited with error code {}".format(e.returncode))
37  
38      @property
39      def swiftc_flags(self):
40          """Flags for this package in a format suitable for passing to `swiftc`.
41  
42          :rtype: list[str]
43          """
44          return (
45                  ["-Xcc {}".format(s) for s in self._cflags_only_other]
46                  + ["-Xlinker {}".format(s) for s in self._libs_only_other]
47                  + self._cflags_only_I
48                  + self._libs_only_L
49                  + self._libs_only_l)
50  
51      @property
52      def cflags(self):
53          """CFLAGS for this package.
54  
55          :rtype: list[str]
56          """
57          return self._cflags
58  
59      @property
60      def ldflags(self):
61          """LDFLAGS for this package.
62  
63          :rtype: list[str]
64          """
65          return self._libs