Repository: mesonbuild/meson

Issue:
-Dcmake_prefix_path does not work for CMake subprojects
This works for both CMake dependencies and CMake subprojects:
```
$ export CMAKE_PREFIX_PATH=<somedir>
$ meson <builddir>
```
And this works for CMake dependencies only:
```
meson -Dcmake_prefix_path=<somedir> <builddir>
```
I think it should be mentioned in the docs to avoid confusion.


Discussion:
Nope, this is an oversight on our part. `cmake_prefix_path` should also affect CMake subprojects.

Resolving patch:
diff --git a/mesonbuild/cmake/executor.py b/mesonbuild/cmake/executor.py
--- a/mesonbuild/cmake/executor.py
+++ b/mesonbuild/cmake/executor.py
@@ -28,6 +28,7 @@
 from .. import mlog, mesonlib
 from ..mesonlib import PerMachine, Popen_safe, version_compare, MachineChoice
 from ..environment import Environment
+from ..envconfig import get_env_var
 
 if T.TYPE_CHECKING:
     from ..dependencies.base import ExternalProgram
@@ -48,6 +49,8 @@ def __init__(self, environment: Environment, version: str, for_machine: MachineC
         self.cmakebin, self.cmakevers = self.find_cmake_binary(self.environment, silent=silent)
         self.always_capture_stderr = True
         self.print_cmout = False
+        self.prefix_paths = []      # type: T.List[str]
+        self.extra_cmake_args = []  # type: T.List[str]
         if self.cmakebin is False:
             self.cmakebin = None
             return
@@ -60,6 +63,21 @@ def __init__(self, environment: Environment, version: str, for_machine: MachineC
             self.cmakebin = None
             return
 
+        self.prefix_paths = self.environment.coredata.builtins_per_machine[self.for_machine]['cmake_prefix_path'].value
+        env_pref_path = get_env_var(
+            self.for_machine,
+            self.environment.is_cross_build(),
+            'CMAKE_PREFIX_PATH')
+        if env_pref_path is not None:
+            env_pref_path = env_pref_path.split(os.pathsep)
+            env_pref_path = [x for x in env_pref_path if x]  # Filter out empty strings
+            if not self.prefix_paths:
+                self.prefix_paths = []
+            self.prefix_paths += env_pref_path
+
+        if self.prefix_paths:
+            self.extra_cmake_args += ['-DCMAKE_PREFIX_PATH={}'.format(';'.join(self.prefix_paths))]
+
     def find_cmake_binary(self, environment: Environment, silent: bool = False) -> T.Tuple['ExternalProgram', str]:
         from ..dependencies.base import ExternalProgram
 
@@ -226,6 +244,7 @@ def call(self, args: T.List[str], build_dir: str, env=None, disable_cache: bool
         if env is None:
             env = os.environ
 
+        args = args + self.extra_cmake_args
         if disable_cache:
             return self._call_impl(args, build_dir, env)
 
@@ -362,5 +381,8 @@ def executable_path(self) -> str:
     def get_command(self) -> T.List[str]:
         return self.cmakebin.get_command()
 
+    def get_cmake_prefix_paths(self) -> T.List[str]:
+        return self.prefix_paths
+
     def machine_choice(self) -> MachineChoice:
         return self.for_machine
diff --git a/mesonbuild/dependencies/base.py b/mesonbuild/dependencies/base.py
--- a/mesonbuild/dependencies/base.py
+++ b/mesonbuild/dependencies/base.py
@@ -1086,21 +1086,7 @@ def __init__(self, name: str, environment: Environment, kwargs, language: T.Opti
         if cm_path:
             cm_args.append('-DCMAKE_MODULE_PATH=' + ';'.join(cm_path))
 
-        pref_path = self.env.coredata.builtins_per_machine[self.for_machine]['cmake_prefix_path'].value
-        env_pref_path = get_env_var(
-            self.for_machine,
-            self.env.is_cross_build(),
-            'CMAKE_PREFIX_PATH')
-        if env_pref_path is not None:
-            env_pref_path = env_pref_path.split(os.pathsep)
-            env_pref_path = [x for x in env_pref_path if x]  # Filter out empty strings
-            if not pref_path:
-                pref_path = []
-            pref_path += env_pref_path
-        if pref_path:
-            cm_args.append('-DCMAKE_PREFIX_PATH={}'.format(';'.join(pref_path)))
-
-        if not self._preliminary_find_check(name, cm_path, pref_path, environment.machines[self.for_machine]):
+        if not self._preliminary_find_check(name, cm_path, self.cmakebin.get_cmake_prefix_paths(), environment.machines[self.for_machine]):
             mlog.debug('Preliminary CMake check failed. Aborting.')
             return
         self._detect_dep(name, modules, components, cm_args)


Test patch:
diff --git a/test cases/cmake/7 cmake options/subprojects/cmOpts/CMakeLists.txt b/test cases/cmake/7 cmake options/subprojects/cmOpts/CMakeLists.txt
--- a/test cases/cmake/7 cmake options/subprojects/cmOpts/CMakeLists.txt	
+++ b/test cases/cmake/7 cmake options/subprojects/cmOpts/CMakeLists.txt	
@@ -1,5 +1,10 @@
 cmake_minimum_required(VERSION 3.7)
+project(testPro)
 
 if(NOT "${SOME_CMAKE_VAR}" STREQUAL "something")
   message(FATAL_ERROR "Setting the CMake var failed")
 endif()
+
+if(NOT "${CMAKE_PREFIX_PATH}" STREQUAL "val1;val2")
+  message(FATAL_ERROR "Setting the CMAKE_PREFIX_PATH failed '${CMAKE_PREFIX_PATH}'")
+endif()
diff --git a/test cases/cmake/7 cmake options/test.json b/test cases/cmake/7 cmake options/test.json
new file mode 100644
--- /dev/null
+++ b/test cases/cmake/7 cmake options/test.json	
@@ -0,0 +1,9 @@
+{
+  "matrix": {
+    "options": {
+      "cmake_prefix_path": [
+        { "val": ["val1", "val2"] }
+      ]
+    }
+  }
+}