Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CMake-gui toggle visibility of variable

Tags:

cmake

I have a CMake setup where the accessibilty of one variable will depend whether another one is set or not. Small snippet:

option(build-compiler "Build the Nap Compiler" ON)
set(include_interrupt_dirs CACHE INTERNAL "interrupts/intr_4" FORCE)

if(build-compiler)
    option(enable-runtime-compilation 
           "Build in the runtime code compilation link in intr_2 & intr_3)" ON)
    if(enable-runtime-compilation)
        list(APPEND include_interrupt_dirs "interrupts/intr_2" "interrupts/intr_3" )
    endif()
endif()

I use cmake-gui for configuring the project, and what I would like to achieve is:

  1. if the user selects the build-compiler the enable-runtime-compilation should also be presented. This part is done.
  2. if the user deselects the build-compiler the enable-runtime-compilation should be hidden from the GUI. This is not working.

Do you have any idea how to make it work?

like image 509
Ferenc Deak Avatar asked Jun 07 '26 17:06

Ferenc Deak


2 Answers

Using unset(var [CACHE]) is subtly tricky. If you just unset the variable, it will remain in the cache (although it will not be visible to the script, it will still be visible to the user). If you also delete it from the cache, then you lose the value that was there.

In my use case, I wanted to hide variables based on some condition. I found it that deleting the variables from the cache might get confusing as when reinstated, they would return to their default state rather than to what the user might have set before.

I prefer to hide the variables using mark_as_advanced(FORCE var) and unhide using mark_as_advanced(CLEAR var). It does exactly what you need-it hides the variable from the GUI but it still exists in the cache. You might use this together with the "soft" unset (the one without CACHE) to make sure that the hidden variable is not still being used in the configuration.

Additionally, there is CMakeDependentOption which is intended specifically for this use case (an option that is only available if some set of condition evaluates as true). This is apparently available since CMake 3.0.2.

like image 130
the swine Avatar answered Jun 10 '26 10:06

the swine


You can use unset(var CACHE) to remove the variable from the cache:

if(build-compiler)
    option(enable-runtime-compilation 
           "Build in the runtime code compilation link in intr_2 & intr_3)" ON)
    if(enable-runtime-compilation)
        list(APPEND include_interrupt_dirs "interrupts/intr_2" "interrupts/intr_3" )
    endif()
else()
    unset(enable-runtime-compilation CACHE)
endif()
like image 37
lrineau Avatar answered Jun 10 '26 11:06

lrineau



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!