diff --git a/conan/internal/api/detect_api.py b/conan/internal/api/detect_api.py index 156cae8a62a..4d66d9da9a8 100644 --- a/conan/internal/api/detect_api.py +++ b/conan/internal/api/detect_api.py @@ -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 diff --git a/conans/client/conf/detect.py b/conans/client/conf/detect.py index f42e18b5c7c..de2422836ad 100644 --- a/conans/client/conf/detect.py +++ b/conans/client/conf/detect.py @@ -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(): @@ -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")) diff --git a/conans/test/unittests/tools/build/test_cppstd.py b/conans/test/unittests/tools/build/test_cppstd.py index 9078e322ef7..b15973ca5ea 100644 --- a/conans/test/unittests/tools/build/test_cppstd.py +++ b/conans/test/unittests/tools/build/test_cppstd.py @@ -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 @@ -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) @@ -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"]), @@ -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 """ diff --git a/conans/test/unittests/util/detect_test.py b/conans/test/unittests/util/detect_test.py index ab18a5d7f0a..c9c03baf8e8 100644 --- a/conans/test/unittests/util/detect_test.py +++ b/conans/test/unittests/util/detect_test.py @@ -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