Detect pthread support by compiling a test program with the -pthread switch, not just check for the header file.
authorGustavo J. A. M. Carneiro <gjc@inescporto.pt>
Sat, 24 Jan 2009 19:25:33 +0000
changeset 4121 4bbe798b0dab
parent 4120 51f223987b89
child 4122 8f403a728b57
Detect pthread support by compiling a test program with the -pthread switch, not just check for the header file.
src/core/wscript
--- a/src/core/wscript	Sat Jan 24 18:47:47 2009 +0000
+++ b/src/core/wscript	Sat Jan 24 19:25:33 2009 +0000
@@ -9,7 +9,30 @@
 
     conf.check(header_name='signal.h', define_name='HAVE_SIGNAL_H')
 
-    conf.env['ENABLE_THREADING'] = conf.check(header_name='pthread.h', define_name='HAVE_PTHREAD_H')
+    # Check for POSIX threads
+    test_env = conf.env.copy()
+    test_env.append_value('LINKFLAGS', '-pthread')
+    test_env.append_value('CXXFLAGS', '-pthread')
+    test_env.append_value('CCFLAGS', '-pthread')
+    fragment = """
+#include <pthread.h>
+int main ()
+{
+   pthread_mutex_t m;
+   pthread_mutex_init (&m, NULL);
+   return 0;
+}
+    """
+    have_pthread = conf.check(header_name='pthread.h', define_name='HAVE_PTHREAD_H',
+                              env=test_env, fragment=fragment,
+                              errmsg='Could not find pthread support (build/config.log for details)',
+                              mandatory=False)
+    if have_pthread:
+        conf.env['CXXFLAGS_PTHREAD'] = '-pthread'
+        conf.env['CCFLAGS_PTHREAD'] = '-pthread'
+        conf.env['LINKFLAGS_PTHREAD'] = '-pthread'
+
+    conf.env['ENABLE_THREADING'] = have_pthread
 
     conf.report_optional_feature("Threading", "Threading Primitives",
                                  conf.env['ENABLE_THREADING'],
@@ -112,6 +135,7 @@
             'unix-system-mutex.cc',
             'unix-system-condition.cc',
             ])
+        core.uselib = 'PTHREAD'
         headers.source.extend([
                 'system-mutex.h',
                 'system-thread.h',