Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve cc version detection #16362

Merged
merged 2 commits into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion conan/internal/api/detect_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,10 @@ def _cc_compiler(compiler_exe="cc"):
if ret != 0:
return None, None, None
compiler = "clang" if "clang" in out else "gcc"
installed_version = re.search(r"([0-9]+(\.[0-9])?)", out).group()
# clang and gcc have version after a space, first try to find that to skip extra numbers
# that might appear in the first line of the output before the version
installed_version = re.search(r" ([0-9]+(\.[0-9])+)", out).group()
installed_version = installed_version or re.search(r"([0-9]+(\.[0-9])?)", out).group()
if installed_version:
ConanOutput(scope="detect_api").info("Found cc=%s-%s" % (compiler, installed_version))
return compiler, Version(installed_version), compiler_exe
Expand Down
16 changes: 16 additions & 0 deletions test/unittests/util/detect_test.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import unittest

import mock
from unittest.mock import patch
import pytest
from parameterized import parameterized

from conan.internal.api.detect_api import _cc_compiler
from conans.client.conf.detect import detect_defaults_settings
from conans.model.version import Version
from conan.test.utils.mocks import RedirectedTestOutput
Expand Down Expand Up @@ -56,3 +59,16 @@ def test_detect_clang_gcc_toolchain(self, _):
with environment_update({"CC": "clang-9 --gcc-toolchain=/usr/lib/gcc/x86_64-linux-gnu/9"}):
detect_defaults_settings()
self.assertIn("CC and CXX: clang-9 --gcc-toolchain", output)


@pytest.mark.parametrize("version_return,expected_version", [
["cc.exe (Rev3, Built by MSYS2 project) 14.1.0", "14.1.0"],
["g++ (Conan-Build-gcc--binutils-2.42) 14.1.0", "14.1.0"],
["clang version 18.1.0rc (https://github.com/llvm/llvm-project.git 461274b81d8641eab64d494accddc81d7db8a09e)", "18.1.0"],
AbrilRBS marked this conversation as resolved.
Show resolved Hide resolved
["cc.exe (Rev3, Built by MSYS2 project) 14.0", "14.0"],
])
@patch("conan.internal.api.detect_api.detect_runner")
def test_detect_cc_versionings(detect_runner_mock, version_return, expected_version):
AbrilRBS marked this conversation as resolved.
Show resolved Hide resolved
detect_runner_mock.return_value = 0, version_return
compiler, installed_version, compiler_exe = _cc_compiler()
assert installed_version == Version(expected_version)