Need a makefile dependency rule that can handle missing files

六眼飞鱼酱① 提交于 2020-01-03 17:12:56

问题


We use GNU Make for our system. At the end of our makefiles, we have an include called Makedepends which generates a bunch of .d files using -MM switch on gcc. We then include the .d file for each .cc file using an include $(CXXFILES:.cc=.d) line. But when we delete file or move files, the dependancies step breaks and we have to manually delete the .d files (even a make clean doesn't work because the dependencies fail)

Is there a way to generate these dependency .d files or include these dependency .d files which will gracefully handle a file deletion or relocation?

EDIT: For example: I have serial.cc and the makefiles generate a serial.d file which has a dependency on buffer.h but then I change it so I don't need buffer.h any more and I delete buffer.h. Next time I run make, it will choke because it includes the .d file which still makes serial.o depend on buffer.h.


回答1:


http://make.mad-scientist.net/papers/advanced-auto-dependency-generation has a description of this exact problem, and a couple of ways around it. The first is a bit misguided, but the "advanced" is essentially spot on.




回答2:


Two possibilities:

First, can you add a rule to your Makefile to run the dependency step:

.SUFFIXES: .d

%.d::
   makedepend_command_here

If not, then from the Last Resort section of the info page for GNU Make:

For example, when testing a makefile, you might not care if the source files contain real data, only that they exist. Then you might do this:

 %::
         touch $@

to cause all the source files needed (as prerequisites) to be created automatically.

Will this work to create empty .d files for you?




回答3:


If you use makepp with the option --rm-stale, it will notice files which are no longer buildable and remove them. If this is a normal usecase for you, you can put that option into .makepprc at the root of your build tree, and it will always be used.

But then of course makepp handles all this dependency detection itself, so you don't need to clutter your makefile. It is even better than your approach, because it can generate needed headers in time for the compiler to pick up, where gcc -MM would fail.

There is much more to makepp. Besides doing almost all that GNU make can, there are lots more useful things, and you can even extend your makefiles with some Perl programming.



来源:https://stackoverflow.com/questions/239004/need-a-makefile-dependency-rule-that-can-handle-missing-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!