DRY config header inclusion #11287

pull danra wants to merge 2 commits into bitcoin:master from danra:refactor/dry-config changing 42 files +47 −117
  1. danra commented at 10:37 AM on September 8, 2017: contributor

    Create a wrapper header file which includes config/bitcoin-config.h only if it exists (according to a macro). Files can include this wrapper header directly instead of having each including file repeat the check-if-config-exists-and-only-include-if-it-does macros.

    p.s. Open to suggestions if anyone has a better filename for the wrapper header.

  2. dougstrong77 approved
  3. fanquake added the label Refactoring on Sep 8, 2017
  4. promag commented at 11:18 AM on September 8, 2017: member

    How about just

    #include "bitcoin-config.h"
    

    which includes config/bitcoin-config.h.

  5. danra commented at 11:24 AM on September 8, 2017: contributor

    @promag I think that would be 1) confusing to readers 2) brittle to include paths changes

  6. promag commented at 11:34 AM on September 8, 2017: member

    brittle to include paths changes

    I believe there is a plan to make all includes relative to src/ cc @laanwj.

    Another option is to change the generated to, for instance

    AC_CONFIG_HEADERS([src/config/bitcoin-config-gen.h])
    

    And always have config/bitcoin-config.h which checks for that.

  7. danra commented at 11:39 AM on September 8, 2017: contributor

    Another option is to change the generated to, for instance

    AC_CONFIG_HEADERS([src/config/bitcoin-config-gen.h]) And always have config/bitcoin-config.h which checks for that.

    That's fine with me if more people agree that's preferable.

    Personally I think "#include "config/bitcoin-config-if-exists.h" or "#include "bitcoin-config-if-exists.h" reads nice and explicit, saying what we are actually doing - including the config file, but just in case it exists.

  8. meshcollider commented at 11:59 AM on September 8, 2017: contributor

    Other than refactoring, does this have any benefit? I'm not too keen on this idea tbh

  9. danra commented at 12:05 PM on September 8, 2017: contributor

    @MeshCollider In general, refactoring in order to DRY code has benefits.

    1. Less overhead to reading and understanding the code. Especially important for security-conscious projects.
    2. Less chance of being wrong when writing new code (e.g. see 3aa60b7ff9587637dcb3e646c2448bf75495a1f4)

    And more. But I think that's enough.

    Also, any particular reason why you are not keen on the idea?

  10. meshcollider commented at 12:57 PM on September 8, 2017: contributor

    @danra, not exactly sure, its just something about creating an entire new file just for 3 lines to import another file. IMO its clear enough as it is, because its just an if-statement, and anyone who would currently forget the ifdef would probably also forget to use this file, and just include bitcoin-config.h directly. Yes DRY is better, but I think this is taking it a bit far, that's all :)

  11. danra commented at 5:26 PM on September 8, 2017: contributor

    @MeshCollider In addition to the considerations I mentioned, it's a macro if, easy to either misspell or get wrong (checking for value instead of just being defined, etc.) Better contain it to a single file and forget about it.

  12. danra force-pushed on Sep 8, 2017
  13. achow101 commented at 7:45 PM on September 8, 2017: member

    @danra can you do the bulk of this as a scripted diff? That would make review easier and also less likely for you to have accidentally missed an include.

  14. danra force-pushed on Sep 8, 2017
  15. danra commented at 8:49 PM on September 8, 2017: contributor

    @achow101 It's fine. I didn't miss anything :P

    Seriously, I'm not sure a script would help very much, since there're subtle variations of the original ifdefs as you can see in the diff. So at best the script would show handling of some variations, and the rest of the code would have to be checked anyway for any other variations not covered by the script.

    At best, the script would prove no misspellings of the new include, but that would be caught as a compile error anyway.

  16. achow101 commented at 9:10 PM on September 8, 2017: member

    A scripted diff is easier to review and verify that the changes are correct, especially for such a change like this which is basically a scripted diff.

  17. danra commented at 10:40 AM on September 9, 2017: contributor

    DAK why Travis is failing to find "config/bitcoin-config-if-present.h" in the build? Is this some cache issue? It builds fine on my local machine.

  18. MarcoFalke commented at 2:42 AM on September 10, 2017: member

    @danra I haven't looked by it could be that out of tree builds are failing.

  19. danra force-pushed on Sep 10, 2017
  20. danra commented at 8:22 PM on September 10, 2017: contributor

    @MarcoFalke Tested out of tree build on my local machine - works fine. Would appreciate any other pointers as to what else might be causing the travis build to fail.

  21. sipa commented at 8:23 PM on September 10, 2017: member

    Run make distcheck.

  22. danra commented at 8:49 PM on September 10, 2017: contributor

    @sipa That reproduced the build error locally. Thanks!

  23. danra force-pushed on Sep 10, 2017
  24. dcousens approved
  25. danra force-pushed on Sep 29, 2017
  26. danra commented at 12:40 PM on September 29, 2017: contributor

    @achow101 Added scripted diff. Rebased

  27. MarcoFalke deleted a comment on Sep 29, 2017
  28. danra force-pushed on Sep 29, 2017
  29. Create a wrapper header file which includes config/bitcoin-config.h only if it exists (according to a macro). Files can include this wrapper header directly instead of having each including file repeat the check-if-config-exists-and-only-include-if-it-does macros. dfa07f8f91
  30. danra force-pushed on Sep 30, 2017
  31. danra force-pushed on Sep 30, 2017
  32. danra force-pushed on Sep 30, 2017
  33. scripted-diff: DRY config header inclusion if one exists
    -BEGIN VERIFY SCRIPT-
    GLOBIGNORE="*bitcoin-config-if-present*" ;
    sed -i '/HAVE_CONFIG_H/ { N;N; s/.*HAVE_CONFIG_H.*\n#include "config\/bitcoin-config.h"\(.*\)\n#endif.*/#include "config\/bitcoin-config-if-present.h"\1/; }' ./src/*.h ./src/*.cpp ./src/*/*.h ./src/*/*.cpp ./src/*/*/*.h ./src/*/*/*.cpp ;
    -END VERIFY SCRIPT-
    512530b106
  34. danra force-pushed on Sep 30, 2017
  35. danra commented at 9:57 AM on September 30, 2017: contributor

    Does anyone know if GLOBIGNORE is supported in the shell on the Travis host which fails the scripted-diff check? It works on my local bash but I can't get it to work here, tried multiple variations.

  36. laanwj commented at 12:11 PM on November 28, 2017: member

    Closing this for now, I don't think there is agreement to do this, and rebasing it will be a ton of work.

  37. laanwj closed this on Nov 28, 2017

  38. MarcoFalke locked this on Sep 8, 2021

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-04-22 06:15 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me