2011-12-31 35 views
2

我試圖提供一個簡單的CMake函數來將PlantUML圖表呈現爲PNG,作爲我的構建過程的一部分。這個想法是,我有一堆.uml文件,其中包含我想要作爲構建過程的一部分呈現給PNG的PlantUML圖。我想要有一個功能類似於add_library()et。人。,它呈現圖像文件比源文件更舊的任何圖。用於新命令行工具的CMake包裝

使用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等)。

如何將此目標添加到默認目標(全部?),以便它可以與其餘的庫和可執行文件自動構建?

回答

3

CMake documentation

add_custom_target:添加一個目標,沒有輸出,所以它總是會建成。

如果指定了ALL選項,則表示應將此目標添加到默認的構建目標,以便每次運行該目標。與列出

依賴DEPENDS參數可在相同的目錄(文件的CMakeLists.txt)參考文件和()與add_custom_command創建的自定義命令的輸出。

如果您使用Visual Studio,唯一的缺點是它會爲每個目標創建一個新項目。

+0

很好找!我會試試看,讓你知道它是怎麼回事。 – 2012-01-01 02:19:52

+0

謝謝,我所要做的就是在'add_custom_target()'調用中添加'ALL',一切都像魅力一樣! – 2012-01-01 03:57:44

+0

如果您正在運行構建的操作系統沒有安裝plantuml,您在做什麼? – AlexTheo 2015-04-08 19:28:52