Search code examples
makefilegnu-make

Makefile to `include` a phony target


Suppose a makefile contains a phony target:

generator: some_files

some_files:
    whatever recipe

.PHONY: generator

Now, what if this target is included ?

-include generator

I expect that this generator is viewed as a makefile, but it cannot be found in the current directory. In this situation, make should attempt to remake generator, but according to the documentation:

make will not attempt to remake makefiles which are marked phony.

Thus nothing should happen. However, the files some_files are actually created! How is this possible? I can imagine that there is some magic in make interpreting generator as a simple target, thus it does the corresponding recipe. (If this is in the documentation, I am interested in a link)

The problem

This behavior is not seen on all systems. I have GNU make 4.3, but some users reported that the files are not generated (as I would expect from the doc). Is there a cleaner way to have the files created? Note that they need to be created at the beginning of the make process.


Solution

  • This difference of behavior was introduced as a result of https://savannah.gnu.org/bugs/?60795 Unfortunately it appears that change was not mentioned in the NEWS file for GNU Make.

    The documentation you are reading online is for the currently released version, and so it doesn't describe the behavior of older versions of GNU Make. You'd have to go look at the documentation for those older versions, to see how they behave. You should have received (or have available) the complete documentation for the version you're using from the same place you received the software.

    As for your question, I don't know of any reliable way to do what you want that's any better than something like this:

    _dummy := $(shell $(MAKE) some_files)
    

    I'm leery of running make inside a shell like this but it might work.

    Alternatively if you have a well-defined set of targets you could modify them to depend on the files to be built then invoke the real target recursively.

    I've considered adding a feature like .SETUP: and .TEARDOWN: (or something like that) where prerequisites of these targets are built before anything and after everything but I've never really investigated it.