conan-io / conan

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

[question] Difference of behavior on CMake from conans vs CMake from conan.tools.cmake #16787

Open diogomatos3 opened 1 month ago

diogomatos3 commented 1 month ago

What is your question?

Hi community,

I have a legacy conan package, that provides the tools for build and the respective CMake toolchain file in the root directory, inside cmake folder. In short the structure of the package is like this:

This package is referred in the conan profile in build_requires section

What I observed is that, if I use:

But instead if I use:

What would be the recomended way to still be able to "detect" the existing cmake_toolchain.cmake from the conan package that provides the tooling? Or this is not really recommended?

Thanks in advance!

Have you read the CONTRIBUTING guide?

AbrilRBS commented 1 month ago

Hi @diogomatos3 - the issue here is that you're mixing old and new integrations, those shown in https://docs.conan.io/1/integrations/build_system/cmake.html and https://docs.conan.io/1/reference/build_helpers/cmake.html (The from conans import CMake). The new integrations, meant to be used from Conan 1 and 2, listed in https://docs.conan.io/1/reference/conanfile/tools/cmake.html#conan-tools-cmake work in a differnt way, and porting to their usage requires some work, but is the recommended approach (Both in Conan 1, and required in Conan 2, as the old ones have been dropped)

Hope this helps, feel free to ask any folloup questions if you have any :)

diogomatos3 commented 1 month ago

Hi @AbrilRBS

Thanks a lot for the clarification!

Could you help me to understand the right way to proceed? I have on my system several compiler versions. In the conan profile, I use buildenv [buildenv] CC=aarch64-linux-gnu-gcc-$CONAN_SETTINGS_COMPILER_MAJOR_VERSION CXX=aarch64-linux-gnu-g++-$CONAN_SETTINGS_COMPILER_MAJOR_VERSION

And on my conan recipe, I use from conan import ConanFile from conan.tools.cmake import CMakeToolchain, CMakeDeps, cmake_layout from conan.tools.env import VirtualRunEnv

And as generators, I use: generators = "cmake", "VirtualRunEnv"

But still, conan takes the default compiler version of the ubuntu, and not the one specified in the conan profile.

What is the right way to be able to select a specific compile version?

Thanks in advance.

memsharded commented 1 month ago

Hi @diogomatos3

But still, conan takes the default compiler version of the ubuntu, and not the one specified in the conan profile.

In general the definition of a given compiler or compiler version in the profile [settings] is not a request to configure the environment, it is just an information to Conan about "this is my current environment, this is the compiler I will use". If this is not the default system compiler, you might need to do extra configuration. There are 2 common ways, both in the profile too:

[buildenv]
CC=(path)/path/to/mycmpiler
CXX=(path)/path/to/mycompiler++
...

To set environment variables. Check the docs for [buildenv]

There is also a specific conf for this, because some build systems do not recognized env-vars, so this conf can help:

[conf]
tools.build:compiler_executables={"CC": "/path/to/compiler", "CXX": ...}
memsharded commented 3 weeks ago

What is the right way to be able to select a specific compile version?

If you specified the environment in the [buildenv] it should be applied automatically. But the legacy integrations like cmake might not work correctly. You need to use the new generators = "CMakeToolchain", "CMakeDeps" integrations. I'd suggest to try your profile with a conan new mypkg/0.1 -m cmake_lib template, which is a simple and modern recipe with CMake.

Please let us know if it works.