xref: /petsc/config/PETSc/options/petscdir.py (revision ccb4e88a40f0b86eaeca07ff64c64e4de2fae686)
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    self.isPetsc      = 1
11    return
12
13  def __str1__(self):
14    if hasattr(self, 'dir'):
15      return '  PETSC_DIR: '+str(self.dir)+'\n'
16    return ''
17
18  def setupHelp(self, help):
19    import nargs
20    help.addArgument('PETSc', '-PETSC_DIR=<root-dir>',                        nargs.Arg(None, None, 'The root directory of the PETSc installation'))
21    return
22
23  def configureDirectories(self):
24    '''Checks PETSC_DIR and sets if not set'''
25    if 'PETSC_DIR' in self.framework.argDB:
26      self.dir = os.path.normpath(self.framework.argDB['PETSC_DIR'])
27      if self.dir == 'pwd':
28        raise RuntimeError('You have set -PETSC_DIR=pwd, you need to use back quotes around the pwd\n  like -PETSC_DIR=`pwd`')
29      if not os.path.isdir(self.dir):
30        raise RuntimeError('The value you set with -PETSC_DIR='+self.dir+' is not a directory')
31    elif 'PETSC_DIR' in os.environ:
32      self.dir = os.path.normpath(os.environ['PETSC_DIR'])
33      if self.dir == 'pwd':
34        raise RuntimeError('''
35The environmental variable PETSC_DIR is set incorrectly. Please use the following: [notice backquotes]
36  For sh/bash  : PETSC_DIR=`pwd`; export PETSC_DIR
37  for csh/tcsh : setenv PETSC_DIR `pwd`''')
38      elif not os.path.isdir(self.dir):
39        raise RuntimeError('The environmental variable PETSC_DIR '+self.dir+' is not a directory')
40    else:
41      self.dir = os.getcwd()
42    if self.isPetsc and not os.path.realpath(self.dir) == os.path.realpath(os.getcwd()):
43      raise RuntimeError('The environmental variable PETSC_DIR '+self.dir+' MUST be the current directory '+os.getcwd())
44    self.version  = 'Unknown'
45    versionHeader = os.path.join(self.dir, 'include', 'petscversion.h')
46    versionInfo = []
47    if os.path.exists(versionHeader):
48      f = open(versionHeader)
49      for line in f:
50        if line.find('define PETSC_VERSION') >= 0:
51          versionInfo.append(line[:-1])
52      f.close()
53    else:
54      raise RuntimeError('Invalid PETSc directory '+str(self.dir)+'. Could not locate '+versionHeader)
55    self.versionRelease = True if versionInfo[0].split(' ')[-1] == '1' else False
56    if self.versionRelease:
57      self.version = '.'.join([line.split(' ')[-1] for line in versionInfo[1:4]])
58    else:
59      self.version = '.'.join([line.split(' ')[-1] for line in versionInfo[1:3]])
60      self.version += '.99'
61    self.logPrint('Version Information:')
62    for line in versionInfo:
63      self.logPrint(line)
64    self.framework.argDB['with-executables-search-path'].append(os.path.join(self.dir, 'lib','petsc','bin', 'win32fe'))
65
66    return
67
68  def configure(self):
69    self.executeTest(self.configureDirectories)
70    return
71