xref: /petsc/config/PETSc/options/arch.py (revision b60faad8a32629c6393c662dc268c57297b08979)
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 not hasattr(self, 'arch'):
149d310bb7SBarry Smith      return ''
159d310bb7SBarry Smith    desc = ['PETSc:']
169d310bb7SBarry Smith    desc.append('  PETSC_ARCH: '+str(self.arch))
179d310bb7SBarry Smith    return '\n'.join(desc)+'\n'
189d310bb7SBarry Smith
199d310bb7SBarry Smith  def setupHelp(self, help):
209d310bb7SBarry Smith    import nargs
219d310bb7SBarry Smith    help.addArgument('PETSc', '-PETSC_ARCH=<string>',     nargs.Arg(None, None, 'The configuration name'))
229d310bb7SBarry Smith    help.addArgument('PETSc', '-with-petsc-arch=<string>',nargs.Arg(None, None, 'The configuration name'))
23dd328ec0SBarry Smith    help.addArgument('PETSc', '-force=<bool>',            nargs.ArgBool(None, 0, 'Bypass configure hash caching, and run to completion'))
24dd328ec0SBarry Smith    return
25dd328ec0SBarry Smith
26dd328ec0SBarry Smith  def setupDependencies(self, framework):
27dd328ec0SBarry Smith    self.sourceControl = framework.require('config.sourceControl',self)
28dd328ec0SBarry Smith    self.petscdir = framework.require('PETSc.options.petscdir', self)
299d310bb7SBarry Smith    return
309d310bb7SBarry Smith
3170211a5bSSatish Balay  def createArchitecture(self):
3270211a5bSSatish Balay    import sys
3370211a5bSSatish Balay    arch = 'arch-' + sys.platform.replace('cygwin','mswin')
3470211a5bSSatish Balay    # use opt/debug, c/c++ tags.s
3570211a5bSSatish Balay    arch+= '-'+self.framework.argDB['with-clanguage'].lower().replace('+','x')
3670211a5bSSatish Balay    if self.framework.argDB['with-debugging']:
3770211a5bSSatish Balay      arch += '-debug'
3870211a5bSSatish Balay    else:
3970211a5bSSatish Balay      arch += '-opt'
4070211a5bSSatish Balay    return arch
4170211a5bSSatish Balay
429d310bb7SBarry Smith  def configureArchitecture(self):
439d310bb7SBarry Smith    '''Checks PETSC_ARCH and sets if not set'''
449d310bb7SBarry Smith    # Warn if PETSC_ARCH doesnt match env variable
459d310bb7SBarry Smith    if 'PETSC_ARCH' in self.framework.argDB and 'PETSC_ARCH' in os.environ and self.framework.argDB['PETSC_ARCH'] != os.environ['PETSC_ARCH']:
469d310bb7SBarry Smith      self.logPrintBox('''\
479d310bb7SBarry SmithWarning: PETSC_ARCH from environment does not match command-line or name of script.
489d310bb7SBarry SmithWarning: Using from command-line or name of script: %s, ignoring environment: %s''' % (str(self.framework.argDB['PETSC_ARCH']), str(os.environ['PETSC_ARCH'])))
4957ea55fdSJed Brown      os.environ['PETSC_ARCH'] = self.framework.argDB['PETSC_ARCH']
509d310bb7SBarry Smith    if 'with-petsc-arch' in self.framework.argDB:
519d310bb7SBarry Smith      self.arch = self.framework.argDB['with-petsc-arch']
5270211a5bSSatish Balay      msg = 'option -with-petsc-arch='+str(self.arch)
539d310bb7SBarry Smith    elif 'PETSC_ARCH' in self.framework.argDB:
549d310bb7SBarry Smith      self.arch = self.framework.argDB['PETSC_ARCH']
5570211a5bSSatish Balay      msg = 'option PETSC_ARCH='+str(self.arch)
5670211a5bSSatish Balay    elif 'PETSC_ARCH' in os.environ:
579d310bb7SBarry Smith      self.arch = os.environ['PETSC_ARCH']
5870211a5bSSatish Balay      msg = 'environment variable PETSC_ARCH='+str(self.arch)
599d310bb7SBarry Smith    else:
6070211a5bSSatish Balay      self.arch = self.createArchitecture()
619d310bb7SBarry Smith    if self.arch.find('/') >= 0 or self.arch.find('\\') >= 0:
6270211a5bSSatish Balay      raise RuntimeError('PETSC_ARCH should not contain path characters, but you have specified with '+msg)
63002ae2c9SLisandro Dalcin    if self.arch.startswith('-'):
6470211a5bSSatish Balay      raise RuntimeError('PETSC_ARCH should not start with "-", but you have specified with '+msg)
6570211a5bSSatish Balay    if self.arch.startswith('.'):
6670211a5bSSatish Balay      raise RuntimeError('PETSC_ARCH should not start with ".", but you have specified with '+msg)
6770211a5bSSatish Balay    if not len(self.arch):
6870211a5bSSatish Balay      raise RuntimeError('PETSC_ARCH cannot be empty string. Use a valid string or do not set one. Currently set with '+msg)
699d310bb7SBarry Smith    self.archBase = re.sub(r'^(\w+)[-_]?.*$', r'\1', self.arch)
709d310bb7SBarry Smith    return
719d310bb7SBarry Smith
72dd328ec0SBarry Smith  def makeDependency(self,hash):
73dd328ec0SBarry Smith    import os
74dd328ec0SBarry Smith    try:
75dd328ec0SBarry Smith      os.makedirs(os.path.join(self.arch,'lib','petsc','conf'))
76dd328ec0SBarry Smith    except:
77dd328ec0SBarry Smith      pass
78dd328ec0SBarry Smith    try:
79dd328ec0SBarry Smith      with open(os.path.join(self.arch,'lib','petsc','conf','configure-hash'), 'w') as f:
80dd328ec0SBarry Smith        f.write(hash)
81dd328ec0SBarry Smith    except:
82dd328ec0SBarry Smith      self.logPrint('Unable to make configure hash file: '+os.path.join(self.arch,'lib','petsc','conf','configure-hash'))
83dd328ec0SBarry Smith      return
84dd328ec0SBarry Smith    self.logPrint('Wrote configure hash file: '+os.path.join(self.arch,'lib','petsc','conf','configure-hash'))
85dd328ec0SBarry Smith
86dd328ec0SBarry Smith  def checkDependency(self):
87dd328ec0SBarry Smith    '''Checks if configure needs to be run'''
88dd328ec0SBarry Smith    '''Checks if files in config have changed, the command line options have changed or the PATH has changed'''
89dd328ec0SBarry Smith    import os
90dd328ec0SBarry Smith    import sys
91dd328ec0SBarry Smith    import hashlib
92*b60faad8SJed Brown    args = sorted(set(filter(lambda x: not (x.startswith('PETSC_ARCH') or x == '--force'),sys.argv[1:])))
93*b60faad8SJed Brown    hash = 'args:\n' + '\n'.join('    '+a for a in args) + '\n'
94*b60faad8SJed Brown    hash += 'PATH=' + os.environ.get('PATH', '') + '\n'
95dd328ec0SBarry Smith    try:
96dd328ec0SBarry Smith      for root, dirs, files in os.walk('config'):
97*b60faad8SJed Brown        if root == 'config':
98*b60faad8SJed Brown          dirs.remove('examples')
99dd328ec0SBarry Smith        for f in files:
100*b60faad8SJed Brown          if not f.endswith('.py') or f.startswith('.') or f.startswith('#'):
101*b60faad8SJed Brown            continue
102dd328ec0SBarry Smith          fname = os.path.join(root, f)
103*b60faad8SJed Brown          with open(fname,'rb') as f:
104*b60faad8SJed Brown            hash += hashlib.sha256(f.read()).hexdigest() + '  ' + fname + '\n'
105dd328ec0SBarry Smith    except:
106*b60faad8SJed Brown      self.logPrint('Error generating file list/hash from config directory for configure hash, forcing new configuration')
107dd328ec0SBarry Smith      return
108dd328ec0SBarry Smith    if self.argDB['force']:
109dd328ec0SBarry Smith      self.makeDependency(hash)
110dd328ec0SBarry Smith      return
111dd328ec0SBarry Smith    a = ''
112dd328ec0SBarry Smith    try:
113dd328ec0SBarry Smith      with open(os.path.join(self.arch,'lib','petsc','conf','configure-hash'), 'r') as f:
114dd328ec0SBarry Smith        a = f.read()
115dd328ec0SBarry Smith    except:
116dd328ec0SBarry Smith      # no previous record so write current hash
117dd328ec0SBarry Smith      self.makeDependency(hash)
118dd328ec0SBarry Smith      return
119dd328ec0SBarry Smith    if a == hash:
120dd328ec0SBarry Smith      self.logPrint('configure hash file: '+os.path.join(self.arch,'lib','petsc','conf','configure-hash')+' matches; no need to run configure.')
121dd328ec0SBarry Smith      print('Your configure options and state has not changed; no need to run configure')
122dd328ec0SBarry Smith      print('However you can force a configure run using the option: --force')
123dd328ec0SBarry Smith      sys.exit()
124dd328ec0SBarry Smith    self.makeDependency(hash)
125dd328ec0SBarry Smith    self.logPrint('configure hash file: '+os.path.join(self.arch,'lib','petsc','conf','configure-hash')+' does not match\n'+a+'\n---\n'+hash+'\n need to run configure')
126dd328ec0SBarry Smith
1279d310bb7SBarry Smith  def configure(self):
1289d310bb7SBarry Smith    self.executeTest(self.configureArchitecture)
1299d310bb7SBarry Smith    # required by top-level configure.py
1309d310bb7SBarry Smith    self.framework.arch = self.arch
131dd328ec0SBarry Smith    self.checkDependency()
1329d310bb7SBarry Smith    return
133