Repository: conan-io/conan
Pull request #16172 (2024-04-29 10:47:42)

MesonToolchain reads conf tools.build:defines
Changelog: Bugfix: MesonToolchain listens to conf variable `tools.build:defines`.
Docs: https://github.com/conan-io/docs/pull/XXXX
Closes: https://github.com/conan-io/conan/issues/16169



Patch:
diff --git a/conan/tools/meson/toolchain.py b/conan/tools/meson/toolchain.py
index 3cd4ce73b..80f133387 100644
--- a/conan/tools/meson/toolchain.py
+++ b/conan/tools/meson/toolchain.py
@@ -352,10 +352,12 @@ class MesonToolchain(object):
         exelinkflags = self._conanfile.conf.get("tools.build:exelinkflags", default=[], check_type=list)
         linker_scripts = self._conanfile.conf.get("tools.build:linker_scripts", default=[], check_type=list)
         linker_script_flags = ['-T"' + linker_script + '"' for linker_script in linker_scripts]
+        defines = [f"-D{d}" for d in self._conanfile.conf.get("tools.build:defines", default=[], check_type=list)]
         return {
             "cxxflags": cxxflags,
             "cflags": cflags,
-            "ldflags": sharedlinkflags + exelinkflags + linker_script_flags
+            "ldflags": sharedlinkflags + exelinkflags + linker_script_flags,
+            "defines": defines
         }
 
     @staticmethod
@@ -377,13 +379,13 @@ class MesonToolchain(object):
         apple_flags = self.apple_isysroot_flag + self.apple_arch_flag + self.apple_min_version_flag
         extra_flags = self._get_extra_flags()
 
-        self.c_args.extend(apple_flags + extra_flags["cflags"])
-        self.cpp_args.extend(apple_flags + extra_flags["cxxflags"])
+        self.c_args.extend(apple_flags + extra_flags["cflags"] + extra_flags["defines"])
+        self.cpp_args.extend(apple_flags + extra_flags["cxxflags"] + extra_flags["defines"])
         self.c_link_args.extend(apple_flags + extra_flags["ldflags"])
         self.cpp_link_args.extend(apple_flags + extra_flags["ldflags"])
         # Objective C/C++
-        self.objc_args.extend(self.c_args)
-        self.objcpp_args.extend(self.cpp_args)
+        self.objc_args.extend(self.c_args + extra_flags["defines"])
+        self.objcpp_args.extend(self.cpp_args + extra_flags["defines"])
         # These link_args have already the LDFLAGS env value so let's add only the new possible ones
         self.objc_link_args.extend(apple_flags + extra_flags["ldflags"])
         self.objcpp_link_args.extend(apple_flags + extra_flags["ldflags"])


Test patch:
diff --git a/conans/test/integration/graph/core/test_build_requires.py b/conans/test/integration/graph/core/test_build_requires.py
index 4e50e1185..73a3fcc75 100644
--- a/conans/test/integration/graph/core/test_build_requires.py
+++ b/conans/test/integration/graph/core/test_build_requires.py
@@ -405,6 +405,24 @@ class TestTestRequire(GraphManagerTest):
         _check_transitive(lib, [(gtest, True, True, False, False),
                                 (zlib, True, True, False, False, False)])
 
+    def test_test_require_loop(self):
+        # https://github.com/conan-io/conan/issues/15412
+        self._cache_recipe("gtest/1.11", GenConanfile())
+        self._cache_recipe("abseil/1.0", GenConanfile().with_test_requires("gtest/[>=1 <1.14]"))
+        self._cache_recipe("gtest/1.14", GenConanfile().with_requires("abseil/1.0"))
+        deps_graph = self.build_graph(GenConanfile("opencv", "1.0").with_test_requires("gtest/1.14"))
+
+        self.assertEqual(4, len(deps_graph.nodes))
+        opencv = deps_graph.root
+        gtest14 = opencv.dependencies[0].dst
+        abseil = gtest14.dependencies[0].dst
+        gtest11 = abseil.dependencies[0].dst
+
+        self._check_node(opencv, "opencv/1.0@", deps=[gtest14], dependents=[])
+        self._check_node(gtest14, "gtest/1.14#123", deps=[abseil], dependents=[opencv])
+        self._check_node(abseil, "abseil/1.0#123", deps=[gtest11], dependents=[gtest14])
+        self._check_node(gtest11, "gtest/1.11#123", deps=[], dependents=[abseil])
+
 
 class TestTestRequiresProblemsShared(GraphManagerTest):
 
diff --git a/conans/test/integration/toolchains/meson/test_mesontoolchain.py b/conans/test/integration/toolchains/meson/test_mesontoolchain.py
index a79b9bed1..740cb931d 100644
--- a/conans/test/integration/toolchains/meson/test_mesontoolchain.py
+++ b/conans/test/integration/toolchains/meson/test_mesontoolchain.py
@@ -87,6 +87,8 @@ def test_extra_flags_via_conf():
         tools.build:cflags=["-flag3", "-flag4"]
         tools.build:sharedlinkflags+=["-flag5"]
         tools.build:exelinkflags+=["-flag6"]
+        # Issue related: https://github.com/conan-io/conan/issues/16169
+        tools.build:defines=["define1=0"]
    """)
     t = TestClient()
     t.save({"conanfile.txt": "[generators]\nMesonToolchain",
@@ -94,8 +96,8 @@ def test_extra_flags_via_conf():
 
     t.run("install . -pr:h=profile -pr:b=profile")
     content = t.load(MesonToolchain.native_filename)
-    assert "cpp_args = ['-flag0', '-other=val', '-flag1', '-flag2', '-D_GLIBCXX_USE_CXX11_ABI=0']" in content
-    assert "c_args = ['-flag0', '-other=val', '-flag3', '-flag4']" in content
+    assert "cpp_args = ['-flag0', '-other=val', '-flag1', '-flag2', '-Ddefine1=0', '-D_GLIBCXX_USE_CXX11_ABI=0']" in content
+    assert "c_args = ['-flag0', '-other=val', '-flag3', '-flag4', '-Ddefine1=0']" in content
     assert "c_link_args = ['-flag0', '-other=val', '-flag5', '-flag6']" in content
     assert "cpp_link_args = ['-flag0', '-other=val', '-flag5', '-flag6']" in content