'Make: Redo some targets if configuration changes

I want to reexecute some targets when the configuration changes.

Consider this example:

I have a configuration variable (that is either read from environment variables or a config.local file):

CONF:=...

Based on this variable CONF, I assemble a header file conf.hpp like this:

conf.hpp:
    buildConfHeader $(CONF)

Now, of course, I want to rebuild this header if the configuration variable changes, because otherwise the header would not reflect the new configuration. But how can I track this with make? The configuration variable is not tied to a file, as it may be read from environment variables.

Is there any way to achieve this?



Solution 1:[1]

There is no way for make to know what to rebuild if the configuration changed via a macro or environment variable.

You can, however, use a target that simply updates the timestamp of conf.hpp, which will force it to always be rebuilt:

conf.hpp: confupdate
    buildConfHeader $(CONF)

confupdate:
    @touch conf.hpp

However, as I said, conf.hpp will always be built, meaning any targets that depend upon it will need rebuilt as well. A much more friendly solution is to generate the makefile itself. CMake or the GNU Autotools are good for this, except you sacrifice a lot of control over the makefile. You could also use a build script that creates the makefile, but I'd advise against this since there exist tools that will allow you to build one much more easily.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1