Add a configuration test for pthread detection #2264

This commit is contained in:
Thomas Nagy 2020-05-02 21:04:04 +02:00
parent 5ee61cfa91
commit 6c99c7cb0b
1 changed files with 81 additions and 0 deletions

81
waflib/extras/pthread.py Normal file
View File

@ -0,0 +1,81 @@
#! /usr/bin/env python
# encoding: UTF-8
# Thomas Nagy 2020 (ita)
from waflib import Utils
from waflib.Configure import conf
PTHREAD_CHECK = '''
#include <pthread.h>
static void* fun(void* params) {
return NULL;
}
int main() {
pthread_t thread;
pthread_create(&thread, NULL, &fun, NULL);
pthread_join(thread, NULL);
}
'''
@conf
def check_pthreads(self, mode=None):
if not mode:
mode = 'cxx' if self.env.CXX else 'c'
if Utils.unversioned_sys_platform() == 'sunos':
flags = ['-pthreads', '-lpthread', '-mt', '-pthread']
else:
flags = ['', '-lpthreads', '-Kthread', '-kthread', '-llthread', '-pthread', '-pthreads', '-mthreads', '-lpthread', '--thread-safe', '-mt']
features = mode
for flag in flags:
self.env.stash()
self.env[mode.upper() + 'FLAGS_PTHREAD'] = [flag]
if flag:
msg = ' -> Trying pthread compilation flag %s' % flag
okmsg = 'needs %s' % flag
else:
msg = 'Checking if a pthread flag is necessary for compiling'
okmsg = 'None'
try:
self.check(features=features, msg=msg, okmsg=okmsg, use='PTHREAD', fragment=PTHREAD_CHECK)
except self.errors.ConfigurationError:
self.env.revert()
continue
else:
break
else:
self.fatal('Could not find a suitable pthreads flag for compiling')
features = '%s %sprogram' % (mode, mode)
for flag in flags:
self.env.stash()
self.env.LINKFLAGS_PTHREAD = [flag]
if flag:
msg = ' -> Trying pthread link flag %s' % flag
okmsg = 'needs %s' % flag
else:
msg = 'Checking if a pthread flag is necessary for linking'
okmsg = 'None'
try:
self.check(features=features, msg=msg, okmsg=okmsg, use='PTHREAD', fragment=PTHREAD_CHECK)
except self.errors.ConfigurationError:
self.env.revert()
continue
else:
break
else:
self.fatal('Could not find a suitable pthreads flag for linking')
def configure(self):
self.check_pthreads()