GNOME Bugzilla – Bug 785955
pthread_setname_np misdetected with meson
Last modified: 2017-08-10 13:36:46 UTC
When using meson to build glib git master, pthread_setname_np prototype is misdetected and build fails with the following (ignore the version, it is git master from today): clang -Iglib/glib-2.0@sha -Iglib -I../glib-2.53.4/glib -I. -I../glib-2.53.4/ -Xclang -fcolor-diagnostics -pipe -D_FILE_OFFSET_BITS=64 -std=gnu89 -march=skylake -fomit-frame-pointer -fstack-protector-strong -O2 -fPIC -pthread '-DG_LOG_DOMAIN="GLib"' -DGLIB_COMPILATION -DPCRE_STATIC -fvisibility=hidden -MMD -MQ 'glib/glib-2.0@sha/gthread-posix.c.o' -MF 'glib/glib-2.0@sha/gthread-posix.c.o.d' -o 'glib/glib-2.0@sha/gthread-posix.c.o' -c ../glib-2.53.4/glib/gthread-posix.c ../glib-2.53.4/glib/gthread-posix.c:1231:27: error: too few arguments to function call, expected 2, have 1 pthread_setname_np (name); /* on OS X and iOS */ ~~~~~~~~~~~~~~~~~~ ^ /usr/include/pthread.h:452:1: note: 'pthread_setname_np' declared here extern int pthread_setname_np (pthread_t __target_thread, const char *__name) Inside meson-log.txt, there's the following: Running compile: Working directory: /tmp/tmppgses23b Command line: clang /tmp/tmppgses23b/testfile.c -march=skylake -fomit-frame-pointer -fstack-protector-strong -O2 -pipe -O0 -std=gnu89 -D_FILE_OFFSET_BITS=64 -o /tmp/tmppgses23b/output.exe Code: #include <pthread.h> int main() { pthread_setname_np("example"); } Compiler stdout: Compiler stderr: Checking if "pthread_setname_np(const char*)" links: YES Now, if I manually compile that program with clang, I get the following $ cat test.c #include <pthread.h> int main() { pthread_setname_np("example"); } $ gcc test.c -pthread test.c: In function ‘main’: test.c:3:20: warning: implicit declaration of function ‘pthread_setname_np’; did you mean ‘pthread_setcanceltype’? [-Wimplicit-function-declaration] pthread_setname_np("example"); ^~~~~~~~~~~~~~~~~~ pthread_setcanceltype $ gcc -std=c89 test.c -pthread $ clang test.c -pthread test.c:3:20: warning: implicit declaration of function 'pthread_setname_np' is invalid in C99 [-Wimplicit-function-declaration] pthread_setname_np("example"); ^ 1 warning generated. $ clang -std=c89 test.c -pthread As you can see, in c89 mode (which meson forces), no warning or error will be generated with either clang or gcc. This is gcc 7.1.1 20170727, clang 4.0.1, glibc 2.26, binutils 2.29 PKGBUILD and meson flags can be found at https://github.com/elkrejzi/pacman/blob/master/pkgbuild/glib/PKGBUILD.meson
The proximal cause is that we need to define _GNU_SOURCE in order to get the pthread_setname_np() with the thread name argument. On top of that, we have a bug in our feature detection test in GLib's meson.build, where we don't pass the thread dependency when checking for pthread features. On top of that, Meson has a bug when it comes to resolving the thread library dependency inside compiler checks; see: https://github.com/mesonbuild/meson/issues/2165
Created attachment 357303 [details] [review] meson: Define _GNU_SOURCE as a project argument We use it pretty much everywhere in order to get feature detection, and that's also what the AC_USE_SYSTEM_EXTENSIONS m4 macro defines in the Autotools build.
Created attachment 357305 [details] [review] meson: Update the pthread feature checks For GNU extensions, we need to define _GNU_SOURCE; but, more importantly, we need to tell Meson to use the threadlib dependency when compiling and linking the feature check. This currently exposes a bug in Meson; see: https://github.com/mesonbuild/meson/issues/2165 But once that's fixed, the check will work as intended.
Good news is that the Meson bug has already been fixed in Git, so we should merge those those attachments, and then wait until the next stable release.
Review of attachment 357303 [details] [review]: ++
Review of attachment 357305 [details] [review]: This looks good from the GLib point of view. I don’t know enough Meson to say if it’s good from that PoV.
Attachment 357303 [details] pushed as 1a755a6 - meson: Define _GNU_SOURCE as a project argument Attachment 357305 [details] pushed as 50eeb24 - meson: Update the pthread feature checks