我需要使用一些makefile依赖模拟cmake
hello: if_this_file_changes.cpp if_this_file_changes.txt
run_some_command
基本上,当我构建它时,我想检查这些文件是否已经更改(每当这些文件中的任何一个发生更改时)?如果是,那么调用一个命令{一些命令,比如TOUCH或一些脚本}如果文件没有改变,什么也不做
cmake_minimum_required(VERSION 3.21)
project(ProjectName)
///////////////////////////////////
if this files have been changed
run command
////////////////////////
add_executable(hello source.cpp)
#简单的例子我想做什么
hello: if_this_file_changes.cpp if_this_file_changes.txt run_some_command
如果run_some_command
创建hello
:
add_custom_command(
OUTPUT hello
COMMAND run_some_command
DEPENDS if_this_file_changes.cpp if_this_file_changes.txt
)
add_custom_target(hello DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/hello")
否则如果你想让hello
是假的,你需要创建一个邮票文件来获得可预测的更新语义:
add_custom_command(
OUTPUT hello.stamp
COMMAND run_some_command
COMMAND "${CMAKE_COMMAND}" -E touch hello.stamp
DEPENDS if_this_file_changes.cpp if_this_file_changes.txt
)
add_custom_target(hello DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/hello.stamp")
让所有*.txt文件成为依赖项的快速示例
file(GLOB_RECURSE A_NAME *.txt)
add_custom_target(a_target_name COMMAND "your/command/here" "multi/commands/ok" DEPENDS ${A_NAME})
你的自定义目标现在有依赖关系,如果他们改变了,cmake会做一些事情。