diff --git a/mesonbuild/dependencies/cmake.py b/mesonbuild/dependencies/cmake.py index ed89622b3..3fe5b8f28 100644 --- a/mesonbuild/dependencies/cmake.py +++ b/mesonbuild/dependencies/cmake.py @@ -15,7 +15,7 @@ from .base import ExternalDependency, DependencyException, DependencyTypeName from ..mesonlib import is_windows, MesonException, OptionKey, PerMachine, stringlistify, extract_as_list from ..mesondata import mesondata -from ..cmake import CMakeExecutor, CMakeTraceParser, CMakeException, CMakeToolchain, CMakeExecScope, check_cmake_args +from ..cmake import CMakeExecutor, CMakeTraceParser, CMakeException, CMakeToolchain, CMakeExecScope, check_cmake_args, CMakeTarget from .. import mlog from pathlib import Path import functools @@ -446,6 +446,37 @@ class CMakeDependency(ExternalDependency): # Failed to guess a target --> try the old-style method if len(modules) == 0: + # Warn when there might be matching imported targets but no automatic match was used + partial_modules: T.List[CMakeTarget] = [] + for k, v in self.traceparser.targets.items(): + tg = k.lower() + lname = name.lower() + if tg.startswith(f'{lname}::'): + partial_modules += [v] + if partial_modules: + mlog.warning(textwrap.dedent(f'''\ + Could not find and exact match for the CMake dependency {name}. + + However, Meson found the following partial matches: + + {[x.name for x in partial_modules]} + + Using imported is recommended, since this approach is less error prone + and better supported by Meson. Consider explicitly specifying one of + these in the dependency call with: + + dependency('{name}', modules: ['{name}::', ...]) + + Meson will now continue to use the old-style {name}_LIBRARIES CMake + variables to extract the dependency information since no explicit + target is currently specified. + + ''')) + mlog.debug('More info for the partial match targets:') + for tgt in partial_modules: + mlog.debug(tgt) + + incDirs = [x for x in self.traceparser.get_cmake_var('PACKAGE_INCLUDE_DIRS') if x] defs = [x for x in self.traceparser.get_cmake_var('PACKAGE_DEFINITIONS') if x] libs = [x for x in self.traceparser.get_cmake_var('PACKAGE_LIBRARIES') if x] diff --git a/test cases/linuxlike/13 cmake dependency/cmake_fake1/cmMesonTestF1Config.cmake b/test cases/linuxlike/13 cmake dependency/cmake_fake1/cmMesonTestF1Config.cmake index e12aeb99a..938c7061b 100644 --- a/test cases/linuxlike/13 cmake dependency/cmake_fake1/cmMesonTestF1Config.cmake +++ b/test cases/linuxlike/13 cmake dependency/cmake_fake1/cmMesonTestF1Config.cmake @@ -4,6 +4,8 @@ if(ZLIB_FOUND OR ZLIB_Found) set(cmMesonTestF1_FOUND ON) set(cmMesonTestF1_LIBRARIES ${ZLIB_LIBRARY}) set(cmMesonTestF1_INCLUDE_DIRS ${ZLIB_INCLUDE_DIR}) + + add_library(CMMesonTESTf1::evil_non_standard_trget UNKNOWN IMPORTED) else() set(cmMesonTestF1_FOUND OFF) endif() diff --git a/test cases/linuxlike/13 cmake dependency/test.json b/test cases/linuxlike/13 cmake dependency/test.json index a12553666..1505986ee 100644 --- a/test cases/linuxlike/13 cmake dependency/test.json +++ b/test cases/linuxlike/13 cmake dependency/test.json @@ -2,5 +2,13 @@ "env": { "CMAKE_PREFIX_PATH": "@ROOT@/cmake_fake1;@ROOT@/cmake_fake2:@ROOT@/cmake_pref_env", "PATH": "@ROOT@/cmake_fake3/bin:@PATH@" - } + }, + "stdout": [ + { + "line": "WARNING: Could not find and exact match for the CMake dependency cmMesonTestF1." + }, + { + "line": " ['CMMesonTESTf1::evil_non_standard_trget']" + } + ] }