[pypy-commit] cffi default: A redo of pull request 19: dynamically determine if the C compiler

arigo noreply at buildbot.pypy.org
Wed Sep 25 10:47:46 CEST 2013


Author: Armin Rigo <arigo at tunes.org>
Branch: 
Changeset: r1349:447c0de7e517
Date: 2013-09-25 10:47 +0200
http://bitbucket.org/cffi/cffi/changeset/447c0de7e517/

Log:	A redo of pull request 19: dynamically determine if the C compiler
	supports "__thread" or not on this platform. Done slightly more
	simply in setup.py.

diff --git a/c/_cffi_backend.c b/c/_cffi_backend.c
--- a/c/_cffi_backend.c
+++ b/c/_cffi_backend.c
@@ -17,12 +17,6 @@
 
 #include "malloc_closure.h"
 
-/* Define USE__THREAD if gcc on your platform supports "__thread"
-   global variables. */
-#if !defined(MS_WIN32) && !defined(X86_DARWIN) && !defined(POWERPC_DARWIN)
-# define USE__THREAD
-#endif
-
 #if PY_MAJOR_VERSION >= 3
 # define STR_OR_BYTES "bytes"
 # define PyText_Type PyUnicode_Type
@@ -202,6 +196,8 @@
    variable */
 #ifndef MS_WIN32
 # ifdef USE__THREAD
+/* This macro ^^^ is defined by setup.py if it finds that it is
+   syntactically valid to use "__thread" with this C compiler. */
 static __thread int cffi_saved_errno = 0;
 static void save_errno(void) { cffi_saved_errno = errno; }
 static void restore_errno(void) { errno = cffi_saved_errno; }
diff --git a/c/check__thread.c b/c/check__thread.c
new file mode 100644
--- /dev/null
+++ b/c/check__thread.c
@@ -0,0 +1,1 @@
+__thread int some_threadlocal_variable_42;
diff --git a/setup.py b/setup.py
--- a/setup.py
+++ b/setup.py
@@ -41,6 +41,17 @@
             #
             resultlist[:] = res
 
+def ask_supports_thread():
+    import distutils.errors
+    from distutils.ccompiler import new_compiler
+    compiler = new_compiler(force=1)
+    try:
+        compiler.compile(['c/check__thread.c'])
+    except distutils.errors.CompileError:
+        print >> sys.stderr, "will not use '__thread' in the C code"
+    else:
+        define_macros.append(('USE__THREAD', None))
+
 def use_pkg_config():
     _ask_pkg_config(include_dirs,       '--cflags-only-I', '-I', sysroot=True)
     _ask_pkg_config(extra_compile_args, '--cflags-only-other')
@@ -71,6 +82,7 @@
                    for filename in _filenames)
 else:
     use_pkg_config()
+    ask_supports_thread()
 
 
 if __name__ == '__main__':


More information about the pypy-commit mailing list