如果用户在docs_html
中选择$ {DO_HTML}切换,我想有条件地将目标ALL
包含到cmake-gui
。没有这个丑陋的代码重复怎么办?
cmake_minimum_required(VERSION 3.3 FATAL_ERROR)
project(docs)
set(DO_HTML 1 CACHE BOOL "Whether generate documentation in static HTML")
if (${DO_HTML})
#This command doesn't work:
# add_dependencies(ALL docs_html)
add_custom_target(docs_html ALL #Code repeat 1
DEPENDS ${HTML_DIR}/index.html
)
else()
add_custom_target(docs_html #Code repeat 2
DEPENDS ${HTML_DIR}/index.html
)
endif()
答案 0 :(得分:1)
您可以使用变量的取消引用来形成命令调用的条件部分。空值(例如,如果不存在变量)将被忽略:
# Conditionally form variable's content.
if (DO_HTML)
set(ALL_OPTION ALL)
# If you prefer to not use uninitialized variables, uncomment next 2 lines.
# else()
# set(ALL_OPTION)
endif()
# Use variable in command's invocation.
add_custom_target(docs_html ${ALL_OPTION}
DEPENDS ${HTML_DIR}/index.html
)
变量可能包含命令的几个参数。例如。一个人可以有条件地为目标添加额外的 COMMAND 子句:
if(NEED_ADDITIONAL_ACTION) # Some condition
set(ADDITIONAL_ACTION COMMAND ./run_something arg1)
endif()
add_custom_target(docs_html ${ALL_OPTION}
${ADDITIONAL_ACTION}
DEPENDS ${HTML_DIR}/index.html
)