CMake图形界面:切换变量可见性

3

我有一个CMake设置,其中一个变量的可访问性将取决于另一个变量是否设置。以下是一小段代码:

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

我使用cmake-gui来配置项目,我想要实现的目标是:
  1. 如果用户选择了build-compiler,则应该出现enable-runtime-compilation选项。这部分已经完成。
  2. 如果用户取消选择build-compiler,则应该在GUI中隐藏enable-runtime-compilation选项。但这一部分目前还没有生效。
你有任何想法如何让它生效吗?
2个回答

2
使用unset(var [CACHE])有些微妙的技巧。如果你只是取消设置变量,它仍然会留在缓存中(虽然脚本看不到它,但用户仍然可以看到)。如果你也从缓存中删除它,那么你就失去了原来的值。
在我的用例中,我想根据某些条件隐藏变量。我发现从缓存中删除变量可能会让人感到困惑,因为当重新启用时,它们将返回到默认状态,而不是返回到用户之前设置的状态。
我更喜欢使用mark_as_advanced(FORCE var)隐藏变量,并使用mark_as_advanced(CLEAR var)取消隐藏。它正好做你需要的事情-它隐藏了变量从GUI中,但它仍然存在于缓存中。你可以与“soft” unset(没有CACHE)一起使用,以确保隐藏的变量不再在配置中使用。
此外,还有一个专门针对这种用例的CMakeDependentOption(如果某些条件评估为true,则只有一个可用选项)。这显然是自CMake 3.0.2以来提供的。

1
您可以使用unset(var 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()

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接