conan-io / docs

conan.io reStructuredText documentation
http://docs.conan.io
MIT License
110 stars 361 forks source link

WITH_FMT not apparent anywhere in the project - tutorial creating_packages/preparing_the_build #3778

Closed markspacehub closed 5 months ago

markspacehub commented 5 months ago

Following the tutorial for creating_packages/preparing_the_build In the conanfile.py generate(): def generate(self): tc = CMakeToolchain(self) if self.options.with_fmt: tc.variables["WITH_FMT"] = True tc.generate()

The tutorial states "We inject the WITH_FMT variable with the value True to the CMakeToolchain so that we can use it in the CMakeLists.txt of the hello library to add the CMake fmt::fmt target conditionally." However I cannot find WITH_FMT anywhere in the entire project at all. When I do conan create . --build=missing -o with_fmt=True and conan create . --build=missing -o with_fmt=False I get the correct output of the program, however nowhere is WITH_FMT apparent (no files or in the terminal output). Clearly it must be set somewhere, otherwise the CMake file that is built would be upset.

Where is WITH_FMT set - is it in a cache file outside of the project? Should the tutorial be updated to state that it is not injected into the toolchain?

(A side note - From my CMake experiece I have picked up that the toolchain might not be the best place to put options, rather than only platform specific variables. Places like CMakePresets are better for this. Conan does seem to generate CMakePresets, but they do not appear anywhere in the terminal when invoking conan create .. Are the presets a new feature that havent been fully embedded yet?)

memsharded commented 5 months ago

Hi @markspacehub

Thanks for reporting.

I think there might be something missing in the examples2 repo, that doc page says:

The optional_fmt branch contains some changes in the code. Let’s see what changed on the CMake side:

But I cannot find that optional_fmt branch in the repo anymore.

I will check what happened here.

czoido commented 5 months ago

Hi @markspacehub,

Thanks for the question, do you mean that you don't find the generated file between the local files, after doing a conan create?

That is because those files are not in the local folder but in the Conan cache, that CMake variable is set while building the library and that generator instantiated in the conanfile will produce files in the local cache. If you go to the local cache (this is just to clarify, normally you don't have to check anything in the Conan cache manually) you will see those generated files in the generators folder. I'm pasting here an screenshot of my local cache:

image

and there, in that conan_toolchain.cmake you will find declared:

set(WITH_FMT ON CACHE BOOL "Variable WITH_FMT conan-toolchain defined")

If you want to try this yourself you can get the exact location in the cache by using the conan cache path command, something similar to:

# get built packages for the latest recipe revision
$ conan list "hello/1.0#latest:*" --format=compact           
Local Cache
  hello/1.0
    hello/1.0#cd10061ee0eac2b19ef81d04b5d15d60%1718732732.3528068 (2024-06-18 17:45:32 UTC)
      hello/1.0#cd10061ee0eac2b19ef81d04b5d15d60:c5a9bae2e169805066b9024fa408a2becc86a608
        settings: Macos, x86_64, Release, apple-clang, gnu17, libc++, 13
        options: fPIC=True, shared=False, with_fmt=True
        requires: fmt/8.1.Z

# copy the reference with the recipe revision and package id and get the folder where the library was built inside the cache
$ conan cache path hello/1.0#cd10061ee0eac2b19ef81d04b5d15d60:c5a9bae2e169805066b9024fa408a2becc86a608 --folder=build
/Users/barbarian/.conan2/p/b/hello3232f23e3462a/b

Then go to /Users/barbarian/.conan2/p/b/hello3232f23e3462a/b and check the build/Release/generators folder Hope this helps, please do not hesitate to ask if you have any more questions about this.

markspacehub commented 5 months ago

I think I get it @czoido and I have found the cmake toolchain file with that in. To try and summarise : In this tutorial, hello is built as a package for use locally, and then tested with test_package. Because it is built as a package to use, it is stored in the cache. I was looking through the project files; including all the stuff found in test_package which is using the hello library, but wouldnt contain the build option (WITH_FMT) as it is just using it!

Thanks for the clarification - I think the whole inject into the toolchain file combined with there being a toolchain file generated (albeit in the testpackage confused me!

markspacehub commented 5 months ago

@memsharded I have found the branch here: https://github.com/conan-io/libhello/blob/optional_fmt/CMakeLists.txt I assume this is correct?

czoido commented 5 months ago

I think I get it @czoido and I have found the cmake toolchain file with that in. To try and summarise : In this tutorial, hello is built as a package for use locally, and then tested with test_package. Because it is built as a package to use, it is stored in the cache. I was looking through the project files; including all the stuff found in test_package which is using the hello library, but wouldnt contain the build option (WITH_FMT) as it is just using it!

Thanks for the clarification - I think the whole inject into the toolchain file combined with there being a toolchain file generated (albeit in the testpackage confused me!

That's correct! I'm happy to see that is clear now. Thanks a lot for taking the time to do the tutorial and please ask anything that comes up in your journey while learning Conan. I'm closing this as I consider it's closed but feel free to open if you have any more questions.