dependencies: Use a wrapper for calling pkg-config

Reduces duplicated code, and also use universal_newlines=True which
avoids having to decode the bytes output to string.

We mostly need this so we can pass the *current* process environment to
pkg-config which might've changed since the module was imported. Without
this, subprocess.Popen invokes pkg-config with a stale environment (used
in the unittest added later)
pull/899/head
Nirbheek Chauhan 8 years ago
parent 814f4909f9
commit 5e384b8396
  1. 41
      mesonbuild/dependencies.py

@ -115,16 +115,12 @@ class PkgConfigDependency(Dependency):
mlog.debug('Determining dependency %s with pkg-config executable %s.' % (name, pkgbin)) mlog.debug('Determining dependency %s with pkg-config executable %s.' % (name, pkgbin))
self.pkgbin = pkgbin self.pkgbin = pkgbin
p = subprocess.Popen([pkgbin, '--modversion', name], ret, self.modversion = self._call_pkgbin(['--modversion', name])
stdout=subprocess.PIPE, if ret != 0:
stderr=subprocess.PIPE)
out = p.communicate()[0]
if p.returncode != 0:
if self.required: if self.required:
raise DependencyException('%s dependency %s not found.' % (self.type_string, name)) raise DependencyException('%s dependency %s not found.' % (self.type_string, name))
self.modversion = 'none' self.modversion = 'none'
return return
self.modversion = out.decode().strip()
found_msg = ['%s dependency' % self.type_string, mlog.bold(name), 'found:'] found_msg = ['%s dependency' % self.type_string, mlog.bold(name), 'found:']
self.version_requirement = kwargs.get('version', None) self.version_requirement = kwargs.get('version', None)
if self.version_requirement is None: if self.version_requirement is None:
@ -149,27 +145,30 @@ class PkgConfigDependency(Dependency):
# Fetch the libraries and library paths needed for using this # Fetch the libraries and library paths needed for using this
self._set_libs() self._set_libs()
def _set_cargs(self): def _call_pkgbin(self, args):
p = subprocess.Popen([self.pkgbin, '--cflags', self.name], p = subprocess.Popen([self.pkgbin] + args,
stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout=subprocess.PIPE, stderr=subprocess.PIPE,
env=os.environ, universal_newlines=True)
out = p.communicate()[0] out = p.communicate()[0]
if p.returncode != 0: return (p.returncode, out.strip())
def _set_cargs(self):
ret, out = self._call_pkgbin(['--cflags', self.name])
if ret != 0:
raise DependencyException('Could not generate cargs for %s:\n\n%s' % \ raise DependencyException('Could not generate cargs for %s:\n\n%s' % \
(self.name, out.decode(errors='ignore'))) (self.name, out.decode(errors='ignore')))
self.cargs = out.decode().split() self.cargs = out.split()
def _set_libs(self): def _set_libs(self):
libcmd = [self.pkgbin, '--libs'] libcmd = [self.name, '--libs']
if self.static: if self.static:
libcmd.append('--static') libcmd.append('--static')
p = subprocess.Popen(libcmd + [self.name], ret, out = self._call_pkgbin(libcmd)
stdout=subprocess.PIPE, stderr=subprocess.PIPE) if ret != 0:
out = p.communicate()[0]
if p.returncode != 0:
raise DependencyException('Could not generate libs for %s:\n\n%s' % \ raise DependencyException('Could not generate libs for %s:\n\n%s' % \
(self.name, out.decode(errors='ignore'))) (self.name, out.decode(errors='ignore')))
self.libs = [] self.libs = []
for lib in out.decode().split(): for lib in out.split():
if lib.endswith(".la"): if lib.endswith(".la"):
shared_libname = self.extract_libtool_shlib(lib) shared_libname = self.extract_libtool_shlib(lib)
shared_lib = os.path.join(os.path.dirname(lib), shared_libname) shared_lib = os.path.join(os.path.dirname(lib), shared_libname)
@ -185,16 +184,14 @@ class PkgConfigDependency(Dependency):
self.libs.append(lib) self.libs.append(lib)
def get_variable(self, variable_name): def get_variable(self, variable_name):
p = subprocess.Popen([self.pkgbin, '--variable=%s' % variable_name, self.name], ret, out = self._call_pkgbin(['--variable=' + variable_name, self.name])
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out = p.communicate()[0]
variable = '' variable = ''
if p.returncode != 0: if ret != 0:
if self.required: if self.required:
raise DependencyException('%s dependency %s not found.' % raise DependencyException('%s dependency %s not found.' %
(self.type_string, self.name)) (self.type_string, self.name))
else: else:
variable = out.decode().strip() variable = out.strip()
mlog.debug('return of subprocess : %s' % variable) mlog.debug('return of subprocess : %s' % variable)
return variable return variable

Loading…
Cancel
Save