This week I noticed that the includes in this project are a mess. Actually, each file should be self-contained, i.e., include the stuff that it needs (and ideally only the stuff that it needs) but at the moment neither of these are true for a lot of files in the repo. Just try compiling a random .h
or _impl.h
file.
This is not per se a problem for our “single translation unit” compilation (I learned that the cool kids call this “unity builds”) that we use in order to leverage the full potential of compiler optimizations without the need for LTO. The more important point (at least for me personally–though I may be the only regular contributor suffering here), it prevents the use of advanced tooling/IDE, e.g., my editor supports language servers such as ccls or clangd which “compile” the open file internally and provide a ton of features, e.g., completion, display of compile errors etc. But none of this works properly if you can’t compile individual files. You can say that’s the fault of these tools but self-contained files are also just good style and good engineering practice.
PR #924 was an attempt to clean up the includes but even the include-what-you-use
tool there used there (or at least the way it has been used there) fixes includes only for the .c
files.
Here are some things we’d need to do:
- Introduce header files for modules: Currently each
_impl.h
file insrc
has a corresponding.h
file for other files to include. But we don’t do this properly insrc/modules
. For example, the schnorrsig module uses function from the extrakeys module but there are only_impl.h
files insrc/modules/extrakeys
and no.h
file for schnorrsig to include. - The same is true for some internal functions declared in
secp256k1.c
. There’s no just header for other files to include. - Then make sure that each file includes at least all the stuff it uses, i.e., compiles on its own. Maybe with the help of a tool like clang-include-fixer.
- …and maybe enforce this on CI.
- Maybe make sure that each file includes at most the stuff it uses, e.g., with the help of a tool like include-what-you-use.
Again, I understand that I may the only one suffering, so fixing this may be on me but I still wanted to document my findings here. I don’t except anyone disagreeing with the changes the mentioned here but please raise your hand if you do.