Bug 235583 - clang error builtin-requires-header on pthread_create on powerpc64
Summary: clang error builtin-requires-header on pthread_create on powerpc64
Status: New
Alias: None
Product: Base System
Classification: Unclassified
Component: threads (show other bugs)
Version: CURRENT
Hardware: Any Any
: --- Affects Some People
Assignee: freebsd-threads (Nobody)
Keywords: patch
Depends on:
Reported: 2019-02-07 18:30 UTC by Alfredo Dal'Ava Junior
Modified: 2019-10-03 12:08 UTC (History)
3 users (show)

See Also:

Disables -Werror,-Wbuiltin-requires-header on powerpc64 (762 bytes, patch)
2019-02-07 18:30 UTC, Alfredo Dal'Ava Junior
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Alfredo Dal'Ava Junior freebsd_committer 2019-02-07 18:30:01 UTC
Created attachment 201820 [details]
Disables -Werror,-Wbuiltin-requires-header on powerpc64

I'm trying to cross compile "base" to powerpc64 using both ports/llm-devel (SNAPDATE=20190122) and llvm from git (revision f324f6dcfba71ede8dd213096ec3b2f1b57ded86) and build breaks with the following error:

/usr/local/bin/clang -fuse-ld=/usr/local/bin/ld.lld -target powerpc64-unknown-freebsd13.0-elfv2 -mabi=elfv2 --sysroot=/usr/obj/usr/src/powerpc.powerpc64/tmp -B/dev/null  -O2 -pipe   -DNO__SCCSID -DNO__RCSID -I/usr/src/lib/libc/include -I/usr/src/include -I/usr/src/lib/libc/powerpc64 -DNLS  -D__DBINTERFACE_PRIVATE -I/usr/src/contrib/gdtoa -I/usr/src/contrib/libc-vis -DINET6 -I/usr/obj/usr/src/powerpc.powerpc64/lib/libc -I/usr/src/lib/libc/resolv -D_ACL_PRIVATE -DPOSIX_MISTAKE -I/usr/src/lib/libmd -I/usr/src/contrib/jemalloc/include -I/usr/src/contrib/tzcode/stdtime -I/usr/src/lib/libc/stdtime -I/usr/src/lib/libc/locale -DBROKEN_DES -DPORTMAP -DDES_BUILTIN -I/usr/src/lib/libc/rpc -DYP -DNS_CACHING -DSYMBOL_VERSIONING -g -MD  -MF.depend.__pthread_mutex_init_calloc_cb_stub.o -MT__pthread_mutex_init_calloc_cb_stub.o -std=gnu99 -fstack-protector-strong -Wsystem-headers -Werror -Wall -Wno-format-y2k -Wno-uninitialized -Wno-pointer-sign -Wno-empty-body -Wno-string-plus-int -Wno-unused-const-variable -Wno-tautological-compare -Wno-unused-value -Wno-parentheses-equality -Wno-unused-function -Wno-enum-conversion -Wno-unused-local-typedef -Wno-address-of-packed-member -Wno-switch -Wno-switch-enum -Wno-knr-promoted-parameter  -Qunused-arguments  -I/usr/src/lib/libutil -I/usr/src/lib/msun/powerpc -I/usr/src/lib/msun/src -c /usr/src/lib/libc/gen/__pthread_mutex_init_calloc_cb_stub.c -o __pthread_mutex_init_calloc_cb_stub.o
In file included from /usr/src/lib/libc/gen/__pthread_mutex_init_calloc_cb_stub.c:36:
/usr/src/include/pthread.h:212:6: error: declaration of built-in function 'pthread_create' requires inclusion of the header <pthread.h>
int             pthread_create(pthread_t * __restrict,
1 error generated.
*** Error code 1

make[2]: stopped in /usr/src/lib/libc

It appears that clang is getting confused about having to include Clang's built-in "pthread.h"  while it's trying to compile FreeBSD "pthread.h" itself.
I found similar issue here: https://lkml.org/lkml/2019/1/11/718

The error repeats mentioning "pthread_create" on many other places. As workaround I created the patch attached.

Please let me know your thoughts.
Comment 1 Alfredo Dal'Ava Junior freebsd_committer 2019-02-18 17:37:00 UTC
I confirmed this affects other platforms, so a new patch was added to:

Comment 2 David Chisnall freebsd_committer 2019-02-20 13:03:49 UTC
Does this affect users, or is it part of the FreeBSD build?  We have a number of problems because the base system build uses `-Wsystem-headers` and so clang reports a lot of things that it shouldn't, because it treats the system headers as if they aren't system headers.  If this is only a problem for the FreeBSD build itself, then I'd recommend against patching the header and instead fix the build system (we add `-Wno-system-headers` elsewhere to work around this).
Comment 3 Jessica Clarke freebsd_committer 2019-02-21 23:34:03 UTC
(In reply to David Chisnall from comment #2)

It shows up for users using the system header if they pass -Wsystem-headers, but otherwise not.

The problem is that *any* declaration of pthread_create will trigger this warning, since GetBuiltinType will return {} with Error = GE_Missing_type due to the fact that its type is "" in Builtins.def, and it only works normally because the warning gets suppressed for system headers. I think the correct thing to do is to give it the proper type, which I'm working on at the moment (Clang now has syntax for function types within intrinsic type strings with a recursive parse call so you can express function pointers; it just also needs to learn about pthread(_attr)_t).
Comment 4 Jessica Clarke freebsd_committer 2019-02-22 00:16:16 UTC
(In reply to James Clarke from comment #3)

https://reviews.llvm.org/D58531 fixes this for me. It will still warn if the declaration is of the wrong type, but no longer warns if the declaration matches the expected type. This makes it behave like the other builtins declared in system headers.
Comment 5 Alfredo Dal'Ava Junior freebsd_committer 2019-10-03 12:08:12 UTC
(In reply to James Clarke from comment #4)
I still see other issues regarding conflicting headers during build.

For ports devel/llvm*, currently it does have a patch that removes conflicting headers, so it's "fixed".
For those using llvm from upstream, I use this as workaround:

cd <path where llvm is installed>/lib/clang/9.0.0/include/
mkdir bkp
mv float.h intrin.h inttypes.h iso646.h limits.h stdalign.h stdarg.h stdatomic.h stdbool.h stddef.h stdint.h stdnoreturn.h tgmath.h unwind.h varargs.h bkp/