cuda: fix `cuda.find_library()` hardcoded to yield true

* Previously, cuda would just plainly prepend `-l` to the libname.
* By relying on the host compiler to find libraries, we now get
  more subtle failures, such as CUDA modules not being found
  anymore.
* We need to simplify these CUDA modules when nvcc is used for
  linking, since this may have side-effects from the cuda toolchain.

Closes: #13240
pull/13250/head
David Seifert 6 months ago committed by Jussi Pakkanen
parent 4c6d370a0e
commit 4861079360
  1. 2
      mesonbuild/compilers/compilers.py
  2. 2
      mesonbuild/compilers/cuda.py
  3. 43
      mesonbuild/dependencies/cuda.py

@ -1046,7 +1046,7 @@ class Compiler(HoldableObject, metaclass=abc.ABCMeta):
return dep.get_compile_args()
def get_dependency_link_args(self, dep: 'Dependency') -> T.List[str]:
return dep.get_link_args()
return dep.get_link_args(self.get_language())
@classmethod
def use_linker_args(cls, linker: str, version: str) -> T.List[str]:

@ -768,7 +768,7 @@ class CudaCompiler(Compiler):
def find_library(self, libname: str, env: 'Environment', extra_dirs: T.List[str],
libtype: LibType = LibType.PREFER_SHARED, lib_prefix_warning: bool = True) -> T.Optional[T.List[str]]:
return ['-l' + libname] # FIXME
return self.host_compiler.find_library(libname, env, extra_dirs, libtype, lib_prefix_warning)
def get_crt_compile_args(self, crt_val: str, buildtype: str) -> T.List[str]:
return self._to_host_flags(self.host_compiler.get_crt_compile_args(crt_val, buildtype))

@ -41,11 +41,6 @@ class CudaDependency(SystemDependency):
req_modules = ['cudart']
if kwargs.get('static', True):
req_modules = ['cudart_static']
machine = self.env.machines[self.for_machine]
if machine.is_linux():
# extracted by running
# nvcc -v foo.o
req_modules += ['rt', 'pthread', 'dl']
self.requested_modules = req_modules + self.requested_modules
(self.cuda_path, self.version, self.is_found) = self._detect_cuda_path_and_version()
@ -61,12 +56,9 @@ class CudaDependency(SystemDependency):
self.incdir = os.path.join(self.cuda_path, 'include')
self.compile_args += [f'-I{self.incdir}']
if self.language != 'cuda':
arch_libdir = self._detect_arch_libdir()
self.libdir = os.path.join(self.cuda_path, arch_libdir)
mlog.debug('CUDA library directory is', mlog.bold(self.libdir))
else:
self.libdir = None
arch_libdir = self._detect_arch_libdir()
self.libdir = os.path.join(self.cuda_path, arch_libdir)
mlog.debug('CUDA library directory is', mlog.bold(self.libdir))
self.is_found = self._find_requested_libraries()
@ -244,7 +236,14 @@ class CudaDependency(SystemDependency):
all_found = True
for module in self.requested_modules:
args = self.clib_compiler.find_library(module, self.env, [self.libdir] if self.libdir else [])
args = self.clib_compiler.find_library(module, self.env, [self.libdir])
if module == 'cudart_static' and self.language != 'cuda':
machine = self.env.machines[self.for_machine]
if machine.is_linux():
# extracted by running
# nvcc -v foo.o
args += ['-lrt', '-lpthread', '-ldl']
if args is None:
self._report_dependency_error(f'Couldn\'t find requested CUDA module \'{module}\'')
all_found = False
@ -286,10 +285,24 @@ class CudaDependency(SystemDependency):
def get_link_args(self, language: T.Optional[str] = None, raw: bool = False) -> T.List[str]:
args: T.List[str] = []
if self.libdir:
args += self.clib_compiler.get_linker_search_args(self.libdir)
for lib in self.requested_modules:
args += self.lib_modules[lib]
link_args = self.lib_modules[lib]
# Turn canonical arguments like
# /opt/cuda/lib64/libcublas.so
# back into
# -lcublas
# since this is how CUDA modules were passed to nvcc since time immemorial
if language == 'cuda':
if lib in frozenset(['cudart', 'cudart_static']):
# nvcc always links these unconditionally
mlog.debug(f'Not adding \'{lib}\' to dependency, since nvcc will link it implicitly')
link_args = []
elif link_args and link_args[0].startswith(self.libdir):
# module included with CUDA, nvcc knows how to find these itself
mlog.debug(f'CUDA module \'{lib}\' found in CUDA libdir')
link_args = ['-l' + lib]
args += link_args
return args
packages['cuda'] = CudaDependency

Loading…
Cancel
Save