conan-io / conan

Conan - The open-source C and C++ package manager
https://conan.io
MIT License
7.95k stars 951 forks source link

[bug] detect_api.detect_clang_compiler(compiler_exe="clang") returns an incorrect version #16476

Open Todiq opened 2 weeks ago

Todiq commented 2 weeks ago

Describe the bug

version: 2.4.1 conan_path: D:\venv\Scripts\conan python version: 3.9.13 sys_version: 3.9.13 (tags/v3.9.13:6de2ca5, May 17 2022, 16:36:42) [MSC v.1929 64 bit (AMD64)] sys_executable: D:\venv\Scripts\python.exe is_frozen: False architecture: AMD64 system version: 10.0.22631 platform: Windows-10-10.0.22631-SP0 system: Windows release: 10 cpu: Intel64 Family 6 Model 186 Stepping 3, GenuineIntel

How to reproduce it

Hello,

I created the following profile in order to try auto-detecting the clang version installed with VS.

{% set compiler, version, compiler_exe = detect_api.detect_default_compiler() %}
{% set runtime, _ = detect_api.default_msvc_runtime(compiler) %}
{% set clang, clang_version, clang_exe = detect_api.detect_clang_compiler(compiler_exe="clang") %}
{% set os = detect_api.detect_os() %}

[settings]
arch={{detect_api.detect_arch()}}
build_type=Release
compiler=msvc
compiler.cppstd=14
compiler.runtime=dynamic
compiler.version=194
os={{os}}

myproj*/*:compiler={{clang}}
myproj*/*:compiler.cppstd=17
myproj*/*:compiler.version={{clang_version}}
myproj*/*:compiler.runtime_version=v144

[conf]
myproj*/*:tools.build:compiler_executables = {"c": "clang-cl", "cpp": "clang-cl", "rc": "llvm-rc", "ar": "llvm-ar", "ld": "ld.lld"}
myproj*/*:tools.cmake.cmake_layout:build_folder_vars = ["settings.os", "settings.compiler", "settings.compiler.version", "settings.arch"]
myproj*/*:tools.build:cxxflags=["-fansi-escape-codes -fcolor-diagnostics"]
myproj*/*:tools.cmake.cmaketoolchain:generator=Ninja Multi-Config

{% if os == "Windows" %}
    myproj*/*:tools.env.virtualenv:powershell=True
{% else %}
    myproj*/*:tools.build:cxxflags+=["/winsdkdir /opt/win-sdk/sdk /vctoolsdir /opt/win-sdk/crt"]
    [buildenv]
    myproj*/*:LDFLAGS=/winsdkdir:/opt/win-sdk/sdk /vctoolsdir:/opt/win-sdk/crt /MANIFEST:NO
{% endif %}

Running a conan install ... returns:

Using lockfile: 'D:\core\conan.lock'
detect_api: Found msvc 17
detect_api: Found clang 17.0
detect_api: Found msvc 17
detect_api: Found clang 17.0

======== Input profiles ========
Profile host:
[settings]
arch=x86_64
build_type=Release
compiler=msvc
compiler.cppstd=14
compiler.runtime=dynamic
compiler.runtime_type=Release
compiler.version=194
os=Windows
myproj*/*:compiler=clang
myproj*/*:compiler.cppstd=17
myproj*/*:compiler.runtime_version=v144
myproj*/*:compiler.version=17.0
[conf]
myproj*/*:tools.build:compiler_executables={'c': 'clang-cl', 'cpp': 'clang-cl', 'rc': 'llvm-rc', 'ar': 'llvm-ar', 'ld': 'ld.lld'}
myproj*/*:tools.build:cxxflags=['-fansi-escape-codes -fcolor-diagnostics']
myproj*/*:tools.cmake.cmake_layout:build_folder_vars=['settings.os', 'settings.compiler', 'settings.compiler.version', 'settings.arch']
myproj*/*:tools.cmake.cmaketoolchain:generator=Ninja Multi-Config
myproj*/*:tools.env.virtualenv:powershell=True

Profile build:
[settings]
arch=x86_64
build_type=Release
compiler=msvc
compiler.cppstd=14
compiler.runtime=dynamic
compiler.runtime_type=Release
compiler.version=194
os=Windows
myproj*/*:compiler=clang
myproj*/*:compiler.cppstd=17
myproj*/*:compiler.runtime_version=v144
myproj*/*:compiler.version=17.0
[conf]
myproj*/*:tools.build:compiler_executables={'c': 'clang-cl', 'cpp': 'clang-cl', 'rc': 'llvm-rc', 'ar': 'llvm-ar', 'ld': 'ld.lld'}
myproj*/*:tools.build:cxxflags=['-fansi-escape-codes -fcolor-diagnostics']
myproj*/*:tools.cmake.cmake_layout:build_folder_vars=['settings.os', 'settings.compiler', 'settings.compiler.version', 'settings.arch']
myproj*/*:tools.cmake.cmaketoolchain:generator=Ninja Multi-Config
myproj*/*:tools.env.virtualenv:powershell=True

ERROR: Invalid setting '17.0' is not a valid 'settings.compiler.version' value.
Possible values are ['3.3', '3.4', '3.5', '3.6', '3.7', '3.8', '3.9', '4.0', '5.0', '6.0', '7.0', '7.1', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18']
Read "http://docs.conan.io/2/knowledge/faq.html#error-invalid-setting"

I ran the command into the Developer Powershell for VS 2022, otherwise clang_version is evaluated to None (expected).

Would mind having a look? Thanks in advance!

memsharded commented 2 weeks ago

Hi @Todiq

I think this is intended behavior of the API. One api is to detect the full version of clang, in your case is 17.0.

Then, Conan default settings might not include certain versions. To map full compiler versions to the actual versions, there is another api called default_compiler_version, that can be used:

compiler.version={{detect_api.default_compiler_version(compiler, version)}}

This way the api gives power to users that want the full clang version, while allowing to also define the predefined settings.yml default versions with the default Conan compatibility.

Check https://docs.conan.io/2/reference/config_files/profiles.html for more details.

Todiq commented 2 weeks ago

Hello @memsharded,

Thanks for your answer. If the API's goal is to detect the full version, shouldn't it then also add the minor version? I am currently running clang 17.0.3, not 17.0.

compiler.version={{detect_api.default_compiler_version(compiler, version)}}

Thanks for the hint, but I cannot use that as is. In fact, when there are more than one compiler installed, I can't be sure of what the result would be. In my case, it returns msvc's version, which is 194.

I guess there would be a way in Jinja2's syntax to remove the .0 of 17.0?

memsharded commented 2 weeks ago

Thanks for the hint, but I cannot use that as is. In fact, when there are more than one compiler installed, I can't be sure of what the result would be. In my case, it returns msvc's version, which is 194.

But you can call it with default_compiler_version(clang, clang_version) from your code above:

{% set clang, clang_version, clang_exe = detect_api.detect_clang_compiler(compiler_exe="clang") %}
{% set clang_final_version = default_compiler_version(clang, clang_version) %}
...
myproj*/*:compiler.version = {{clang_final_version}}

Thanks for your answer. If the API's goal is to detect the full version, shouldn't it then also add the minor version? I am currently running clang 17.0.3, not 17.0.

To be accurate, Conan default settings define in some cases, as gcc, both major and major+minor versions:

"10", "10.1", "10.2", "10.3", "10.4", "10.5",
"11", "11.1", "11.2", "11.3", "11.4",

If users want to chose which one of the defaults they use, they can use the one with the minor, or the one returned by default_compiler_version. It is true that we haven't seen anyone so far that wanted to model down to the patch version of the compiler.