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?
From the CMake documentation:
If you are using Visual Studio, the only drawback is that it will create a new project for each target.