Repository: mesonbuild/meson

Issue:
Meson 1.3.0 cmake dependency check Bug.
**Describe the bug**
Meson 1.3.0 missing correct handling of cmake config with missing dependency.

**To Reproduce**
code like this.
`    dep = dependency(
        pkg_name,
        method: 'cmake',
        cmake_module_path: module_path,
        static: true,
        required: true,
    )
`
when to config cmake dependency, 1.3.0 will throw error for some cmake package:

```
[2023-11-27T06:16:08.405Z] Traceback (most recent call last):

[2023-11-27T06:16:08.406Z]   File "/usr/local/lib/python3.8/site-packages/mesonbuild/mesonmain.py", line 194, in run

[2023-11-27T06:16:08.406Z]     return options.run_func(options)

[2023-11-27T06:16:08.406Z]   File "/usr/local/lib/python3.8/site-packages/mesonbuild/msetup.py", line 358, in run

[2023-11-27T06:16:08.406Z]     app.generate()

[2023-11-27T06:16:08.406Z]   File "/usr/local/lib/python3.8/site-packages/mesonbuild/msetup.py", line 181, in generate

[2023-11-27T06:16:08.406Z]     return self._generate(env, capture, vslite_ctx)

[2023-11-27T06:16:08.406Z]   File "/usr/local/lib/python3.8/site-packages/mesonbuild/msetup.py", line 226, in _generate

[2023-11-27T06:16:08.406Z]     intr.run()

[2023-11-27T06:16:08.406Z]   File "/usr/local/lib/python3.8/site-packages/mesonbuild/interpreter/interpreter.py", line 3004, in run

[2023-11-27T06:16:08.406Z]     super().run()

[2023-11-27T06:16:08.406Z]   File "/usr/local/lib/python3.8/site-packages/mesonbuild/interpreterbase/interpreterbase.py", line 172, in run

[2023-11-27T06:16:08.406Z]     self.evaluate_codeblock(self.ast, start=1)

[2023-11-27T06:16:08.406Z]   File "/usr/local/lib/python3.8/site-packages/mesonbuild/interpreterbase/interpreterbase.py", line 198, in evaluate_codeblock

[2023-11-27T06:16:08.406Z]     raise e

[2023-11-27T06:16:08.406Z]   File "/usr/local/lib/python3.8/site-packages/mesonbuild/interpreterbase/interpreterbase.py", line 190, in evaluate_codeblock

[2023-11-27T06:16:08.406Z]     self.evaluate_statement(cur)

[2023-11-27T06:16:08.406Z]   File "/usr/local/lib/python3.8/site-packages/mesonbuild/interpreterbase/interpreterbase.py", line 243, in evaluate_statement

[2023-11-27T06:16:08.406Z]     self.evaluate_foreach(cur)

[2023-11-27T06:16:08.406Z]   File "/usr/local/lib/python3.8/site-packages/mesonbuild/interpreterbase/interpreterbase.py", line 481, in evaluate_foreach

[2023-11-27T06:16:08.406Z]     self.evaluate_codeblock(node.block)

[2023-11-27T06:16:08.406Z]   File "/usr/local/lib/python3.8/site-packages/mesonbuild/interpreterbase/interpreterbase.py", line 198, in evaluate_codeblock

[2023-11-27T06:16:08.406Z]     raise e

[2023-11-27T06:16:08.406Z]   File "/usr/local/lib/python3.8/site-packages/mesonbuild/interpreterbase/interpreterbase.py", line 190, in evaluate_codeblock

[2023-11-27T06:16:08.406Z]     self.evaluate_statement(cur)

[2023-11-27T06:16:08.406Z]   File "/usr/local/lib/python3.8/site-packages/mesonbuild/interpreterbase/interpreterbase.py", line 208, in evaluate_statement

[2023-11-27T06:16:08.406Z]     self.assignment(cur)

[2023-11-27T06:16:08.406Z]   File "/usr/local/lib/python3.8/site-packages/mesonbuild/interpreterbase/interpreterbase.py", line 644, in assignment

[2023-11-27T06:16:08.406Z]     value = self.evaluate_statement(node.value)

[2023-11-27T06:16:08.406Z]   File "/usr/local/lib/python3.8/site-packages/mesonbuild/interpreterbase/interpreterbase.py", line 204, in evaluate_statement

[2023-11-27T06:16:08.406Z]     return self.function_call(cur)

[2023-11-27T06:16:08.406Z]   File "/usr/local/lib/python3.8/site-packages/mesonbuild/interpreterbase/interpreterbase.py", line 530, in function_call

[2023-11-27T06:16:08.406Z]     res = func(node, func_args, kwargs)

[2023-11-27T06:16:08.406Z]   File "/usr/local/lib/python3.8/site-packages/mesonbuild/interpreterbase/decorators.py", line 809, in wrapped

[2023-11-27T06:16:08.406Z]     return f(*wrapped_args, **wrapped_kwargs)

[2023-11-27T06:16:08.406Z]   File "/usr/local/lib/python3.8/site-packages/mesonbuild/interpreterbase/decorators.py", line 809, in wrapped

[2023-11-27T06:16:08.406Z]     return f(*wrapped_args, **wrapped_kwargs)

[2023-11-27T06:16:08.406Z]   File "/usr/local/lib/python3.8/site-packages/mesonbuild/interpreterbase/decorators.py", line 809, in wrapped

[2023-11-27T06:16:08.406Z]     return f(*wrapped_args, **wrapped_kwargs)

[2023-11-27T06:16:08.406Z]   [Previous line repeated 4 more times]

[2023-11-27T06:16:08.406Z]   File "/usr/local/lib/python3.8/site-packages/mesonbuild/interpreterbase/decorators.py", line 108, in wrapped

[2023-11-27T06:16:08.406Z]     ret = f(*wrapped_args, **wrapped_kwargs)

[2023-11-27T06:16:08.406Z]   File "/usr/local/lib/python3.8/site-packages/mesonbuild/interpreterbase/decorators.py", line 126, in wrapped

[2023-11-27T06:16:08.406Z]     return f(*wrapped_args, **wrapped_kwargs)

[2023-11-27T06:16:08.406Z]   File "/usr/local/lib/python3.8/site-packages/mesonbuild/interpreterbase/decorators.py", line 260, in wrapper

[2023-11-27T06:16:08.406Z]     return f(*nargs, **wrapped_kwargs)

[2023-11-27T06:16:08.406Z]   File "/usr/local/lib/python3.8/site-packages/mesonbuild/interpreterbase/decorators.py", line 579, in wrapper

[2023-11-27T06:16:08.406Z]     return f(*wrapped_args, **wrapped_kwargs)

[2023-11-27T06:16:08.406Z]   File "/usr/local/lib/python3.8/site-packages/mesonbuild/interpreter/interpreter.py", line 1794, in func_dependency

[2023-11-27T06:16:08.406Z]     d = df.lookup(kwargs)

[2023-11-27T06:16:08.406Z]   File "/usr/local/lib/python3.8/site-packages/mesonbuild/interpreter/dependencyfallbacks.py", line 355, in lookup

[2023-11-27T06:16:08.406Z]     dep = func(kwargs, func_args, func_kwargs)

[2023-11-27T06:16:08.406Z]   File "/usr/local/lib/python3.8/site-packages/mesonbuild/interpreter/dependencyfallbacks.py", line 87, in _do_dependency

[2023-11-27T06:16:08.406Z]     dep = dependencies.find_external_dependency(name, self.environment, kwargs)

[2023-11-27T06:16:08.406Z]   File "/usr/local/lib/python3.8/site-packages/mesonbuild/dependencies/detect.py", line 124, in find_external_dependency

[2023-11-27T06:16:08.406Z]     d = c()

[2023-11-27T06:16:08.406Z]   File "/usr/local/lib/python3.8/site-packages/mesonbuild/dependencies/cmake.py", line 156, in __init__

[2023-11-27T06:16:08.406Z]     self._detect_dep(name, package_version, modules, components, cm_args)

[2023-11-27T06:16:08.406Z]   File "/usr/local/lib/python3.8/site-packages/mesonbuild/dependencies/cmake.py", line 552, in _detect_dep

[2023-11-27T06:16:08.406Z]     rtgt = resolve_cmake_trace_targets(i, self.traceparser, self.env,

[2023-11-27T06:16:08.406Z]   File "/usr/local/lib/python3.8/site-packages/mesonbuild/cmake/tracetargets.py", line 58, in resolve_cmake_trace_targets

[2023-11-27T06:16:08.406Z]     res.libraries += clib_compiler.find_library(curr, env, [])

[2023-11-27T06:16:08.406Z] TypeError: 'NoneType' object is not iterable
```

But no exception throwed for Meson 1.2.3.


**Expected behavior**
Should behave the same as old version like 1.2.3

**system parameters**
CentOS 8.0
Python 3.8
meson 1.3.0  



Discussion:

Resolving patch:
diff --git a/mesonbuild/cmake/tracetargets.py b/mesonbuild/cmake/tracetargets.py
--- a/mesonbuild/cmake/tracetargets.py
+++ b/mesonbuild/cmake/tracetargets.py
@@ -55,7 +55,11 @@ def resolve_cmake_trace_targets(target_name: str,
                 # CMake brute-forces a combination of prefix/suffix combinations to find the
                 # right library. Assume any bare argument passed which is not also a CMake
                 # target must be a system library we should try to link against.
-                res.libraries += clib_compiler.find_library(curr, env, [])
+                flib = clib_compiler.find_library(curr, env, [])
+                if flib is not None:
+                    res.libraries += flib
+                else:
+                    not_found_warning(curr)
             else:
                 not_found_warning(curr)
             continue
diff --git a/mesonbuild/dependencies/base.py b/mesonbuild/dependencies/base.py
--- a/mesonbuild/dependencies/base.py
+++ b/mesonbuild/dependencies/base.py
@@ -39,15 +39,34 @@
     )
     from ..interpreter.type_checking import PkgConfigDefineType
 
+    _MissingCompilerBase = Compiler
+else:
+    _MissingCompilerBase = object
+
 
 class DependencyException(MesonException):
     '''Exceptions raised while trying to find dependencies'''
 
 
-class MissingCompiler:
+class MissingCompiler(_MissingCompilerBase):
     """Represent a None Compiler - when no tool chain is found.
     replacing AttributeError with DependencyException"""
 
+    # These are needed in type checking mode to avoid errors, but we don't want
+    # the extra overhead at runtime
+    if T.TYPE_CHECKING:
+        def __init__(self) -> None:
+            pass
+
+        def get_optimization_args(self, optimization_level: str) -> T.List[str]:
+            return []
+
+        def get_output_args(self, outputname: str) -> T.List[str]:
+            return []
+
+        def sanity_check(self, work_dir: str, environment: 'Environment') -> None:
+            return None
+
     def __getattr__(self, item: str) -> T.Any:
         if item.startswith('__'):
             raise AttributeError()


Test patch: