find_library: Validate and sort globbed shared library files

We need to pick the library with the highest version, which is what
the OpenBSD linker also does.

https://github.com/mesonbuild/meson/issues/3570#issuecomment-403638752
pull/3871/head
Nirbheek Chauhan 7 years ago committed by Nirbheek Chauhan
parent 09ad29ec56
commit 47c68a5993
  1. 21
      mesonbuild/compilers/c.py
  2. 24
      run_unittests.py

@ -866,12 +866,29 @@ class CCompiler(Compiler):
return patterns
@staticmethod
def _get_trials_from_pattern(pattern, directory, libname):
def _sort_shlibs_openbsd(libs):
filtered = []
for lib in libs:
# Validate file as a shared library of type libfoo.so.X.Y
ret = lib.rsplit('.so.', maxsplit=1)
if len(ret) != 2:
continue
try:
float(ret[1])
except ValueError:
continue
filtered.append(lib)
float_cmp = lambda x: float(x.rsplit('.so.', maxsplit=1)[1])
return sorted(filtered, key=float_cmp, reverse=True)
@classmethod
def _get_trials_from_pattern(cls, pattern, directory, libname):
f = os.path.join(directory, pattern.format(libname))
# Globbing for OpenBSD
if '*' in pattern:
# NOTE: globbing matches directories and broken symlinks
# so we have to do an isfile test on it later
return glob.glob(f)
return cls._sort_shlibs_openbsd(glob.glob(f))
return [f]
@staticmethod

@ -522,12 +522,28 @@ class InternalTests(unittest.TestCase):
self.assertEqual(p, shr)
p = cc.get_library_naming(env, 'static')
self.assertEqual(p, stc)
p = cc.get_library_naming(env, 'default')
self.assertEqual(p, shr + stc)
p = cc.get_library_naming(env, 'shared-static')
self.assertEqual(p, shr + stc)
p = cc.get_library_naming(env, 'static-shared')
self.assertEqual(p, stc + shr)
p = cc.get_library_naming(env, 'shared-static')
self.assertEqual(p, shr + stc)
p = cc.get_library_naming(env, 'default')
self.assertEqual(p, shr + stc)
# Test find library by mocking up openbsd
if platform != 'openbsd':
return
with tempfile.TemporaryDirectory() as tmpdir:
with open(os.path.join(tmpdir, 'libfoo.so.6.0'), 'w') as f:
f.write('')
with open(os.path.join(tmpdir, 'libfoo.so.5.0'), 'w') as f:
f.write('')
with open(os.path.join(tmpdir, 'libfoo.so.54.0'), 'w') as f:
f.write('')
with open(os.path.join(tmpdir, 'libfoo.so.66a.0b'), 'w') as f:
f.write('')
with open(os.path.join(tmpdir, 'libfoo.so.70.0.so.1'), 'w') as f:
f.write('')
found = cc.find_library_real('foo', env, [tmpdir], '', 'default')
self.assertEqual(os.path.basename(found[0]), 'libfoo.so.54.0')
def test_find_library_patterns(self):
'''

Loading…
Cancel
Save