-
Notifications
You must be signed in to change notification settings - Fork 37.7k
Try also linking without -pthread and do not use it when not needed (clang on OS X) #5516
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Try also linking without -pthread and do not use it when not needed (clang on OS X) #5516
Conversation
"-pthreads" is a compiler and linker flag. On some platforms it enables various macros in C/C++ headers. It is the "I am using threads" command line switch. As such, a poor test can silently fail (== appear to not need -pthread), resulting in code that is compiled as single-threaded code. Silently compiling to single-threaded code is a far worse outcome than an annoying warning on compiler+platform combinations where -pthread is superfluous. |
@jgarzik "-pthread" is a compiler and linker flag: no. This only applies to good old GNU gcc and GNU linker world only (compare pthreads(7) on GNU/Linux: On Linux, programs that use the Pthreads API should be compiled using cc -pthread.). But it is a bit different on OS X. You don't need any compiler/linker flags because all system libs contain threads support. See https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man3/pthread.3.html#//apple_ref/doc/man/3/pthread for more details - especially the section INSTALLATION: "The default system libraries include pthread functions. No additional libraries or CFLAGS are necessary to use these interfaces." |
75f4dbc
to
00262a8
Compare
@paveljanik I am well aware of that. My comment remains applicable. You must consider the case of building with gcc + third party libraries on OSX that trigger different thread/non-thread behavior at compile time. The entire app stack on OSX needs to be verified free of MT behavior switching in headers, not just the system libs. e.g. Boost in particular is very sensitive on OSX, and is header-heavy. |
But this is "solved" in ax_pthread.m4 already IMO, where it checks thread functionality (not completely though, of course) with the provided compiler/libs and auto-selects the needed flags. On OS X:
but:
Ie. when clang is used to compile&link, it doesn't emit warning. Compile alone is OK but link alone emits warning. |
diff --git a/src/Makefile.am b/src/Makefile.am
index d6ac6e1..d7968bd 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -1,5 +1,6 @@
DIST_SUBDIRS = secp256k1
-AM_LDFLAGS = $(PTHREAD_CFLAGS) $(LIBTOOL_LDFLAGS)
+AM_LDFLAGS = $(PTHREAD_LIBS) $(LIBTOOL_LDFLAGS)
+AM_CFLAGS = $(PTHREAD_CFLAGS)
if EMBEDDED_LEVELDB Isn't that all that needs to be done? The m4 macro already does all kinds of checking for us, we just weren't using the output correctly. Clang is perfectly happy to eat "-pthreads" for compile, it just doesn't like it in the link-line. |
@theuni yes, this is all to be done. Thank you! Will you PR it or should I use it here? |
+1 @theuni |
@paveljanik You're welcome to just take that here, to keep the discussion in one place. |
00262a8
to
86d295e
Compare
Done. Thank you again. |
Travis failed: https://travis-ci.org/bitcoin/bitcoin/jobs/44878072 on ARM. Looks like -pthread is missing when linking or something similar... But can't test easily. Adding debugging prints to configure. |
Yup, confirmed:
|
@theuni Any hint now? |
Other platforms are OK. On all of them, _LIBS is empty, e.g.:
|
Well that's annoying. I had assumed that the m4 would treat LIBS as LDFLAGS, sticking linker options (-pthread) in LIBS as needed. But upon closer inspection, it doesn't even try to compile without linking! It just assumes you'll forward CFLAGS to the linker, which explains why I had it setup that way to begin with. After trying a few things and checking deep in some docs, I now agree with @jgarzik. Your original suggestion was probably the correct thing to do, but it's just not worth doing for us. I'd rather leave a harmless warning around than risk trouble in the future. |
See https://savannah.gnu.org/patch/index.php?8585 for a bugreport to GNU Autoconf Archive. Is paveljanik@e7f114c worth cherrypicking so we have auto-detected values saved in the Travis logs/configure log? Otherwise we can't do more here. Thank you all. Closing. |
JFYI: ax_pthreads.m4 is getting fixed upstream: |
Labels: Build system, Mac, Priority Low
clang on OS X doesn't want -pthread option when linking and thus emits this on link phase:
Our configure is using http://www.gnu.org/software/autoconf-archive/ax_pthread.html for pthread options detection but it doesn't (yet?) try to run clang without -pthread in the current version (compare Savannah patch http://savannah.gnu.org/patch/?8186) on OS X. This PR adds this and configure thus tries it. After this change the configure output changes like this (diff against previous configure log):
and the actual build log does not contain the above mentioned warnings at all.
The other possible approach to this is removing the darwin special case completely. This leads to configure trying -lpthreads before no options without success and ending in no options. Ie. the result is almost the same, but this way is safe (we can't know other/old environments and someone could have libpthreads doing something else).
@theuni please have a look.