本文介绍了CMake:add_custom_command with PRE_BUILD不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以下CMakeLists.txt文件中,虽然我已使用 PRE_BUILD 选项设置了 add_custom_command 命令,命令并不总是在生成主可执行文件之前执行:

In the following CMakeLists.txt file, although I have set an add_custom_command command with PRE_BUILD option, the custom command is not always executed before making the main executable:

cmake_minimum_required(VERSION 2.8)
project(VersioningTest)

set(MAJOR 1)
set(MINOR 0)
set(PATCH 0)

set(PRODUCT App-${MAJOR}-${MINOR}-${PATCH})

ADD_EXECUTABLE(${PRODUCT}
                main.cpp
                Engine.cpp)

add_custom_command(TARGET ${PRODUCT} PRE_BUILD
                COMMAND ${CMAKE_COMMAND}
                -DMAJOR=${MAJOR}
                -DMINOR=${MINOR}
                -DPATCH=${PATCH}
                -P ${CMAKE_SOURCE_DIR}/SetVersion.cmake
                WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
                COMMENT "setting version...")

所以我决定用 add_custom_target 替换 add_custom_command add_dependencies

So I decided to replace add_custom_command with add_custom_target and add_dependencies:

add_custom_target(SetVersion
                COMMAND ${CMAKE_COMMAND}
                -DMAJOR=${MAJOR}
                -DMINOR=${MINOR}
                -DPATCH=${PATCH}
                -P ${CMAKE_SOURCE_DIR}/SetVersion.cmake
                WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
                COMMENT "setting version...")

add_dependencies(${PRODUCT} SetVersion)

它工作。现在 SetVersion.cmake 文件在执行主可执行文件之前被执行。在我的CMake文件中 add_custom_command 有什么问题?

And it worked. Now the SetVersion.cmake file is executed everytime before making the main executable. What's wrong with add_custom_command in my CMake file?

SetVersion.cmake 档案:

EXECUTE_PROCESS(
    COMMAND svn info ${CMAKE_SOURCE_DIR}
    COMMAND grep Revision
    OUTPUT_VARIABLE REVISION
    OUTPUT_STRIP_TRAILING_WHITESPACE
)

EXECUTE_PROCESS(
     COMMAND svn diff ${CMAKE_SOURCE_DIR}
     OUTPUT_VARIABLE SVNDIFF
     OUTPUT_STRIP_TRAILING_WHITESPACE
 )

if(SVNDIFF STREQUAL "")
    set(LOCALCHANGES 0)
    message("No local changes detected")
else(SVNDIFF STREQUAL "")
    set(LOCALCHANGES 1)
    message("Local changes detected!")
endif(SVNDIFF STREQUAL "")

configure_file(${CMAKE_SOURCE_DIR}/version.h.in ${CMAKE_SOURCE_DIR}/version.h)
message("Version set") # For testing only

以及 version.h.in 的内容:

#define MAJOR_VERSION "${MAJOR}"
#define MINOR_VERSION "${MINOR}"
#define PATCH_VERSION "${PATCH}"
#define REVISION "${REVISION}"
#define LOCALCHANGES "${LOCALCHANGES}"


推荐答案

这取决于您使用的CMake生成器。以下是文档中的相关部分:

It depends on the CMake generator you use. Here is the relevant section from the add_custom_command documentation:

Note that the PRE_BUILD option is only supported on Visual Studio 7 or later.
For all other generators PRE_BUILD will be treated as PRE_LINK.

在OS X PRE_BUILD 对于Xcode,它没有正确记录。

Under OS X PRE_BUILD also seems to work for Xcode, which is not documented properly.

这篇关于CMake:add_custom_command with PRE_BUILD不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 05:52