Repository: mesonbuild/meson

Issue:
Differing ninja target behavior when accessing cmake subproject via sub_proj.target() method versus via __CMake_build dir
**Describe the bug**
I am importing a large cmake projectwith quite a few targets. If I attempt to use Meson to retrieve a cmake target from within the subproject and execute it, I get errors. If I use ninja directly on the __CMake_build dir I can run the same target with no issues.

**To Reproduce**
I've created a repo to showcase an example ESP/meson integrated project but at the moment it's serving as an example of the problem behavior:
https://github.com/Michael-Densberger/meson_esp32

To produce the behavior I ran:
`mkdir /opt/meson_esp32`
`cd /opt/meson_esp32/`
`git clone git@github.com:Michael-Densberger/meson_esp32.git .`
`meson esp32/ build/ --cross-file=esp32/cross.txt`
`ninja -C build/ flash_from_cmake_root` <- Works and flashes no problem.
`ninja -C build/ flash` <- Build errors that vary. 

Note flash and flash_from_cmake_root are Meson `run_targets` that I defined in `meson.build` in the repo.

**Expected behavior**
The flash from the top level which is a `cmake.subproject` target would behave the same as the flash target in the lower level subproject `__CMake_build` folder.

**system parameters**
* Cross build.
* Up-to-date Arch linux.
* Python: 3.8.2
* Meson: 0.54.0
* Ninja: 1.10.0



Discussion:
Note I've applied the changes proposed in #6941 to my local Meson installation. I imagine anyone reproducing would need to do the same.
I am unable to reproduce this with my AUR setup. I get the following CMake error:

```
||CMake Error at /home/daniel/builds/esp-idf/meson_esp32/esp32/subprojects/esp32/main/CMakeLists.txt:1 (idf_component_register):
||  Unknown CMake command "idf_component_register".
||Call Stack (most recent call first):
||  /opt/esp-idf/tools/cmake/scripts/expand_requirements.cmake:107 (include)
||  /opt/esp-idf/tools/cmake/scripts/expand_requirements.cmake:217 (expand_component_requirements)
||
||
||CMake Error at /opt/esp-idf/tools/cmake/idf_functions.cmake:338 (message):
||  Failed to expand component requirements
||Call Stack (most recent call first):
||  /opt/esp-idf/tools/cmake/project.cmake:173 (idf_import_components)
||  CMakeLists.txt:3 (project)
```

Are you running the newer version?

Also, `/subprojects/esp32/__CMake_build` is the build directory generated by CMake itself and (mostly) unaffected by meson. Meson generates it's own `build.ninja` from the CMake introspection data (+ other stuff but this is a different story) and never interacts with the generated build artifacts from CMake.
Yes, I'll push a version that works with the AUR package, sorry about that. Will update you.
Okay run a git pull and try now. The monitor target is not present in this version and has been removed. The `flash_from_cmake_root` target should build the libraries and executable and fail when /dev/ttyUSB0 is not found (or maybe you have an ESP32). 
Interesting note, I think Meson gets confused during the introspection of `__CMake_build` about how to order headers.
The esp-idf toolchain has a scheme where there are base header files replaced or `#include_next`'d by controller specific header files. 

I noticed that when I was trying to build an link an executable to the cmake targets, I would get different behavior that I traced back to a difference in how meson was ordering the `-I/some/dir` statements. Sometimes it would get them in the right order, sometimes it wouldn't; the results indicating that some class or struct method isn't present because the wrong header file was found first. 

Edit: To clarify, I got different results when I cleaned the build directory and rebuilt. Running ninja repeatedly on the same build files always produced the same output (as probably expected).

Resolving patch:
diff --git a/mesonbuild/cmake/interpreter.py b/mesonbuild/cmake/interpreter.py
--- a/mesonbuild/cmake/interpreter.py
+++ b/mesonbuild/cmake/interpreter.py
@@ -22,7 +22,7 @@
 from .traceparser import CMakeTraceParser, CMakeGeneratorTarget
 from .. import mlog
 from ..environment import Environment
-from ..mesonlib import MachineChoice, version_compare
+from ..mesonlib import MachineChoice, OrderedSet, version_compare
 from ..compilers.compilers import lang_suffixes, header_suffixes, obj_suffixes, lib_suffixes, is_header
 from enum import Enum
 from functools import lru_cache
@@ -441,8 +441,8 @@ def custom_target(x: str):
             return x
 
         build_dir_rel = os.path.relpath(self.build_dir, os.path.join(self.env.get_build_dir(), subdir))
-        self.includes = list(set([rel_path(x, True, False) for x in set(self.includes)] + [build_dir_rel]))
-        self.sys_includes = list(set([rel_path(x, True, False) for x in set(self.sys_includes)]))
+        self.includes = list(OrderedSet([rel_path(x, True, False) for x in OrderedSet(self.includes)] + [build_dir_rel]))
+        self.sys_includes = list(OrderedSet([rel_path(x, True, False) for x in OrderedSet(self.sys_includes)]))
         self.sources = [rel_path(x, False, False) for x in self.sources]
         self.generated = [rel_path(x, False, True) for x in self.generated]
 
@@ -507,7 +507,7 @@ def process_object_libs(self, obj_target_list: T.List['ConverterTarget'], linker
                         self._append_objlib_sources(i)
                     else:
                         self.includes += i.includes
-                        self.includes = list(set(self.includes))
+                        self.includes = list(OrderedSet(self.includes))
                         self.object_libs += [i]
                     break
 
@@ -518,9 +518,9 @@ def _append_objlib_sources(self, tgt: 'ConverterTarget') -> None:
         self.includes += tgt.includes
         self.sources += tgt.sources
         self.generated += tgt.generated
-        self.sources = list(set(self.sources))
-        self.generated = list(set(self.generated))
-        self.includes = list(set(self.includes))
+        self.sources = list(OrderedSet(self.sources))
+        self.generated = list(OrderedSet(self.generated))
+        self.includes = list(OrderedSet(self.includes))
 
         # Inherit compiler arguments since they may be required for building
         for lang, opts in tgt.compile_opts.items():
@@ -546,7 +546,7 @@ def process_inter_target_dependencies(self):
                 to_process += [x for x in i.depends if x not in processed]
             else:
                 new_deps += [i]
-        self.depends = list(set(new_deps))
+        self.depends = list(OrderedSet(new_deps))
 
     def cleanup_dependencies(self):
         # Clear the dependencies from targets that where moved from
@@ -720,7 +720,7 @@ def process_inter_target_dependencies(self):
                 to_process += [x for x in i.depends if x not in processed]
             else:
                 new_deps += [i]
-        self.depends = list(set(new_deps))
+        self.depends = list(OrderedSet(new_deps))
 
     def get_ref(self, fname: str) -> T.Optional[CustomTargetReference]:
         fname = os.path.basename(fname)
@@ -863,7 +863,7 @@ def initialise(self, extra_cmake_options: T.List[str]) -> None:
             cmake_files = self.fileapi.get_cmake_sources()
             self.bs_files = [x.file for x in cmake_files if not x.is_cmake and not x.is_temp]
             self.bs_files = [os.path.relpath(x, self.env.get_source_dir()) for x in self.bs_files]
-            self.bs_files = list(set(self.bs_files))
+            self.bs_files = list(OrderedSet(self.bs_files))
 
             # Load the codemodel configurations
             self.codemodel_configs = self.fileapi.get_cmake_configurations()
@@ -888,7 +888,7 @@ def initialise(self, extra_cmake_options: T.List[str]) -> None:
         src_dir = bs_reply.src_dir
         self.bs_files = [x.file for x in bs_reply.build_files if not x.is_cmake and not x.is_temp]
         self.bs_files = [os.path.relpath(os.path.join(src_dir, x), self.env.get_source_dir()) for x in self.bs_files]
-        self.bs_files = list(set(self.bs_files))
+        self.bs_files = list(OrderedSet(self.bs_files))
         self.codemodel_configs = cm_reply.configs
 
     def analyse(self) -> None:


Test patch:
diff --git a/test cases/cmake/17 include path order/main.cpp b/test cases/cmake/17 include path order/main.cpp
new file mode 100644
--- /dev/null
+++ b/test cases/cmake/17 include path order/main.cpp	
@@ -0,0 +1,10 @@
+#include <iostream>
+#include <cmMod.hpp>
+
+using namespace std;
+
+int main(void) {
+  cmModClass obj("Hello");
+  cout << obj.getStr() << endl;
+  return 0;
+}
diff --git a/test cases/cmake/17 include path order/meson.build b/test cases/cmake/17 include path order/meson.build
new file mode 100644
--- /dev/null
+++ b/test cases/cmake/17 include path order/meson.build	
@@ -0,0 +1,9 @@
+project('include_path_order', ['c', 'cpp'])
+
+cm = import('cmake')
+
+sub_pro = cm.subproject('cmMod')
+sub_dep = sub_pro.dependency('cmModLib++')
+
+exe1 = executable('main', ['main.cpp'], dependencies: [sub_dep])
+test('test1', exe1)
diff --git a/test cases/cmake/17 include path order/subprojects/cmMod/CMakeLists.txt b/test cases/cmake/17 include path order/subprojects/cmMod/CMakeLists.txt
new file mode 100644
--- /dev/null
+++ b/test cases/cmake/17 include path order/subprojects/cmMod/CMakeLists.txt	
@@ -0,0 +1,34 @@
+cmake_minimum_required(VERSION 3.5)
+
+project(cmMod)
+set (CMAKE_CXX_STANDARD 14)
+
+include_directories(
+  ${CMAKE_CURRENT_BINARY_DIR}
+
+  # The one and only correct include dir
+  ${CMAKE_CURRENT_SOURCE_DIR}/incG
+
+  # All of these are traps
+  ${CMAKE_CURRENT_SOURCE_DIR}/incL
+  ${CMAKE_CURRENT_SOURCE_DIR}/incM
+  ${CMAKE_CURRENT_SOURCE_DIR}/incO
+  ${CMAKE_CURRENT_SOURCE_DIR}/incF
+  ${CMAKE_CURRENT_SOURCE_DIR}/incI
+  ${CMAKE_CURRENT_SOURCE_DIR}/incE
+  ${CMAKE_CURRENT_SOURCE_DIR}/incD
+  ${CMAKE_CURRENT_SOURCE_DIR}/incH
+  ${CMAKE_CURRENT_SOURCE_DIR}/incN
+  ${CMAKE_CURRENT_SOURCE_DIR}/incA
+  ${CMAKE_CURRENT_SOURCE_DIR}/incB
+  ${CMAKE_CURRENT_SOURCE_DIR}/incJ
+  ${CMAKE_CURRENT_SOURCE_DIR}/incP
+  ${CMAKE_CURRENT_SOURCE_DIR}/incC
+  ${CMAKE_CURRENT_SOURCE_DIR}/incK
+)
+
+add_definitions("-DDO_NOTHING_JUST_A_FLAG=1")
+
+add_library(cmModLib++ SHARED cmMod.cpp)
+include(GenerateExportHeader)
+generate_export_header(cmModLib++)
diff --git a/test cases/cmake/17 include path order/subprojects/cmMod/cmMod.cpp b/test cases/cmake/17 include path order/subprojects/cmMod/cmMod.cpp
new file mode 100644
--- /dev/null
+++ b/test cases/cmake/17 include path order/subprojects/cmMod/cmMod.cpp	
@@ -0,0 +1,11 @@
+#include "cmMod.hpp"
+
+using namespace std;
+
+cmModClass::cmModClass(string foo) {
+  str = foo + " World";
+}
+
+string cmModClass::getStr() const {
+  return str;
+}
diff --git a/test cases/cmake/17 include path order/subprojects/cmMod/incA/cmMod.hpp b/test cases/cmake/17 include path order/subprojects/cmMod/incA/cmMod.hpp
new file mode 100644
--- /dev/null
+++ b/test cases/cmake/17 include path order/subprojects/cmMod/incA/cmMod.hpp	
@@ -0,0 +1,4 @@
+// cmMod.hpp (A)
+#pragma once
+
+#error "cmMod.hpp in incA must not be included"
diff --git a/test cases/cmake/17 include path order/subprojects/cmMod/incB/cmMod.hpp b/test cases/cmake/17 include path order/subprojects/cmMod/incB/cmMod.hpp
new file mode 100644
--- /dev/null
+++ b/test cases/cmake/17 include path order/subprojects/cmMod/incB/cmMod.hpp	
@@ -0,0 +1,4 @@
+// cmMod.hpp (B)
+#pragma once
+
+#error "cmMod.hpp in incB must not be included"
diff --git a/test cases/cmake/17 include path order/subprojects/cmMod/incC/cmMod.hpp b/test cases/cmake/17 include path order/subprojects/cmMod/incC/cmMod.hpp
new file mode 100644
--- /dev/null
+++ b/test cases/cmake/17 include path order/subprojects/cmMod/incC/cmMod.hpp	
@@ -0,0 +1,4 @@
+// cmMod.hpp (C)
+#pragma once
+
+#error "cmMod.hpp in incC must not be included"
diff --git a/test cases/cmake/17 include path order/subprojects/cmMod/incD/cmMod.hpp b/test cases/cmake/17 include path order/subprojects/cmMod/incD/cmMod.hpp
new file mode 100644
--- /dev/null
+++ b/test cases/cmake/17 include path order/subprojects/cmMod/incD/cmMod.hpp	
@@ -0,0 +1,4 @@
+// cmMod.hpp (D)
+#pragma once
+
+#error "cmMod.hpp in incD must not be included"
diff --git a/test cases/cmake/17 include path order/subprojects/cmMod/incE/cmMod.hpp b/test cases/cmake/17 include path order/subprojects/cmMod/incE/cmMod.hpp
new file mode 100644
--- /dev/null
+++ b/test cases/cmake/17 include path order/subprojects/cmMod/incE/cmMod.hpp	
@@ -0,0 +1,4 @@
+// cmMod.hpp (E)
+#pragma once
+
+#error "cmMod.hpp in incE must not be included"
diff --git a/test cases/cmake/17 include path order/subprojects/cmMod/incF/cmMod.hpp b/test cases/cmake/17 include path order/subprojects/cmMod/incF/cmMod.hpp
new file mode 100644
--- /dev/null
+++ b/test cases/cmake/17 include path order/subprojects/cmMod/incF/cmMod.hpp	
@@ -0,0 +1,4 @@
+// cmMod.hpp (F)
+#pragma once
+
+#error "cmMod.hpp in incF must not be included"
diff --git a/test cases/cmake/17 include path order/subprojects/cmMod/incG/cmMod.hpp b/test cases/cmake/17 include path order/subprojects/cmMod/incG/cmMod.hpp
new file mode 100644
--- /dev/null
+++ b/test cases/cmake/17 include path order/subprojects/cmMod/incG/cmMod.hpp	
@@ -0,0 +1,14 @@
+#pragma once
+
+#include "cmmodlib++_export.h"
+#include <string>
+
+class CMMODLIB___EXPORT cmModClass {
+private:
+  std::string str;
+
+public:
+  cmModClass(std::string foo);
+
+  std::string getStr() const;
+};
diff --git a/test cases/cmake/17 include path order/subprojects/cmMod/incH/cmMod.hpp b/test cases/cmake/17 include path order/subprojects/cmMod/incH/cmMod.hpp
new file mode 100644
--- /dev/null
+++ b/test cases/cmake/17 include path order/subprojects/cmMod/incH/cmMod.hpp	
@@ -0,0 +1,4 @@
+// cmMod.hpp (H)
+#pragma once
+
+#error "cmMod.hpp in incH must not be included"
diff --git a/test cases/cmake/17 include path order/subprojects/cmMod/incI/cmMod.hpp b/test cases/cmake/17 include path order/subprojects/cmMod/incI/cmMod.hpp
new file mode 100644
--- /dev/null
+++ b/test cases/cmake/17 include path order/subprojects/cmMod/incI/cmMod.hpp	
@@ -0,0 +1,4 @@
+// cmMod.hpp (I)
+#pragma once
+
+#error "cmMod.hpp in incI must not be included"
diff --git a/test cases/cmake/17 include path order/subprojects/cmMod/incJ/cmMod.hpp b/test cases/cmake/17 include path order/subprojects/cmMod/incJ/cmMod.hpp
new file mode 100644
--- /dev/null
+++ b/test cases/cmake/17 include path order/subprojects/cmMod/incJ/cmMod.hpp	
@@ -0,0 +1,4 @@
+// cmMod.hpp (J)
+#pragma once
+
+#error "cmMod.hpp in incJ must not be included"
diff --git a/test cases/cmake/17 include path order/subprojects/cmMod/incL/cmMod.hpp b/test cases/cmake/17 include path order/subprojects/cmMod/incL/cmMod.hpp
new file mode 100644
--- /dev/null
+++ b/test cases/cmake/17 include path order/subprojects/cmMod/incL/cmMod.hpp	
@@ -0,0 +1,4 @@
+// cmMod.hpp (L)
+#pragma once
+
+#error "cmMod.hpp in incL must not be included"
diff --git a/test cases/cmake/17 include path order/subprojects/cmMod/incM/cmMod.hpp b/test cases/cmake/17 include path order/subprojects/cmMod/incM/cmMod.hpp
new file mode 100644
--- /dev/null
+++ b/test cases/cmake/17 include path order/subprojects/cmMod/incM/cmMod.hpp	
@@ -0,0 +1,4 @@
+// cmMod.hpp (M)
+#pragma once
+
+#error "cmMod.hpp in incM must not be included"
diff --git a/test cases/cmake/17 include path order/subprojects/cmMod/incN/cmMod.hpp b/test cases/cmake/17 include path order/subprojects/cmMod/incN/cmMod.hpp
new file mode 100644
--- /dev/null
+++ b/test cases/cmake/17 include path order/subprojects/cmMod/incN/cmMod.hpp	
@@ -0,0 +1,4 @@
+// cmMod.hpp (N)
+#pragma once
+
+#error "cmMod.hpp in incN must not be included"
diff --git a/test cases/cmake/17 include path order/subprojects/cmMod/incO/cmMod.hpp b/test cases/cmake/17 include path order/subprojects/cmMod/incO/cmMod.hpp
new file mode 100644
--- /dev/null
+++ b/test cases/cmake/17 include path order/subprojects/cmMod/incO/cmMod.hpp	
@@ -0,0 +1,4 @@
+// cmMod.hpp (O)
+#pragma once
+
+#error "cmMod.hpp in incO must not be included"
diff --git a/test cases/cmake/17 include path order/subprojects/cmMod/incP/cmMod.hpp b/test cases/cmake/17 include path order/subprojects/cmMod/incP/cmMod.hpp
new file mode 100644
--- /dev/null
+++ b/test cases/cmake/17 include path order/subprojects/cmMod/incP/cmMod.hpp	
@@ -0,0 +1,4 @@
+// cmMod.hpp (P)
+#pragma once
+
+#error "cmMod.hpp in incP must not be included"