复制文件仅在源文件存在时。

huangapple go评论57阅读模式
英文:

Copy File only if Source File Exists

问题

我想在我的CMake中添加一个自定义命令,将项目中的文件(与其他CMake项目分开构建)复制到一个可被需要的CMake项目使用的目录中。这很简单:

add_custom_command(
TARGET ${PROJECT_NAME}
PRE_BUILD
COMMAND ${CMAKE_COMMAND}
ARGS -E copy_if_different ${LIGHT_THEME_SOURCE} "${LIGHT_THEME_REF}"
)

问题是`${LIGHT_THEME_SOURCE}`并不总是存在,所以这会导致错误。有可能修改命令仅在**源**文件存在时才复制吗?目标文件始终存在,所以这不是问题。(澄清一下,变量`${LIGHT_THEME_SOURCE}`会存在,只是它引用的文件不一定存在)

如果可能的话,我想尝试将其作为自定义命令的一部分来完成,因为当CMake生成项目文件时,源文件可能不存在,但后来源文件会存在。如果不可能,在CMake生成时执行,那也可以接受。

供参考,当前使用的是CMake 3.22版本,但如果需要,可以使用更新的版本。
英文:

I'm wanting to add a custom command to my CMake which copies files from a project (build separate from the rest of the CMake projects) into a directory usable by the CMake projects that need it. This is simple enough:

add_custom_command(
    TARGET ${PROJECT_NAME}
    PRE_BUILD
    COMMAND ${CMAKE_COMMAND}
    ARGS -E copy_if_different ${LIGHT_THEME_SOURCE} "${LIGHT_THEME_REF}"
)

The problem is, that the ${LIGHT_THEME_SOURCE} doesn't always exist, so this causes an error. Is it possible to change the command to only copy if the source file exists? The destination file will always exist, so it's not the problem. (to clarify, the variable ${LIGHT_THEME_SOURCE} will exist, just not necessarily the file it refers to)

I'd like to try to do it as part of the custom command, if possible, since when CMake generates the project files the source file might not exist, but then later on the source file would. If that's not possible, and it has to be done when CMake is generating, that's acceptable though.

For reference, currently using CMake 3.22, but can use later versions if needed.

答案1

得分: 1

如果您只需要在配置时进行检查,请使用 if(EXISTS)

如果您需要在自定义命令执行时进行检查,请编写一个脚本执行检查,仅在检查通过时复制文件,然后在您的自定义命令中调用该脚本。

add_custom_command(
    TARGET ${PROJECT_NAME}
    PRE_BUILD
    COMMAND ${CMAKE_COMMAND}
        "-DLIGHT_THEME_SOURCE=${LIGHT_THEME_SOURCE}"
        "-DLIGHT_THEME_REF=${LIGHT_THEME_REF}"
        -P cmake/copy_theme.cmake
)

cmake/copy_theme.cmake:

if(EXISTS "${LIGHT_THEME_SOURCE}")
    file(COPY_FILE "${LIGHT_THEME_SOURCE}" "${LIGHT_THEME_REF}" ONLY_IF_DIFFERENT)
endif()
英文:

If you only need to do the check at configuration time, then use if(EXISTS).

If you need the check with the execution of the custom command, write a script that does the check and only copies the file if the check passes, and then call that script in your custom command.

add_custom_command(
    TARGET ${PROJECT_NAME}
    PRE_BUILD
    COMMAND ${CMAKE_COMMAND}
        "-DLIGHT_THEME_SOURCE=${LIGHT_THEME_SOURCE}"
        "-DLIGHT_THEME_REF=${LIGHT_THEME_REF}"
        -P cmake/copy_theme.cmake
)

cmake/copy_theme.cmake:

if(EXISTS "${LIGHT_THEME_SOURCE}")
    file(COPY_FILE "${LIGHT_THEME_SOURCE}" "${LIGHT_THEME_REF}" ONLY_IF_DIFFERENT)
endif()

huangapple
  • 本文由 发表于 2023年3月9日 20:21:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/75684548.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定