2

我需要一种将其他依赖项添加到自定义目标的方法。我有一个将资源文件添加到特定项目的宏,使用如下:

ADD_RESOURCES( ${TARGET} some/path pattern1 pattern2 )
ADD_RESOURCES( ${TARGET} another/path pattern1 )

我创建了一个名为的目标${TARGET}_ASSETS,并希望将所有这些资源的生成附加到一个目标。add_dependencies但是只接受其他目标。因此,如果我通过 a 生成文件,add_custom_command则无法将其用作依赖项。

解决方法可能是为每次调用创建一个新的自定义标记ADD_RESOURCES,然后将其附加到资产目标。然而,每个目标都需要一个唯一的名称,而这些无法从 ADD_RESOURCES 的参数中生成这个唯一的名称。

4

2 回答 2

1

一种解决方法是推迟${target}_ASSETS自定义目标的生成,直到所有依赖项都已通过调用ADD_RESOURCES.

宏必须将依赖项记录在一个全局变量中,而不是立即将依赖项添加到自定义目标中,该ADD_RESOURCES变量的名称取决于目标:

macro (ADD_RESOURCES _targetName)
    set (_dependencies ${ARGN})
    ...
    # record depencies in a target dependency variable
    if (DEFINED ${_targetName}_Dependencies)
        list (APPEND ${_targetName}_Dependencies ${_dependencies})
    else()
        set (${_targetName}_Dependencies ${_dependencies})
    endif()
endmacro()

然后添加另一个帮助宏,它通过反射确定所有定义的目标依赖变量,并为每个目标设置一个自定义目标:

macro (SETUP_ASSETS_TARGETS)
    get_cmake_property(_vars VARIABLES)
    foreach (_var ${_vars})
        if (_var MATCHES "(.+)_Dependencies")
            set (_targetName ${CMAKE_MATCH_1})
            set (_targetDependencies ${${_var}})
            message("${_targetName} depends on ${_targetDependencies}")
            add_custom_target(${_targetName}_ASSETS DEPENDS ${_targetDependencies})
        endif()
    endforeach()
endmacro()

在您CMakeLists.txt通过调用添加所有必要的依赖项时ADD_RESOURCES,然后调用SETUP_ASSETS_TARGETS宏以定义所有自定义目标。

ADD_RESOURCES( target1 some/path pattern1 pattern2 )
ADD_RESOURCES( target1 another/path pattern1 )
ADD_RESOURCES( target2 foo/bar pattern1 )
...
...
SETUP_ASSETS_TARGETS()
于 2012-08-11T17:47:38.043 回答
1

我知道这是一个迟到的答案,但我为所有搜索此问题的人发布了我的解决方案:

function(target_resources THIS)

    if (NOT TARGET ${THIS}_res)
        # this is just a pseudo command which can be appended later
        add_custom_command(OUTPUT ${THIS}_dep COMMAND cd ${CMAKE_CURRENT_SOURCE_DIR})

        # add a dependency with a target, as a command itself can not be a dependency
        add_custom_target(${THIS}_res DEPENDS ${THIS}_dep)
        add_dependencies(${THIS} ${THIS}_res)
    endif ()

    get_target_property(RUNTIME_OUTPUT_DIRECTORY ${THIS} RUNTIME_OUTPUT_DIRECTORY)

    foreach (RES_FILE IN LISTS ARGN)

        if (IS_ABSOLUTE ${RES_FILE})
            file(RELATIVE_PATH PATH ${CMAKE_CURRENT_SOURCE_DIR} ${RES_FILE})
        endif ()

        # append the resource command with our resource
        add_custom_command(OUTPUT ${THIS}_dep
                COMMAND ${CMAKE_COMMAND} -E copy
                ${CMAKE_CURRENT_SOURCE_DIR}/${RES_FILE}
                ${RUNTIME_OUTPUT_DIRECTORY}/${RES_FILE}
                APPEND)

    endforeach ()

endfunction()

这个解决方案的好处是不依赖全局变量,也不需要调用设置宏。

于 2021-01-10T14:39:01.633 回答