Friday, October 29, 2021

[SOLVED] GNU Make rule that executes only if target exists?

Issue

In a fairly complex Makefile, I'd occasionally like to invoke certain rules only if a target does exist. For example, I may have created a local directory and used losetup to mount a file onto it. I'd like to leave the mountpoint and the directory open while working on them, but automatically close them before certain operations.

When it's time to package things up, I want to sync the mounted file, umount it, and then do something with the underlying file. Is there are way to invert the sense of a rule so that it executes only if a particular target is present?


Solution

How about something like:

target = $(wildcard somefile)

$(target): ; @echo build $@

.PHONY: $(target)


Answered By - MadScientist