新命令行工具的 CMake 包装器

发布于 2024-12-23 17:45:52 字数 1540 浏览 4 评论 0原文

我试图提供一个简单的 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 foonmake foojom 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无法回应 2024-12-30 17:45:52

来自 CMake 文档

add_custom_target:添加一个没有输出的目标,以便始终构建它。

如果指定ALL选项,则表示应将此目标添加到默认构建目标中,以便每次都会运行它。

使用 DEPENDS 参数列出的依赖项可能会引用同一目录(CMakeLists.txt 文件)中使用 add_custom_command() 创建的自定义命令的文件和输出。

如果您使用 Visual Studio,唯一的缺点是它将为每个目标创建一个新项目。

From the CMake documentation:

add_custom_target: Add a target with no output so it will always be built.

If the ALL option is specified it indicates that this target should be added to the default build target so that it will be run every time.

Dependencies listed with the DEPENDS argument may reference files and outputs of custom commands created with add_custom_command() in the same directory (CMakeLists.txt file).

If you are using Visual Studio, the only drawback is that it will create a new project for each target.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文