TL;DR, how to add scons dependency on an optional file which may or may not exist?
Let's say that we have an scons builder like the one below where the command processor
reads an optional config file .processorrc
which configures its operation.
I understand how to add a dependency on a file that exists or can be created automatically but how can I represent a dependency on the optional file .processorrc
such that any change/deletion/creation of ti will cause processor
to be executed?
bld = Builder(action = 'processor < $SOURCE > $TARGET')
Don't promise you can get this to work in a way that suits your needs, but you might be able to rig something up with ParseDepends
. That method reads an external dependency file to add dependency relationships, and by default just skips it if the dep file isn't there. However, it would be an additional file you have to create if the dot file you want to optionally depend on exists, because the dep file itself has to be in a specific format (target: dep1 dep2... depN
). See if this gives you any ideas:
https://scons.org/doc/production/HTML/scons-user.html#id1330
(sorry that's one of the User Guide chapters that hasn't yet been converted to named section anchors, so it's kind of a funky link).
Edited: here's a very simple-minded example of this in play, with stuff hardcoded, etc.:
import pathlib
DOTFILEPATH = ".fuggle"
DEPFILEPATH = "fuggle.dep"
def dot_dep(source, target, env) -> None:
"""Manage dependency file based on existence of DOTFILEPATH."""
dotfile = pathlib.Path(DOTFILEPATH)
depfile = pathlib.Path(DEPFILEPATH)
if dotfile.exists():
if not depfile.exists():
deptext = f"foo: {DOTFILEPATH}\n"
depfile.write_text(deptext)
else:
depfile.unlink(missing_ok=True)
Execute(dot_dep)
ParseDepends(DEPFILEPATH)
Command("foo", "foo.in", Copy("$TARGET", "$SOURCE"))
Here's the file being removed in action:
$ scons -Q --tree=all --debug=explain
dot_dep([], [])
scons: `.' is up to date.
+-.
+-.fuggle
+-SConstruct
+-foo
| +-foo.in
| +-.fuggle
+-foo.in
$ rm .fuggle
$ scons -Q --tree=all --debug=explain
dot_dep([], [])
scons: rebuilding `foo' because `.fuggle' is no longer a dependency
Copy("foo", "foo.in")
+-.
+-.fuggle
+-SConstruct
+-foo
| +-foo.in
+-foo.in