Repository: mesonbuild/meson

Issue:
cmake module: support include_type for dependencies
With meson 0.52 the `include_type` argument for `dependency()` was introduced. It seems, that the cmake subproject module does not support it.

It would be nice to write meson code like:
```
cmake = import('cmake')
some_proj = cmake.subproject('some_project'))
some_proj_dep = some_proj.dependency('some_target', include_type: 'system')
```
Currently, meson seems not to be respecting the `include_type` argument.



Discussion:

Resolving patch:
diff --git a/mesonbuild/interpreter.py b/mesonbuild/interpreter.py
--- a/mesonbuild/interpreter.py
+++ b/mesonbuild/interpreter.py
@@ -3687,9 +3687,17 @@ def func_dependency(self, node, args, kwargs):
             if not_found_message:
                 self.message_impl([not_found_message])
             raise
+        assert isinstance(d, DependencyHolder)
         if not d.found() and not_found_message:
             self.message_impl([not_found_message])
             self.message_impl([not_found_message])
+        # Ensure the correct include type
+        if 'include_type' in kwargs:
+            wanted = kwargs['include_type']
+            actual = d.include_type_method([], {})
+            if wanted != actual:
+                mlog.debug('Current include type of {} is {}. Converting to requested {}'.format(name, actual, wanted))
+                d = d.as_system_method([wanted], {})
         # Override this dependency to have consistent results in subsequent
         # dependency lookups.
         if name and d.found():
diff --git a/mesonbuild/modules/cmake.py b/mesonbuild/modules/cmake.py
--- a/mesonbuild/modules/cmake.py
+++ b/mesonbuild/modules/cmake.py
@@ -20,7 +20,7 @@
 
 from .. import build, dependencies, mesonlib, mlog
 from ..cmake import SingleTargetOptions, TargetOptions, cmake_defines_to_args
-from ..interpreter import ConfigurationDataHolder, InterpreterException, SubprojectHolder
+from ..interpreter import ConfigurationDataHolder, InterpreterException, SubprojectHolder, DependencyHolder
 from ..interpreterbase import (
     InterpreterObject,
     ObjectHolder,
@@ -105,11 +105,18 @@ def _args_to_info(self, args):
     def get_variable(self, args, kwargs):
         return self.held_object.get_variable_method(args, kwargs)
 
-    @noKwargs
+    @FeatureNewKwargs('dependency', '0.56.0', ['include_type'])
+    @permittedKwargs({'include_type'})
     @stringArgs
     def dependency(self, args, kwargs):
         info = self._args_to_info(args)
-        return self.get_variable([info['dep']], kwargs)
+        orig = self.get_variable([info['dep']], {})
+        assert isinstance(orig, DependencyHolder)
+        actual = orig.include_type_method([], {})
+        if 'include_type' in kwargs and kwargs['include_type'] != actual:
+            mlog.debug('Current include type is {}. Converting to requested {}'.format(actual, kwargs['include_type']))
+            return orig.as_system_method([kwargs['include_type']], {})
+        return orig
 
     @noKwargs
     @stringArgs


Test patch:
diff --git a/test cases/cmake/1 basic/meson.build b/test cases/cmake/1 basic/meson.build
--- a/test cases/cmake/1 basic/meson.build	
+++ b/test cases/cmake/1 basic/meson.build	
@@ -3,11 +3,12 @@ project('cmakeSubTest', ['c', 'cpp'])
 cm = import('cmake')
 
 sub_pro = cm.subproject('cmMod')
-sub_dep = sub_pro.dependency('cmModLib++')
+sub_dep = sub_pro.dependency('cmModLib++', include_type: 'system')
 
 assert(sub_pro.found(), 'found() method reports not found, but should be found')
 assert(sub_pro.target_list() == ['cmModLib++'], 'There should be exactly one target')
 assert(sub_pro.target_type('cmModLib++') == 'shared_library', 'Target type should be shared_library')
+assert(sub_dep.include_type() == 'system', 'the include_type kwarg of dependency() works')
 
 exe1 = executable('main', ['main.cpp'], dependencies: [sub_dep])
 test('test1', exe1)
diff --git a/test cases/common/226 include_type dependency/meson.build b/test cases/common/226 include_type dependency/meson.build
--- a/test cases/common/226 include_type dependency/meson.build	
+++ b/test cases/common/226 include_type dependency/meson.build	
@@ -33,6 +33,9 @@ sp_dep_sys = sp_dep.as_system('system')
 assert(sp_dep_sys.include_type() == 'system', 'changing include_type works')
 assert(sp_dep.include_type() == 'preserve', 'as_system must not mutate the original object')
 
+fallback = dependency('sdffgagf_does_not_exist', include_type: 'system', fallback: ['subDep', 'subDep_dep'])
+assert(fallback.include_type() == 'system', 'include_type works with dependency fallback')
+
 # Check that PCH works with `include_type : 'system'` See https://github.com/mesonbuild/meson/issues/7167
 main_exe = executable('main_exe', 'main.cpp', cpp_pch: 'pch/test.hpp', dependencies: boost_dep)
 test('main_test', main_exe)