Skip to content

Commit

Permalink
pkgconfig: Fix linking to a custom target
Browse files Browse the repository at this point in the history
When generating pkgconfig file for a library that links to an
uninstalled static library built by custom_target() Meson was crashing
when trying to access some attributes that does not exist on that class.

Also fix is_internal() implementation, it only really make sense on a
CustomTargetIndex or if CustomTarget has only a single output.
  • Loading branch information
xclaesse committed Nov 17, 2020
1 parent 8992729 commit f6aa1e4
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
19 changes: 11 additions & 8 deletions mesonbuild/build.py
Expand Up @@ -2283,13 +2283,12 @@ def get_all_link_deps(self):
return []

def is_internal(self) -> bool:
if not self.should_install():
return True
for out in self.get_outputs():
# Can't check if this is a static library, so try to guess
if not out.endswith(('.a', '.lib')):
return False
return True
'''
Returns True iif this is a not installed static library.
'''
if len(self.outputs) != 1:
return False
return CustomTargetIndex(self, self.outputs[0]).is_internal()

def extract_all_objects_recurse(self):
return self.get_outputs()
Expand Down Expand Up @@ -2443,7 +2442,11 @@ def should_install(self) -> bool:
return self.target.should_install()

def is_internal(self) -> bool:
return self.target.is_internal()
'''
Returns True iif this is a not installed static library
'''
suf = os.path.splitext(self.output)[-1]
return (suf == '.a' or suf == '.lib') and not self.should_install()

def extract_all_objects_recurse(self):
return self.target.extract_all_objects_recurse()
Expand Down
3 changes: 2 additions & 1 deletion mesonbuild/modules/pkgconfig.py
Expand Up @@ -186,7 +186,8 @@ def _add_link_whole(self, t, public):
# lists in case a library is link_with and link_whole at the same time.
# See remove_dups() below.
self.link_whole_targets.append(t)
self._add_lib_dependencies(t.link_targets, t.link_whole_targets, t.external_deps, public)
if isinstance(t, build.BuildTarget):
self._add_lib_dependencies(t.link_targets, t.link_whole_targets, t.external_deps, public)

def add_version_reqs(self, name, version_reqs):
if version_reqs:
Expand Down
16 changes: 16 additions & 0 deletions test cases/common/45 pkgconfig-gen/meson.build
Expand Up @@ -18,6 +18,8 @@ if v.version_compare('<0.29')
error('MESON_SKIP_TEST: pkg-config version \'' + v + '\' too old')
endif

python = import('python').find_installation()
fs = import('fs')
pkgg = import('pkgconfig')

lib = shared_library('simple', 'simple.c')
Expand Down Expand Up @@ -96,3 +98,17 @@ stat2 = static_library('stat2', 'simple.c', install: true)
simple4 = library('simple4', 'simple.c', link_with: stat2)
simple5 = library('simple5', 'simple5.c', link_with: simple4, link_whole: stat2)
pkgg.generate(simple5)

# Regression test: A library linking to an uninstalled custom_target static
# library used to crash when generating its pkgconfig file.
# Copy libstat2.a to libstat3.a to have a static library as custom target.
infile = stat2.full_path()
outfile = meson.current_build_dir() / 'libstat3.a'
script = 'import shutil ; shutil.copyfile("@0@", "@1@")'.format(infile, outfile)
ct = custom_target('stat3',
input: stat2,
output: fs.name(outfile),
command: [python, '-c', script],
)
simple6 = library('simple6', link_with: ct)
pkgg.generate(simple6)

0 comments on commit f6aa1e4

Please sign in to comment.