• <legend id='X0UT1'><style id='X0UT1'><dir id='X0UT1'><q id='X0UT1'></q></dir></style></legend>
    <i id='X0UT1'><tr id='X0UT1'><dt id='X0UT1'><q id='X0UT1'><span id='X0UT1'><b id='X0UT1'><form id='X0UT1'><ins id='X0UT1'></ins><ul id='X0UT1'></ul><sub id='X0UT1'></sub></form><legend id='X0UT1'></legend><bdo id='X0UT1'><pre id='X0UT1'><center id='X0UT1'></center></pre></bdo></b><th id='X0UT1'></th></span></q></dt></tr></i><div id='X0UT1'><tfoot id='X0UT1'></tfoot><dl id='X0UT1'><fieldset id='X0UT1'></fieldset></dl></div>
  • <tfoot id='X0UT1'></tfoot>

          <bdo id='X0UT1'></bdo><ul id='X0UT1'></ul>

        <small id='X0UT1'></small><noframes id='X0UT1'>

        当生成器或输入在 CMake 中发生变化时,如何仅构建自动生成的代码?

        时间:2023-08-27
          <bdo id='7iUsq'></bdo><ul id='7iUsq'></ul>

            <tbody id='7iUsq'></tbody>

              <legend id='7iUsq'><style id='7iUsq'><dir id='7iUsq'><q id='7iUsq'></q></dir></style></legend>
              <i id='7iUsq'><tr id='7iUsq'><dt id='7iUsq'><q id='7iUsq'><span id='7iUsq'><b id='7iUsq'><form id='7iUsq'><ins id='7iUsq'></ins><ul id='7iUsq'></ul><sub id='7iUsq'></sub></form><legend id='7iUsq'></legend><bdo id='7iUsq'><pre id='7iUsq'><center id='7iUsq'></center></pre></bdo></b><th id='7iUsq'></th></span></q></dt></tr></i><div id='7iUsq'><tfoot id='7iUsq'></tfoot><dl id='7iUsq'><fieldset id='7iUsq'></fieldset></dl></div>

                  <tfoot id='7iUsq'></tfoot>
                • <small id='7iUsq'></small><noframes id='7iUsq'>

                  本文介绍了当生成器或输入在 CMake 中发生变化时,如何仅构建自动生成的代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在开发一个源代码存储库,该存储库通过运行输出标头和实现的 python 脚本生成一些 C++ 代码.此代码随后被编译并链接到我的库和可执行文件.我知道生成的代码只有在两个条件之一为真时才会改变:

                  I am working on a source code repository that generates some C++ code by running a python script outputting headers and implementation. This code is subsequently compiled and linked to my libraries and executables. I know that the generated code will only change if one of two conditions are true:

                  1. 生成器代码本身发生了变化
                  2. 生成器的输入(一个 XML 文件)发生变化

                  我想使用 cmake 来管理构建过程.目前,我正在使用 execute_process 来触发生成器.但是,每次我运行 cmake 时都会运行它并且它会触及文件,导致我生成的代码被重新编译并增加了我的总编译时间.

                  I want to use cmake to manage the build process. At the moment, I am using execute_process to fire off the generator. However, this runs every time I run cmake and it touches the files, causing my generated code to be recompiled and adding to my total compile time.

                  我还想确保生成的代码始终在我的库之前运行.换句话说,我希望库依赖于生成器运行.

                  I also want to make sure that the generated code is always run before my libraries. In other words, I want the libraries to depend on the generator to have run.

                  在 cmake 中处理这种情况的正确方法是什么?我看过以前的答案:在构建库之前让 CMake 执行项目中的目标".但这依赖于预先知道的代码生成器的输出.我的代码生成器将生成可变数量的文件.

                  What is the proper way to handle such a situation in cmake? I have seen this previous answer: "Get CMake to execute a target in project before building a library". But this relies on the output of the code generator being known in advance. My code generator will generate a variable number of files.

                  推荐答案

                  使用 ADD_CUSTOM_COMMAND 来触发你的生成器.它允许您定义输入和输出依赖关系,并且只有在输出比输入更旧时才会运行.

                  Use ADD_CUSTOM_COMMAND to trigger your generator. It allows you to define input and output dependencies and will only run if the outputs are older than the inputs.

                  ADD_CUSTOM_COMMAND( OUTPUT generatedfile1 generatedfile2
                                      COMMAND python generateSources.py xmlfile1 xmlfile2
                                      DEPENDS xmlfile1 xmlfile2 generateSources.py 
                                      COMMENT "Generating source code from XML" )
                  

                  确保生成的文件不会在多个可能并行编译的独立目标中使用,否则您可能(将)在构建过程中遇到冲突.为了确保这一点,以下内容应该可以解决问题:

                  Make sure that the generated files are not used in more than one independent target that may compile in parallel or you may(will) get a conflict during your build. To ensure this, the following should do the trick:

                  ADD_CUSTOM_TARGET( RunGenerator DEPENDS generatedfile1 generatedfile2 
                                     COMMENT "Checking if re-generation is required" )
                  

                  然后让你的其他目标依赖于这个:

                  Then make your other targets depend on this one:

                  ADD_DEPENDENCIES( MyTarget RunGenerator )
                  

                  注意: RunGenerator 目标将始终被视为过时,因此始终运行.但是,因为在这种情况下它什么都不做(除了打印注释和检查依赖项),所以没有关系.如果需要,自定义命令将负责重新生成.

                  NB: The RunGenerator target will always be considered out-of-date and, thus, always run. However, since it does nothing (besides printing the comment and checking the dependencies) in this case, that doesn't matter. The custom command will take care of regeneration IF required.

                  评论后更新:

                  如果不知道文件名,可以使用

                  If you do not know the name of the files, you can use

                  ADD_CUSTOM_COMMAND( OUTPUT generated.timestamp
                                      COMMAND python generateSources.py xmlfile1 xmlfile2
                                      COMMAND ${CMAKE_COMMAND} -E touch generated.timestamp
                                      DEPENDS xmlfile1 xmlfile2 generateSources.py 
                                      COMMENT "Generating source code from XML" )
                  

                  但是:使用 GLOB 需要您显式运行 CMake 来更新您的文件列表.将此集成到自定义命令中可能会扰乱您的构建过程(如果多个项目正在并行构建并且一个项目重新启动 CMake 配置).IIRC,当您知道 python 脚本或 XML 文件发生更改时,您可以手动运行 CMake,但您的问题是当其他任何事情需要重新运行 CMake 时,这些文件会被触及.

                  However: Using GLOB requires you to explicitly run CMake to update your file lists. Integrating this into the custom command would probably mess up your build process (if several projects are building in parallel and one project restarts CMake configuration). IIRC, it is ok for you to run CMake manually when you know that either the python script or the XML files changed but your problem is that those files are touched when anything else requires a re-run of CMake.

                  如果 python 脚本运行时间不长,你可以让它在每次 CMake 运行时运行(就像你现在做的那样),但要确保未更改的文件不会被触及,你可以尝试以下(未经测试):

                  If the python script does not take too long to run, you could let it run with each CMake run (like you do now) but make sure that the unchanged files do not get touched, you can try the following (untested):

                  # generated sources files into a temporary directory (adjust your current execute_process)
                  EXECUTE_PROCESS( COMMAND python ../generateSources.py ../xmlfile1 ../xmlfile2 
                                   WORKING_DIRECTORY tmp )
                  
                  # get the filenames
                  FILE( GLOB GENERATED_TEMP_FILES tmp/* )
                  
                  # copy to the "expected" directory, but only if content CHANGED
                  FOREACH( F ${GENERATED_TEMP_FILES} )
                      GET_FILENAME_COMPONENT( "${F}" FN NAME)
                      CONFIGURE_FILE( "${F}" "./generated/${FN}" COPY_ONLY )
                  ENDFOREACH()
                  
                  # use your current globbing command
                  FILE( GLOB GENERATED_SOURCES ./generated/* )
                  

                  这篇关于当生成器或输入在 CMake 中发生变化时,如何仅构建自动生成的代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:构建库时的 Clang 和未定义符号 下一篇:cmake 简单配置文件示例

                  相关文章

                  <small id='0D3Y5'></small><noframes id='0D3Y5'>

                  <i id='0D3Y5'><tr id='0D3Y5'><dt id='0D3Y5'><q id='0D3Y5'><span id='0D3Y5'><b id='0D3Y5'><form id='0D3Y5'><ins id='0D3Y5'></ins><ul id='0D3Y5'></ul><sub id='0D3Y5'></sub></form><legend id='0D3Y5'></legend><bdo id='0D3Y5'><pre id='0D3Y5'><center id='0D3Y5'></center></pre></bdo></b><th id='0D3Y5'></th></span></q></dt></tr></i><div id='0D3Y5'><tfoot id='0D3Y5'></tfoot><dl id='0D3Y5'><fieldset id='0D3Y5'></fieldset></dl></div>

                  <legend id='0D3Y5'><style id='0D3Y5'><dir id='0D3Y5'><q id='0D3Y5'></q></dir></style></legend>
                  1. <tfoot id='0D3Y5'></tfoot>
                      <bdo id='0D3Y5'></bdo><ul id='0D3Y5'></ul>