7

In GNU Make 3.81, I need to remove a lockfile in the event of an error in any part of the toolchain. Is there a special target that will allow me to do this? Do I need to write a wrapper script?

In the example below, I need unlock_id to happen if the rule for file.out fails.

Thanks! -Jeff

all: lock_id file.out unlock_id

file.out: file.in
    file-maker < file.in > $@

lock_id:
    lockfile file.lock

unlock_id:
    rm -rf file.lock
Jeff
  • 552
  • 1
  • 8
  • 16

3 Answers3

7

I would do the lock/unlock in the same target as file-maker:

file.out: file.in
        lockfile $@.lock
        file-maker < $< > $@; \
        status=$$?; \
        rm -f $@.lock; \
        exit $$status

This executes the file-maker and unlock steps in the same shell, saving the status of file-maker so make will fail if file-maker fails.

Jack Kelly
  • 18,264
  • 2
  • 56
  • 81
4

This is kind of a kludge, but it works:

all:
        @$(MAKE) file.out || $(MAKE) unlock_id
Beta
  • 96,650
  • 16
  • 149
  • 150
  • You can also spam this after any commands that might fail as an alternative to Beta's global solution. `./commandThatMightFail.sh || $(MAKE) unlock_id` – reasgt Apr 26 '12 at 04:00
0

You want the .DELETE_ON_ERROR target, which allows you to specify files to be deleted upon errors.

http://www.gnu.org/s/hello/manual/make/Special-Targets.html

EDIT

My bad, that's a half-truth. It allows you specify that you want files to be deleted, but as for which ones and under what circumstances, that's up to make.

Chris Eberle
  • 47,994
  • 12
  • 82
  • 119
  • Hi Chris, I have a .DELETE_ON_ERROR, but the documentation indicates it works differently. It looks like it will delete $@ if the respective target fails, for all targets in the Makefile. That being said, I never really got the behavior of .DELETE_ON_ERROR to work as I expected… ".DELETE_ON_ERROR If .DELETE_ON_ERROR is mentioned as a target anywhere in the makefile, then make will delete the target of a rule if it has changed and its recipe exits with a nonzero exit status, just as it does when it receives a signal. See Errors in Recipes." Thanks! -Jeff – Jeff Jul 01 '11 at 21:51
  • 2
    @Jeff: `.DELETE_ON_ERROR` works for me (GNUMake 3.81), but only if the name of the target is the name of the file. In your case, it looks as if the name of the target is `lock_id`, but the file you're trying to build (and want to delete) is `foo.lock`. – Beta Jul 01 '11 at 22:28