在scons中,我如何注入要构建的目标?

Ros*_*ers 5 scons

我想注入一个"清理"目标,它取决于许多其他目标在它关闭之前完成和gzip的一些日志文件.重要的是我不要早点gzip,因为这会导致一些工具失败.

如何为Scons注入一个清理目标来执行?

例如,我有目标foo和bar.我想注入一个名为'cleanup'的新自定义目标,它依赖于foo和bar,并在它们完成后运行,而无需用户指定

% scons foo cleanup
Run Code Online (Sandbox Code Playgroud)

我希望他们输入:

% scons foo
Run Code Online (Sandbox Code Playgroud)

但是让scons像用户输入一样执行

% scons foo cleanup
Run Code Online (Sandbox Code Playgroud)

我已经尝试创建清理目标并附加到sys.argv,但似乎scons在到达我的代码时已经处理了sys.argv,因此它不处理我手动附加到的'cleanup'目标sys.argv中.

g_d*_*iel 12

您不应该使用_Add_Targets或未记录的功能,您只需将清理目标添加到BUILD_TARGETS:

from SCons.Script import BUILD_TARGETS
BUILD_TARGETS.append('cleanup')
Run Code Online (Sandbox Code Playgroud)

如果你使用这个记录的目标列表而不是未记录的函数,那么在进行簿记时不会混淆scons.此评论块可在以下位置找到SCons/Script/__init__.py:

# BUILD_TARGETS can be modified in the SConscript files.  If so, we
# want to treat the modified BUILD_TARGETS list as if they specified
# targets on the command line.  To do that, though, we need to know if
# BUILD_TARGETS was modified through "official" APIs or by hand.  We do
# this by updating two lists in parallel, the documented BUILD_TARGETS
# list, above, and this internal _build_plus_default targets list which
# should only have "official" API changes.  Then Script/Main.py can
# compare these two afterwards to figure out if the user added their
# own targets to BUILD_TARGETS.
Run Code Online (Sandbox Code Playgroud)

所以我想它的目的是改变BUILD_TARGETS而不是调用内部辅助函数


Ros*_*ers 1

在 SCons 1.1.0.d20081104 版本中,您可以使用私有内部 SCons 方法:

SCons.Script._Add_Targets( [ 'MY_INJECTED_TARGET' ] )
Run Code Online (Sandbox Code Playgroud)

如果用户输入:

% scons foo bar 
Run Code Online (Sandbox Code Playgroud)

上面的代码片段将导致 SCons 的行为就像用户键入了:

% scons foo bar MY_INJECTED_TARGET
Run Code Online (Sandbox Code Playgroud)

  • 不要使用未记录的“功能”;特别是当有可使用的记录功能时:BUILD_TARGETS.append('cleanup') (2认同)