cmake不会在更改时重建externalProject [英] cmake doesnt rebuild externalProject on changes

查看:155
本文介绍了cmake不会在更改时重建externalProject的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下CMakeLists.txt:

I have the following 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

Now if I modify a source file in external project: touch ../external/genLib/genLib.cpp

并重新编译
ninja
没什么变化:
ninja:无事可做。

and recompile ninja nothing changes: ninja: no work to do.

我已经使用make而不是Ninja进行了完全相同的行为测试。
我还测试了在我的custom_command中添加DEPENDS子句,该子句可用于单个文件,但不适用于整个项目(并且似乎很难将外部项目的所有源文件作为DEPENDS子句添加...)

I have tested it using make instead of Ninja with exactly same behavior. I also have tested to add DEPENDS clause in my custom_command, which is working for individual files but not the full project (and it seems very hacky to add all source files of the external project as DEPENDS clause...)

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

Does anyone have more experience using external project and can explain the behavior of cmake decision to rebuild or not dependencies ?

推荐答案

如果您至少可以使用cmake 3.1版,请尝试将 BUILD_ALWAYS 1 添加到您的 ExternalProject_Add 命令:

If you can use at least cmake version 3.1, try adding BUILD_ALWAYS 1 to your ExternalProject_Add command:

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中修改源代码。

Enabling this option forces the build step to always be run. This can be the easiest way to robustly ensure that the external project’s own build dependencies are evaluated rather than relying on the default success timestamp-based method. This option is not normally needed unless developers are expected to modify something the external project’s build depends on in a way that is not detectable via the step target dependencies (e.g. SOURCE_DIR is used without a download method and developers might modify the sources in SOURCE_DIR).

这篇关于cmake不会在更改时重建externalProject的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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