Skip to content

Commit ccaecfd

Browse files
committed
fix rustc -l argument on Windows
On Windows, rustc searches both FOO.lib and libFOO.lib when passed -lfoo. This means that the check for nonstandard names is too struct, and indeed it breaks passing for example -lkernel32 (which adds kernel32.lib to the link). Fix this by special casing Windows. Extract the logic to the Rust compiler class for clarity. Signed-off-by: Paolo Bonzini <[email protected]>
1 parent b527ed5 commit ccaecfd

File tree

2 files changed

+28
-9
lines changed

2 files changed

+28
-9
lines changed

mesonbuild/backend/ninjabackend.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2050,14 +2050,8 @@ def _link_library(libname: str, static: bool, bundle: bool = False) -> None:
20502050
if rustc.has_verbatim():
20512051
modifiers.append('+verbatim')
20522052
else:
2053-
# undo the effects of -l without verbatim
2054-
badname = not is_library(libname)
2055-
libname, ext = os.path.splitext(libname)
2056-
if libname.startswith('lib'):
2057-
libname = libname[3:]
2058-
else:
2059-
badname = True
2060-
if badname:
2053+
libname = rustc.lib_file_to_l_arg(self.environment, libname)
2054+
if libname is None:
20612055
raise MesonException(f"rustc does not implement '-l{type_}:+verbatim'; cannot link to '{orig_libname}' due to nonstandard name")
20622056

20632057
if modifiers:

mesonbuild/compilers/rust.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from .. import options
1414
from ..mesonlib import EnvironmentException, MesonException, Popen_safe_logged, version_compare
1515
from ..options import OptionKey
16-
from .compilers import Compiler, CompileCheckMode, clike_debug_args
16+
from .compilers import Compiler, CompileCheckMode, clike_debug_args, is_library
1717

1818
if T.TYPE_CHECKING:
1919
from ..options import MutableKeyedOptionDictType
@@ -208,6 +208,31 @@ def has_verbatim(self) -> bool:
208208
# being linked. However, Meson uses "bundle", not "whole_archive".
209209
return False
210210

211+
def lib_file_to_l_arg(self, env: Environment, libname: str) -> T.Optional[str]:
212+
"""Undo the effects of -l on the filename, returning the
213+
argument that can be passed to -l, or None if the
214+
library name is not supported."""
215+
if not is_library(libname):
216+
return None
217+
libname, ext = os.path.splitext(libname)
218+
219+
# On Windows, -lfoo searches either foo.lib or libfoo.a.
220+
# Elsewhere, it only searches both static and shared libraries with
221+
# the "lib" prefix; for simplicity just skip .lib which is never used.
222+
if env.machines[self.for_machine].is_windows():
223+
if ext == 'lib':
224+
return libname
225+
if ext != 'a':
226+
return None
227+
else:
228+
if ext == 'lib':
229+
return None
230+
231+
if not libname.startswith('lib'):
232+
return None
233+
libname = libname[3:]
234+
return libname
235+
211236
def get_debug_args(self, is_debug: bool) -> T.List[str]:
212237
return clike_debug_args[is_debug]
213238

0 commit comments

Comments
 (0)