如何有条件地将ALL选项添加到add_custom_target()? [英] How to conditionally add ALL option to add_custom_target()?

查看:55
本文介绍了如何有条件地将ALL选项添加到add_custom_target()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果用户在 cmake-gui 中选择$ {DO_HTML}开关,我想有条件地将目标 docs_html 包含在 ALL 中.没有这种丑陋的代码重复怎么办?

I would like to conditionally include the target docs_html to ALL if user selects ${DO_HTML} switch in cmake-gui. How to do without this ugly code repetition?

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

推荐答案

您可以使用变量的取消引用来形成命令调用的条件部分.空值(例如,如果没有变量)将被忽略:

You may use variable's dereference to form conditional parts of command's invocation. Empty values (e.g. if variable is absent) is simply ignored:

# 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 子句:


Variable may contain even several parameters to the command. E.g. one may conditionally add additional COMMAND clause for the target:

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
)

这篇关于如何有条件地将ALL选项添加到add_custom_target()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆