2010-03-01 276 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依赖于自定义目标。然后,自定义目标应该在构建库目标之前构建。