question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[question] CMakeToolchain: When use cached_variables vs variables

See original GitHub issue

The documentation only explains CMakeToolchain.variables and CMakeToolchain.cached_variables features, one is better for multiple configuration and another is single configuration:

https://docs.conan.io/en/latest/reference/conanfile/tools/cmake/cmaketoolchain.html#variables

But it’s not clear when each one should be used, based on real examples.

So far, we are migrating recipes in CCI and there is a doubt about which one should be adopted by default when using CMakeToolchain.

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
SpaceImcommented, Aug 23, 2022
  • CMakeToolchain.variables are variables that are defined directly in the generated conan_toolchain.cmake file (with set(VAR_NAME "var_value")

From what I’ve seen, they are passed as set(VAR_NAME "var_value" CACHE <type> "Variable VAR_NAME conan-toolchain defined") (https://github.com/conan-io/docs/issues/2689). So the new type cache_variables is quite misleading (it’s cache_variables in CMakePresets terminology AFAIK). If they were not passed as CACHE variables, they couldn’t override options until CMake >=3.13 policies. It’s what happens to BUILD_SHARED_LIBS: https://github.com/conan-io/conan/issues/11840

From my experimentations:

  • CMAKE_POLICY_DEFAULT_CMPxxxx must be passed as cache_variables.
  • Anything else can be passed as variables.
  • if BUILD_SHARED_LIBS or CMAKE_POSITION_INDEPENDENT_CODE are turned as option() in upstream CMakeLists and cmake_minimum_required() is lower than 3.13 , you have to either:
  • if BUILD_SHARED_LIBS or CMAKE_POSITION_INDEPENDENT_CODE are turned as CACHE variables in upstream CMakeLists, you have to pass BUILD_SHARED_LIBS/CMAKE_POSITION_INDEPENDENT_CODE as variables (or cache_variables).
  • if upstream CMakeLists is illformed, things can go wrong, specially for variables (and it may not be obvious to see it), for example:
    • cmake_minimum_required() missing.
    • project() before cmake_minimum_required().
    • option() or CACHE variables between cmake_minimum_required() & project() (pernicious, you may not see that recipe options injected through conan toolchain are not honored).
0reactions
uilianriescommented, Aug 23, 2022

@jwillikers Sorry! 😓

Read more comments on GitHub >

github_iconTop Results From Across the Web

Check CMake Cache Variable in Toolchain File - Stack Overflow
Problem : CMake runs toolchain files multiple times, but can't read cache variables on some runs. # Workaround: On first run (in which...
Read more >
Docs should mention that CMAKE_TOOLCHAIN_FILE does ...
I am unsure if this is intended behavior. When setting the CMAKE_SYSROOT variable in a Toolchain file, cache variables defined inside the ...
Read more >
Variables and the Cache · Modern CMake
Variables and the Cache. Local Variables. We will cover variables first. A local variable is set like this: set(MY_VARIABLE "value"). The names of...
Read more >
Configure and build with CMake Presets in Visual Studio
Use these files to drive CMake in Visual Studio and Visual Studio Code, in a ... Environment variables set in a Configure Preset...
Read more >
System environment variable reading in CMakeSettings.json ...
when I want to read this variable in CMakeSettings.json Visual Studio does not retrieve this value and generating of CMake cache is hung....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found