conan-io / conan

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

[question] What is the replacement to the `CMake.definitions` list in Conan 2? #16495

Closed PauloCarvalhoRJ closed 1 week ago

PauloCarvalhoRJ commented 1 week ago

Hello,

So, I'm migrating recipes from Conan 1 to Conan 2 and I have code like this that used to work (in build() method):

cmake.definitions["WITH_MINIZIP"] = True

However, in Conan 2, I get this error:

AttributeError: 'CMake' object has no attribute 'definitions'

Question is: How do I define CMake variables in Conan 2?

thanks,

PC

Have you read the CONTRIBUTING guide?

memsharded commented 1 week ago

Hi @PauloCarvalhoRJ

Thanks for your question.

It is the CMakeToolchain.preprocessor_definitions: https://docs.conan.io/2/reference/tools/cmake/cmaketoolchain.html#preprocessor-definitions for preprocessor definitions

It is CMakeToolchain.cache_variables: https://docs.conan.io/2/reference/tools/cmake/cmaketoolchain.html#cache-variables for CMake variables stored in CMakePresets.json (passed in command line)

It is CMakeToolchain.variables: https://docs.conan.io/2/reference/tools/cmake/cmaketoolchain.html#variables, for CMake variables to be defined in the conan_toolchain.cmake.

All of them defined in the generate() method.

PauloCarvalhoRJ commented 1 week ago

Hello, @memsharded ,

The new improved API is becoming clearer now. Thanks for the pointers and the further reading.

regards,

PC

memsharded commented 1 week ago

The new improved API is becoming clearer now. Thanks for the pointers and the further reading.

Yes, there is a much more clear separation of concerns now, with the generate() and generators defining files, and build() with the helpers just executing them. This way it is also possible to achieve exactly the same build with calling cmake ... than when calling build(), which is very important and it was not possible with Conan 1 legacy approach.

PauloCarvalhoRJ commented 1 week ago

True. So far, the API elements seem to have better defined roles than in Conan 1. Congratulations.