Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Makefile: how to detect changes within the makefile itself?

Tags:

makefile

I'm aware of the idea of using recursive makefiles. Will the subsequent makefiles such as the following be called be updated solely on any changes to the subsequent makefiles themselves?

e.g.:

#parent makefile. no changes here. 
subsystem:
    cd subdir && $(MAKE)

If the makefile within subdir was changed such that the following does not hold (e.g. only a gcc flag was changed), then will the object files be updated?

The recompilation must be done if the source file, or any of the header files named as dependencies, is more recent than the object file, or if the object file does not exist.

like image 689
imagineerThat Avatar asked Jan 06 '15 03:01

imagineerThat


2 Answers

The only reason that, as written, make even runs that rule at all is because subsystem and subdir do not match.

If a subsystem file or directory were ever to be created in that directory that rule would cease to function.

If .PHONY: subsystem1 were added that problem would be fixed and that rule would always be run when listed on the command line (i.e. make subsystem). (As indicated in the comments .PHONY is a GNU Make extension. The section following the linked section discusses a portable alternative. Though it is worth noting that they are not completely identical in that .PHONY has some extra benefits and some extra limitations.)

In neither of those cases is the subsystem target paying any attention to modification dates of anything (as it lists no prerequisites).

To have a target depend on changes to a makefile you need to list the makefile(s) as prerequisites like anything else (i.e. subsystem: subdir/Makefile). Listing it as .PHONY is likely more correct and more what you want.

No, nothing in make itself tracks non-prerequisites. So flag changes/etc. do not trigger rebuilds. There are ways to make that work for make however (they involve storing the used flags in files that themselves are prerequisites of the targets that use those flags, etc.). There are questions and answers on SO about doing that (I don't have them ready offhand though).

Other tools do handle flag changes automatically however. I believe Electric Cloud's tools do this. I believe CMake does as well. There might also be others.

like image 178
Etan Reisner Avatar answered Sep 20 '22 05:09

Etan Reisner


Recursive makefiles are executed whether or not anything changed. This is exactly one of the objections pointed out by Paul Miller in his Recursive make considered harmful paper from almost 20 years ago.

With that said, a makefile is just like any other dependency and can be added to a production rule to trigger that rule if the makefile is altered.

like image 25
Edward Avatar answered Sep 19 '22 05:09

Edward