在下面的示例中,有两个cmake子文件夹:一个生成文件file1.txt
,第二个生成file2.txt
。第二个命令取决于第一个。
|-- generator
|-- CMakeLists.txt -> file1.txt
|-- producer
|-- CMakeLists.txt -> file2.txt (depends on file1.txt)
发电机:
add_custom_command(
OUTPUT "${PROJECT_SOURCE_DIR}/file1.txt"
COMMAND ${CMAKE_COMMAND} -E touch "${PROJECT_SOURCE_DIR}/file1.txt"
COMMENT "Generate first file"
)
生产者:
add_custom_command(
OUTPUT "${PROJECT_SOURCE_DIR}/file2.txt"
COMMAND ${CMAKE_COMMAND} -E touch "${PROJECT_SOURCE_DIR}/file2.txt"
DEPENDS "${PROJECT_SOURCE_DIR}/file1.txt"
COMMENT "Generate second file"
)
add_custom_target(mytarget DEPENDS "${PROJECT_SOURCE_DIR}/file2.txt")
如果单个CMakeLists.txt文件的两个命令都在同一作用域中,则没有问题。但是我想通过生成的文件连接来自不同作用域的两个命令。
$ cmake .. && make mytarget
Scanning dependencies of target mytarget
make[3]: *** No rule to make target '../file1.txt', needed by '../file2.txt'. Stop.
CMakeFiles/Makefile2:103: recipe for target 'producer/CMakeFiles/mytarget.dir/all' failed
make[2]: *** [producer/CMakeFiles/mytarget.dir/all] Error 2
CMakeFiles/Makefile2:110: recipe for target 'producer/CMakeFiles/mytarget.dir/rule' failed
make[1]: *** [producer/CMakeFiles/mytarget.dir/rule] Error 2
Makefile:118: recipe for target 'mytarget' failed
make: *** [mytarget] Error 2