xref: /petsc/config/PETSc/options/petscdir.py (revision 3106df1b66e9b8e72f2b9582a35c59177bd911d7)
19d310bb7SBarry Smithimport config.base
29d310bb7SBarry Smithimport os
39d310bb7SBarry Smithimport re
49d310bb7SBarry Smith
59d310bb7SBarry Smithclass Configure(config.base.Configure):
69d310bb7SBarry Smith  def __init__(self, framework):
79d310bb7SBarry Smith    config.base.Configure.__init__(self, framework)
89d310bb7SBarry Smith    self.headerPrefix = 'PETSC'
99d310bb7SBarry Smith    self.substPrefix  = 'PETSC'
109d310bb7SBarry Smith    return
119d310bb7SBarry Smith
129d310bb7SBarry Smith  def __str1__(self):
139d310bb7SBarry Smith    if hasattr(self, 'dir'):
149d310bb7SBarry Smith      return '  PETSC_DIR: '+str(self.dir)+'\n'
159d310bb7SBarry Smith    return ''
169d310bb7SBarry Smith
179d310bb7SBarry Smith  def setupHelp(self, help):
189d310bb7SBarry Smith    import nargs
199d310bb7SBarry Smith    help.addArgument('PETSc', '-PETSC_DIR=<root-dir>',                        nargs.Arg(None, None, 'The root directory of the PETSc installation'))
209d310bb7SBarry Smith    return
219d310bb7SBarry Smith
229d310bb7SBarry Smith  def configureDirectories(self):
239d310bb7SBarry Smith    '''Checks PETSC_DIR and sets if not set'''
249d310bb7SBarry Smith    if 'PETSC_DIR' in self.framework.argDB:
252b2c43dbSSatish Balay      self.dir = os.path.normpath(self.framework.argDB['PETSC_DIR'])
26856b83eaSSatish Balay      msg1 = 'The configure option'
27856b83eaSSatish Balay      msg2 = ''
289d310bb7SBarry Smith    elif 'PETSC_DIR' in os.environ:
292b2c43dbSSatish Balay      self.dir = os.path.normpath(os.environ['PETSC_DIR'])
30856b83eaSSatish Balay      msg1 = 'The environmental variable'
31856b83eaSSatish Balay      msg2 = 'export'
329d310bb7SBarry Smith    else:
339d310bb7SBarry Smith      self.dir = os.getcwd()
34856b83eaSSatish Balay      msg1 = ''
35856b83eaSSatish Balay      msg2 = ''
36856b83eaSSatish Balay
37856b83eaSSatish Balay    if self.dir == 'pwd':
38856b83eaSSatish Balay      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))
39856b83eaSSatish Balay    elif self.dir.find(' ') > -1:
40856b83eaSSatish Balay      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))
41856b83eaSSatish Balay    elif not os.path.isabs(self.dir):
42856b83eaSSatish Balay      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)))
43856b83eaSSatish Balay    elif not os.path.isdir(self.dir):
44856b83eaSSatish Balay      raise RuntimeError('{0} PETSC_DIR={1} is not a directory'.format(msg1, self.dir))
45856b83eaSSatish Balay    elif os.path.realpath(self.dir) != os.path.realpath(os.getcwd()):
46856b83eaSSatish Balay      raise RuntimeError('{0} PETSC_DIR={1} MUST be the current directory {2}'.format(msg1, self.dir, os.getcwd()))
47856b83eaSSatish Balay
489d310bb7SBarry Smith    self.version  = 'Unknown'
499d310bb7SBarry Smith    versionHeader = os.path.join(self.dir, 'include', 'petscversion.h')
509d310bb7SBarry Smith    versionInfo = []
519d310bb7SBarry Smith    if os.path.exists(versionHeader):
52c6ef1b5bSJed Brown      f = open(versionHeader)
539d310bb7SBarry Smith      for line in f:
549d310bb7SBarry Smith        if line.find('define PETSC_VERSION') >= 0:
559d310bb7SBarry Smith          versionInfo.append(line[:-1])
569d310bb7SBarry Smith      f.close()
579d310bb7SBarry Smith    else:
589d310bb7SBarry Smith      raise RuntimeError('Invalid PETSc directory '+str(self.dir)+'. Could not locate '+versionHeader)
59ea7278aeSSatish Balay    self.versionRelease = True if versionInfo[0].split(' ')[-1] == '1' else False
60ea7278aeSSatish Balay    if self.versionRelease:
619d310bb7SBarry Smith      self.version = '.'.join([line.split(' ')[-1] for line in versionInfo[1:4]])
62ea7278aeSSatish Balay    else:
63ea7278aeSSatish Balay      self.version = '.'.join([line.split(' ')[-1] for line in versionInfo[1:3]])
64ea7278aeSSatish Balay      self.version += '.99'
65df57f80eSBarry Smith    self.logPrint('PETSC_VERSION_RELEASE of 1 indicates the code is from a release branch or a branch created from a release branch.')
669d310bb7SBarry Smith    self.logPrint('Version Information:')
679d310bb7SBarry Smith    for line in versionInfo:
689d310bb7SBarry Smith      self.logPrint(line)
69*9ad87f5aSSatish Balay    dirs = self.argDB['with-executables-search-path']
70*9ad87f5aSSatish Balay    if not isinstance(dirs, list): dirs = dirs.split(os.path.pathsep)
71*9ad87f5aSSatish Balay    dirs.append(os.path.join(self.dir, 'lib','petsc','bin', 'win32fe'))
72*9ad87f5aSSatish Balay    self.framework.argDB['with-executables-search-path'] = dirs
739d310bb7SBarry Smith    return
749d310bb7SBarry Smith
759d310bb7SBarry Smith  def configure(self):
769d310bb7SBarry Smith    self.executeTest(self.configureDirectories)
779d310bb7SBarry Smith    return
78