[Scons-users] [c++] Automatically choosing sources for linking

Viktor Dick viktordick86 at gmail.com
Wed Aug 27 04:41:05 EDT 2014


Hi everyone,
it has been a few months and I finally came around to attack this
problem again. I just tried to create an account in the wiki, but got an
"Unknown action newaccount" error. Well, for the time being let me
paraphrase my current solution:

I created a Builder that creates a .dep file from a .cpp file, where the
.dep file contains the list of other .cpp files whose object files
should also be linked whenever the object file of this file is linked.

During parsing of the SConstruct file, each "main_*.cpp" file that is
found triggers a recursive search through the .dep files and thus
creates the list of sources for the executable.

The problem is that in the parsing phase the files might not yet be
populated. I only want to rebuild the '.dep' files if necessary
(otherwise I could build the database inside the SConstruct file, but
that takes too long). So my current solution is to create an explicit
dependency of the Program node for each executable to the list of '.dep'
nodes that are found recursively *at the time of parsing*, even if they
are not up-to-date yet.

Assume I have the files 'main_a.cpp', 'b.h', 'b.cpp', 'c.h', 'd.h',
'd.cpp', and the following includes:
main_a.cpp: b.h c.h
b.cpp: d.h

First run of SCons:
create 'main_a.dep' with content 'b'
compile 'main_a.cpp' -> 'main_a.o'
link 'main_a.o' -> 'a' (failing)

Second run, parsing:
read 'main_a.dep' and conclude that 'b' is a link-time dependency
Declare that 'a' should be created from 'main_a.cpp' and 'b.cpp'
Declare explicit dependency of 'a' on 'main_a.dep' and 'b.dep'

Second run, building:
create 'b.dep' with content 'd'
compile 'b.cpp' -> 'b.o'
link 'main_a.o', 'b.o' -> 'a' (still failing)

Third run:
create 'd.dep', empty
compile 'd.cpp' -> 'd.o'
link 'main_a.o', 'b.o', 'd.o' -> 'a' (success)

On the third run, all necessary .dep files are finally in place and the
linking will succeed. Particularly, on any further runs it will just use
the .dep files from the last run and as long as these don't change the
linking will be successful. And only those .dep files that really need
to are rebuilt.

The whole thing is of course a little bit of a hack, but I guess as long
as SCons has the clear distinction between parsing and building phase
there is no way around it. In order to make a DNRY (do not repeat
yourself) compliant SConstruct file for this case we would neet the
possibility to change the source list for a Program node and add new
targets while already in the building phase. I have read about Emitters,
which can change the source list of a node, but AFAIR i tested their
behavior and they are executed during the parsing phase.

I do not know how difficult it is to include this in the design of
SCons, but what we would need is an 'execute this function when you
think you are ready to build this node (and all dependencies are
satisfied), but the function may add dependencies and sources, so you
might need to rebuild the dependency tree and postpone the building of
this node'.

Is there already such a possibility and I just didn't find it? Or is it
possible to include it without completely rewriting SCons?

Regards,
Viktor
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0xE3CD1E09.asc
Type: application/pgp-keys
Size: 37525 bytes
Desc: not available
URL: <http://four.pairlist.net/pipermail/scons-users/attachments/20140827/25338048/attachment-0001.key>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 473 bytes
Desc: OpenPGP digital signature
URL: <http://four.pairlist.net/pipermail/scons-users/attachments/20140827/25338048/attachment-0001.pgp>


More information about the Scons-users mailing list