86

In a makefile, can I call a rule from another rule?

Similar to:

rule1:
        echo "bye"
rule2:
        date
rule3:
        @echo "hello"
        rule1

5 Answers 5

132

Either use dependencies or recursive making to connect from one rule to another.

Dependencies would be done like this (though the order will be different):

rule1:
        echo "bye"
rule2:
        date
rule3: rule1
        @echo "hello"

Recursive make would be done like this (though it does involve a subprocess):

rule1:
        echo "bye"
rule2:
        date
rule3:
        @echo "hello"
        $(MAKE) rule1

Neither is perfect; indeed, with recursive make you can get into significant problems if you build a loop. You also probably ought to add a .PHONY rule so as to mark those rules above as synthetic, so that a stray rule1 (etc.) in the directory won't cause confusion.

35

Just add a new rule with the ordering you want..

rule1:
        echo "bye"
rule2:
        date
rule3:
        @echo "hello"

rule4: rule3 rule1
5
  • Imho this is a much nicer solution than the other answers. Jul 3, 2017 at 13:33
  • Simple - I like it. Nov 20, 2017 at 21:31
  • 14
    This won't impose the ordering. It will just assure that rule1 and rule3 are done before rule4, no assurance on ordering Dec 8, 2017 at 20:31
  • 1
    I second CristianDonosoC's comment. Since there is no ordering between rule1 and rule3 here, doing a parallel make (make -j2, for instance) will give you indeterminant execution order.
    – seanahern
    Sep 6, 2018 at 14:30
  • 1
    There is a way to run pre-requisites in a specified order: gnu.org/software/make/manual/html_node/Prerequisite-Types.html – Using this, rule4 should be: rule4: | rule3 rule1
    – Yoone
    Oct 15, 2020 at 23:11
20

Makefiles are not procedural; "rules" are not like functions. That said, you can specify that one rule is a prerequisite of another:

rule1:
    @echo "Rule 1"

rule2: rule1
    @echo "Rule 2"

If you do make rule2, you should see:

Rule 1
Rule 2
4
  • 3
    Frustratingly, though, you can't get "Rule 2\nRule 1" out of it Dec 27, 2011 at 16:14
  • 2
    @Tomalak: Depends on your definition of "frustrating"! Once the OP accepts that rules are not procedural functions, then there's not much to be frustrated about. Dec 27, 2011 at 16:16
  • 1
    It is not answering the question. It should output Rule 2 and then Rule 1. This is what the author want to achieve.
    – nosbor
    Nov 1, 2016 at 20:22
  • 1
    @nosbor - the OP does not state what specific output is desired. Nov 1, 2016 at 23:47
5

There are two advanced functions in GNU Make which can do this, although it should only be used in extenuating circumstances. This SO is top rated in google.

Rule prerequisites are more recommended, but sometimes you need a post-requisite.

GNU Make Call function

GNU Make Eval function

Essentially, Eval lets you build targets on the fly, and Call allows function like "defines" to be created.

0

A simple way to do it is:

ifeq (a, b)
    build_target:=one
else
    build_target:=two
endif

mytarget: $(build_target)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.