From 3969a9a153f5f0f1cc7855684dbc57f8d70db22f Mon Sep 17 00:00:00 2001 From: Sam Clegg Date: Fri, 23 Jul 2021 15:13:45 -0700 Subject: [PATCH] Warn when we map unrecognized library files to `-l` flags. This is rather an odd feature and I think we should notify users if they are relying on it. Honestly I doubt many are. See #14689 for an example of how confusing it can be when it goes wrong. Also, make sure we strip off the full suffix in the case of `.so.1.2.3` suffixes. --- emcc.py | 17 +++++++++++++++-- tests/test_other.py | 5 +++-- tools/shared.py | 1 + 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/emcc.py b/emcc.py index 1a49ccb832891..999314afc0820 100755 --- a/emcc.py +++ b/emcc.py @@ -892,6 +892,17 @@ def get_file_suffix(filename): return '' +def get_library_basename(filename): + """Similar to get_file_suffix this strips off all numeric suffixes and then + then final non-numeric one. For example for 'libz.so.1.2.8' returns 'libz'""" + filename = os.path.basename(filename) + while filename: + filename, suffix = os.path.splitext(filename) + # Keep stipping suffixes until we strip a non-numeric one. + if not suffix[1:].isdigit(): + return filename + + def get_secondary_target(target, ext): # Depending on the output format emscripten creates zero or more secondary # output files (e.g. the .wasm file when creating JS output, or the @@ -1236,8 +1247,10 @@ def phase_setup(options, state, newargs, settings_map): # For shared libraries that are neither bitcode nor wasm, assuming its local native # library and attempt to find a library by the same name in our own library path. # TODO(sbc): Do we really need this feature? See test_other.py:test_local_link - libname = strip_prefix(unsuffixed_basename(arg), 'lib') - add_link_flag(state, i, '-l' + libname) + libname = strip_prefix(get_library_basename(arg), 'lib') + flag = '-l' + libname + diagnostics.warning('map-unrecognized-libraries', f'unrecognized file type: `{arg}`. Mapping to `{flag}` and hoping for the best') + add_link_flag(state, i, flag) else: input_files.append((i, arg)) elif arg.startswith('-L'): diff --git a/tests/test_other.py b/tests/test_other.py index f1176a94c6ac7..24817268b6386 100644 --- a/tests/test_other.py +++ b/tests/test_other.py @@ -1226,7 +1226,7 @@ def test_local_link(self): ''') ensure_dir('subdir') - create_file('subdir/libfile.so', 'this is not llvm bitcode!') + create_file('subdir/libfile.so.1.2.3', 'this is not llvm bitcode!') create_file('libfile.cpp', ''' #include @@ -1236,7 +1236,8 @@ def test_local_link(self): ''') self.run_process([EMXX, 'libfile.cpp', '-shared', '-o', 'libfile.so'], stderr=PIPE) - self.run_process([EMXX, 'main.cpp', Path('subdir/libfile.so'), '-L.']) + err = self.run_process([EMXX, 'main.cpp', Path('subdir/libfile.so.1.2.3'), '-L.'], stderr=PIPE).stderr + self.assertContained('Mapping to `-lfile` and hoping for the best [-Wmap-unrecognized-libraries]', err) self.assertContained('hello from lib', self.run_js('a.out.js')) def test_identical_basenames(self): diff --git a/tools/shared.py b/tools/shared.py index 24e39dba9da07..dc14c8366f215 100644 --- a/tools/shared.py +++ b/tools/shared.py @@ -65,6 +65,7 @@ diagnostics.add_warning('deprecated', shared=True) diagnostics.add_warning('version-check') diagnostics.add_warning('export-main') +diagnostics.add_warning('map-unrecognized-libraries') diagnostics.add_warning('unused-command-line-argument', shared=True) diagnostics.add_warning('pthreads-mem-growth')