Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top