xref: /petsc/config/PETSc/options/sharedLibraries.py (revision 7fca349c50edcd7f1fb972b385e42d282fcb03b6)
19d310bb7SBarry Smith#!/usr/bin/env python
29d310bb7SBarry Smithfrom __future__ import generators
39d310bb7SBarry Smithimport user
49d310bb7SBarry Smithimport config.base
59d310bb7SBarry Smith
69d310bb7SBarry Smithclass Configure(config.base.Configure):
79d310bb7SBarry Smith  def __init__(self, framework):
89d310bb7SBarry Smith    config.base.Configure.__init__(self, framework)
99d310bb7SBarry Smith    self.headerPrefix = ''
109d310bb7SBarry Smith    self.substPrefix  = ''
119d310bb7SBarry Smith    self.useShared    = 0
129d310bb7SBarry Smith    return
139d310bb7SBarry Smith
149d310bb7SBarry Smith  def __str1__(self):
159d310bb7SBarry Smith    if not hasattr(self, 'useShared'):
169d310bb7SBarry Smith      return ''
179d310bb7SBarry Smith    txt = ''
189d310bb7SBarry Smith    if self.useShared:
199d310bb7SBarry Smith      txt += '  shared libraries: enabled\n'
209d310bb7SBarry Smith    else:
219d310bb7SBarry Smith      txt += '  shared libraries: disabled\n'
229d310bb7SBarry Smith    return txt
239d310bb7SBarry Smith
249d310bb7SBarry Smith  def setupHelp(self, help):
259d310bb7SBarry Smith    import nargs
269d310bb7SBarry Smith    help.addArgument('PETSc', '-with-shared-libraries=<bool>', nargs.ArgBool(None, 1, 'Make PETSc libraries shared -- libpetsc.so (Unix/Linux) or libpetsc.dylib (Mac)'))
279d310bb7SBarry Smith    help.addArgument('PETSc', '-with-serialize-functions=<bool>', nargs.ArgBool(None, 0, 'Allows function pointers to be serialized to binary files with string representations'))
289d310bb7SBarry Smith    return
299d310bb7SBarry Smith
309d310bb7SBarry Smith  def setupDependencies(self, framework):
319d310bb7SBarry Smith    config.base.Configure.setupDependencies(self, framework)
329d310bb7SBarry Smith    self.arch         = framework.require('PETSc.options.arch', self)
339d310bb7SBarry Smith    self.debuggers    = framework.require('config.utilities.debuggers', self)
349d310bb7SBarry Smith    self.setCompilers = framework.require('config.setCompilers', self)
359d310bb7SBarry Smith    return
369d310bb7SBarry Smith
379d310bb7SBarry Smith  def checkSharedDynamicPicOptions(self):
389d310bb7SBarry Smith    # uf user specified 'with-shared' or 'with-dynamic' - flag an error
399d310bb7SBarry Smith    if 'with-shared' in self.framework.argDB:
409d310bb7SBarry Smith      raise RuntimeError('Option "--with-shared" no longer exists. Use "--with-shared-libraries".')
419d310bb7SBarry Smith    if 'with-dynamic' in self.framework.argDB or 'with-dynamic-loading' in self.framework.argDB:
429d310bb7SBarry Smith      raise RuntimeError('Option "--with-dynamic" and "--with-dynamic-loading" no longer exist.')
439d310bb7SBarry Smith    # if user specifies inconsistant 'with-dynamic-loading with-shared-libraries with-pic' options - flag error
449d310bb7SBarry Smith    if self.framework.argDB['with-shared-libraries'] and not self.framework.argDB['with-pic'] and 'with-pic' in self.framework.clArgDB:
459d310bb7SBarry Smith      raise RuntimeError('If you use --with-shared-libraries you cannot disable --with-pic')
469d310bb7SBarry Smith
479d310bb7SBarry Smith    # default with-shared-libraries=1 => --with-pic=1
489d310bb7SBarry Smith    # Note: there is code in setCompilers.py that uses this as default.
499d310bb7SBarry Smith    if self.framework.argDB['with-shared-libraries'] and not self.framework.argDB['with-pic']: self.framework.argDB['with-pic'] = 1
509d310bb7SBarry Smith    return
519d310bb7SBarry Smith
529d310bb7SBarry Smith
539d310bb7SBarry Smith  def configureSharedLibraries(self):
549d310bb7SBarry Smith    '''Checks whether shared libraries should be used, for which you must
559d310bb7SBarry Smith      - Specify --with-shared-libraries
569d310bb7SBarry Smith      - Have found a working shared linker
579d310bb7SBarry Smith    Defines PETSC_USE_SHARED_LIBRARIES if they are used'''
589d310bb7SBarry Smith    import sys
599d310bb7SBarry Smith
609d310bb7SBarry Smith    self.useShared = self.framework.argDB['with-shared-libraries'] and not self.setCompilers.staticLibraries
619d310bb7SBarry Smith
629d310bb7SBarry Smith    if self.useShared:
63*7fca349cSMatthew G. Knepley      #if config.setCompilers.Configure.isSolaris(self.log) and config.setCompilers.Configure.isGNU(self.framework.getCompiler(),self.log):
649d310bb7SBarry Smith      #  self.addMakeRule('shared_arch','shared_'+self.arch.hostOsBase+'gnu')
659d310bb7SBarry Smith      #elif '-qmkshrobj' in self.setCompilers.sharedLibraryFlags:
669d310bb7SBarry Smith      #  self.addMakeRule('shared_arch','shared_linux_ibm')
679d310bb7SBarry Smith      #else:
689d310bb7SBarry Smith      #  self.addMakeRule('shared_arch','shared_'+self.arch.hostOsBase)
699d310bb7SBarry Smith
709d310bb7SBarry Smith      # Linux is the default
719d310bb7SBarry Smith      if hasattr(self.debuggers, 'dsymutil'):
729d310bb7SBarry Smith        # Check for Mac OSX by the presence of dsymutil
739d310bb7SBarry Smith        #   could also check flags: -dynamiclib -single_module -multiply_defined suppress -undefined dynamic_lookup
749d310bb7SBarry Smith        self.addMakeRule('shared_arch','shared_darwin')
759d310bb7SBarry Smith        self.addMakeMacro('SONAME_FUNCTION', '$(1).$(2).dylib')
769d310bb7SBarry Smith        self.addMakeMacro('SL_LINKER_FUNCTION', '-dynamiclib -install_name $(call SONAME_FUNCTION,$(1),$(2)) -compatibility_version $(2) -current_version $(3) -single_module -multiply_defined suppress -undefined dynamic_lookup')
779d310bb7SBarry Smith      else:
789d310bb7SBarry Smith        # TODO: check that -Wl,-soname,${LIBNAME}.${SL_LINKER_SUFFIX} can be passed (might fail on Intel)
799d310bb7SBarry Smith        # TODO: check whether to use -qmkshrobj or -shared (maybe we can just use self.setCompilers.sharedLibraryFlags)
809d310bb7SBarry Smith        # TODO: check whether we need to specify dependent libraries on the link line (long test)
819d310bb7SBarry Smith        self.addMakeRule('shared_arch','shared_linux')
829d310bb7SBarry Smith        self.addMakeMacro('SONAME_FUNCTION', '$(1).so.$(2)')
839d310bb7SBarry Smith        self.addMakeMacro('SL_LINKER_FUNCTION', '-shared -Wl,-soname,$(call SONAME_FUNCTION,$(notdir $(1)),$(2))')
849d310bb7SBarry Smith      self.addMakeMacro('BUILDSHAREDLIB','yes')
859d310bb7SBarry Smith    else:
869d310bb7SBarry Smith      self.addMakeRule('shared_arch','')
879d310bb7SBarry Smith      self.addMakeMacro('BUILDSHAREDLIB','no')
889d310bb7SBarry Smith    if self.setCompilers.sharedLibraries:
899d310bb7SBarry Smith      self.addDefine('HAVE_SHARED_LIBRARIES', 1)
909d310bb7SBarry Smith    if self.useShared:
919d310bb7SBarry Smith      self.addDefine('USE_SHARED_LIBRARIES', 1)
929d310bb7SBarry Smith    else:
939d310bb7SBarry Smith      self.logPrint('Shared libraries - disabled')
949d310bb7SBarry Smith    return
959d310bb7SBarry Smith
969d310bb7SBarry Smith  def configureDynamicLibraries(self):
979d310bb7SBarry Smith    '''Checks whether dynamic loading is available (with dlfcn.h and libdl)'''
989d310bb7SBarry Smith    if self.setCompilers.dynamicLibraries:
999d310bb7SBarry Smith      self.addDefine('HAVE_DYNAMIC_LIBRARIES', 1)
1009d310bb7SBarry Smith    return
1019d310bb7SBarry Smith
1029d310bb7SBarry Smith  def configureSerializedFunctions(self):
1039d310bb7SBarry Smith    '''
1049d310bb7SBarry Smith    Defines PETSC_SERIALIZE_FUNCTIONS if they are used
1059d310bb7SBarry Smith    Requires shared libraries'''
1069d310bb7SBarry Smith    import sys
1079d310bb7SBarry Smith
1089d310bb7SBarry Smith    if self.framework.argDB['with-serialize-functions'] and self.setCompilers.dynamicLibraries:
1099d310bb7SBarry Smith      self.addDefine('SERIALIZE_FUNCTIONS', 1)
1109d310bb7SBarry Smith
1119d310bb7SBarry Smith
1129d310bb7SBarry Smith  def configure(self):
1139d310bb7SBarry Smith    # on windows use with-shared-libraries=0 as default
114*7fca349cSMatthew G. Knepley    if self.setCompilers.isCygwin(self.log) and 'with-shared-libraries' not in self.framework.clArgDB: self.framework.argDB['with-shared-libraries'] = 0
1159d310bb7SBarry Smith    self.executeTest(self.checkSharedDynamicPicOptions)
1169d310bb7SBarry Smith    self.executeTest(self.configureSharedLibraries)
1179d310bb7SBarry Smith    self.executeTest(self.configureDynamicLibraries)
1189d310bb7SBarry Smith    self.executeTest(self.configureSerializedFunctions)
1199d310bb7SBarry Smith    return
120