I ran into a case where I wanted to augment the clean and clobber targets in a local Makefile but did not want to either add to the SOURCE list or copy the entire rule into the local Makefile. I have found two solutions to this problem:
- An alternative that is used often for clean-like rules that allows for multiple definitions of a target, all of which are executed, using GNU Make
:: syntax. This makes sense to me for the clean and clobber rules, but mixing : and :: does not work. I have not figure out how to override the :: rules if we do change this, i.e. what would you do if you wanted to really replace clean. I would suggest this would be unwise anyway, and renaming the target to myclean or something else hideous would still work. Example based on this stack exchange question:
.
clean::
-rm -f netcdf_test
clean::
-rm -f xgeoclaw
is equivalent to
clean:
-rm -f xgeoclaw
-rm -f netcdf_test
- Another alternative is to recursively define the clean as is done in this stack exchange post. This will illicit warnings about overriding commands, but this happens for other cases anyway. Example:
clean:
$(MAKE) -f Makefile.common $@
-rm -f netcdf_test
with Makefile.common remaining the same.
I would propose that more generally adding :: to the Makefile.common would be a better, cleaner solution more generally. This would obviously require a minor change to Makefile.common and changes to any downstream Makefile that overrides the clean or clobber. In the meantime I am going to implement the second option, which does not require any changes but does throw warnings.
I ran into a case where I wanted to augment the
cleanandclobbertargets in a local Makefile but did not want to either add to theSOURCElist or copy the entire rule into the local Makefile. I have found two solutions to this problem:::syntax. This makes sense to me for thecleanandclobberrules, but mixing:and::does not work. I have not figure out how to override the::rules if we do change this, i.e. what would you do if you wanted to really replaceclean. I would suggest this would be unwise anyway, and renaming the target tomycleanor something else hideous would still work. Example based on this stack exchange question:.
is equivalent to
with
Makefile.commonremaining the same.I would propose that more generally adding
::to theMakefile.commonwould be a better, cleaner solution more generally. This would obviously require a minor change toMakefile.commonand changes to any downstream Makefile that overrides thecleanorclobber. In the meantime I am going to implement the second option, which does not require any changes but does throw warnings.