cmake不会在更改时重建externalProject

时间:2017-10-12 11:10:01

标签: cmake

我有以下CMakeLists.txt:

cmake_minimum_required( VERSION 3.0)
project(addProject)
include (ExternalProject)

set(ExternalProjectCMakeArgs
  -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
  -DCMAKE_INSTALL_PREFIX=${PROJECT_BINARY_DIR}/external/genLib
)

ExternalProject_Add(genLib
SOURCE_DIR ${PROJECT_SOURCE_DIR}/external/genLib
BINARY_DIR ${PROJECT_BINARY_DIR}/external/genLib
INSTALL_COMMAND ""
INSTALL_DIR ${PROJECT_BINARY_DIR}/external
CMAKE_ARGS ${ExternalProjectCMakeArgs}
)
add_custom_command( OUTPUT "generated.cpp"
COMMAND ${PROJECT_BINARY_DIR}/external/genLib/genLib "generated.cpp"
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})

add_executable(add main.cpp ${CMAKE_CURRENT_BINARY_DIR}/generated.cpp)
add_dependencies (add genLib)

第一次构建总是正确的:

mkdir build; cd build; cmake -GNinja ../; ninja

给出以下输出:

-- The C compiler identification is GNU 4.4.7
-- The CXX compiler identification is GNU 4.4.7
-- Check for working C compiler using: Ninja
-- Check for working C compiler using: Ninja -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler using: Ninja
-- Check for working CXX compiler using: Ninja -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /Devel/scratc/testingExternalProject2/build
41% 5/12@2.7 0: Performing configure step for 'genLib'
-- The C compiler identification is GNU 4.4.7
-- The CXX compiler identification is GNU 4.4.7
-- Check for working C compiler using: Ninja
-- Check for working C compiler using: Ninja -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler using: Ninja
-- Check for working CXX compiler using: Ninja -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /Devel/scratc/testingExternalProject2/build/external/genLib
50% 6/12@3.0 0: Performing build step for 'genLib'
50% 1/2@? 1: Building CXX object CMakeFiles/genLib.dir/genLib.cpp.o
100% 2/2@40.2 1: Linking CXX executable genLib
100% 12/12@5.6 0: Linking CXX executable add

现在,如果我在外部项目中修改源文件:     touch ../ external / genLib / genLib.cpp

并重新编译     忍者 没有什么变化:     忍者:没有工作要做。

我使用make而不是Ninja测试了它的行为完全相同。 我还测试过在我的custom_command中添加DEPENDS子句,该子句适用于单个文件但不适用于整个项目(将外部项目的所有源文件添加为DEPENDS子句似乎非常麻烦......)

有没有人有更多使用外部项目的经验,可以解释cmake决定重建或不依赖的行为?

1 个答案:

答案 0 :(得分:5)

如果至少可以使用cmake 3.1版,请尝试在BUILD_ALWAYS 1命令中添加ExternalProject_Add

ExternalProject_Add(genLib
    SOURCE_DIR ${PROJECT_SOURCE_DIR}/external/genLib
    BINARY_DIR ${PROJECT_BINARY_DIR}/external/genLib
    BUILD_ALWAYS 1
    INSTALL_COMMAND ""
    INSTALL_DIR ${PROJECT_BINARY_DIR}/external
    CMAKE_ARGS ${ExternalProjectCMakeArgs}
)

根据ExternalProject_Add命令的文档

  

启用此选项将强制始终执行构建步骤。这个可以   是可靠地确保外部项目自己的最简单方法   评估构建依赖关系,而不是依赖默认值   基于成功时间戳的方法。通常不需要此选项   除非开发人员希望修改外部   项目的构建方式依赖于无法通过   步进目标相关性(例如,使用SOURCE_DIR无需下载   方法,开发人员可能会在SOURCE_DIR中修改源代码。