Skip to content

Commit

Permalink
Backport cppstd_flag from Conan 2.x (conan-io#15731)
Browse files Browse the repository at this point in the history
Add wrapper for cppstd_std with conanfile

Signed-off-by: Uilian Ries <[email protected]>
  • Loading branch information
uilianries authored Feb 22, 2024
1 parent 1763159 commit f71f2d1
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
1 change: 1 addition & 0 deletions conan/tools/build/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
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
16 changes: 16 additions & 0 deletions conan/tools/build/flags.py
Original file line number Diff line number Diff line change
@@ -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)
13 changes: 12 additions & 1 deletion conans/test/unittests/client/build/cpp_std_flags_test.py
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -399,3 +401,12 @@ def test_mcst_lcc_cppstd_flag(self):
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")

0 comments on commit f71f2d1

Please sign in to comment.