Best practice for makefile calling other makefile

Daniel Walker

I have a Makefile which, for a particular target, calls another Makefile. Suppose that the main Makefile contains

some_dir/some_target:
    cd some_dir && make some_target

and that some_dir/Makefile contains

some_target: file1 file2
    do_stuff

Here's my conundrum: What should the dependencies be for the target in the main Makefile? If I put no dependencies, then, according to the GNU make manual, some_dir/Makefile will only be invoked if some_dir/some_target doesn't exist. I could instead copy the dependencies from some_dir/Makefile. However, that creates the danger of later changing the dependencies in the subdirectory's Makefile and forgetting to update the main Makefile.

Is there a way to tell the main Makefile, "I don't know if the target is out of date or not. Go ask the other Makefile"?

John Bollinger

Is there a way to tell the main Makefile, "I don't know if the target is out of date or not. Go ask the other Makefile"?

There is no provision specifically for delegating to another makefile, but you can achieve a similar result by ensuring that the recipe for the target in question is always run. There are mechanisms and conventions for that.

The old-school approach is to declare a dependency on a target that does not exist and is never actually built. You may see such a target being named FORCE, though that's only a convention. The name is not directly significant. Example:

some_dir/some_target: FORCE
    cd some_dir && make some_target

# Dummy target
FORCE:

As long is there is not, in fact, a file named FORCE in the working directory, the FORCE target will be considered initially out of date, so every target that directly or indirectly depends on it will be built (see also below).

There is, of course, a weakness in that: what if a file named FORCE actually is created? However unlikely that may be, it screws up the whole scheme if it happens. Some make implementations, notably GNU's, have an implementation-specific way to address that. GNU's approach is to recognize a special, built-in target named .PHONY (do not overlook the leading .). All prerequisites of .PHONY are considered out of date on every build, notwithstanding anything on the filesystem. Inasmuch as make implementations that do not recognize that convention are unlikely to be troubled by its use, there is little drawback to putting it in play:

.PHONY: FORCE

You could also skip FORCE and just directly declare some_dir/some_target itself to be phony, as another answer suggests, but there are at least two problems with that:

  • it's not really phony in the usual sense. You expect that target to be built. Declaring it phony is therefore confusing.

  • if you happen to try to use that approach with a make that does not recognize .PHONY, then the whole scheme falls apart. If you instead use an intermediate phony target (such as FORCE, above) then your makefile still works even with such makes, except in the unlikely event that a file named the same as the dummy target is created.

But note well that however implemented, any such scheme has a significant drawback: if you force some_dir/some_target be considered out of date on every build, so that the sub-make will be run unconditionally, then every other target that depends directly or indirectly on some_dir/some_target will also be rebuilt every time. On the other hand, if you do not force it to be rebuilt, then it might not be rebuilt when it ought to be, as you already recognize. This is the topic of the well-known paper Recursive Make Considered Harmful. As an alternative, then, you should consider not using recursive make.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Makefile: Run other makefile from makefile

From Dev

Why is makefile not calling this function?

From Dev

makefile rules in other rules

From Dev

makefile to link each other

From Dev

Makefile for calling make with multiple targets

From Dev

makefile always calling the last target

From Dev

Makefile with commands depending on each other

From Dev

makefile hangs when calling shell command head

From Java

Best way to handle pipes and their exit status in a makefile

From Dev

What are the best ways to check multiple conditions in the makefile?

From Dev

Makefile Creates Two Libraries - One Depends On Other

From Java

How to call Makefile located in other directory?

From Dev

Makefile composed rule not executing defined other rule

From Dev

Makefile match rule based on variable in other file

From Dev

Makefile ignores dependency every other time

From Dev

Best practice for calling save() many times

From Dev

Best practice for calling two different endpoints for a widget?

From Dev

Vue best practice for calling a method in a child component

From Dev

Best Practice to avoid using 'ifs' for calling methods

From Dev

Best practice when calling self object methods

From Dev

Best practice for calling the NgbModal open method

From Dev

Calling a external API class on Observer best practice

From Dev

Rust - Calling function with multiple types best practice

From Dev

Best practice for calling objects that are private members of a class?

From Dev

Makefile target references Makefile

From Dev

What's the generally accepted best practice when calling NON-Async functions, from within other async functions?

From Dev

Has a batter practice to write Makefile(s) used to build docker images?

From Dev

"write error: stdout" when calling "make" from Makefile

From Dev

Makefile - multiple recipes calling one, with variables - only the first gets run

Related Related

  1. 1

    Makefile: Run other makefile from makefile

  2. 2

    Why is makefile not calling this function?

  3. 3

    makefile rules in other rules

  4. 4

    makefile to link each other

  5. 5

    Makefile for calling make with multiple targets

  6. 6

    makefile always calling the last target

  7. 7

    Makefile with commands depending on each other

  8. 8

    makefile hangs when calling shell command head

  9. 9

    Best way to handle pipes and their exit status in a makefile

  10. 10

    What are the best ways to check multiple conditions in the makefile?

  11. 11

    Makefile Creates Two Libraries - One Depends On Other

  12. 12

    How to call Makefile located in other directory?

  13. 13

    Makefile composed rule not executing defined other rule

  14. 14

    Makefile match rule based on variable in other file

  15. 15

    Makefile ignores dependency every other time

  16. 16

    Best practice for calling save() many times

  17. 17

    Best practice for calling two different endpoints for a widget?

  18. 18

    Vue best practice for calling a method in a child component

  19. 19

    Best Practice to avoid using 'ifs' for calling methods

  20. 20

    Best practice when calling self object methods

  21. 21

    Best practice for calling the NgbModal open method

  22. 22

    Calling a external API class on Observer best practice

  23. 23

    Rust - Calling function with multiple types best practice

  24. 24

    Best practice for calling objects that are private members of a class?

  25. 25

    Makefile target references Makefile

  26. 26

    What's the generally accepted best practice when calling NON-Async functions, from within other async functions?

  27. 27

    Has a batter practice to write Makefile(s) used to build docker images?

  28. 28

    "write error: stdout" when calling "make" from Makefile

  29. 29

    Makefile - multiple recipes calling one, with variables - only the first gets run

HotTag

Archive