Scons: Generate version file only if target is changed

I have a requirement to create a version.cc file from SCons Script. This file should only be generated if any source file has been modified for the purpose.

Assume the SCons script has the following statements

#python function which generates version.cc in the same folder where libtest.a is generated. This will always generate a differnt version.cc because the version string contained inside that will have timestamp GenerateVersionCode() #target which uses version.cc libtest = env.Library('test', ['a.cc', 'b.cc', 'version.cc']) 

The first time I run the above code, everything is fine. But when I run the same script again, the target "test" will be rebuilt due to the new .cc version that was generated. My requirement is that we should not generate a new version of the version.cc file if the file is already present, and no changes in any of the sources (namely, a.cc and b.cc in this example)

    if not version_file_present:
         GenerateVersionCode ()
     else 
         if no_changes_in_source:  
             GenerateVersionCode ()


     #target which uses version.cc which could be newly generated one or previous one
     libtest = env.Library ('test', ['a.cc', 'b.cc', 'version.cc'])

One related question on this site suggested the following:

     env.Command (target = "version.c", source = "version-in.c",
         action = PythonFunctionToUpdateContents)
     env.Program ("foo", ["foo.c", "version.c"])

Wr in the above assumption, I would like to know the contents of the PythonFunctionToUpdateContents function, which checks for changes to the source files since they were created.

+4
source share
1 answer

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

If so, you can consider two sets of dependencies, both of which will be controlled by the SCons dependency check.

It is not clear what version.cc generation consists of, but suppose the python GenerateVersionCode () function will do just that: generate the .cc version, but will not have any dependency related to the dependency 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 does not have to be necessary, but it can be done one more step by explicitly setting the dependency on the Library target to target.cc using the SCons Depends () function.

Here is the result that I get when creating, and instead of using the GenerateVersionCode () function, I use the simple shell script versionGen.sh, thereby changing the Command () call 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 ao -c a.cc g++ -o bo -c b.cc ./versionGen.sh g++ -o version.o -c version.cc ar rc libtest.a version.o ao bo ranlib libtest.a scons: done building targets. 

Then, without changing anything, I build again, and he 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 ao -c a.cc ./versionGen.sh g++ -o version.o -c version.cc ar rc libtest.a version.o ao bo ranlib libtest.a scons: done building targets. 
+5
source

Source: https://habr.com/ru/post/1488348/


All Articles