How to execute a command before turning on the Makefile component?

I am trying to create a subdirectory in my project (let it have $PROJECT/child ) that needs to pull out the Makefile (call it ../Makefile.inc ) from its parent, $PROJECT/Makefile.inc . Later I want to copy $PROJECT/child to another location so that it can work regardless of $PROJECT .

There is a generic Makefile that needs to be included in both projects and sent when copying a subdirectory, and I want it to be included in both cases. So I thought that I would bind it during the child build if it was not found. (I don’t want to just turn on ../Makefile.inc because it will disappear when I copy the project, and I don’t want the call system to be responsible for installing Makefile.inc .)

With these limitations, here is a terrible hack that I came up with to do this in $PROJECT/child/Makefile :

 HACK = $(shell test -f Makefile.inc || ln -f ../Makefile.inc .) include $(HACK)Makefile.inc 

Pay attention to the extra special adhesive tape of this second command. I should actually include $(HACK) , even if it ends up empty, so $(shell ...) will be evaluated.; -)

Is there a cleaner way to do this?

+4
source share
1 answer

Give the rule to build Makefile.inc . ( make will complain that Makefile.inc does not exist when it parses the include string, but it will continue parsing the main makefile, apply any rule to build the included files, and return and reanalyze the main makefile with the included files.)

 include Makefile.inc Makefile.inc: ln ../Makefile.inc $@ 
+5
source

All Articles