2

Build systems like Make or Ninja should be able to handle the dependencies and re-compile only the part affected by changes. Then why ccache helps?

I understand that ccache and build systems work in different ways. ccache looks into the file content while build systems only care about files. So change a file and change it back, ccache works, sure, but I assume that is not the main use case? And similarly it works after make clean && make.

But is there a case that ccache really solves the problem that build systems can't handle? Like the same file is really compiled many times in a project? Or a case where build systems cannot resolve the dependencies and decide to re-compile every time? Real world use cases are preferred. Thanks in advance :)

liuzikai
  • 23
  • 2
  • 3
    This isn't really a question appropriate for SO. But, ninja and make rely on filesystem timestamps to detect whether a file has changed. Any situation where those timestamps can't help you, means they will rebuild. If you touch a file without modifying it (maybe switch to another Git branch and back again), or only edit a comment but not the code, or you clean your workspace and want to build again, or even (if you've set it up) have two different workspaces, make and ninja always rebuild. ccache, based on preprocessor output, understands no actual change was made and reuses object files. – MadScientist Jul 31 '23 at 13:24

1 Answers1

1

But is there a case that ccache really solves the problem that build systems can't handle?

Ccache (by default) use a global cache. Same file compiled in two different workspaces (assuming same output, e.g. compiler flags) is put and gotten from the same cache. Neither Make nor Ninja can do that.

Though probably irrelevant for most, ccache size can be limited (default 5GB), replacing files by least recently used. Combined with having the build system removing intermediate files (e.g. object files used to build a library or executable) you could end up with smaller disk space footprint at almost equal performance.

Ccache may also compress the files, reducing disk footprint further.

Finally, ccache can have its cache located remotely: https://ccache.dev/manual/latest.html#_remote_storage_backends

Make and Ninja are (in general) fooled by timestamps changes such as when checking out different branches of development. Ccache is not.


Note: You can't truly generalize "build systems" when it comes to ccache. Some have their own built-in cache system, e.g. Bazel. Having ccache as well would be redundant.

Andreas
  • 5,086
  • 3
  • 16
  • 36