I'm wondering if there's a way to implement the similar functionality as you get in bash scripts using `trap', but for gmake, such that if the user presses CTRL-C, or if make itself fails, it can call a particular target or macro.
make produces return codes. As far as I can remember right now, it returns 0 for success, 2 for failure (please check the documentation). Therefore, would it be enough for you to wrap make inside a shell script for example?
No. GNU make’s signal handling already leaves a lot to be desired. From within its signal handler, it calls functions like printf
that are not safe to be called from within a signal handler. I have seen this cause problems, for example .DELETE_ON_ERROR
rules don’t always run if stderr
is redirected to stdout
.
Make does not support it, but using BASH tricks you can accomplish something similar.
default: complete
complete: do_mount
echo "Do something here..."
do_mount:
mkdir -p "$(MOUNTPOINT)"
( while ps -p $$PPID >/dev/null ; do \
sleep 1 ; \
done ; \
unmount "$(MOUNTPOINT)" \
) &
mount "$(MOUNTSOURCE)" "$(MOUNTPOINT)" -o bind
The "unmount" will run after the "make" completes. This is usually a satisfactory solution if you are attempting to cleanup operations that may occur during the build but are not cleaned up normally on "make" exit.