【发布时间】:2020-04-26 16:34:32
【问题描述】:
在这个简单的 CMakefile 中,第一个脚本 list.sh 输出 2 个生成文件 file1.proto;file2.proto 的列表,指示 CMake 可以从源代码 source.xml 构建它们(使用第二个脚本 gen.sh)。
cmake_minimum_required(VERSION 3.13)
set(source "source.xml")
execute_process(
COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/list.sh ${source}
OUTPUT_VARIABLE protos
)
message("${protos}: ${source}")
add_custom_command(
OUTPUT ${protos}
COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/gen.sh ${source}
DEPENDS ${source}
)
add_custom_target(my_target DEPENDS ${protos})
如果我运行一切正常:
$ cmake ..
file1.proto;file2.proto: source.xml
-- Configuring done
-- Generating done
-- Build files have been written to: /build
$ make my_target
[100%] Generating file1.proto, file2.proto
[100%] Built target my_target
我应该添加什么才能运行代码生成:
$ make file1.proto
[编辑] 自动完成仅建议命令 make 的以下内容:
$ make (TAB TAB)
all cmake_force edit_cache/ preinstall
clean default_target help preinstall/
clean/ depend my_target rebuild_cache
cmake_check_build_system edit_cache my_target/ rebuild_cache/
【问题讨论】:
-
尝试
make /the/full/absolute/path/to/file1.proto发布文件的绝对路径。在大多数 shell 中,按下make <tab><tab>将列出所有可用的目标。从字面上看,foreach(i IN LISTS ${protos}) get_filename_compoenent(<extarct filename here>) add_custom_target(the_filename_file1.proto DEPENDS the_full_path_in_protos)很可能在 cmake 中使用了完整的绝对路径。 -
感谢您的快速回答。不幸的是,它似乎不起作用,我在
make的自动完成中找不到我的 2 个文件(添加到问题的正文中)。 -
@KamilCuk 随意写一个答案:如果没有其他提交,我会接受你的作为最好的解决方案。
-
很好地总结了从/到生成文件声明依赖关系时可能出错的所有内容(TLDR:基本上不可能):samthursfield.wordpress.com/2015/11/21/…
标签: cmake dependencies code-generation target