新命令行工具的 CMake 包装器
我试图提供一个简单的 CMake 函数来将 PlantUML 图渲染为 PNG,作为我构建过程的一部分。我的想法是,我有一堆包含 PlantUML 图的 .uml 文件,我想将它们渲染为 PNG 作为构建过程的一部分。我想要一个类似于 add_library() 的函数 等。 al. 渲染图像文件早于源文件的任何图表。
使用 add_custom_command()
,我想出了以下代码片段:
#
# Create top-level target that renders a PlantUML diagram to a PNG image.
#
function(add_diagram target source)
# Program used to render the diagram.
set(plantuml java -jar ${PLANTUML_JARFILE})
# Diagram source file basename used to create output file name.
get_filename_component(output ${source} NAME_WE)
# Render the diagram and write an "${output}.png"
# file in the current binary folder.
add_custom_command(
OUTPUT
${CMAKE_CURRENT_BINARY_DIR}/${output}.png
COMMAND
${plantuml} -o ${CMAKE_CURRENT_BINARY_DIR} -tpng ${source}
MAIN_DEPENDENCY
${source}
COMMENT
"Rendering diagram '${output}'."
)
# Top-level target to build the output file.
add_custom_target(${target}
DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${output}.png)
endfunction()
我将此函数调用为:
add_diagram(foo ${CMAKE_CURRENT_SOURCE_DIR}/foo.uml)
其中 foo.uml 是包含 PlantUML 图的文件。在非常基本的层面上,这个“有效”是因为它创建了一个我可以手动构建的命名顶级目标(例如使用 make foo
、nmake foo
、jom foo
等)。
如何将此目标添加到默认目标(全部?),以便使用其余的库和可执行文件自动构建?
I'm trying to provide a simple CMake function to render PlantUML diagrams to PNG as part of my build process. The idea is that I have a bunch of .uml
files containing PlantUML diagrams that I want to render to PNG as part of my build process. I would like to have a function similar to add_library()
et. al. that renders any diagram for which the image file is older than the source file.
Using add_custom_command()
, I came up with the following snippet:
#
# Create top-level target that renders a PlantUML diagram to a PNG image.
#
function(add_diagram target source)
# Program used to render the diagram.
set(plantuml java -jar ${PLANTUML_JARFILE})
# Diagram source file basename used to create output file name.
get_filename_component(output ${source} NAME_WE)
# Render the diagram and write an "${output}.png"
# file in the current binary folder.
add_custom_command(
OUTPUT
${CMAKE_CURRENT_BINARY_DIR}/${output}.png
COMMAND
${plantuml} -o ${CMAKE_CURRENT_BINARY_DIR} -tpng ${source}
MAIN_DEPENDENCY
${source}
COMMENT
"Rendering diagram '${output}'."
)
# Top-level target to build the output file.
add_custom_target(${target}
DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${output}.png)
endfunction()
And I invoke this function as:
add_diagram(foo ${CMAKE_CURRENT_SOURCE_DIR}/foo.uml)
where foo.uml
is a file containing a PlantUML diagram. At a very basic level, this "works" in that it creates a named top-level target that I can build manually (e.g. using make foo
, nmake foo
, jom foo
, etc.).
How can I add this target to the default target (all?) such that this is automatically built with the rest of the libraries and executables?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
来自 CMake 文档:
如果您使用 Visual Studio,唯一的缺点是它将为每个目标创建一个新项目。
From the CMake documentation:
If you are using Visual Studio, the only drawback is that it will create a new project for each target.