xref: /petsc/config/PETSc/options/petscdir.py (revision 3106df1b66e9b8e72f2b9582a35c59177bd911d7)
1import config.base
2import os
3import re
4
5class Configure(config.base.Configure):
6  def __init__(self, framework):
7    config.base.Configure.__init__(self, framework)
8    self.headerPrefix = 'PETSC'
9    self.substPrefix  = 'PETSC'
10    return
11
12  def __str1__(self):
13    if hasattr(self, 'dir'):
14      return '  PETSC_DIR: '+str(self.dir)+'\n'
15    return ''
16
17  def setupHelp(self, help):
18    import nargs
19    help.addArgument('PETSc', '-PETSC_DIR=<root-dir>',                        nargs.Arg(None, None, 'The root directory of the PETSc installation'))
20    return
21
22  def configureDirectories(self):
23    '''Checks PETSC_DIR and sets if not set'''
24    if 'PETSC_DIR' in self.framework.argDB:
25      self.dir = os.path.normpath(self.framework.argDB['PETSC_DIR'])
26      msg1 = 'The configure option'
27      msg2 = ''
28    elif 'PETSC_DIR' in os.environ:
29      self.dir = os.path.normpath(os.environ['PETSC_DIR'])
30      msg1 = 'The environmental variable'
31      msg2 = 'export'
32    else:
33      self.dir = os.getcwd()
34      msg1 = ''
35      msg2 = ''
36
37    if self.dir == 'pwd':
38      raise RuntimeError('{0} PETSC_DIR=pwd is incorrect. You need to use back quotes around the pwd - i.e: {1} PETSC_DIR=`pwd`'.format(msg1, msg2))
39    elif self.dir.find(' ') > -1:
40      raise RuntimeError('{0} PETSC_DIR="{1}" has spaces in it; this is not allowed. Change the directory with PETSc to not have spaces in it'.format(msg1, self.dir))
41    elif not os.path.isabs(self.dir):
42      raise RuntimeError('{0} PETSC_DIR={1} is a relative path. Use absolute path - i.e: {2} PETSC_DIR={3}'.format(msg1, self.dir, msg2, os.path.abspath(self.dir)))
43    elif not os.path.isdir(self.dir):
44      raise RuntimeError('{0} PETSC_DIR={1} is not a directory'.format(msg1, self.dir))
45    elif os.path.realpath(self.dir) != os.path.realpath(os.getcwd()):
46      raise RuntimeError('{0} PETSC_DIR={1} MUST be the current directory {2}'.format(msg1, self.dir, os.getcwd()))
47
48    self.version  = 'Unknown'
49    versionHeader = os.path.join(self.dir, 'include', 'petscversion.h')
50    versionInfo = []
51    if os.path.exists(versionHeader):
52      f = open(versionHeader)
53      for line in f:
54        if line.find('define PETSC_VERSION') >= 0:
55          versionInfo.append(line[:-1])
56      f.close()
57    else:
58      raise RuntimeError('Invalid PETSc directory '+str(self.dir)+'. Could not locate '+versionHeader)
59    self.versionRelease = True if versionInfo[0].split(' ')[-1] == '1' else False
60    if self.versionRelease:
61      self.version = '.'.join([line.split(' ')[-1] for line in versionInfo[1:4]])
62    else:
63      self.version = '.'.join([line.split(' ')[-1] for line in versionInfo[1:3]])
64      self.version += '.99'
65    self.logPrint('PETSC_VERSION_RELEASE of 1 indicates the code is from a release branch or a branch created from a release branch.')
66    self.logPrint('Version Information:')
67    for line in versionInfo:
68      self.logPrint(line)
69    dirs = self.argDB['with-executables-search-path']
70    if not isinstance(dirs, list): dirs = dirs.split(os.path.pathsep)
71    dirs.append(os.path.join(self.dir, 'lib','petsc','bin', 'win32fe'))
72    self.framework.argDB['with-executables-search-path'] = dirs
73    return
74
75  def configure(self):
76    self.executeTest(self.configureDirectories)
77    return
78