如何限制SCons Command构建器仅在其依赖项已更改时才运行?

时间:2021-07-21 07:27:14

I am using the Command builder in scons to specify that a particular script needs to be invoked to produce a particular file.

我正在使用scons中的Command构建器来指定需要调用特定脚本来生成特定文件。

I would like to only run the script if it has been modified since the file was previously generated. The default behaviour of the Command builder seems to be to always run the script. How can I change this?

我想只运行该脚本,因为该文件之前已经生成过。 “命令”构建器的默认行为似乎是始终运行脚本。我怎么能改变这个?

This is my current SConstruct:

这是我目前的SConstruct:

speed = Command('speed_analysis.tex','','python code/speed.py')
report = PDF(target = 'report.pdf', source = 'report.tex')
Depends(report, speed)

2 个解决方案

#1


First, it looks like code/speed.py has no control on the output filename... Hardcoded output filenames are usually considered bad practice in scons (see yacc tool). It would read better like this:

首先,看起来code / speed.py无法控制输出文件名...硬编码输出文件名在scons中通常被认为是不好的做法(参见yacc工具)。它会更好地读取:

speed = Command('speed_analysis.tex', [], 'python code/speed.py -o $TARGET')

Now, the PDF target produces a report.pdf from report.tex. I'm guessing there's an implicit dependency from report.tex to speed_analysis.tex (through Tex include or something like that).

现在,PDF目标从report.tex生成report.pdf。我猜测有一个从report.tex到speed_analysis.tex的隐式依赖(通过Tex include或类似的东西)。

This:

Depends(report, speed)

Is correct to express that dependency if it's missing. Though I'm surprised the scanner for the PDF builder did not see that implicit dependency...

如果它缺失则表达依赖是正确的。虽然我很惊讶PDF构建器的扫描程序没有看到隐式依赖...

You should verify the dep tree using:

您应该使用以下方法验证dep树:

scons --tree=all

It should look something like this:

它应该看起来像这样:

+ report.pdf
  + report.tex
  + speed_analysis.tex
    + code/speed.py
    + /usr/bin/python
  + /usr/bin/pdflatex

Now, to answer your question about the script (speed.py) always running, that's because it has no input. There's nothing for scons to check against. That script file must be reading something as an input, if only the py file itself. You need to tell scons about all direct and implicit dependencies for it to short-circuit subsequent runs:

现在,回答你关于脚本(speed.py)始终运行的问题,那是因为它没有输入。没有什么可以让scons检查。该脚本文件必须读取内容作为输入,如果只是py文件本身。您需要告诉scons所有直接和隐式依赖关系,以便短路后续运行:

Command('speed_analysis.tex', 'code/speed.py', 'python $SOURCE -o $TARGET')

#2


Maybe your example is incomplete, but aren't you supposed to do:

也许你的例子不完整,但你不应该这样做:

env = Environment()
env.Command(....

I think you need to specify your dependencies as the second argument to Command:

我认为您需要将您的依赖项指定为Command的第二个参数:

Command('speed_analysis.tex','code/speed.py','python code/speed.py')

#1


First, it looks like code/speed.py has no control on the output filename... Hardcoded output filenames are usually considered bad practice in scons (see yacc tool). It would read better like this:

首先,看起来code / speed.py无法控制输出文件名...硬编码输出文件名在scons中通常被认为是不好的做法(参见yacc工具)。它会更好地读取:

speed = Command('speed_analysis.tex', [], 'python code/speed.py -o $TARGET')

Now, the PDF target produces a report.pdf from report.tex. I'm guessing there's an implicit dependency from report.tex to speed_analysis.tex (through Tex include or something like that).

现在,PDF目标从report.tex生成report.pdf。我猜测有一个从report.tex到speed_analysis.tex的隐式依赖(通过Tex include或类似的东西)。

This:

Depends(report, speed)

Is correct to express that dependency if it's missing. Though I'm surprised the scanner for the PDF builder did not see that implicit dependency...

如果它缺失则表达依赖是正确的。虽然我很惊讶PDF构建器的扫描程序没有看到隐式依赖...

You should verify the dep tree using:

您应该使用以下方法验证dep树:

scons --tree=all

It should look something like this:

它应该看起来像这样:

+ report.pdf
  + report.tex
  + speed_analysis.tex
    + code/speed.py
    + /usr/bin/python
  + /usr/bin/pdflatex

Now, to answer your question about the script (speed.py) always running, that's because it has no input. There's nothing for scons to check against. That script file must be reading something as an input, if only the py file itself. You need to tell scons about all direct and implicit dependencies for it to short-circuit subsequent runs:

现在,回答你关于脚本(speed.py)始终运行的问题,那是因为它没有输入。没有什么可以让scons检查。该脚本文件必须读取内容作为输入,如果只是py文件本身。您需要告诉scons所有直接和隐式依赖关系,以便短路后续运行:

Command('speed_analysis.tex', 'code/speed.py', 'python $SOURCE -o $TARGET')

#2


Maybe your example is incomplete, but aren't you supposed to do:

也许你的例子不完整,但你不应该这样做:

env = Environment()
env.Command(....

I think you need to specify your dependencies as the second argument to Command:

我认为您需要将您的依赖项指定为Command的第二个参数:

Command('speed_analysis.tex','code/speed.py','python code/speed.py')