conan-io / cmake-conan

CMake wrapper for conan C and C++ package manager
MIT License
821 stars 250 forks source link

[question] How to use cmake-conan with `CMakeToolchain` generator? #410

Open hwhsu1231 opened 2 years ago

hwhsu1231 commented 2 years ago

In Conan documentation, it says that we need to add -DCMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake in the command-line. That is, we have to generate the conan_toolchain.cmake file "before" we run cmake to configure the project.

However, with CMake-Conan, we use conan_cmake_configure() to generate conanfile.txt file, and use conan_cmake_install() to run conan install command. If we add CMakeToolchain generator in conan_cmake_configure(), the conan_toolchain.cmake file will be generated after the conan_cmake_install() is conducted.

It looks like the conan_toolchain.cmake file is generated "after" we run cmake executable.

How to deal with this situation?

# 'conanfile.txt' file is generated after this command
conan_cmake_configure(
  GENERATORS  CMakeToolchain  # Specify generator
  REQUIRES    fmt/8.0.0
  OPTIONS     fmt:shared=True
  IMPORTS     "bin, *.dll -> ../bin"
)

conan_cmake_autodetect(settings)

# 'conan_toolchain.cmake' file is generated after this command
conan_cmake_install(
  PATH_OR_REFERENCE "."
  INSTALL_FOLDER    "./conan"
  BUILD             missing
  REMOTE            conancenter
  SETTINGS          ${settings}
)
memsharded commented 2 years ago

This is kind of a chicken and egg problem, you cannot pass the toolchain because it doesnt exist yet. Unless you want to build a cmake flow that actually stops at the first pass after generating the conan stuff, and then it can use the toolchain at the second run, then, using the toolchain file is impossible by CMake behavior. Even if that is possible, then it is certainly not a conventional flow, and if you need to teach developers to do that, why not just letting them run a "conan install"?

In any case the conan_toolchain.cmake is mostly there to try go guarantee that your CMake configuration follows the Conan settings. As the goal of cmake-conan is the opposite, to obtain the Conan settings from the current CMake configuration, that renders the conan_toolchain.cmake mostly useles, because it should largely contain what is somewhat already defined in the current CMake run.

hwhsu1231 commented 2 years ago

@memsharded Are you saying,

It's "not" possible/reasonable/recommended to use CMake-Conan with CMakeToolchain generator?

lbckmnn commented 2 years ago

Can somebody clarify this? I would also like to use cmake-conan (but with an existing conanfile.py) because this would integrate with IDEs which doesn't have a good conan integration yet.

hwhsu1231 commented 2 years ago

Can somebody clarify this? I would also like to use cmake-conan (but with an existing conanfile.py) because this would integrate with IDEs which doesn't have a good conan integration yet.

@lbckmnn, If you want to use the existing conanfile.py, then you won't need to use conan_cmake_configure(). Because this command is used to generate a conanfile.txt recipe file.

If you want to use CMake-Conan, I suggest that you use CMakeDeps generator, instead. Because it seems that it has some problems with CMakeToolchain generator when using CMake-Conan.

hwhsu1231 commented 2 years ago

@czoido @memsharded

If CMakeToolchain generator is NOT appropriate to use with CMake-Conan, should it add some NOTES on README.md to tell users not to use it?

czoido commented 2 years ago

Hi @hwhsu1231, I think so, we will add a note about that in the docs. Also, please check we added a note about compatibility with Conan 2.0 in the README in develop branch.

hwhsu1231 commented 1 year ago

This CAE problem is probably solved by the solution that I proposed previously. (https://github.com/conan-io/cmake-conan/issues/449)

Project Link: https://github.com/hwhsu1231/cmake-conan-setup