Repository: mesonbuild/meson

Issue:
Meson cannot build CMake subprojects relying on generated files directly
**Describe the bug**

When building a CMake subproject through Meson, if the target depends directly on a file generated through `add_custom_command(OUTPUT xxx)`, the Meson-generated Ninja file will actually cause the subproject to fail building.

This is because Meson isn't taking into account CMake's

```ninja
#############################################
# Order-only phony target for XXX

build cmake_object_order_depends_target_XXX: phony || 
    cmake_object_order_depends_target_common generated_file_here
```

rules that would enforce generated files to be created prior to the target being built.

(edit) It is not necessary to set or change build type; this rule is ignored whether the target points directly or transitively to a generated file.

**To Reproduce**

(edited for a custom, out-of-the-box build failure)

https://github.com/amyspark/mesonbug-cmake/tree/bug-11607

**Expected behavior**

Build should succeed. Instead we get 

```
[99/872] Compiling C++ object deps/openal-soft/OpenAL.dll.p/core_hrtf.cpp.obj
FAILED: deps/openal-soft/OpenAL.dll.p/core_hrtf.cpp.obj
"cl" "-Ideps\openal-soft\OpenAL.dll.p" "-Ideps\openal-soft" "-I..\deps\openal-soft" "-I..\deps\openal-soft\include" "-Ideps\openal-soft\__CMake_build" "-I..\deps\openal-soft\__CMake_build" "-I..\deps\openal-soft\common" "-I..\deps\openal-soft\include\AL" "/MD" "/nologo" "/showIncludes" "/utf-8" "/Zc:__cplusplus" "/W4" "/EHsc" "/std:c++14" "/permissive-" "/O2" "/Zi" "-D_CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES=1" "-D_CRT_NONSTDC_NO_WARNINGS=1" "/DWIN32" "/D_WINDOWS" "/EHsc" "/MD" "/Ob1" "/DNDEBUG" "/permissive-" "/w14640" "/wd4065" "/wd4127" "/wd4268" "/wd4324" "/wd5030" "/wd5051" "-DALC_API=__declspec(dllexport)" "-DAL_ALEXT_PROTOTYPES" "-DAL_API=__declspec(dllexport)" "-DAL_BUILD_LIBRARY" "-DNOMINMAX" "-DOpenAL_EXPORTS" "-DRESTRICT=__restrict" "-D_CRT_SECURE_NO_WARNINGS" "-D_WIN32" "/Fddeps\openal-soft\OpenAL.dll.p\core_hrtf.cpp.pdb" /Fodeps/openal-soft/OpenAL.dll.p/core_hrtf.cpp.obj "/c" ../deps/openal-soft/core/hrtf.cpp
../deps/openal-soft/core/hrtf.cpp(1210): fatal error C1083: Cannot open include file: 'default_hrtf.txt': No such file or directory
``` 

There's the one single, unconnected rule to generate that file:

```ninja
build deps/openal-soft/default_hrtf.txt: CUSTOM_COMMAND ../deps/openal-soft/hrtf/Default$ HRTF.mhr ../deps/openal-soft/cmake/bin2h.script.cmake | C$:/Users/Amalia/AppData/Roaming/Python/Python310/Scripts/meson.exe
 COMMAND = "C:\Users\Amalia\AppData\Roaming\Python\Python310\Scripts\meson.exe" "--internal" "cmake_run_ctgt" "-o" "deps/openal-soft/default_hrtf.txt" "-O" "<snip>/build/deps/openal-soft/__CMake_build/default_hrtf.txt" "-d" "C:/<snip>/deps/openal-soft" "C:/Program$ Files/Microsoft$ Visual$ Studio/2022/Preview/Common7/IDE/CommonExtensions/Microsoft/CMake/CMake/bin/cmake.exe" "-D" "INPUT_FILE=<snip>/deps/openal-soft/hrtf/Default$ HRTF.mhr" "-D" "OUTPUT_FILE=<snip>/build/deps/openal-soft/__CMake_build/default_hrtf.txt" "-P" "<snip>/deps/openal-soft/cmake/bin2h.script.cmake" ";;;"
 description = Generating$ deps/openal-soft/custom_tgt_0$ with$ a$ custom$ command
```

and no phony rule to regulate the order.

**system parameters**
* Is this a [cross build](https://mesonbuild.com/Cross-compilation.html) or just a plain native build (for the same computer)? **Native**
* what operating system (e.g. MacOS Catalina, Windows 10, CentOS 8.0, Ubuntu 18.04, etc.) **Windows 10 21H2**
* what Python version are you using e.g. 3.8.0 **3.10.7**
* what `meson --version` **1.0.1**
* what `ninja --version` if it's a Ninja build **1.10.2.git.kitware.jobserver-1**



Discussion:

Resolving patch:
diff --git a/mesonbuild/cmake/interpreter.py b/mesonbuild/cmake/interpreter.py
--- a/mesonbuild/cmake/interpreter.py
+++ b/mesonbuild/cmake/interpreter.py
@@ -362,7 +362,15 @@ def postprocess(self, output_target_map: OutputTargetMap, root_src_dir: Path, su
             supported += list(lang_suffixes[i])
         supported = [f'.{x}' for x in supported]
         self.sources = [x for x in self.sources if any(x.name.endswith(y) for y in supported)]
-        self.generated_raw = [x for x in self.generated_raw if any(x.name.endswith(y) for y in supported)]
+        # Don't filter unsupported files from generated_raw because they
+        # can be GENERATED dependencies for other targets.
+        # See: https://github.com/mesonbuild/meson/issues/11607
+        # However, the dummy CMake rule files for Visual Studio still
+        # need to be filtered out. They don't exist (because the project was
+        # not generated at this time) but the fileapi will still
+        # report them on Windows.
+        # See: https://stackoverflow.com/a/41816323
+        self.generated_raw = [x for x in self.generated_raw if not x.name.endswith('.rule')]
 
         # Make paths relative
         def rel_path(x: Path, is_header: bool, is_generated: bool) -> T.Optional[Path]:


Test patch:
diff --git a/test cases/cmake/8 custom command/subprojects/cmMod/CMakeLists.txt b/test cases/cmake/8 custom command/subprojects/cmMod/CMakeLists.txt
--- a/test cases/cmake/8 custom command/subprojects/cmMod/CMakeLists.txt	
+++ b/test cases/cmake/8 custom command/subprojects/cmMod/CMakeLists.txt	
@@ -46,9 +46,12 @@ add_custom_command(
   DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/cpyBase.hpp.in"
 )
 
+# Slight tuning to the file extension (it's still a C++ header)
+# checks that file extensions are not considered for the purposes
+# of target dependencies
 add_custom_command(
-  OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/cpyBase.hpp"
-  COMMAND mycpy cpyBase.hpp.something                        cpyBase.hpp
+  OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/cpyBase.txt"
+  COMMAND mycpy cpyBase.hpp.something                        cpyBase.txt
   DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/cpyBase.hpp.something"
 )
 
@@ -127,7 +130,7 @@ add_custom_command(
 )
 include_directories("${CMAKE_CURRENT_BINARY_DIR}/cpyTest/some")
 
-add_library(cmModLib SHARED cmMod.cpp genTest.cpp cpyBase.cpp cpyBase.hpp cpyNext.cpp cpyNext.hpp cpyTest.cpp cpyTest.hpp cpyTest2.hpp cpyTest3.hpp cpyTest/some/directory/cpyTest5.hpp)
+add_library(cmModLib SHARED cmMod.cpp genTest.cpp cpyBase.cpp cpyBase.txt cpyNext.cpp cpyNext.hpp cpyTest.cpp cpyTest.hpp cpyTest2.hpp cpyTest3.hpp cpyTest/some/directory/cpyTest5.hpp)
 include(GenerateExportHeader)
 generate_export_header(cmModLib)
 
diff --git a/test cases/cmake/8 custom command/subprojects/cmMod/cmMod.cpp b/test cases/cmake/8 custom command/subprojects/cmMod/cmMod.cpp
--- a/test cases/cmake/8 custom command/subprojects/cmMod/cmMod.cpp	
+++ b/test cases/cmake/8 custom command/subprojects/cmMod/cmMod.cpp	
@@ -1,6 +1,6 @@
 #include "cmMod.hpp"
 #include "genTest.hpp"
-#include "cpyBase.hpp"
+#include "cpyBase.txt"
 #include "cpyNext.hpp"
 #include "cpyTest.hpp"
 #include "cmModLib.hpp"
diff --git a/test cases/cmake/8 custom command/subprojects/cmMod/cpyBase.cpp.am b/test cases/cmake/8 custom command/subprojects/cmMod/cpyBase.cpp.am
--- a/test cases/cmake/8 custom command/subprojects/cmMod/cpyBase.cpp.am	
+++ b/test cases/cmake/8 custom command/subprojects/cmMod/cpyBase.cpp.am	
@@ -1,4 +1,4 @@
-#include "cpyBase.hpp"
+#include "cpyBase.txt"
 
 std::string getStrCpy() {
   return "Hello Copied File";