From 6c99c7cb0b697057992d25ccafc1ed61d5269013 Mon Sep 17 00:00:00 2001 From: Thomas Nagy Date: Sat, 2 May 2020 21:04:04 +0200 Subject: [PATCH] Add a configuration test for pthread detection #2264 --- waflib/extras/pthread.py | 81 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 waflib/extras/pthread.py diff --git a/waflib/extras/pthread.py b/waflib/extras/pthread.py new file mode 100644 index 00000000..bc100297 --- /dev/null +++ b/waflib/extras/pthread.py @@ -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 + +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() +