Skip to content
\n

This allows me to enter scons --run to build & run. But if I up-arrow in the terminal to rerun this command, scon says (rightly) that there's nothing to build and does not do the post option.

\n

How can I make it do the post option in this second case?

","upvoteCount":1,"answerCount":3,"acceptedAnswer":{"@type":"Answer","text":"

At its very simplest, this should work:

\n
prog = Program(\"hello.c\")\npname = str(prog[0])\nrunit = Command(target=f'r_{pname}', source=prog, action=(f'-./{pname}'))
\n

In this case, Command has a target that uses a made-up name, just prepending something to the name of the Program 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.

\n

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 AlwaysBuild would come into play). The Depends call from the recipe isn't needed if you are okay having the built program as the source, otherwise you'd need it.

\n

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:

\n
prog = Program(\"hello.c\")\nDefault(prog)\npname = str(prog[0])\nrunit = Command(target=f'r_{pname}', source=prog, action=(f'-./{pname}'))\nalias = Alias(\"run\", runit)
\n

which might look like:

\n
$ scons -Q\ngcc -o hello.o -c hello.c\ngcc -o hello hello.o\n$ scons -Q\nscons: `hello' is up to date.\n$ scons -Q run\n./hello\nHello world\n$ rm hello\n$ scons -Q run\ngcc -o hello hello.o\n./hello\nHello world\n
\n

I'd recommend taking the simplest version and running some experiments, like build with --tree=all to make sure the plumbing looks right, and don't get more complicated unless you need to.

","upvoteCount":1,"url":"https://github.com/SCons/scons/discussions/4582#discussioncomment-10230561"}}}
Discussion options

You must be logged in to vote

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, Command has a target that uses a made-up name, just prepending something to the name of the Program 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…

Replies: 3 comments 3 replies

Comment options

You must be logged in to vote
1 reply
@mark-summerfield
Comment options

Comment options

You must be logged in to vote
1 reply
@mark-summerfield
Comment options

Comment options

You must be logged in to vote
1 reply
@mark-summerfield
Comment options

Answer selected by mark-summerfield
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants