Skip to content

Commit bb31279

Browse files
committed
attempt to fix libatomic requirement detection
1 parent 08297dd commit bb31279

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

build_ffi_module.py

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212

1313
import os
14+
import subprocess
1415
import shlex
1516
from cffi import FFI
1617

@@ -807,10 +808,38 @@
807808
compiler_args = []
808809
libraries = []
809810

811+
812+
def check_linker_need_libatomic():
813+
"""
814+
Test if linker on system needs libatomic.
815+
This has been copied from https://github.com/grpc/grpc/blob/master/setup.py#L205
816+
"""
817+
code_test = (b'#include <atomic>\n' +
818+
b'int main() { return std::atomic<int64_t>{}; }')
819+
cxx = shlex.split(os.environ.get('CXX', 'c++'))
820+
cpp_test = subprocess.Popen(cxx + ['-x', 'c++', '-std=c++14', '-'],
821+
stdin=subprocess.PIPE,
822+
stdout=subprocess.PIPE,
823+
stderr=subprocess.PIPE)
824+
cpp_test.communicate(input=code_test)
825+
if cpp_test.returncode == 0:
826+
return False
827+
# Double-check to see if -latomic actually can solve the problem.
828+
# https://github.com/grpc/grpc/issues/22491
829+
cpp_test = subprocess.Popen(cxx +
830+
['-x', 'c++', '-std=c++14', '-', '-latomic'],
831+
stdin=subprocess.PIPE,
832+
stdout=subprocess.PIPE,
833+
stderr=subprocess.PIPE)
834+
cpp_test.communicate(input=code_test)
835+
return cpp_test.returncode == 0
836+
837+
810838
if os.name == "posix":
811839
compiler_args = ["-g1", "-O3", "-ffast-math"]
812-
libraries = ["m", "pthread", "dl", "atomic"]
813-
840+
libraries = ["m", "pthread", "dl"]
841+
if check_linker_need_libatomic():
842+
libraries.append("atomic")
814843
if "PYMINIAUDIO_EXTRA_CFLAGS" in os.environ:
815844
compiler_args += shlex.split(os.environ.get("PYMINIAUDIO_EXTRA_CFLAGS", ""))
816845

@@ -844,4 +873,5 @@
844873

845874

846875
if __name__ == "__main__":
876+
print("NEED LIBATOMIC?", check_linker_need_libatomic())
847877
ffibuilder.compile(verbose=True)

0 commit comments

Comments
 (0)