Skip to content

Commit

Permalink
compilers: use correct version comparison for openbsd libraries
Browse files Browse the repository at this point in the history
It should *be* a version comparison. We are guaranteed to get a
two-element version number, which also parses as a float but a float
doesn't correctly handle version sorting when the second component
differs in number of digits.

The standard way to handle this is by comparing tuples such that each
component is an integer. Do so here.

Fixes #12195

Co-authored-by: George Koehler <[email protected]>
(for unittests)
  • Loading branch information
eli-schwartz committed Sep 26, 2023
1 parent fb6fd5a commit 5b317c5
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
9 changes: 6 additions & 3 deletions mesonbuild/compilers/mixins/clike.py
Original file line number Diff line number Diff line change
Expand Up @@ -1084,19 +1084,22 @@ def get_library_naming(self, env: 'Environment', libtype: LibType, strict: bool

@staticmethod
def _sort_shlibs_openbsd(libs: T.List[str]) -> T.List[str]:
def tuple_key(x: str) -> T.Tuple[int, ...]:
ver = x.rsplit('.so.', maxsplit=1)[1]
return tuple(int(i) for i in ver.split('.'))

filtered: T.List[str] = []
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])
tuple(int(i) for i in ret[1].split('.'))
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)
return sorted(filtered, key=tuple_key, reverse=True)

@classmethod
def _get_trials_from_pattern(cls, pattern: str, directory: str, libname: str) -> T.List[Path]:
Expand Down
5 changes: 4 additions & 1 deletion unittests/internaltests.py
Original file line number Diff line number Diff line change
Expand Up @@ -549,11 +549,14 @@ def _test_all_naming(self, cc, env, patterns, platform):
if platform != 'openbsd':
return
with tempfile.TemporaryDirectory() as tmpdir:
for i in ['libfoo.so.6.0', 'libfoo.so.5.0', 'libfoo.so.54.0', 'libfoo.so.66a.0b', 'libfoo.so.70.0.so.1']:
for i in ['libfoo.so.6.0', 'libfoo.so.5.0', 'libfoo.so.54.0', 'libfoo.so.66a.0b', 'libfoo.so.70.0.so.1',
'libbar.so.7.10', 'libbar.so.7.9', 'libbar.so.7.9.3']:
libpath = Path(tmpdir) / i
libpath.write_text('', encoding='utf-8')
found = cc._find_library_real('foo', env, [tmpdir], '', LibType.PREFER_SHARED, lib_prefix_warning=True)
self.assertEqual(os.path.basename(found[0]), 'libfoo.so.54.0')
found = cc._find_library_real('bar', env, [tmpdir], '', LibType.PREFER_SHARED, lib_prefix_warning=True)
self.assertEqual(os.path.basename(found[0]), 'libbar.so.7.10')

def test_find_library_patterns(self):
'''
Expand Down

0 comments on commit 5b317c5

Please sign in to comment.