1

I'd like to get scons to read a previous version number from a file, update a source file with a new version number and current date and then write the number back to the original file ready for the next build.

This needs to happen only when the target is out of date. IOW the version number doesn't change if no build takes place. The original file is source controlled and isn't a source file else it could trigger another build on check-in (due to CI). CLARIFICATION From scons' point of view the code will always be out of date due to the auto-generated source file but scons will only be run from a Continuous Integration job (Jenkins) when a SCM change is detected.

I've looked into AddPostMethod, but this seems to fire for all files within the list of source files.
Command and Builder methods use the VARIANT_DIR so I can't edit these files and then check them back in as they no longer map to the repo.

I'm hoping I'm just misunderstanding some of the finer details of scons else I'm running out of ideas!

Update Thinking this through some more, Tom's comment is correct. Although I have two files, one version controlled text file (non-source code) and one non-source controlled source file there is no way to check one file in and prevent a continuous build/check-in cycle. Jenkins will see the new text file and spin off a build, and scons will see the new generated file. So unless I delete the generated file at some point, although this seems to go against the workflow of both tools.

Does anyone have any method for achieving this? It seems pretty straightforward. Ultimately I just want to generate build numbers each time a build is started.

Adam Naylor
  • 6,172
  • 10
  • 49
  • 69
  • I'm not sure I understand the question. It seems to me that you'll always trigger a rebuild like that, Because once you complete a build after something has changed, then you "update a source file with the new number and date", And then the next time you do a build, the source file has changed. So it causes a new build. – Tom Tanner Jul 02 '13 at 07:39
  • @TomTanner correct except the file won't be checked in therefore CI (Jenkins in this case) won't detect the change and run scons. But you're right from an scons point of view the source has changed. I'll make that more clear in the question. – Adam Naylor Jul 02 '13 at 09:45
  • You can always do nothing in the post action if the source doesn't match the one you want? – Tom Tanner Jul 02 '13 at 09:57
  • I did go down that route but it felt like I was trying to bend the post action method to do something it wasn't designed to do. I may take another look... – Adam Naylor Jul 02 '13 at 10:01
  • you should be able to do `AddPostAction('magic source file.o', func)` if I'm reading the documentation correctly, but I've never used a post action, so I'm not entirely sure – Tom Tanner Jul 02 '13 at 10:50
  • Ah that may work better. I was using `AddPostAction(someProgram, func)` which explains why I was seeing it run for every source file rather than the single. – Adam Naylor Jul 02 '13 at 11:55
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/32760/discussion-between-adam-naylor-and-tom-tanner) – Adam Naylor Jul 02 '13 at 14:10

1 Answers1

1

From SCons User Guide section 8, Order-Only Dependencies, you can use the Requires method:

import time

# put whatever text you want in your version.c; this is just regular python
version_c_text = """
char *date = "%s";
""" % time.ctime(time.time())
open('version.c', 'w').write(version_c_text)

version_obj = Object('version.c')

hello = Program('hello.c',
                LINKFLAGS = str(version_obj[0]))

Requires(hello, version_obj)

Two things to note: first you have to add the explicit Requires dependency. Second, you can't make version_obj a source of the Program builder, you have to cheat (here we pass it as a linkflag), otherwise you'll get an automatic full dependency on it.

This will update the version.c always, but won't rebuild just because version.c changed.

GaryO
  • 5,873
  • 1
  • 36
  • 61