Skip to content

Commit

Permalink
Fix wrong cppstd detection for newer apple-clang compilers (#14837)
Browse files Browse the repository at this point in the history
* Fix wrong cppstd detection for newer apple-clang compilers

* Update test to cover real detection

* Separate detect_cppstd from default_cppstd
  • Loading branch information
AbrilRBS authored Sep 28, 2023
1 parent 844f112 commit 99d74a8
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 16 deletions.
10 changes: 10 additions & 0 deletions conan/internal/api/detect_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,16 @@ def _mcst_lcc_cppstd_default(version):
return default


def detect_cppstd(compiler, compiler_version):
cppstd = default_cppstd(compiler, compiler_version)
if compiler == "apple-clang" and compiler_version >= "11":
# Conan does not detect the default cppstd for apple-clang,
# because it's still 98 for the compiler (eben though xcode uses newer in projects)
# and having it be so old would be annoying for users
cppstd = "gnu17"
return cppstd


def detect_compiler():
"""
find the default compiler on the build machine
Expand Down
4 changes: 2 additions & 2 deletions conans/client/conf/detect.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from conan.api.output import ConanOutput
from conan.internal.api.detect_api import detect_os, detect_arch, default_msvc_runtime, \
detect_libcxx, default_cppstd, detect_compiler, default_compiler_version
detect_libcxx, detect_cppstd, detect_compiler, default_compiler_version


def detect_defaults_settings():
Expand Down Expand Up @@ -31,7 +31,7 @@ def detect_defaults_settings():
libcxx = detect_libcxx(compiler, version)
if libcxx:
result.append(("compiler.libcxx", libcxx))
cppstd = default_cppstd(compiler, version)
cppstd = detect_cppstd(compiler, version)
if cppstd:
result.append(("compiler.cppstd", cppstd))
result.append(("build_type", "Release"))
Expand Down
34 changes: 20 additions & 14 deletions conans/test/unittests/tools/build/test_cppstd.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import pytest

from conan.internal.api.detect_api import detect_cppstd
from conan.tools.build import supported_cppstd, check_min_cppstd, valid_min_cppstd, default_cppstd
from conans.errors import ConanException, ConanInvalidConfiguration
from conans.model.version import Version
from conans.test.utils.mocks import MockSettings, ConanFileMock


Expand All @@ -25,6 +27,23 @@ def test_supported_cppstd_clang(compiler, compiler_version, values):
assert sot == values


@pytest.mark.parametrize("compiler,compiler_version,result", [
("gcc", "5", 'gnu98'),
("gcc", "8", "gnu14"),
("gcc", "12", "gnu17"),
("msvc", "190", "14"),
("apple-clang", "10.0", "gnu98"),
# We diverge from the default cppstd for apple-clang >= 11
("apple-clang", "12.0", "gnu17"),
("clang", "4", "gnu98"),
("clang", "6", "gnu14"),
("clang", "17", "gnu17")
])
def test_detected_cppstd(compiler, compiler_version, result):
sot = detect_cppstd(compiler, Version(compiler_version))
assert sot == result


def test_supported_cppstd_with_specific_values():
settings = MockSettings({})
conanfile = ConanFileMock(settings)
Expand Down Expand Up @@ -104,6 +123,7 @@ def test_supported_cppstd_mcst(compiler, compiler_version, values):
sot = supported_cppstd(conanfile)
assert sot == values


@pytest.mark.parametrize("compiler,compiler_version,values", [
("qcc", "4.4", ['98', 'gnu98']),
("qcc", "5.4", ['98', 'gnu98', '11', 'gnu11', "14", "gnu14", "17", "gnu17"]),
Expand All @@ -116,20 +136,6 @@ def test_supported_cppstd_qcc(compiler, compiler_version, values):
assert sot == values


@pytest.mark.parametrize("compiler,compiler_version,result", [
("gcc", "5", 'gnu98'),
("gcc", "8", "gnu14"),
("msvc", "190", "14"),
("apple-clang", "12.0", "gnu98"),
("clang", "6", "gnu14"),
])
def test_default_cppstd_gcc(compiler, compiler_version, result):
settings = MockSettings({"compiler": compiler, "compiler.version": compiler_version})
conanfile = ConanFileMock(settings)
sot = default_cppstd(conanfile)
assert sot == result


def test_check_cppstd_type():
""" cppstd must be a number
"""
Expand Down
1 change: 1 addition & 0 deletions conans/test/unittests/util/detect_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from parameterized import parameterized

from conan.internal.api.detect_api import detect_cppstd
from conans.client.conf.detect import detect_defaults_settings
from conans.model.version import Version
from conans.test.utils.mocks import RedirectedTestOutput
Expand Down

0 comments on commit 99d74a8

Please sign in to comment.