Document FreeBSD quirk. Fix FreeBSD build: Use std::min<int>(...) to allow for compilation under certain FreeBSD versions. #13503

pull practicalswift wants to merge 1 commits into bitcoin:master from practicalswift:document-freebsd-quirk changing 1 files +2 −1
  1. practicalswift commented at 1:48 PM on June 19, 2018: contributor
    • Document FreeBSD quirk.
    • Fix FreeBSD build: Cast to int to allow std::min to work under FreeBSD.

    Context: #9598 (comment)

  2. fanquake added the label Linux/Unix on Jun 19, 2018
  3. practicalswift cross-referenced this on Jun 19, 2018 from issue Improve readability by removing redundant casts to same type (on all platforms) by practicalswift
  4. fanquake commented at 2:14 PM on June 19, 2018: member

    So this was originally applied in #2695:

    When compiling on FreeBSD, the calculation here returns an unsigned int. This causes a compile-time error with std::min, which cannot compare signed with unsigned integers. This pull inserts an explicit cast, treating the calculated value as signed, keeping the compiler happy.

    Then reverted in #9598 to:

    Improve readability by removing redundant casts to same type (on all platforms)

    Now we're re-adding (int) again but only for FreeBSD.

    I've just built master (3f398d7) successfully on a FreeBSD 11.1 VM, so I'm curious to know which version of FreeBSD this is broken on?

    Regardless of the above, the current patch doesn't compile:

    fvisibility=hidden -c -o libbitcoin_server_a-init.o `test -f 'init.cpp' || echo './'`init.cpp
    init.cpp:969:2: error: invalid preprocessing directive #fi
     #fi
      ^~
    init.cpp:964:0: error: unterminated #else
     #if defined(__FreeBSD__) || defined(__DragonFly__)
     
    make[2]: *** [libbitcoin_server_a-init.o] Error 1
    Makefile:5817: recipe for target 'libbitcoin_server_a-init.o' failed
    
  5. in src/init.cpp:969 in d0f7f6b52c outdated
     960 | @@ -961,7 +961,12 @@ bool AppInitParameterInteraction()
     961 |      nMaxConnections = std::max(nUserMaxConnections, 0);
     962 |  
     963 |      // Trim requested connection counts, to fit into system limitations
     964 | +#if defined(__FreeBSD__) || defined(__DragonFly__)
     965 | +    // See #2695: Explictly cast to int to allow std::min to work under FreeBSD
     966 | +    nMaxConnections = std::max(std::min(nMaxConnections, (int)(FD_SETSIZE - nBind - MIN_CORE_FILEDESCRIPTORS - MAX_ADDNODE_CONNECTIONS)), 0);
     967 | +#else
     968 |      nMaxConnections = std::max(std::min(nMaxConnections, FD_SETSIZE - nBind - MIN_CORE_FILEDESCRIPTORS - MAX_ADDNODE_CONNECTIONS), 0);
     969 | +#fi
    


    ken2812221 commented at 2:15 PM on June 19, 2018:

    This is not bash script :)

  6. practicalswift force-pushed on Jun 19, 2018
  7. practicalswift commented at 2:20 PM on June 19, 2018: contributor

    @fanquake @ken2812221 Sorry about the bashism :-) Not fixed!

  8. practicalswift commented at 2:23 PM on June 19, 2018: contributor

    @fanquake Good question regarding FreeBSD version. @anton48, what FreeBSD version are you running?

  9. practicalswift commented at 2:25 PM on June 19, 2018: contributor

    If this is fixed in FreeBSD 11.1 I'm not sure we have a problem to solve :-)

  10. practicalswift commented at 2:28 PM on June 19, 2018: contributor

    Closing this temporarily while waiting for input from @anton48.

    If we can reproduce under FreeBSD 11.1 I'll re-open.

  11. practicalswift closed this on Jun 19, 2018

  12. anton48 commented at 8:42 PM on June 19, 2018: none

    @practicalswift @fanquake the source of the problem is not a version of FreeBSD itself, but version of clang. currently there are two production versions of FreeBSD: 11.1 and 10.4. former contains clang 4, latter clang 3.4. "new" code in init.cpp can be compiled with clang 4, but not with 3.4. @practicalswift your patch with "if defined(FreeBSD)" works for me at 10.3, 10.4 and 11.1.

  13. fanquake reopened this on Jun 21, 2018

  14. fanquake commented at 1:39 AM on June 21, 2018: member

    Thanks for following up @anton48.

    Not sure if we want to add a FreeBSD specific "work around", or if we should just revert the init.cpp change from #9598.

  15. MarcoFalke commented at 1:54 AM on June 21, 2018: member

    Agree with @fanquake.

    Having different code for each platform makes testing a nightmare. Btw. I believe you can specify the function template you want to call with std::min<type>(a,b)

  16. practicalswift force-pushed on Jun 21, 2018
  17. Document FreeBSD quirk. Fix FreeBSD build. 629a47a154
  18. practicalswift force-pushed on Jun 21, 2018
  19. practicalswift renamed this:
    Document FreeBSD quirk. Fix FreeBSD build: Cast to int to allow std::min to work under FreeBSD.
    Document FreeBSD quirk. Fix FreeBSD build: Use std::min<int>(...) allow compilation under certain FreeBSD versions.
    on Jun 21, 2018
  20. practicalswift renamed this:
    Document FreeBSD quirk. Fix FreeBSD build: Use std::min<int>(...) allow compilation under certain FreeBSD versions.
    Document FreeBSD quirk. Fix FreeBSD build: Use std::min<int>(...) to allow for compilation under certain FreeBSD versions.
    on Jun 21, 2018
  21. practicalswift commented at 7:33 AM on June 21, 2018: contributor

    @fanquake @MarcoFalke Thanks for reviewing. Feedback addressed. Please re-review :-) @anton48 Can you confirm that the updated version works as expected for you?

  22. anton48 commented at 7:55 AM on June 21, 2018: none

    @practicalswift by updated version you mean nMaxConnections = std::max(std::min<int>(nMaxConnections, FD_SETSIZE - nBind - MIN_CORE_FILEDESCRIPTORS - MAX_ADDNODE_CONNECTIONS), 0);? yes, it can be compiled without errors on FreeBSD (tested on 10 and 11 versions).

  23. practicalswift commented at 8:13 AM on June 21, 2018: contributor

    @anton48 Yes, that's the new version. Thanks for confirming.

  24. laanwj commented at 2:05 PM on June 24, 2018: member

    I'm confused. I build bitcoin core on FreeBSD all the time and have never needed this. #2695 is ancient.

  25. MarcoFalke commented at 2:18 PM on June 24, 2018: member

    The cast to int was only recently removed in master and I believe it only fails on FreeBSD 10, not 11.

  26. MarcoFalke commented at 9:18 PM on June 24, 2018: member

    utACK 629a47a1543a6e77cbf9c73917e2e419669b04df

  27. sipa commented at 1:03 AM on June 27, 2018: member

    utACK 629a47a1543a6e77cbf9c73917e2e419669b04df

  28. fanquake commented at 8:12 AM on June 27, 2018: member

    utACK 629a47a for fixing the FreeBSD 10.x build. As noted above, the issue comes from Clang 3.4

  29. MarcoFalke merged this on Jun 27, 2018
  30. MarcoFalke closed this on Jun 27, 2018

  31. MarcoFalke referenced this in commit c655b2c2df on Jun 27, 2018
  32. Bushstar cross-referenced this on Jun 28, 2018 from issue commits from bitcoin/master by Bushstar
  33. practicalswift cross-referenced this on Feb 4, 2019 from issue ci: Build and run tests once on freebsd by MarcoFalke
  34. codablock referenced this in commit ac02dcf7d5 on Apr 7, 2020
  35. codablock cross-referenced this on Apr 7, 2020 from issue Backport poll() code from Bitcoin by codablock
  36. codablock referenced this in commit 9591199629 on Apr 8, 2020
  37. deadalnix referenced this in commit ee323c8b68 on Jun 9, 2020
  38. ftrader referenced this in commit add9abcee0 on Aug 17, 2020
  39. ckti referenced this in commit 136846ae40 on Mar 28, 2021
  40. practicalswift deleted the branch on Apr 10, 2021
  41. gades referenced this in commit 1b227c47ff on Jun 25, 2021
  42. hebasto cross-referenced this on Sep 25, 2021 from issue doc: Remove outdated comments by hebasto
  43. laanwj referenced this in commit 8b523f2e55 on Sep 27, 2021
  44. sidhujag referenced this in commit e84c360598 on Sep 27, 2021
  45. gades referenced this in commit 8c955a9ce0 on Apr 1, 2022
  46. bitcoin locked this on Aug 18, 2022

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-05-19 06:54 UTC