I have a target such as this:
.PHONY: do-symlink
do-symlink:
ln -s $(HERE_ONE_FILE) $(THERE_ONE_DIR)/
ln -s $(HERE_TWO_FILE) $(THERE_ONE_DIR)/
# check if those succeed or failed. I want to make sure both have passed
if succeeded:
do_something
else:
do_something_else
How can i check that they are both succeed? and if so, do something based on it?
So first of all, if a make recipe line fails, make fails, and stops executing the recipe. So if you put a recipe line at the end, @echo "succeeded"
, then this will only run if all previous lines worked. As far as printing if something specific if one fails, you can use bash ||
for that
all:
@command1 || { echo "command1 failed"; exit 1 }
@command2 || { echo "command2 failed"; exit 1 }
@echo "command1 and command2 passed"
Notice the exit 1
's in there. Normally, false || echo "false"
would have a return status of 0
(pass), because the exit status is taken from the last command run (echo "false"
), which will always succeed. This would cause make to always continue running the next recipe line. You may want this, however, you can preserve the failure by compounding the statement and doing an exit 1
at the end.
For running both commands regardless of exit status, and then handling the exits after, prefix the recipe lines with -
. This will cause make to not stop running if the command fails. Notice however that each recipe line is run in its own shell, so one recipe line cannot directly access the return code from another line. You could output to a file and then access that file in a later recipe line:
all:
-@command1; echo $? > .commands.result
-@command2; echo $? >> .commands.result
@if grep -q "^00$" .commands.result; then \
echo both commands passed; \
else \
echo "at least one command failed"
(or you could always concatenate all the recipes lines into a single bash command and pass the variables around as well).