Makefile: how to detect changes inside the makefile itself?

I know the idea of ​​using recursive makefiles. Will subsequent makefiles, such as the following, be updated solely on any changes in subsequent makefiles?

eg:.

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

      

If the makefile in subdir was modified such that the following fails (for example, only the gcc flag was changed), will the object files be updated?

Recompilation must be performed if the source file or any of the header files, called dependencies, are later than the file object, or if the object file does not exist.

+3


source to share


3 answers


The only reason that, as written, even launches this rule at all, because subsystem

and subdir

do not match.

If a file or directory was ever created in this directory subsystem

, this rule would cease to function.

If .PHONY: subsystem

1 was added the problem would be fixed, and this rule will always be if they are specified on the command line (i.e. make subsystem

). (As pointed out in the comments, it .PHONY

is an extension of GNU Make. The section following the linked section discusses a portable alternative. It is worth noting that they are not completely identical in that they .PHONY

have some additional benefits and some additional limitations.)

In none of these cases subsystem

does the target pay any attention to the modification dates of something (since it lacks any preconditions).



To make the target depend on changes in the makefile, you need to specify the makefile as prerequisites, just like everything else (i.e. subsystem: subdir/Makefile

). List it as .PHONY

most likely more correct and more than what you want.

No, nothing is about forcing yourself to keep track of non-necessary conditions. Thus, the flag is changed by / etc. don't run rebuilds. However, there are ways to make this work for make (these include storing the flags used in files that are themselves prerequisites for the targets using those flags, etc.). There are questions and answers on SO on how to do this (I don't have them, although they are ready).

Other tools handle flag changes automatically. I believe Electric Cloud is doing this. I believe CMake . There may be others as well.

+3


source


You can include the makefile as a dependency just like any other file:



mytarget.o: mytarget.c Makefile

      

+2


source


Recursive makefiles are executed regardless of whether anything has changed. This is just one of the objections Paul Miller mentioned in his "Recursive", which was considered dangerous almost 20 years ago.

With that said, the makefile will be like any other dependency and can be added to a production rule to run that rule if the makefile is changed.

+2


source







All Articles