From fe43a15b35c28ce460a95c4e2a8ba08f82628962 Mon Sep 17 00:00:00 2001 From: Luke Elliott Date: Sun, 12 Mar 2023 12:56:00 +0000 Subject: [PATCH] fix meson cpp_std for VS2019 and 2022 c++20. Note meson 1.0.1 is required for it to accept c++20 with clang-cl. --- conan/tools/build/flags.py | 10 +++++++--- conan/tools/meson/helpers.py | 15 ++++++++------- conan/tools/meson/toolchain.py | 6 +++++- .../unittests/client/build/cpp_std_flags_test.py | 3 ++- 4 files changed, 22 insertions(+), 12 deletions(-) diff --git a/conan/tools/build/flags.py b/conan/tools/build/flags.py index 2e378e01800..ddf92dfbf93 100644 --- a/conan/tools/build/flags.py +++ b/conan/tools/build/flags.py @@ -185,7 +185,7 @@ def cppstd_flag(settings): return flag -def _cppstd_msvc(visual_version, cppstd): +def cppstd_msvc_flag(visual_version, cppstd): # https://docs.microsoft.com/en-us/cpp/build/reference/std-specify-language-standard-version v14 = None v17 = None @@ -198,11 +198,15 @@ def _cppstd_msvc(visual_version, cppstd): if visual_version >= "191": v17 = "c++17" v20 = "c++latest" - if visual_version >= "193": + if visual_version >= "192": v20 = "c++20" + if visual_version >= "193": v23 = "c++latest" - flag = {"14": v14, "17": v17, "20": v20, "23": v23}.get(str(cppstd), None) + return {"14": v14, "17": v17, "20": v20, "23": v23}.get(str(cppstd), None) + +def _cppstd_msvc(visual_version, cppstd): + flag = cppstd_msvc_flag(visual_version, cppstd) return "/std:%s" % flag if flag else None diff --git a/conan/tools/meson/helpers.py b/conan/tools/meson/helpers.py index fc5231c9093..e3a4c4d96b1 100644 --- a/conan/tools/meson/helpers.py +++ b/conan/tools/meson/helpers.py @@ -1,3 +1,5 @@ +from conan.tools.build.flags import cppstd_msvc_flag + __all__ = ["to_meson_machine", "to_meson_value", "to_cppstd_flag"] # https://mesonbuild.com/Reference-tables.html#operating-system-names @@ -47,11 +49,6 @@ 'x86_64': ('x86_64', 'x86_64', 'little') } -_vs_cppstd_map = { - '14': "vc++14", - '17': "vc++17", - '20': "vc++latest" -} # Meson valid values # "none", "c++98", "c++03", "c++11", "c++14", "c++17", "c++1z", "c++2a", "c++20", @@ -102,14 +99,18 @@ def to_meson_value(value): # FIXME: Move to another more common module -def to_cppstd_flag(compiler, cppstd): +def to_cppstd_flag(compiler, compiler_version, cppstd): """Gets a valid cppstd flag. :param compiler: ``str`` compiler name. + :param compiler_version: ``str`` compiler version. :param cppstd: ``str`` cppstd version. :return: ``str`` cppstd flag. """ if compiler == "msvc": - return _vs_cppstd_map.get(cppstd) + # Meson's logic with 'vc++X' vs 'c++X' is possibly a little outdated. + # Presumably the intent is 'vc++X' is permissive and 'c++X' is not, + # but '/permissive-' is the default since 16.8. + return 'v%s' % cppstd_msvc_flag(compiler_version, cppstd) else: return _cppstd_map.get(cppstd) diff --git a/conan/tools/meson/toolchain.py b/conan/tools/meson/toolchain.py index 9ed22b466fe..5e90aba7933 100644 --- a/conan/tools/meson/toolchain.py +++ b/conan/tools/meson/toolchain.py @@ -122,8 +122,12 @@ def __init__(self, conanfile, backend=None): compiler = self._conanfile.settings.get_safe("compiler") if compiler is None: raise ConanException("MesonToolchain needs 'settings.compiler', but it is not defined") + compiler_version = self._conanfile.settings.get_safe("compiler.version") + if compiler_version is None: + raise ConanException("MesonToolchain needs 'settings.compiler.version', but it is not defined") + cppstd = self._conanfile.settings.get_safe("compiler.cppstd") - self._cpp_std = to_cppstd_flag(compiler, cppstd) + self._cpp_std = to_cppstd_flag(compiler, compiler_version, cppstd) if compiler == "msvc": vscrt = msvc_runtime_flag(self._conanfile) 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 f76c5593e1f..abe0593d4b2 100644 --- a/conans/test/unittests/client/build/cpp_std_flags_test.py +++ b/conans/test/unittests/client/build/cpp_std_flags_test.py @@ -235,6 +235,7 @@ def test_visual_cppstd_flags(self): self.assertEqual(_make_cppstd_flag("msvc", "170", "17"), None) self.assertEqual(_make_cppstd_flag("msvc", "180", "11"), None) + self.assertEqual(_make_cppstd_flag("msvc", "190", "14"), '/std:c++14') self.assertEqual(_make_cppstd_flag("msvc", "190", "17"), '/std:c++latest') @@ -244,7 +245,7 @@ def test_visual_cppstd_flags(self): self.assertEqual(_make_cppstd_flag("msvc", "191", "20"), '/std:c++latest') self.assertEqual(_make_cppstd_flag("msvc", "192", "17"), '/std:c++17') - self.assertEqual(_make_cppstd_flag("msvc", "192", "20"), '/std:c++latest') + self.assertEqual(_make_cppstd_flag("msvc", "192", "20"), '/std:c++20') self.assertEqual(_make_cppstd_flag("msvc", "193", "20"), '/std:c++20') self.assertEqual(_make_cppstd_flag("msvc", "193", "23"), '/std:c++latest')