@@ -214,6 +214,43 @@ else
214
214
lapack_dep = declare_dependency(dependencies : [lapack, blas_dep])
215
215
endif
216
216
217
+ # Determine whether it is necessary to link libatomic. This could be the case
218
+ # e.g. on 32-bit platforms when atomic operations are used on 64-bit types.
219
+ # The check is copied from SciPy, who copied it from Mesa
220
+ # <https://www.mesa3d.org/>.
221
+ null_dep = dependency ('' , required : false )
222
+ atomic_dep = null_dep
223
+ code_non_lockfree = '''
224
+ #include <stdint.h>
225
+ int main() {
226
+ struct {
227
+ uint64_t *v;
228
+ } x;
229
+ return (int)__atomic_load_n(x.v, __ATOMIC_ACQUIRE) &
230
+ (int)__atomic_add_fetch(x.v, (uint64_t)1, __ATOMIC_ACQ_REL);
231
+ }
232
+ '''
233
+ if cc.get_id() != ' msvc'
234
+ if not cc.links(
235
+ code_non_lockfree,
236
+ name : ' Check atomic builtins without -latomic'
237
+ )
238
+ atomic_dep = cc.find_library (' atomic' , required : false )
239
+ if atomic_dep.found()
240
+ # We're not sure that with `-latomic` things will work for all compilers,
241
+ # so verify and only keep libatomic as a dependency if this works. It is
242
+ # possible the build will fail later otherwise - unclear under what
243
+ # circumstances (compilers, runtimes, etc.) exactly.
244
+ if not cc.links(
245
+ code_non_lockfree,
246
+ dependencies : atomic_dep,
247
+ name : ' Check atomic builtins with -latomic'
248
+ )
249
+ atomic_dep = null_dep
250
+ endif
251
+ endif
252
+ endif
253
+ endif
217
254
218
255
# Copy the main __init__.py|pxd files to the build dir (needed for Cython)
219
256
__init__py = fs.copyfile(' __init__.py' )
0 commit comments