Repository: conan-io/conan
Pull request #15706 (2024-02-19 18:36:34)

[Meson] Using `--destdir` variable 
Changelog: Fix: Avoiding reconfigure process. Using `--destdir` param instead. Deprecated `reconfigure` param in `Meson.configure() function`.
Changelog: Feature: `conan.tools.meson` helper requires meson >= 0.57.0.
Docs: https://github.com/conan-io/docs/pull/XXXX
Closes: https://github.com/conan-io/conan/issues/11910



Patch:
diff --git a/conan/tools/build/__init__.py b/conan/tools/build/__init__.py
index 829886a8d..da2223ff1 100644
--- a/conan/tools/build/__init__.py
+++ b/conan/tools/build/__init__.py
@@ -3,3 +3,4 @@ from conan.tools.build.cross_building import cross_building, can_run
 from conan.tools.build.cppstd import check_min_cppstd, valid_min_cppstd, default_cppstd, \
     supported_cppstd
 from conan.tools.build.stdcpp_library import stdcpp_library
+from conan.tools.build.flags import cppstd_flag
diff --git a/conan/tools/build/flags.py b/conan/tools/build/flags.py
new file mode 100644
index 000000000..9cd9f0638
--- /dev/null
+++ b/conan/tools/build/flags.py
@@ -0,0 +1,16 @@
+from conan.tools._compilers import cppstd_flag as cppstd_flag_settings
+
+
+def cppstd_flag(conanfile):
+    """
+    Returns flags specific to the C++ standard based on the ``conanfile.settings.compiler``,
+    ``conanfile.settings.compiler.version`` and ``conanfile.settings.compiler.cppstd``.
+    It also considers when using GNU extension in ``settings.compiler.cppstd``, reflecting it in the
+    compiler flag. Currently, it supports GCC, Clang, AppleClang, MSVC, Intel, MCST-LCC.
+    In case there is no ``settings.compiler`` or ``settings.cppstd`` in the profile, the result will
+    be an **empty string**.
+    :param conanfile: The current recipe object. Always use ``self``.
+    :return: ``str`` with the standard C++ flag used by the compiler. e.g. "-std=c++11", "/std:c++latest"
+    """
+    settings = conanfile.settings
+    return cppstd_flag_settings(settings)
diff --git a/conan/tools/meson/meson.py b/conan/tools/meson/meson.py
index e16326afa..19515272e 100644
--- a/conan/tools/meson/meson.py
+++ b/conan/tools/meson/meson.py
@@ -1,8 +1,8 @@
 import os
 
 from conan.tools.build import build_jobs
-from conan.tools.meson.toolchain import MesonToolchain
 from conan.tools.meson.mesondeps import MesonDeps
+from conan.tools.meson.toolchain import MesonToolchain
 
 
 class Meson(object):
@@ -10,6 +10,9 @@ class Meson(object):
         self._conanfile = conanfile
 
     def configure(self, reconfigure=False):
+        if reconfigure:
+            self._conanfile.output.warning("reconfigure param has been deprecated."
+                                           " Removing in Conan 2.x.")
         source_folder = self._conanfile.source_folder
         build_folder = self._conanfile.build_folder
         cmd = "meson setup"
@@ -35,10 +38,8 @@ class Meson(object):
 
         cmd += "".join([f'{cmd_param} "{meson_option}"' for meson_option in meson_filenames])
         cmd += ' "{}" "{}"'.format(build_folder, source_folder)
-        if self._conanfile.package_folder:
-            cmd += ' -Dprefix="{}"'.format(self._conanfile.package_folder)
-        if reconfigure:
-            cmd += ' --reconfigure'
+        # Issue related: https://github.com/mesonbuild/meson/issues/12880
+        cmd += ' --prefix=/'  # this must be an absolute path, otherwise, meson complains
         self._conanfile.output.info("Meson configure cmd: {}".format(cmd))
         self._conanfile.run(cmd)
 
@@ -54,9 +55,10 @@ class Meson(object):
         self._conanfile.run(cmd)
 
     def install(self):
-        self.configure(reconfigure=True)  # To re-do the destination package-folder
-        meson_build_folder = self._conanfile.build_folder
-        cmd = 'meson install -C "{}"'.format(meson_build_folder)
+        meson_build_folder = self._conanfile.build_folder.replace("\\", "/")
+        meson_package_folder = self._conanfile.package_folder.replace("\\", "/")
+        # Assuming meson >= 0.57.0
+        cmd = f'meson install -C "{meson_build_folder}" --destdir "{meson_package_folder}"'
         self._conanfile.run(cmd)
 
     def test(self):


Test patch:
diff --git a/conans/test/unittests/client/build/cpp_std_flags_test.py b/conans/test/unittests/client/build/cpp_std_flags_test.py
index 1badf2c90..3c30e8371 100644
--- a/conans/test/unittests/client/build/cpp_std_flags_test.py
+++ b/conans/test/unittests/client/build/cpp_std_flags_test.py
@@ -1,9 +1,11 @@
 import unittest
 
 from conans.client.build.cppstd_flags import cppstd_default
-from conans.test.utils.mocks import MockSettings
+from conans.test.utils.mocks import MockSettings, ConanFileMock
 from conans.tools import cppstd_flag
 
+from conan.tools.build import cppstd_flag as cppstd_flag_conanfile
+
 
 def _make_cppstd_flag(compiler, compiler_version, cppstd=None, compiler_base=None):
     settings = MockSettings({"compiler": compiler,
@@ -399,3 +401,12 @@ class CompilerFlagsTest(unittest.TestCase):
         self.assertEqual(_make_cppstd_flag("mcst-lcc", "1.25", "14", "gcc"), "-std=c++14")
         self.assertEqual(_make_cppstd_flag("mcst-lcc", "1.25", "17", "gcc"), "-std=c++17")
         self.assertEqual(_make_cppstd_flag("mcst-lcc", "1.25", "20", "gcc"), "-std=c++2a")
+
+    def test_cppstd_flag_conanfile(self):
+        """The conan.tools.build.cppstd_flag should work when passing a ConanFile instance
+        """
+        conanfile = ConanFileMock()
+        conanfile.settings = MockSettings({"compiler": "gcc",
+                                          "compiler.version": "9",
+                                          "compiler.cppstd": "17"})
+        self.assertEqual(cppstd_flag_conanfile(conanfile), "-std=c++17")