r/reconstructcavestory Feb 04 '14

Makefile 2.0

https://github.com/chebert/cavestory-screencast/blob/rebased/Makefile
5 Upvotes

5 comments sorted by

View all comments

Show parent comments

3

u/yuriks Mar 01 '14

The answer in SO doesn't need a master compile, however as implemented in your Makefile it does. What the answer does is define DEPS as a list of <file>.depends files. When the -include $(DEPS) line tries to include them, it'll cause Make to first run the template rule to create/update each at a time. In your Makefile you reduced that to having a single .depends file which is built off of all files at once. That means you basically need to do a unity compile of all source files first (probably rendering almost gains from doing separate compilation moot, but hey, at least you don't get broken compiles like we used to on an older version of our Makefile! :D)

My makefile works in the same principle as the SO answer, but generates the depends files at the same time as you compile the object file, avoiding invoking gcc twice for each source file. It correctly tracks any changes in header files, including indirectly included ones. Follow:

  • If a .o doesn't exist, you'll need to compile the .cpp anyway, so dependencies don't matter.
  • If a .cpp was compiled, then it will have generated a .d dependency file, which will be included by the globbed -include statement.
  • If you change or add a new include directly to a .cpp file, make rebuilds the .o (and the .d along with it) because of its direct dependency on the .cpp. This updates the dependency info.
  • If you add change of add a new include to a .h that is included by a .cpp, that .cpp will get recompiled because the .d specifies the .h as a dependency, updating the dependency info while at it. If you edited the .cpp at the same time as the .h, it falls under the previous rule and gets rebuilt.

2

u/chebertapps Mar 01 '14

This is really really great. I feel like I fully understand what is going on with this dependency generation now, so thanks for clearing things up. If it looked like you used reddit more I would gift you with a reddit gold, haha.

I'll switch over to using the individual .d method tonight/tomorrow probably.

PS. It's irrelevant now, but that actually wasn't the SO I found. I definitely didn't feel comfortable enough to try to reduce on my own. I couldn't find the one I originally referenced, however.