Search code examples
makefiledependencies

Makefile execute phonytargets of a file only if file does not exists


Is there any way I can only execute the phony dependency of a file if this file does not exist?

If the file has no phony dependency it works and only executes the content of this rule. But if I add a phony target as dependency it keeps executing the dependency rule and the rule of generating the existing file.

I simplified my Makefile so you can see my problem:

 
.PHONY: phonytarget all clean

all: $(CURDIR)/a.a
    @echo done

$(CURDIR)/a.a: phonytarget
    @touch $@

phonytarget:
    @echo what the heck is wrong with you
 

Solution

  • Use order-only prerequisites:

    Occasionally, however, you have a situation where you want to impose a specific ordering on the rules to be invoked without forcing the target to be updated if one of those rules is executed. In that case, you want to define order-only prerequisites. Order-only prerequisites can be specified by placing a pipe symbol (|) in the prerequisites list: any prerequisites to the left of the pipe symbol are normal; any prerequisites to the right are order-only:

    targets : normal-prerequisites | order-only-prerequisites
    

    The normal prerequisites section may of course be empty. Also, you may still declare multiple lines of prerequisites for the same target: they are appended appropriately (normal prerequisites are appended to the list of normal prerequisites; order-only prerequisites are appended to the list of order-only prerequisites).

    .PHONY: phonytarget all clean
    
    all: $(CURDIR)/a.a
        @echo done
    
    $(CURDIR)/a.a: | phonytarget
        @touch $@
    
    phonytarget:
        @echo what the hack is wrong with you