【问题标题】:CMake: require building of files in addition to building targetsCMake:除了构建目标外,还需要构建文件
【发布时间】: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


【解决方案1】:

@KamilCuk 的解决方案:
添加以下内容可以单独构建每个 proto 文件
(它有效,但随后 cmake 抱怨循环依赖!)

foreach(p ${protos})
  add_custom_target(${p} DEPENDS {CMAKE_CURRENT_BINARY_DIR}/${p})
endforeach()

【讨论】:

    猜你喜欢
    • 2012-10-15
    • 1970-01-01
    • 1970-01-01
    • 2019-08-17
    • 1970-01-01
    • 2023-04-07
    • 1970-01-01
    • 2018-03-10
    • 1970-01-01
    相关资源
    最近更新 更多