xref: /petsc/config/BuildSystem/config/types.py (revision db527f055844eaecc161799bb933f00febbc4fb3)
1import config.base
2
3import os
4
5class Configure(config.base.Configure):
6  def __init__(self, framework):
7    config.base.Configure.__init__(self, framework)
8    self.headerPrefix = ''
9    self.substPrefix  = ''
10    self.sizes = {}
11    self.c99_complex = 0
12    self.cxx_complex = 0
13    return
14
15  def setupHelp(self, help):
16    import nargs
17    help.addArgument('Visibility', '-with-visibility=<bool>', nargs.ArgBool(None, 1, 'Use compiler visibility flags to limit symbol visibility'))
18    return
19
20  def setupDependencies(self, framework):
21    config.base.Configure.setupDependencies(self, framework)
22    self.compilers = framework.require('config.compilers', self)
23    self.headers   = framework.require('config.headers', self)
24    return
25
26  def check(self, typeName, defaultType = None, includes = []):
27    '''Checks that "typeName" exists, and if not defines it to "defaultType" if given'''
28    self.log.write('Checking for type: '+typeName+'\n')
29    include = '''
30#include <sys/types.h>
31#include <stdlib.h>
32#include <stddef.h>
33%s
34    ''' % ('\n'.join(['#include<%s>' % inc for inc in includes]))
35    found = self.checkCompile(include,typeName+' a;\n(void)a')
36    if not found and defaultType:
37      self.addTypedef(defaultType, typeName)
38    else:
39      self.log.write(typeName+' found\n')
40    return found
41
42  def check_struct_sigaction(self):
43    '''Checks if "struct sigaction" exists in signal.h. This check is for C89 check.'''
44    if self.check('struct sigaction', includes = ['signal.h']):
45      self.addDefine('HAVE_STRUCT_SIGACTION',1)
46    return
47
48  def check__int64(self):
49    '''Checks if __int64 exists. This is primarily for windows.'''
50    if self.check('__int64'):
51      self.addDefine('HAVE___INT64',1)
52    return
53
54  def checkSizeTypes(self):
55    '''Checks for types associated with sizes, such as size_t.'''
56    self.check('size_t', 'int')
57    return
58
59  def checkIntegerTypes(self):
60    '''Checks for types associated with integers, such as int32_t.'''
61    self.check('int32_t', 'int')
62    return
63
64  def checkFileTypes(self):
65    '''Checks for types associated with files, such as mode_t, off_t, etc.'''
66    self.check('mode_t', 'int')
67    self.check('off_t', 'int')
68    return
69
70  def checkPID(self):
71    '''Checks for pid_t, and defines it if necessary'''
72    return self.check('pid_t', 'int')
73
74  def checkUID(self):
75    '''Checks for uid_t and gid_t, and defines them if necessary'''
76    if self.outputPreprocess('#include <sys/types.h>').find('uid_t') < 0:
77      self.addDefine('uid_t', 'int')
78      self.addDefine('gid_t', 'int')
79    return
80
81  def checkC99Complex(self):
82    '''Check for complex numbers in in C99 std
83       Note that since PETSc source code uses _Complex we test specifically for that, not complex'''
84    includes = '#include <complex.h>\n'
85    body     = 'double _Complex x;\n x = I;\n(void)x'
86    if not self.checkCompile(includes, body): return    # checkLink can succeed even if checkCompile fails
87    if self.checkLink(includes, body):
88      # recheck with _GNU_SOURCE active (Issues with crayCC on aarch64)
89      includes = '#define _GNU_SOURCE\n#include <complex.h>\n'
90      if not self.checkCompile(includes, body): return
91      self.addDefine('HAVE_C99_COMPLEX', 1)
92      self.c99_complex = 1
93    return
94
95  def checkCxxComplex(self):
96    '''Check for complex numbers in namespace std'''
97    self.pushLanguage('C++')
98    includes = '#include <complex>\n'
99    body     = 'std::complex<double> x;\n(void)x'
100    if self.checkLink(includes, body):
101      self.addDefine('HAVE_CXX_COMPLEX', 1)
102      self.cxx_complex = 1
103    self.popLanguage()
104    return
105
106  def checkSizeof(self, typeName, typeSizes, otherInclude = None, lang='C', save=True, codeBegin=''):
107    '''Determines the size of type "typeName", and defines SIZEOF_"typeName" to be the size'''
108    self.log.write('Checking for size of type: ' + typeName + '\n')
109    typename = typeName.replace(' ', '-').replace('*', 'p')
110    includes = '''
111#include <sys/types.h>
112#include <stdlib.h>
113#include <stdio.h>
114#include <stddef.h>'''
115    mpiFix = '''
116#define MPICH_IGNORE_CXX_SEEK
117#define MPICH_SKIP_MPICXX 1
118#define OMPI_SKIP_MPICXX 1\n'''
119    if otherInclude:
120      if otherInclude == 'mpi.h':
121        includes += mpiFix
122      includes += '#include <' + otherInclude + '>\n'
123    size = None
124    checkName = typeName
125    if typeName == 'enum':
126      checkName = 'enum{ENUM_DUMMY}'
127    with self.Language(lang):
128      for s in typeSizes:
129        body = 'char assert_sizeof[(sizeof({0})=={1})*2-1];'.format(checkName, s)
130        if self.checkCompile(includes, body, codeBegin=codeBegin, codeEnd='\n'):
131          size = s
132          break
133    if size is None:
134      raise RuntimeError('Unable to determine size of {0} not found'.format(typeName))
135    if save:
136      self.sizes[typename] = size
137      self.addDefine('SIZEOF_'+typename.replace('-', '_').upper(), str(size))
138    return size
139
140  def checkVisibility(self):
141    if not self.argDB['with-shared-libraries']:
142      self.argDB['with-visibility'] = 0
143      self.log.write('Disabled visibility attributes due to static build')
144    elif self.argDB['with-visibility']:
145      self.pushLanguage('C')
146      if self.checkCompile('','__attribute__((visibility ("default"))) int foo(void);'):
147        self.addDefine('USE_VISIBILITY_C',1)
148      else:
149        self.log.write('Cannot use visibility attributes with C')
150        self.argDB['with-visibility'] = 0
151      self.popLanguage()
152      if hasattr(self.compilers, 'CXX'):
153        self.pushLanguage('C++')
154        if self.checkCompile('','__attribute__((visibility ("default"))) int foo(void);'):
155          self.addDefine('USE_VISIBILITY_CXX',1)
156        else:
157          self.log.write('Cannot use visibility attributes with C++')
158        self.popLanguage()
159    else:
160      self.log.write('User turned off visibility attributes')
161
162  def checkMaxPathLen(self):
163    '''Sets PETSC_MAX_PATH_LEN depending on values available on the system'''
164    import re
165    HASHLINESPACE = ' *(?:\n#.*\n *)*'
166    self.log.write('Determining PETSC_MAX_PATH_LEN\n')
167    include = ''
168    if self.headers.haveHeader('sys/param.h'):
169      include = (include + '\n#include <sys/param.h>')
170    if self.headers.haveHeader('sys/types.h'):
171      include = (include + '\n#include <sys/types.h>')
172    length = include + '''
173#if defined(MAXPATHLEN)
174#  define PETSC_MAX_PATH_LEN MAXPATHLEN
175#elif defined(MAX_PATH)
176#  define PETSC_MAX_PATH_LEN MAX_PATH
177#elif defined(_MAX_PATH)
178#  define PETSC_MAX_PATH_LEN _MAX_PATH
179#else
180#  define PETSC_MAX_PATH_LEN 4096
181#endif
182#define xstr(s) str(s)
183#define str(s) #s
184char petsc_max_path_len[] = xstr(PETSC_MAX_PATH_LEN);
185'''
186    MaxPathLength = 'unknown'
187    if self.checkCompile(length):
188      buf = self.outputPreprocess(length)
189      try:
190        MaxPathLength = re.compile('\nchar petsc_max_path_len\s?\[\s?\] = '+HASHLINESPACE+'\"([0-9]+)\"'+HASHLINESPACE+';').search(buf).group(1)
191      except:
192        raise RuntimeError('Unable to determine PETSC_MAX_PATH_LEN')
193    if MaxPathLength == 'unknown' or not MaxPathLength.isdigit():
194      raise RuntimeError('Unable to determine PETSC_MAX_PATH_LEN')
195    else:
196      self.addDefine('MAX_PATH_LEN',MaxPathLength)
197
198
199  def configure(self):
200    self.executeTest(self.check_struct_sigaction)
201    self.executeTest(self.check__int64)
202    self.executeTest(self.checkSizeTypes)
203    self.executeTest(self.checkFileTypes)
204    self.executeTest(self.checkIntegerTypes)
205    self.executeTest(self.checkPID)
206    self.executeTest(self.checkUID)
207    self.executeTest(self.checkC99Complex)
208    if hasattr(self.compilers, 'CXX'):
209      self.executeTest(self.checkCxxComplex)
210    for t, sizes in {'void *': (8, 4),
211                     'short': (2, 4, 8),
212                     'int': (4, 8, 2),
213                     'long': (8, 4),
214                     'long long': (8,),
215                     'enum': (4, 8),
216                     'size_t': (8, 4)}.items():
217      self.executeTest(self.checkSizeof, args=[t, sizes])
218    if self.sizes['void-p'] == 8:
219      self.addDefine('USING_64BIT_PTR',1)
220    self.executeTest(self.checkVisibility)
221    self.executeTest(self.checkMaxPathLen)
222    return
223