如何只能建自动生成的code当C进行发电机或输入的变化? [英] How to only build auto generated code when the generator or input changes in CMake?

查看:80
本文介绍了如何只能建自动生成的code当C进行发电机或输入的变化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作,通过运行一个python脚本输出头和实施产生了一些C ++ code源$ C ​​$ C库。这code随后被编译和链接到我的库和可执行文件。我知道,如果两个条件之一为真,将生成的code只会改变:

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. 发电机code本身的变化

  2. 输入(XML文件)到发电机的变化

我想用cmake的管理构建过程。目前,我使用 execute_process 来火了发电机。然而,这种运行每次运行cmake的时间,其接触到的文件,造成我产生code重新编译,并增加我的总编译时间。

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.

我也想确保生成的code之前我总是库运行。换句话说,我希望库依靠发电机已运行。

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的这种情况的正确方法?我已经看到了这previous回答:<一个href=\"http://stackoverflow.com/questions/5385148/get-cmake-to-execute-a-target-in-project-before-building-a-library\">Get CMake的构建库之前项目执行的目标。但是这依赖于code发生器事先是已知的输出。我的code发生器会产生不同数量的文件。

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_DEPENDENCY( 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,这是确定您手动运行CMake的,当你知道,无论是python脚本或XML文件改变,但你的问题是,当别的需要的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/* )

这篇关于如何只能建自动生成的code当C进行发电机或输入的变化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆