build: split pthread flags out of ldflags and dont use when building libconsensus #19558

pull fanquake wants to merge 4 commits into bitcoin:master from fanquake:split_out_pthread_flags changing 7 files +288 −263
  1. fanquake commented at 8:01 am on July 20, 2020: member

    TLDR: Split pthread flags out of ldflags, and stop using them when building libconsensus.

    Building libconsensus on Linux using Clang currently warns. i.e:

    0./autogen.sh
    1./configure --disable-tests --disable-bench --with-utils=no --with-daemon=no --with-gui=no --disable-wallet --with-libs=yes CC=clang CXX=clang++
    2make V=1 -j6
    3... -Wl,-z -Wl,relro -Wl,-z -Wl,now   -pthread -Wl,-soname -Wl,libbitcoinconsensus.so.0 -o .libs/libbitcoinconsensus.so.0.0.0
    4clang: warning: argument unused during compilation: '-pthread' [-Wunused-command-line-argument]
    5clang: warning: argument unused during compilation: '-pthread' [-Wunused-command-line-argument]
    

    Besides wanting to quiet the warnings, after digging into this it seemed we could clean up how we are passing around the pthread flags. I also learnt a bit more about how libtools builds shared libraries, and that passing -pthread on the link line wouldn’t be enough to link against pthreads anyways, due to libtools usage of -nostdlib (see related discussion where we build DLLs).

    This can be demonstrated with a patch to libconsensus:

     0diff --git a/src/script/bitcoinconsensus.cpp b/src/script/bitcoinconsensus.cpp
     1index 15e204062..10bf3582f 100644
     2--- a/src/script/bitcoinconsensus.cpp
     3+++ b/src/script/bitcoinconsensus.cpp
     4@@ -10,6 +10,8 @@
     5 #include <script/interpreter.h>
     6 #include <version.h>
     7 
     8+#include <pthread.h>
     9+
    10 namespace {
    11 
    12 /** A class that deserializes a single CTransaction one time. */
    13@@ -127,3 +129,10 @@ unsigned int bitcoinconsensus_version()
    14     // Just use the API version for now
    15     return BITCOINCONSENSUS_API_VER;
    16 }
    17+
    18+void *func_pthread(void *x) { return x; }
    19+
    20+void f() {
    21+	pthread_t t;
    22+	pthread_create(&t,0,func_pthread,0);
    23+}
    

    After building, you’ll find you have a libbitcoinconsensus.so using pthread symbols, but which isn’t linked against libpthread:

    0ldd -r src/.libs/libbitcoinconsensus.so
    1	linux-vdso.so.1 (0x00007ffe49378000)
    2	libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f553cee7000)
    3	libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f553cda2000)
    4	libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f553cd88000)
    5	libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f553cbc5000)
    6	/lib64/ld-linux-x86-64.so.2 (0x00007f553d15d000)
    7undefined symbol: pthread_create	(src/.libs/libbitcoinconsensus.so)
    

    This libtool behaviour has been known about for some time, i.e this thread from 2005, describes the same issue. The suggestion from libtool maintainers at the time is to add -lpthread to LDFLAGS.

    Also worth noting is that some of the users in those threads were also using the AX_PTHREADS macro, same as us, to determine how to compile with/link against pthreads. This macro has recently been updated, with reference to this issue. You can compare the output from the version we currently use, to the new version:

    0# our ax_pthread macro:
    1  PTHREAD_CFLAGS = -pthread
    2  PTHREAD_LIBS  = 
    3  PTHREAD_CC    = gcc / clang
    4
    5# the new ax_pthread macro
    6  PTHREAD_CFLAGS = -pthread
    7  PTHREAD_LIBS  = -lpthread
    8  PTHREAD_CC    = gcc / clang
    

    Note that as part of this PR I’ve also added PTHREAD_LIBS to the split out flags. Although we weren’t using it anywhere previously (and wouldn’t have seemed to matter for the most part, given it was likely empty for most builders), the macro assumes it’s use. i.e:

    NOTE: You are assumed to not only compile your program with these flags, but also to link with them as well. For example, you might link with $PTHREAD_CC $CFLAGS $PTHREAD_CFLAGS $LDFLAGS … $PTHREAD_LIBS $LIBS

  2. fanquake added the label Build system on Jul 20, 2020
  3. DrahtBot commented at 8:16 am on July 20, 2020: member

    The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

    Conflicts

    Reviewers, this pull request conflicts with the following ones:

    • #19522 (build: fix building libconsensus with reduced exports for Darwin targets by fanquake)
    • #19077 (wallet: Add sqlite as an alternative wallet database and use it for new descriptor wallets by achow101)
    • #18242 (Add BIP324 encrypted p2p transport de-/serializer (only used in tests) by jonasschnelli)
    • #18077 (net: Add NAT-PMP port forwarding support by hebasto)
    • #16546 (External signer support - Wallet Box edition by Sjors)

    If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

  4. in configure.ac:1713 in 6e6e4e948e outdated
    1715+echo "  CPPFLAGS       = $DEBUG_CPPFLAGS $HARDENED_CPPFLAGS $CPPFLAGS"
    1716+echo "  CXX            = $CXX"
    1717+echo "  CXXFLAGS       = $DEBUG_CXXFLAGS $HARDENED_CXXFLAGS $WARN_CXXFLAGS $NOWARN_CXXFLAGS $ERROR_CXXFLAGS $GPROF_CXXFLAGS $CXXFLAGS"
    1718+echo "  LDFLAGS        = $HARDENED_LDFLAGS $GPROF_LDFLAGS $LDFLAGS"
    1719+echo "  ARFLAGS        = $ARFLAGS"
    1720+echo "  PTHREAD_CFLAGS = $PTHREAD_CFLAGS"
    


    laanwj commented at 7:38 pm on July 22, 2020:
    I’m not sure listing these separately here is worth it. We don’t do so dor other libraries, and don’t want this list to get too long. If someone really wants to know they can check config.log.

    fanquake commented at 6:32 am on July 24, 2020:
    Fair enough. I’ve removed split out.
  5. laanwj commented at 7:39 pm on July 22, 2020: member
    Concept ACK. I don’t think there are plans to ever make libconsensus use threads (definitely not directly), because this would make it harder to use from some languages. So this seems fine.
  6. fanquake force-pushed on Jul 24, 2020
  7. laanwj commented at 5:19 pm on August 12, 2020: member
    @dongcarl @theuni can you take a look here please
  8. dongcarl added the label Needs gitian build on Aug 13, 2020
  9. dongcarl added the label Needs Guix build on Aug 13, 2020
  10. DrahtBot commented at 11:26 pm on August 14, 2020: member

    Guix builds

    File commit b4d0366b47dd9b8fe29cc9a100dcdf6ca1d3cabf(master) commit c70581b0395d421d19468d2bd751111378df01a5(master and this pull)
    *-aarch64-linux-gnu-debug.tar.gz f44891293fcb6802... b04cf55872990aec...
    *-aarch64-linux-gnu.tar.gz e64f1f4378b738f3... 9b9b8e8de21335ac...
    *-arm-linux-gnueabihf-debug.tar.gz cfa499656def350f... 08b6b3d701eafb61...
    *-arm-linux-gnueabihf.tar.gz 94d27cf2a82082dc... b513f93f26ba691b...
    *-riscv64-linux-gnu-debug.tar.gz 3da435713be4835e... f7e52848bd6e6a14...
    *-riscv64-linux-gnu.tar.gz 7376f67d61282cdb... 9ac63f5f56bfd0c8...
    *-win-unsigned.tar.gz e7abd33ebe905812... e00bd1928b334c2e...
    *-win64-debug.zip c1bdd3af288c2b3c... ed68a32b2734c800...
    *-win64-setup-unsigned.exe 1f452e7866f1b3c2... 3bd9d9d984389b1d...
    *-win64.zip 67f5d4d31bc76406... 23e7fa49afbcac3c...
    *-x86_64-linux-gnu-debug.tar.gz e0e91e785ee6d671... a5cc7946b1aa482b...
    *-x86_64-linux-gnu.tar.gz 2c9d094b26f44968... 4b27418b80809894...
    *.tar.gz 69ec36f4e4bee888... 53d19915ef572de8...
    guix_build.log 51e3d570bbcc6dfe... 0ca38fb0d7173f62...
    guix_build.log.diff 870d46915b1ae6a6...
  11. DrahtBot removed the label Needs Guix build on Aug 14, 2020
  12. DrahtBot removed the label Needs gitian build on Aug 15, 2020
  13. MarcoFalke added the label Needs gitian build on Aug 16, 2020
  14. DrahtBot commented at 5:32 pm on August 17, 2020: member

    Gitian builds

    File commit a57af897ec16976b28de05aa0b9c3f6a96d73ede(master) commit d652bc864a532f673d915fa266a038ac5fe82f3f(master and this pull)
    *-aarch64-linux-gnu-debug.tar.gz 8b4a1a1ea553f69f... 0100e3d6e6f3650f...
    *-aarch64-linux-gnu.tar.gz 1f93a63d8397aff2... 4d459f3b5b5b4a6b...
    *-arm-linux-gnueabihf-debug.tar.gz 5053f76c45b29f42... 9523c18a33bd7307...
    *-arm-linux-gnueabihf.tar.gz af4e8080df6b4769... c94d938674bcf0f3...
    *-osx-unsigned.dmg 7ee6f85d4853b531... e963cf3148aa550b...
    *-osx64.tar.gz 29c33fb36c82c936... 8ec58484be56e4e8...
    *-riscv64-linux-gnu-debug.tar.gz 1552e87d2e59612b... b0a10a775d3722a0...
    *-riscv64-linux-gnu.tar.gz f206c62e44133795... 3f5828cc420b2295...
    *-win64-debug.zip 023e60578db1b77a... 5d578d71637e65b9...
    *-win64-setup-unsigned.exe 6c693991817b9e93... fddd11948a0c592e...
    *-win64.zip 16b1820ff7a065b1... 7d751e438a9d91fe...
    *-x86_64-linux-gnu-debug.tar.gz 74380833b29172a1... 1c50918631a05f96...
    *-x86_64-linux-gnu.tar.gz 5af950dccb812a51... 62c5c03c5f28380b...
    *.tar.gz 0d444abfdfc84fe3... c0e9f9df02767176...
    bitcoin-core-linux-0.21-res.yml ecdf987d89eb5ffe... 19fb5a157a9ee300...
    bitcoin-core-osx-0.21-res.yml 4d0d5b398e691077... 9472d1f35d58d190...
    bitcoin-core-win-0.21-res.yml 80fe6cf465b08ae0... 08a3c5c6b8a3b224...
    linux-build.log 2ee1352c78ea1b7b... 17c92db9472ae683...
    osx-build.log 16dd5e35db85be0c... 3729d2cb9b39ac3e...
    win-build.log 69ec5c8efa855981... ce8588b63a06945b...
    bitcoin-core-linux-0.21-res.yml.diff 2bf76e4cd39d50cb...
    bitcoin-core-osx-0.21-res.yml.diff 3792ce96aef742d3...
    bitcoin-core-win-0.21-res.yml.diff 6c8babde3859707b...
    linux-build.log.diff ef672e9cb9552e16...
    osx-build.log.diff 562cbb7690bfe7bf...
    win-build.log.diff 141b267628a20d25...
  15. DrahtBot removed the label Needs gitian build on Aug 17, 2020
  16. dongcarl commented at 3:18 pm on August 19, 2020: member
    Looks like the builds are working. A quick skim of the code looks correct, however, I willingly admit that I’m not the most familiar with automake idiosyncrasies and conventions.
  17. fanquake deleted a comment on Aug 20, 2020
  18. hebasto commented at 11:20 am on August 21, 2020: member
    Concept ACK. Hope this would make #19772 fixing easier.
  19. hebasto approved
  20. hebasto commented at 12:30 pm on August 21, 2020: member

    ACK 9c5bef450f7bf4dd4307cae8bce76dc131643574, tested on Linux Mint 20 (x86_64) – no clang warnings now. Verified that ax_pthread.m4 has no our custom patches.

    Mind making 8331ad7538e1e3c6efc98fc8fe5662c3d6d953b6 a scripted-diff:

     0-BEGIN VERIFY SCRIPT-
     1sed -i -e 's/\$(RELDFLAGS) \$(AM_LDFLAGS) \$(LIBTOOL_APP_LDFLAGS)$/\$(FUZZ_SUITE_LDFLAGS_COMMON)/' src/Makefile.test.include
     2patch -p1 << "EOF"
     3--- a/src/Makefile.test.include
     4+++ b/src/Makefile.test.include
     5@@ -318,6 +318,8 @@ endif
     6 
     7 if ENABLE_FUZZ
     8 
     9+FUZZ_SUITE_LDFLAGS_COMMON = $(RELDFLAGS) $(AM_LDFLAGS) $(LIBTOOL_APP_LDFLAGS)
    10+
    11 test_fuzz_addition_overflow_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES)
    12 test_fuzz_addition_overflow_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
    13 test_fuzz_addition_overflow_LDADD = $(FUZZ_SUITE_LD_COMMON)
    14EOF
    15-END VERIFY SCRIPT-
    

    ?

  21. fanquake force-pushed on Aug 24, 2020
  22. fanquake force-pushed on Aug 24, 2020
  23. fanquake commented at 1:07 pm on August 24, 2020: member

    Mind making 8331ad7 a scripted-diff:

    It’s now a scripted diff.

  24. hebasto approved
  25. hebasto commented at 1:38 pm on August 24, 2020: member

    re-ACK d42f2150496128fde0a9d2d28707a000a7a0d900

     0$ git range-diff master 9c5bef450f7bf4dd4307cae8bce76dc131643574 d42f2150496128fde0a9d2d28707a000a7a0d900
     11:  ae409c1b4 = 1:  3711cb7ce build: add PTHREAD_LIBS to LDFLAGS configure output
     22:  8331ad753 ! 2:  b3ee267a6 test: add FUZZ_SUITE_LDFLAGS_COMMON
     3    @@ Metadata
     4     Author: fanquake <fanquake@gmail.com>
     5     
     6      ## Commit message ##
     7    -    test: add FUZZ_SUITE_LDFLAGS_COMMON
     8    +    scripted-diff: add FUZZ_SUITE_LDFLAGS_COMMON
     9    +
    10    +    -BEGIN VERIFY SCRIPT-
    11    +    sed -i -e 's/\$(RELDFLAGS) \$(AM_LDFLAGS) \$(LIBTOOL_APP_LDFLAGS)$/\$(FUZZ_SUITE_LDFLAGS_COMMON)/' src/Makefile.test.include
    12    +    patch -p1 << "EOF"
    13    +    --- a/src/Makefile.test.include
    14    +    +++ b/src/Makefile.test.include
    15    +    @@ -318,6 +318,8 @@ endif
    16    +
    17    +     if ENABLE_FUZZ
    18    +
    19    +    +FUZZ_SUITE_LDFLAGS_COMMON = $(RELDFLAGS) $(AM_LDFLAGS) $(LIBTOOL_APP_LDFLAGS)
    20    +    +
    21    +     test_fuzz_addition_overflow_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES)
    22    +     test_fuzz_addition_overflow_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
    23    +     test_fuzz_addition_overflow_LDADD = $(FUZZ_SUITE_LD_COMMON)
    24    +    EOF
    25    +    -END VERIFY SCRIPT-
    26     
    27      ## src/Makefile.test.include ##
    28     @@ src/Makefile.test.include: endif
    293:  06e0771ec = 3:  abdd60af7 build: split PTHREAD_* flags out of AM_LDFLAGS
    304:  9c5bef450 = 4:  d42f21504 build: AX_PTHREAD serial 27
    
  26. in src/Makefile.am:10 in abdd60af71 outdated
     3@@ -4,9 +4,10 @@
     4 
     5 DIST_SUBDIRS = secp256k1 univalue
     6 
     7-AM_LDFLAGS = $(PTHREAD_CFLAGS) $(LIBTOOL_LDFLAGS) $(HARDENED_LDFLAGS) $(GPROF_LDFLAGS) $(SANITIZER_LDFLAGS)
     8+AM_LDFLAGS = $(LIBTOOL_LDFLAGS) $(HARDENED_LDFLAGS) $(GPROF_LDFLAGS) $(SANITIZER_LDFLAGS)
     9 AM_CXXFLAGS = $(DEBUG_CXXFLAGS) $(HARDENED_CXXFLAGS) $(WARN_CXXFLAGS) $(NOWARN_CXXFLAGS) $(ERROR_CXXFLAGS) $(GPROF_CXXFLAGS) $(SANITIZER_CXXFLAGS)
    10 AM_CPPFLAGS = $(DEBUG_CPPFLAGS) $(HARDENED_CPPFLAGS)
    11+AM_PTHREAD_FLAGS = $(PTHREAD_CFLAGS) $(PTHREAD_LIBS)
    


    theuni commented at 7:28 pm on September 4, 2020:
    AM_FOO usually have some meaning to Automake, so this should probably drop the AM_ prefix.

    fanquake commented at 6:00 am on September 11, 2020:
    Good call. Switch to PTHREAD_FLAGS.
  27. theuni commented at 7:35 pm on September 4, 2020: member

    @hebasto Good call on the scripted diff. That removes a big hurdle to this change.

    Pretty sure we’re still not really using the pthreads macro as intended, but our usage has held up in the real-world so far, and I don’t think this is likely to break anything.

    Concept ACK.

  28. fanquake force-pushed on Sep 11, 2020
  29. hebasto commented at 5:48 pm on September 12, 2020: member
    It seems in 69a933ca29af37f6568d5faa07cb33c4cffed602 the patched lines location should be updated.
  30. build: add PTHREAD_LIBS to LDFLAGS configure output
    Also moves $PTHREAD_CFLAGS to the CFLAGS.
    afecde8046
  31. fanquake force-pushed on Sep 14, 2020
  32. fanquake commented at 8:28 am on September 14, 2020: member

    It seems in 69a933c the patched lines location should be updated.

    Thanks. This should be addressed now.

  33. scripted-diff: add FUZZ_SUITE_LDFLAGS_COMMON
    -BEGIN VERIFY SCRIPT-
    sed -i -e 's/\$(RELDFLAGS) \$(AM_LDFLAGS) \$(LIBTOOL_APP_LDFLAGS)$/\$(FUZZ_SUITE_LDFLAGS_COMMON)/' src/Makefile.test.include
    patch -p1 << "EOF"
    --- a/src/Makefile.test.include
    +++ b/src/Makefile.test.include
    @@ -323,6 +323,8 @@ endif
    
     if ENABLE_FUZZ
    
    +FUZZ_SUITE_LDFLAGS_COMMON = $(RELDFLAGS) $(AM_LDFLAGS) $(LIBTOOL_APP_LDFLAGS)
    +
     test_fuzz_addition_overflow_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES)
     test_fuzz_addition_overflow_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
     test_fuzz_addition_overflow_LDADD = $(FUZZ_SUITE_LD_COMMON)
    EOF
    -END VERIFY SCRIPT-
    68e3e22944
  34. build: split PTHREAD_* flags out of AM_LDFLAGS
    Note that with this change we are no-longer including PTHREAD_* flags
    when building libbitcoinconsensus.
    
    Also note that we are including PTHREAD_LIBS in AM_PTHREAD_FLAGS
    15c27c4441
  35. build: AX_PTHREAD serial 27 fc9278d162
  36. fanquake force-pushed on Sep 14, 2020
  37. hebasto approved
  38. hebasto commented at 8:56 am on September 14, 2020: member
    re-ACK fc9278d162c342bace8a147da6bc4f9941d8d9d7, only rebased and renamed s/AM_PTHREAD_FLAGS/PTHREAD_FLAGS/ since my previous review..
  39. kallewoof commented at 10:42 am on September 15, 2020: member

    ACK fc9278d162c342bace8a147da6bc4f9941d8d9d7

    Code review + compiled and ran tests on mac & linux.

  40. laanwj commented at 10:48 am on September 15, 2020: member
    Looks like a straightforward improvement. Code review ACK fc9278d162c342bace8a147da6bc4f9941d8d9d7
  41. laanwj merged this on Sep 15, 2020
  42. laanwj closed this on Sep 15, 2020

  43. fanquake deleted the branch on Sep 15, 2020
  44. sidhujag referenced this in commit a239f29f53 on Sep 15, 2020
  45. DrahtBot locked this on Feb 15, 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: 2024-11-17 12:12 UTC

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