我有这个自定义构建,它调用matlab将.slx文件编译成.dll文件。
function(BUILD_SIMULINK model)
set(EXECUTE_COMMAND matlab -r "rtwbuild( ${model} )" )
add_custom_target(
${model} ALL
COMMAND ${EXECUTE_COMMAND}
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${model}.slx
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${model}.dll
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
COMMENT "Building: ${model}"
)
endfunction(BUILD_SIMULINK)
但是我的问题是每当我使用cmake --build .
时,总会执行此命令。
当DEPENDS未更改且OUTPUT存在时,如何防止执行此命令?我正在寻找的是类似于当源没有改变并且存在适当的目标文件时cmake如何避免重新编译c / cpp文件。
答案 0 :(得分:1)
请参阅add_custom_target()
command documentation:
目标没有输出文件,并且即使命令尝试创建具有目标名称的文件,始终也会被视为过时。使用
add_custom_command()
命令生成具有依赖项的文件。
没有OUTPUT
个关键字。我认为它只被接受,因为CMake认为OUTPUT
是一个依赖。实际上,当我运行你的代码时,我收到了一条CMake警告:
...
This project specifies custom command DEPENDS on files in the build tree
that are not specified as the OUTPUT or BYPRODUCTS of any
add_custom_command or add_custom_target:
test_model.dll
您需要使用add_custom_command()
:
cmake_minimum_required(VERSION 2.6)
project(TestCustomTargetWithDependency NONE)
function(BUILD_SIMULINK model)
#set(EXECUTE_COMMAND matlab -r "rtwbuild( ${model} )" )
set(EXECUTE_COMMAND "${CMAKE_COMMAND}" -E touch "${model}.dll")
add_custom_command(
OUTPUT "${model}.dll"
COMMAND ${EXECUTE_COMMAND}
DEPENDS "${model}.slx"
COMMENT "Building: ${model}"
)
add_custom_target(
${model} ALL
DEPENDS "${model}.dll"
)
endfunction(BUILD_SIMULINK)
file(WRITE "test_model.slx" "")
BUILD_SIMULINK(test_model)
:Sources / Dependencies默认为CMAKE_CURRENT_SOURCE_DIR
,输出默认值为CMAKE_CURRENT_BINARY_DIR
。无需明确地为这些添加前缀。