#1012058 debhelper: riscv64 needs THREADS_PREFER_PTHREAD_FLAG in cmake for atomics-related ftbfs

#1012058#5
Date:
2022-05-29 14:49:59 UTC
From:
To:
Dear Maintainer,

On riscv64, many FTBFS issues share a root cause, and I believe it would
be appropriate to address it in debhelper. This architecture lacks hardware
support for small atomics. These operations must be implemented in software
instead of as instructions. The pthreads library includes this support, but
needs to be linked with "-pthread", and not "-lpthread" to use this
functionality.

There is also currently a gcc bug related to this, with ongoing
discussion on what to do long-term.
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81358

The following cmake directive causes the build process to prefer -pthread
over -lpthread, and in this case, makes it possible to use small atomic
operations on riscv64.
set(THREADS_PREFER_PTHREAD_FLAG ON)

Rather than applying a patch for this flag to every affected package
individually, it seems more sensible to apply it through debhelper. If
the architecture is riscv64, then the above cmake directive should be set.
I think this could be added in lib/Debian/Debhelper/Buildsystem/cmake.pm

Below I have gathered several relevant lists of packages.
These lists are probably incomplete.

Likely affected with no patch pending:
	fairy-stockfish
	leela-zero
	muse
	neochat
	rust-jemalloc-sys
	wsclean
	zynaddsubfx

Affected with a patch pending:
	simgear https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=1011260
	thinkfan https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=1011953
	warzone2100 https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=1011367

Affected with a patch applied:
	ceph https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=953003
	webkit2gtk https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=895969
	yoshimi https://sources.debian.org/src/yoshimi/2.2.0-2/debian/patches/fix_ftbfs_on_risc64.patch/

Affected with a patch applied, but does not use cmake:
	libzstd https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=895259

Similar unfixed problem, but not currently using -lpthread (need to use -latomic):
	numactl
	opentracing-cpp
	tiledb-r

Also, see here for existing occurrences of this cmake directive in Debian packages:
https://codesearch.debian.net/search?q=THREADS_PREFER_PTHREAD_FLAG

Special thanks to Bo YU <tsu.yubo@gmail.com> for help in preparing this bug report.
-Alan Beadle <ab.beadle@gmail.com>

#1012058#10
Date:
2022-05-31 14:40:59 UTC
From:
To:
Apparently glibc 2.34 breaks this fix anyway, because cmake ignores
the aforementioned directive when using glibc 2.34.
Refer to the first "Major new features" point here:
https://sourceware.org/pipermail/libc-alpha/2021-August/129718.html

Here is a blog post with a deeper exploration of the problem:
https://blog.jiejiss.com/A-RISC-V-gcc-pitfall-revealed-by-a-glibc-update/

Applying the cmake directive will not fix this problem for very long.
We will need to find a different solution, so I am closing this bug.
-Alan Beadle