xref: /petsc/config/BuildSystem/config/types.py (revision 7b5fd022a6ba26727040df7457b27566b4c6742d)
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 checkC99Complex(self):
75    '''Check for complex numbers in C99 std
76       Note that since PETSc source code uses _Complex we test specifically for that, not complex'''
77    includes = '#include <complex.h>\n'
78    body     = 'double _Complex x;\n x = I;\n(void)x'
79    if not self.checkCompile(includes, body): return    # checkLink can succeed even if checkCompile fails
80    if self.checkLink(includes, body):
81      # recheck with _GNU_SOURCE active (Issues with crayCC on aarch64)
82      includes = '#define _GNU_SOURCE\n#include <complex.h>\n'
83      if not self.checkCompile(includes, body): return
84      self.addDefine('HAVE_C99_COMPLEX', 1)
85      self.c99_complex = 1
86    return
87
88  def checkCxxComplex(self):
89    '''Check for complex numbers in namespace std'''
90    self.pushLanguage('C++')
91    includes = '#include <complex>\n'
92    body     = 'std::complex<double> x;\n(void)x'
93    if self.checkLink(includes, body):
94      self.addDefine('HAVE_CXX_COMPLEX', 1)
95      self.cxx_complex = 1
96    self.popLanguage()
97    return
98
99  def checkSizeof(self, typeName, typeSizes, otherInclude = None, lang='C', save=True, codeBegin=''):
100    '''Determines the size of type "typeName", and defines SIZEOF_"typeName" to be the size'''
101    self.log.write('Checking for size of type: ' + typeName + '\n')
102    typename = typeName.replace(' ', '-').replace('*', 'p')
103    includes = '''
104#include <sys/types.h>
105#include <stdlib.h>
106#include <stdio.h>
107#include <stddef.h>\n'''
108    mpiFix = '''
109#define MPICH_IGNORE_CXX_SEEK
110#define MPICH_SKIP_MPICXX 1
111#define OMPI_SKIP_MPICXX 1\n'''
112    if otherInclude:
113      if otherInclude == 'mpi.h':
114        includes += mpiFix
115      includes += '#include <' + otherInclude + '>\n'
116    size = None
117    checkName = typeName
118    if typeName == 'enum':
119      checkName = 'enum{ENUM_DUMMY}'
120    with self.Language(lang):
121      for s in typeSizes:
122        body = 'char assert_sizeof[(sizeof({0})=={1})*2-1];'.format(checkName, s)
123        if self.checkCompile(includes, body, codeBegin=codeBegin, codeEnd='\n'):
124          size = s
125          break
126    if size is None:
127      raise RuntimeError('Unable to determine size of {0} not found'.format(typeName))
128    if save:
129      self.sizes[typename] = size
130      self.addDefine('SIZEOF_'+typename.replace('-', '_').upper(), str(size))
131    return size
132
133  def checkVisibility(self):
134    if not self.argDB['with-shared-libraries']:
135      self.argDB['with-visibility'] = 0
136      self.log.write('Disabled visibility attributes due to static build')
137    elif self.argDB['with-visibility']:
138      self.pushLanguage('C')
139      if self.checkCompile('','__attribute__((visibility ("default"))) int foo(void);'):
140        self.addDefine('USE_VISIBILITY_C',1)
141      else:
142        self.log.write('Cannot use visibility attributes with C')
143        self.argDB['with-visibility'] = 0
144      self.popLanguage()
145      if hasattr(self.compilers, 'CXX'):
146        self.pushLanguage('C++')
147        if self.checkCompile('','__attribute__((visibility ("default"))) int foo(void);'):
148          self.addDefine('USE_VISIBILITY_CXX',1)
149        else:
150          self.log.write('Cannot use visibility attributes with C++')
151        self.popLanguage()
152    else:
153      self.log.write('User turned off visibility attributes')
154
155  def checkMaxPathLen(self):
156    '''Sets PETSC_MAX_PATH_LEN depending on values available on the system'''
157    import re
158    HASHLINESPACE = ' *(?:\n#.*\n *)*'
159    self.log.write('Determining PETSC_MAX_PATH_LEN\n')
160    include = ''
161    if self.headers.haveHeader('sys/param.h'):
162      include = (include + '\n#include <sys/param.h>')
163    if self.headers.haveHeader('sys/types.h'):
164      include = (include + '\n#include <sys/types.h>')
165    length = include + '''
166#if defined(MAXPATHLEN)
167#  define PETSC_MAX_PATH_LEN MAXPATHLEN
168#elif defined(MAX_PATH)
169#  define PETSC_MAX_PATH_LEN MAX_PATH
170#elif defined(_MAX_PATH)
171#  define PETSC_MAX_PATH_LEN _MAX_PATH
172#else
173#  define PETSC_MAX_PATH_LEN 4096
174#endif
175#define xstr(s) str(s)
176#define str(s) #s
177char petsc_max_path_len[] = xstr(PETSC_MAX_PATH_LEN);
178'''
179    MaxPathLength = 'unknown'
180    if self.checkCompile(length):
181      buf = self.outputPreprocess(length)
182      try:
183        MaxPathLength = re.compile('\n'+r'char petsc_max_path_len\s?\[\s?\] = '+HASHLINESPACE+'\"([0-9]+)\"'+HASHLINESPACE+';').search(buf).group(1)
184      except:
185        raise RuntimeError('Unable to determine PETSC_MAX_PATH_LEN')
186    if MaxPathLength == 'unknown' or not MaxPathLength.isdigit():
187      raise RuntimeError('Unable to determine PETSC_MAX_PATH_LEN')
188    else:
189      self.addDefine('MAX_PATH_LEN',MaxPathLength)
190
191  def configure(self):
192    self.executeTest(self.check_struct_sigaction)
193    self.executeTest(self.check__int64)
194    self.executeTest(self.checkSizeTypes)
195    self.executeTest(self.checkFileTypes)
196    self.executeTest(self.checkIntegerTypes)
197    self.executeTest(self.checkPID)
198    self.executeTest(self.checkC99Complex)
199    if hasattr(self.compilers, 'CXX'):
200      self.executeTest(self.checkCxxComplex)
201    for t, sizes in {'void *': (8, 4),
202                     'int': (4, 8, 2),
203                     'long': (8, 4),
204                     'long long': (8,),
205                     'size_t': (8, 4)}.items():
206      self.executeTest(self.checkSizeof, args=[t, sizes])
207    if self.sizes['void-p'] == 8:
208      self.addDefine('USING_64BIT_PTR',1)
209    self.executeTest(self.checkVisibility)
210    self.executeTest(self.checkMaxPathLen)
211    return
212