Scons: Generating version file only if target has changed

 ̄綄美尐妖づ 提交于 2019-12-05 17:04:39

As I understand it, you only want to generate version.cc if any of the source files change, and you only want to build the library if version.cc changes or if any of the library source files change. That is, consider version.cc as one of the source files for the library.

If this is the case, you could consider 2 sets of dependencies, both of which would be controlled by the SCons dependency checking.

Its not real clear what the version.cc generation consists of, but lets assume that the python function GenerateVersionCode() will do exactly that: generate version.cc, but wont have any dependency checking related logic.

Here is the SConscript code:

def GenerateVersionCode(env, target, source):
   # fill in generation code here

# The version.cc checking
env.Command(target='version.cc',
            source=['a.cc', 'b.cc'],
            action=GenerateVersionCode)

# The library
env.Library(target='test', source=['version.cc', 'a.cc', 'b.cc'])

It shouldnt be necessary, but this could be taken one step further by explicitly setting a dependency from the Library target to the version.cc target with the SCons Depends() function.

Here is the output I get when I build, and instead of using the GenerateVersionCode() function, I use a simple shell script versionGen.sh, thus changing the call to Command() to this:

env.Command(target='version.cc',
            source=['a.cc', 'b.cc'],
            action='./versionGen.sh')

Here is the first build:

> scons
scons: Reading SConscript files ...
scons: done reading SConscript files.
scons: Building targets ...
g++ -o a.o -c a.cc
g++ -o b.o -c b.cc
./versionGen.sh
g++ -o version.o -c version.cc
ar rc libtest.a version.o a.o b.o
ranlib libtest.a
scons: done building targets.

Then, without having changed anything, I build again, and it does nothing:

> scons
scons: Reading SConscript files ...
scons: done reading SConscript files.
scons: Building targets ...
scons: `.' is up to date.
scons: done building targets.

Then, I modify a.cc, and build again, and it generates a new version of version.cc:

> vi a.cc
> scons
scons: Reading SConscript files ...
scons: done reading SConscript files.
scons: Building targets ...
g++ -o a.o -c a.cc
./versionGen.sh
g++ -o version.o -c version.cc
ar rc libtest.a version.o a.o b.o
ranlib libtest.a
scons: done building targets.
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!