cmake add_custom_command

发布于 2024-08-23 11:29:19 字数 886 浏览 5 评论 0原文

我正在努力解决 add_custom_command 问题。让我详细解释一下这个问题。

我有这些 cxx 文件和 hxx 文件集。我在每个文件上运行一个 perl 脚本来生成某种类型的翻译文件。 对于 header.hxx 文件,该命令看起来

perl trans.pl source.cxx -o source_cxx_tro

也类似。

因此,我最终会得到一些多个命令(每个命令对应一个文件),

然后我对这些命令生成的输出运行另一个 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 命令依赖于之前的自定义命令吗?

有什么建议吗?

提前致谢, 苏里亚

I'm struggling with add_custom_command. Let me explain the problem in detail.

I've these set of cxx files and hxx files. I run a perl script on each of them to generate a certain kind of translation file. The command looks like

perl trans.pl source.cxx -o source_cxx_tro

and similarly for header.hxx files as well.

So I'll end up with some multiple commands (each for a file)

Then I run another perl scripn on the output generated from these commands (source_cxx_tro, header_hxx_tro)

perl combine.pl source_cxx_tro header_hxx_tro -o dir.trx

dir.trx is the output file.

I've something like this.

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)

What I expect is when building the post build target, the trofiles will be built first. but it is not the case. The ${trofiles} are not getting built and hence the post build command ends in a failure.
Is there any way I can tell the POST_BUILD command depend on the previous custom command ?

Any suggestions ?

Thanks in advance,
Surya

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情徒 2024-08-30 11:29:19

使用add_custom_command创建文件转换链

  • *.(cxx|hxx) -> *_(cxx|hxx)_tro
  • *_(cxx|hxx)_tro -> Foo.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)

Use add_custom_command's to create a file transformation chain

  • *.(cxx|hxx) -> *_(cxx|hxx)_tro
  • *_(cxx|hxx)_tro -> Foo.trx

and make the last transformation an first class entity in cmake by using add_custom_target. By default this target won't be build, unless you mark it with ALL or let another target that is built depend on it.

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)
等风来 2024-08-30 11:29:19

您想要创建一个使用自定义命令的输出的自定义目标。然后使用 ADD_DEPENDENCIES 确保命令以正确的顺序运行。

这可能有点接近你想要的:
https://gitlab.kitware.com/cmake/community/-/wikis/FAQ#how-do-i-use-cmake-to-build-latex-documents

基本上每个生成的文件都有一个 add_custom_command ,收集这些文件(trofile)的列表,然后使用 add_custom_target 和 DEPENDS 列表trofile。然后使用 add_dependency 使 LibraryTarget 依赖于自定义目标。然后应该在构建库目标之前构建自定义目标。

You want to create a custom target that consumes the output of the custom commands. Then use ADD_DEPENDENCIES to make sure the commands are run in the right order.

This might be sort of close to what you want:
https://gitlab.kitware.com/cmake/community/-/wikis/FAQ#how-do-i-use-cmake-to-build-latex-documents

Basically one add_custom_command for each file generated, collect a list of those files (trofiles), then use add_custom_target with a DEPENDS on the list trofiles. Then use add_dependencies to make the LibraryTarget depend on the custom target. Then the custom target should be built before the library target is built.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文