cmake-conan icon indicating copy to clipboard operation
cmake-conan copied to clipboard

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

Open hwhsu1231 opened this issue 3 years ago • 7 comments

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}
)

hwhsu1231 avatar Apr 11 '22 11:04 hwhsu1231

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.

memsharded avatar Apr 11 '22 21:04 memsharded

@memsharded Are you saying,

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

hwhsu1231 avatar Apr 12 '22 06:04 hwhsu1231

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 avatar Apr 19 '22 09:04 lbckmnn

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 avatar Apr 19 '22 13:04 hwhsu1231

@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?

hwhsu1231 avatar May 25 '22 04:05 hwhsu1231

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.

czoido avatar May 25 '22 05:05 czoido

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

hwhsu1231 avatar Oct 31 '22 05:10 hwhsu1231