Skip to content

Commit

Permalink
[feature] Export cppstd_flag as public tool (#15710)
Browse files Browse the repository at this point in the history
* Use ConanFile for cppstd_flag

Signed-off-by: Uilian Ries <uilianries@gmail.com>

* expose cppstd_flag

Signed-off-by: Uilian Ries <uilianries@gmail.com>

* lazy import to break cyclic import

Signed-off-by: Uilian Ries <uilianries@gmail.com>

* centralize to_cppstd_flag

Signed-off-by: Uilian Ries <uilianries@gmail.com>

* Update method description

Signed-off-by: Uilian Ries <uilianries@gmail.com>

* improve method documentation

Signed-off-by: Uilian Ries <uilianries@gmail.com>

* Revert "centralize to_cppstd_flag"

This reverts commit a98b289.

---------

Signed-off-by: Uilian Ries <uilianries@gmail.com>
  • Loading branch information
uilianries authored Feb 21, 2024
1 parent 852c526 commit caf137e
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 15 deletions.
2 changes: 1 addition & 1 deletion conan/tools/apple/xcodetoolchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def generate(self):
@property
def _cppstd(self):
from conan.tools.build.flags import cppstd_flag
cppstd = cppstd_flag(self._conanfile.settings)
cppstd = cppstd_flag(self._conanfile)
if cppstd.startswith("-std="):
return cppstd[5:]
return cppstd
Expand Down
1 change: 1 addition & 0 deletions conan/tools/build/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import sys
from shlex import quote

from conan.tools.build.flags import cppstd_flag
from conan.tools.build.cppstd import check_max_cppstd, check_min_cppstd, \
valid_max_cppstd, valid_min_cppstd, default_cppstd, supported_cppstd
from conan.tools.build.cpu import build_jobs
Expand Down
23 changes: 18 additions & 5 deletions conan/tools/build/flags.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from conan.tools.apple.apple import _to_apple_arch
from conans.model.version import Version


Expand All @@ -7,6 +6,7 @@ def architecture_flag(settings):
returns flags specific to the target architecture and compiler
Used by CMakeToolchain and AutotoolsToolchain
"""
from conan.tools.apple.apple import _to_apple_arch
compiler = settings.get_safe("compiler")
arch = settings.get_safe("arch")
the_os = settings.get_safe("os")
Expand Down Expand Up @@ -165,10 +165,23 @@ def build_type_flags(settings):
return []


def cppstd_flag(settings):
compiler = settings.get_safe("compiler")
compiler_version = settings.get_safe("compiler.version")
cppstd = settings.get_safe("compiler.cppstd")
def cppstd_flag(conanfile) -> str:
"""
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"
"""
compiler = conanfile.settings.get_safe("compiler")
compiler_version = conanfile.settings.get_safe("compiler.version")
cppstd = conanfile.settings.get_safe("compiler.cppstd")

if not compiler or not compiler_version or not cppstd:
return ""
Expand Down
2 changes: 1 addition & 1 deletion conan/tools/gnu/autotoolstoolchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def __init__(self, conanfile, namespace=None, prefix="/"):
self.build_type_flags = build_type_flags(self._conanfile.settings)
self.build_type_link_flags = build_type_link_flags(self._conanfile.settings)

self.cppstd = cppstd_flag(self._conanfile.settings)
self.cppstd = cppstd_flag(self._conanfile)
self.arch_flag = architecture_flag(self._conanfile.settings)
self.libcxx, self.gcc_cxx11_abi = libcxx_flags(self._conanfile)
self.fpic = self._conanfile.options.get_safe("fPIC")
Expand Down
2 changes: 1 addition & 1 deletion conan/tools/google/toolchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def __init__(self, conanfile):
#: String used to add --dynamic_mode=["fully"|"off"]. Depends on self.options.shared value.
self.dynamic_mode = "fully" if shared else "off"
#: String used to add --cppstd=[FLAG]. Depends on your settings.
self.cppstd = cppstd_flag(self._conanfile.settings)
self.cppstd = cppstd_flag(self._conanfile)
#: List of flags used to add --copt=flag1 ... --copt=flagN
self.copt = []
#: List of flags used to add --conlyopt=flag1 ... --conlyopt=flagN
Expand Down
2 changes: 1 addition & 1 deletion conan/tools/microsoft/nmaketoolchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def _cl(self):
cflags.extend(self.extra_cflags)

cxxflags = []
cppstd = cppstd_flag(self._conanfile.settings)
cppstd = cppstd_flag(self._conanfile)
if cppstd:
cxxflags.append(cppstd)
cxxflags.extend(self._conanfile.conf.get("tools.build:cxxflags", default=[], check_type=list))
Expand Down
9 changes: 5 additions & 4 deletions conans/test/unittests/client/build/cpp_std_flags_test.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import unittest

from conan.internal.api.detect_api import default_cppstd
from conan.tools.build.flags import cppstd_flag
from conan.tools.build import cppstd_flag
from conans.model.version import Version
from conans.test.utils.mocks import MockSettings
from conans.test.utils.mocks import MockSettings, ConanFileMock


def _make_cppstd_flag(compiler, compiler_version, cppstd=None):
settings = MockSettings({"compiler": compiler,
conanfile = ConanFileMock()
conanfile.settings = MockSettings({"compiler": compiler,
"compiler.version": compiler_version,
"compiler.cppstd": cppstd})
return cppstd_flag(settings)
return cppstd_flag(conanfile)


def _make_cppstd_default(compiler, compiler_version):
Expand Down
5 changes: 3 additions & 2 deletions conans/test/unittests/tools/intel/test_intel_cc.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,16 @@ def test_architecture_flag_if_intel_cc(os_, arch, expected):
("gnu23", "gnu++2b"),
])
def test_cppstd_flag_if_intel_cc(cppstd, flag):
settings = MockSettings({
conanfile = ConanFileMock()
conanfile.settings = MockSettings({
"compiler": "intel-cc",
"compiler.version": "2021.3",
"compiler.mode": "classic",
"arch": "x86_64",
"os": "Linux",
"compiler.cppstd": cppstd
})
assert cppstd_flag(settings) == "-std=%s" % flag
assert cppstd_flag(conanfile) == "-std=%s" % flag


@pytest.mark.parametrize("mode", ["icx", "dpcpp"])
Expand Down

0 comments on commit caf137e

Please sign in to comment.