matchesLibraryName function

bool matchesLibraryName(
  1. String basename, {
  2. required String canonicalName,
  3. required bool acceptVersionedNames,
})

Whether the given filename matches a canonical library name, including versioned variants like libfoo.so.1.2.

Implementation

bool matchesLibraryName(
  String basename, {
  required String canonicalName,
  required bool acceptVersionedNames,
}) {
  if (basename == canonicalName) return true;

  if (!acceptVersionedNames) return false;

  // Match versioned .so: libfoo.so.1, libfoo.so.1.2.3
  if (canonicalName.endsWith('.so')) {
    return basename.startsWith('$canonicalName.');
  }

  // Match versioned .dylib: libfoo.1.2.3.dylib
  if (canonicalName.endsWith('.dylib')) {
    final stem = canonicalName.substring(
      0,
      canonicalName.length - '.dylib'.length,
    );
    return basename.startsWith('$stem.') && basename.endsWith('.dylib');
  }

  return false;
}