如何在SCons构建中添加前处理和后处理操作? [英] How to add pre and post-process actions to SCons build?

查看:140
本文介绍了如何在SCons构建中添加前处理和后处理操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用SCons构建项目时,我试图添加预处理和后处理动作.

SConstruct和SConscript文件位于项目的顶部.

预处理操作:生成代码(通过调用不同的工具):->不知道在此预处理之后将要生成的确切文件(可以创建用于确定生成哪些文件的附加预处理,以便向SCons馈送文件)

->运行外部脚本(python,pearl脚本),在编译前执行

后期处理操作:

->运行外部工具,运行应该在链接后执行的外部脚本

直到现在我一直在尝试:

对于预处理:

  • 要从python使用os.system来运行cmd.(工作正常,但我正在寻找"SCons解决方案")
  • 要使用SCons中的 AddPreAction(target,action)函数.不幸的是,此功能是在编译项目后执行的,如SCons用户手册所述:指定的pre_action将在scons调用实际生成的链接命令之前执行可执行程序二进制foo,而不是在将foo.c文件编译为目标文件之前."

对于后处理:

  • 要使用 AddPostAction(target,action),这很好,很好.

我正在寻找可以使SCons某种程度上了解此前后过程的解决方案.

我的问题如下:

对于上述要求,使用SCons的最佳方法是什么?有没有一种方法可以在使用SCons内置函数进行编译之前执行预处理操作?

解决方案

对于要使预处理部分正常工作的尝试,您没有提供太多详细信息.通常,您应该尝试为代码生成部分创建真正的生成器...这将使SCons(以及您作为用户;)的依赖项的检测和处理更加容易.您可能想在 https://bitbucket.org/scons/scons/wiki上查看我们的Wiki./ToolsForFools ,我们在其中详细解释了如何编写新的Builders.

如果您需要在每个版本上运行其他脚本,则应该可以通过 os.system()或相应的 subprocess 调用正确触发这些脚本例如,顶层SConstruct的开始.但是,我从您的最新编辑中得到的,主要是您提出的第一个问题,是您正在尝试对某种分阶段"的构建过程进行建模.您认为您需要一个预处理"阶段,您可以在其中通过调用脚本来创建所需的所有其他头文件和源代码,并进行连接.我的猜测是,您正在尝试重写诸如原始make/autotools设置之类的东西,并希望在可能的情况下重用零件,这当然不是一个坏主意.但是SCons不是阶段驱动的,它是依赖驱动的...所以您当前的方法不合适,并且可能迟早会导致问题.

您能做的最好的事情就是忘记Pre-和PostActions并弄清楚您的依赖项.除了编写自己的Builder来替换脚本之外,您还必须为每个Builder实施适当的Emitter.此Emitter(检查上述工具指南)将必须解析进入脚本的输入文件,并返回在脚本实际运行时将生成的文件名列表.这样,SCons就会知道先验,一旦运行构建脚本,就会生成哪些文件,并且可以使用这些名称来解决依赖关系(即使实际文件尚不存在)./p>

对于后处理部分:通常使用标准的Python atexit处理程序来处理.参见例如在每次构建后如何运行一些代码scons?.

I'm trying to add pre and post-process actions when building a project with SCons.

The SConstruct and SConscript files are at the top of the project.

Pre-process actions: Generating code(by calling different tools): -> without knowing the exact files that will be generated after this pre-process (additional pre-process for deciding which files were generated can be created in order to feed SCons with them)

-> running external scripts(python, pearl scripts), executed before compilation

Post-process actions:

->running external tools, running external scripts that should be executed after linking

What I tried until now:

For pre-process:

  • To use os.system from python in order to run a cmd. ( works fine but I'm looking for a "SCons solution" )
  • To use AddPreAction(target, action) function from SCons. Unfortunately this function is executed after compiling the project as the SCons user manual states: "The specified pre_action would be executed before scons calls the link command that actually generates the executable program binary foo, not before compiling the foo.c file into an object file."

For post-process:

  • To use AddPostAction(target, action) and this works fine, fortunately.

I'm looking for solutions that will make SCons somehow aware of this pre and post processes.

My question is the following:

What is the best approach, for the requirements stated above, using SCons ? Is there a way to execute pre-process actions before compilation using SCons built-in functions ?

解决方案

You don't give very much detail about what you've tried to get your pre-processing part working. In general, you should try to create real Builders for the Code generation part...this will make the detection and handling of dependencies easier for SCons (and for you as the user ;) ). You may want to check out our Wiki at https://bitbucket.org/scons/scons/wiki/ToolsForFools , where we explain in large detail how to write new Builders.

If you need to run additional scripts on every build, you should be able to trigger these fine with the os.system() or an appropriate subprocess call right at the start of your top-level SConstruct for example. But what I get from your latest edit, and I'll refer mainly to the first of the questions you asked, is that you're trying to model some sort of "staged" build process. You think you need a "preprocess" stage, where you can hook into and create all the additional headers and sources you might need, by calling your scripts. My guess is, that you're trying to rewrite something like an original make/autotools setup and would like to reuse parts wherever possible, which isn't a bad idea of course. But SCons isn't stage-driven, it's dependency-driven...so your current approach is a bad fit and might lead to problems sooner or later.

The best thing you can do, is to forget Pre- and PostActions and get your dependencies straight. In addition to writing your own Builder(s) to replace your scripts, you'd have to implement a proper Emitter for each of these Builders. This Emitter (check the Tools guide mentioned above) would have to parse your input file that goes into the script, and return the list of filenames that will be generated when the script gets actually run. Like this, SCons will then know a priori which files get generated once the build script is run, and can use these names for resolving dependencies already (even if the actual files don't exist yet).

For the post-processing part: this is usually handled by using the standard Python atexit handler. See e.g. How do I run some code after every build in scons? for an example.

这篇关于如何在SCons构建中添加前处理和后处理操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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