2010-03-01 275 views
13

我很努力add_custom_command。讓我詳細解釋這個問題。cmake add_custom_command

我有這些cxx文件和hxx文件的集合。我在每個腳本上運行perl腳本來生成某種類型的翻譯文件。該命令看起來像

perl trans.pl source.cxx -o source_cxx_tro 

以及類似的header.hxx文件。

因此,我將結束了一些多個命令(每個爲一個文件)

然後我運行從這些命令所產生的輸出的另一perl的scripn(source_cxx_tro,header_hxx_tro)

perl combine.pl source_cxx_tro header_hxx_tro -o dir.trx 

DIR .trx是輸出文件。

我有這樣的事情。

Loop_Over_All_Files() 
Add_Custom_Command (OUTPUT ${trofile} COMMAND perl trans.pl ${file} -o ${file_tro}) 
List (APPEND trofiles ${file_tro}) 
End_Loop() 

Add_Custom_Command (TARGET LibraryTarget POST_BUILD COMMAND perl combine.pl ${trofiles} -o LibraryTarget.trx) 

我期望的是在構建後構建目標時,將首先構建trofiles。但事實並非如此。 $ {trofiles}沒有被構建,因此後期構建命令以失敗告終。 有什麼辦法可以告訴POST_BUILD命令依賴於以前的自定義命令嗎?

有什麼建議嗎?

由於提前, 蘇里亞

回答

27

使用add_custom_command的創建文件變換鏈

  • *(CXX | HXX) - > * _。(CXX | HXX)_tro
  • * _(CXX | HXX)_tro - >富。 trx

並使用add_custom_target將cmake中的第一個類實體作爲最後一個轉換。默認情況下,這個目標不會被建立,除非你用ALL標記它或讓另一個目標被建立依賴它。

 
set(SOURCES foo.cxx foo.hxx) 
add_library(Foo ${SOURCES}) 

set(trofiles) 
foreach(_file ${SOURCES}) 
    string(REPLACE "." "_" file_tro ${_file}) 
    set(file_tro "${file_tro}_tro") 
    add_custom_command(
    OUTPUT ${file_tro} 
    COMMAND perl ${CMAKE_CURRENT_SOURCE_DIR}/trans.pl ${CMAKE_CURRENT_SOURCE_DIR}/${_file} -o ${file_tro} 
    DEPENDS ${_file} 
) 
    list(APPEND trofiles ${file_tro}) 
endforeach() 
add_custom_command(
    OUTPUT Foo.trx 
    COMMAND perl ${CMAKE_CURRENT_SOURCE_DIR}/combine.pl ${trofiles} -o Foo.trx 
    DEPENDS ${trofiles} 
) 
add_custom_target(do_trofiles DEPENDS Foo.trx) 
add_dependencies(Foo do_trofiles) 
3

你想創建消耗的自定義命令的輸出的自定義目標。然後使用ADD_DEPENDENCIES確保命令以正確的順序運行。

這可能是有點接近你想要什麼: http://www.cmake.org/Wiki/CMake_FAQ#How_do_I_use_CMake_to_build_LaTeX_documents.3F

基本上一個add_custom_command每個生成的文件,收集這些文件(trofiles)的列表,然後使用add_custom_target用就行了trofiles而定。然後使用add_dependencies使LibraryTarget依賴於自定義目標。然後,自定義目標應該在構建庫目標之前構建。