How can I make an action run if build is successful or unneeded #4582
-
I want to make an action run if a build is successful or if the build isn't needed (i.e., no source changes). This will make an action run if the build is successful:
This allows me to enter How can I make it do the post option in this second case? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 3 replies
-
You've already realized that a post (or pre) action attached to a target doesn't trigger if the target isn't rebuilt. From your description, it's not clear why you want to run something even if the target wasn't built... if you mean you want to actually execute the command (like might be the case for unit tests), you have to take a bit of a different approach. Don't take this as the definitive answer, but you could try this (there are several ways to skin this cat, and this may not be the best):
Now build as |
Beta Was this translation helpful? Give feedback.
-
You could just setup an atexit. See: https://docs.python.org/3/library/atexit.html for info @mwichmann's is another scons-only way to do this. |
Beta Was this translation helpful? Give feedback.
-
At its very simplest, this should work: prog = Program("hello.c")
pname = str(prog[0])
runit = Command(target=f'r_{pname}', source=prog, action=(f'-./{pname}')) In this case, The more detailed recipe accounts for more complexity, such as when there are multiple programs to run (the accumulating nature of Alias is useful for that), or when the action actually produces something that looks like a target - like an output log (if you use a logfile or other tangible thing as the runner target, that's where SCons by default selects all targets under the project root, so in the minimal example both building the program and running it are selected. If you want the default to be "build" and "running" to have to be explicitly asked for, you can expand the recipe a bit: prog = Program("hello.c")
Default(prog)
pname = str(prog[0])
runit = Command(target=f'r_{pname}', source=prog, action=(f'-./{pname}'))
alias = Alias("run", runit) which might look like:
I'd recommend taking the simplest version and running some experiments, like build with |
Beta Was this translation helpful? Give feedback.
At its very simplest, this should work:
In this case,
Command
has a target that uses a made-up name, just prepending something to the name of theProgram
target, and its source is the built program, and since the action never "creates" that target, it's always out of date and so runs every time.The more detailed recipe accounts for more complexity, such as when there are multiple programs to run (the accumulating nature of Alias is useful for that), or when the action actually produces something that looks like a target - like an output log (if you use a logfile or oth…