Skip to content

Commit

Permalink
Improve cc version detection (#16362)
Browse files Browse the repository at this point in the history
* Improve cc version detection

* Add more cases
  • Loading branch information
AbrilRBS authored Jun 3, 2024
1 parent 737f9c4 commit ff23b2b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
11 changes: 9 additions & 2 deletions conan/internal/api/detect_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -438,8 +438,15 @@ 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()
if installed_version:
# 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)
# Try only major but with spaces next
installed_version = installed_version or re.search(r" ([0-9]+(\.[0-9])?)", out)
# Fallback to the first number we find optionally followed by other version fields
installed_version = installed_version or re.search(r"([0-9]+(\.[0-9])?)", out)
if installed_version and installed_version.group():
installed_version = installed_version.group()
ConanOutput(scope="detect_api").info("Found cc=%s-%s" % (compiler, installed_version))
return compiler, Version(installed_version), compiler_exe
except (Exception,): # to disable broad-except
Expand Down
18 changes: 18 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,18 @@ 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"],
["cc.exe (Rev3, Built by MSYS2 project) 14.0", "14.0"],
["clang version 18 (https://github.com/llvm/llvm-project.git 461274b81d8641eab64d494accddc81d7db8a09e)", "18"],
["cc.exe (Rev3, Built by MSYS2 project) 14", "14"],
])
@patch("conan.internal.api.detect_api.detect_runner")
def test_detect_cc_versionings(detect_runner_mock, version_return, expected_version):
detect_runner_mock.return_value = 0, version_return
compiler, installed_version, compiler_exe = _cc_compiler()
assert installed_version == Version(expected_version)

0 comments on commit ff23b2b

Please sign in to comment.