xref: /petsc/config/PETSc/options/sharedLibraries.py (revision 7b5fd022a6ba26727040df7457b27566b4c6742d)
19d310bb7SBarry Smithfrom __future__ import generators
29d310bb7SBarry Smithimport config.base
39d310bb7SBarry Smith
49d310bb7SBarry Smithclass Configure(config.base.Configure):
59d310bb7SBarry Smith  def __init__(self, framework):
69d310bb7SBarry Smith    config.base.Configure.__init__(self, framework)
79d310bb7SBarry Smith    self.headerPrefix = ''
89d310bb7SBarry Smith    self.substPrefix  = ''
99d310bb7SBarry Smith    self.useShared    = 0
109d310bb7SBarry Smith    return
119d310bb7SBarry Smith
129d310bb7SBarry Smith  def __str1__(self):
13e8e972b2SVaclav Hapla    txt =  '  Single library: %s\n' % ('yes' if self.framework.argDB['with-single-library'] else 'no')
14e8e972b2SVaclav Hapla    txt += '  Shared libraries: %s\n' % ('yes' if self.useShared else 'no')
159d310bb7SBarry Smith    return txt
169d310bb7SBarry Smith
179d310bb7SBarry Smith  def setupHelp(self, help):
189d310bb7SBarry Smith    import nargs
199d310bb7SBarry Smith    help.addArgument('PETSc', '-with-shared-libraries=<bool>', nargs.ArgBool(None, 1, 'Make PETSc libraries shared -- libpetsc.so (Unix/Linux) or libpetsc.dylib (Mac)'))
209d310bb7SBarry Smith    help.addArgument('PETSc', '-with-serialize-functions=<bool>', nargs.ArgBool(None, 0, 'Allows function pointers to be serialized to binary files with string representations'))
219d310bb7SBarry Smith    return
229d310bb7SBarry Smith
239d310bb7SBarry Smith  def setupDependencies(self, framework):
249d310bb7SBarry Smith    config.base.Configure.setupDependencies(self, framework)
259d310bb7SBarry Smith    self.arch         = framework.require('PETSc.options.arch', self)
269d310bb7SBarry Smith    self.debuggers    = framework.require('config.utilities.debuggers', self)
279d310bb7SBarry Smith    self.setCompilers = framework.require('config.setCompilers', self)
28184ce1a2SMatthew G. Knepley    self.headers      = framework.require('config.headers', self)
29184ce1a2SMatthew G. Knepley    self.functions    = framework.require('config.functions', self)
30184ce1a2SMatthew G. Knepley    self.ftm          = framework.require('config.utilities.featureTestMacros', self)
319d310bb7SBarry Smith    return
329d310bb7SBarry Smith
339d310bb7SBarry Smith  def checkSharedDynamicPicOptions(self):
347b65ca21SBarry Smith    '''if user specified out-dated 'with-shared' or 'with-dynamic' - flag an error'''
359d310bb7SBarry Smith    if 'with-shared' in self.framework.argDB:
369d310bb7SBarry Smith      raise RuntimeError('Option "--with-shared" no longer exists. Use "--with-shared-libraries".')
379d310bb7SBarry Smith    if 'with-dynamic' in self.framework.argDB or 'with-dynamic-loading' in self.framework.argDB:
389d310bb7SBarry Smith      raise RuntimeError('Option "--with-dynamic" and "--with-dynamic-loading" no longer exist.')
399d310bb7SBarry Smith    if self.framework.argDB['with-shared-libraries'] and not self.framework.argDB['with-pic'] and 'with-pic' in self.framework.clArgDB:
409d310bb7SBarry Smith      raise RuntimeError('If you use --with-shared-libraries you cannot disable --with-pic')
419d310bb7SBarry Smith
429d310bb7SBarry Smith    # default with-shared-libraries=1 => --with-pic=1
439d310bb7SBarry Smith    # Note: there is code in setCompilers.py that uses this as default.
449d310bb7SBarry Smith    if self.framework.argDB['with-shared-libraries'] and not self.framework.argDB['with-pic']: self.framework.argDB['with-pic'] = 1
459d310bb7SBarry Smith    return
469d310bb7SBarry Smith
479d310bb7SBarry Smith  def configureSharedLibraries(self):
489d310bb7SBarry Smith    '''Checks whether shared libraries should be used, for which you must
499d310bb7SBarry Smith      - Specify --with-shared-libraries
509d310bb7SBarry Smith      - Have found a working shared linker
519d310bb7SBarry Smith    Defines PETSC_USE_SHARED_LIBRARIES if they are used'''
529d310bb7SBarry Smith    import sys
539d310bb7SBarry Smith
549d310bb7SBarry Smith    self.useShared = self.framework.argDB['with-shared-libraries'] and not self.setCompilers.staticLibraries
559d310bb7SBarry Smith
569d310bb7SBarry Smith    if self.useShared:
577fca349cSMatthew G. Knepley      #if config.setCompilers.Configure.isSolaris(self.log) and config.setCompilers.Configure.isGNU(self.framework.getCompiler(),self.log):
589d310bb7SBarry Smith      #  self.addMakeRule('shared_arch','shared_'+self.arch.hostOsBase+'gnu')
599d310bb7SBarry Smith      #elif '-qmkshrobj' in self.setCompilers.sharedLibraryFlags:
609d310bb7SBarry Smith      #  self.addMakeRule('shared_arch','shared_linux_ibm')
619d310bb7SBarry Smith      #else:
629d310bb7SBarry Smith      #  self.addMakeRule('shared_arch','shared_'+self.arch.hostOsBase)
639d310bb7SBarry Smith
649d310bb7SBarry Smith      # Linux is the default
65af521b60SSatish Balay      if self.setCompilers.isDarwin(self.log):
669d310bb7SBarry Smith        self.addMakeRule('shared_arch','shared_darwin')
679d310bb7SBarry Smith        self.addMakeMacro('SONAME_FUNCTION', '$(1).$(2).dylib')
688143cb67SSatish Balay        self.addMakeMacro('SONAME_SFX_FUNCTION', '$(1)$(LIB_NAME_SUFFIX).$(2).dylib')
69accab91bSPierre Jolivet        self.addMakeMacro('SL_LINKER_FUNCTION', '-dynamiclib -install_name $(call SONAME_FUNCTION,$(1),$(2)) -compatibility_version $(2) -current_version $(3) -undefined dynamic_lookup')
701b8cab44SSatish Balay      elif self.setCompilers.CC.find('win32fe') >=0:
711b8cab44SSatish Balay        self.addMakeMacro('SONAME_FUNCTION', '$(1).dll')
728143cb67SSatish Balay        self.addMakeMacro('SONAME_SFX_FUNCTION', '$(1)$(LIB_NAME_SUFFIX).dll')
731b8cab44SSatish Balay        self.addMakeMacro('SL_LINKER_FUNCTION', '-LD')
74aa11c055SSatish Balay        self.addMakeMacro('PETSC_DLL_EXPORTS', '1')
759d310bb7SBarry Smith      else:
769d310bb7SBarry Smith        # TODO: check that -Wl,-soname,${LIBNAME}.${SL_LINKER_SUFFIX} can be passed (might fail on Intel)
779d310bb7SBarry Smith        # TODO: check whether we need to specify dependent libraries on the link line (long test)
789d310bb7SBarry Smith        self.addMakeRule('shared_arch','shared_linux')
791e6f8e71SStefano Zampini        self.addMakeMacro('SONAME_FUNCTION', '$(1).$(SL_LINKER_SUFFIX).$(2)')
808143cb67SSatish Balay        self.addMakeMacro('SONAME_SFX_FUNCTION', '$(1)$(LIB_NAME_SUFFIX).$(SL_LINKER_SUFFIX).$(2)')
811e6f8e71SStefano Zampini        self.addMakeMacro('SL_LINKER_FUNCTION', self.framework.getSharedLinkerFlags() + ' -Wl,-soname,$(call SONAME_FUNCTION,$(notdir $(1)),$(2))')
82aa11c055SSatish Balay        if config.setCompilers.Configure.isMINGW(self.framework.getCompiler(),self.log):
83aa11c055SSatish Balay          self.addMakeMacro('PETSC_DLL_EXPORTS', '1')
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.useShared:
899d310bb7SBarry Smith      self.addDefine('USE_SHARED_LIBRARIES', 1)
909d310bb7SBarry Smith    else:
919d310bb7SBarry Smith      self.logPrint('Shared libraries - disabled')
929d310bb7SBarry Smith    return
939d310bb7SBarry Smith
949d310bb7SBarry Smith  def configureDynamicLibraries(self):
959d310bb7SBarry Smith    '''Checks whether dynamic loading is available (with dlfcn.h and libdl)'''
969d310bb7SBarry Smith    if self.setCompilers.dynamicLibraries:
979d310bb7SBarry Smith      self.addDefine('HAVE_DYNAMIC_LIBRARIES', 1)
989d310bb7SBarry Smith    return
999d310bb7SBarry Smith
1009d310bb7SBarry Smith  def configureSerializedFunctions(self):
1019d310bb7SBarry Smith    '''
1029d310bb7SBarry Smith    Defines PETSC_SERIALIZE_FUNCTIONS if they are used
1039d310bb7SBarry Smith    Requires shared libraries'''
1049d310bb7SBarry Smith    import sys
1059d310bb7SBarry Smith
1069d310bb7SBarry Smith    if self.framework.argDB['with-serialize-functions'] and self.setCompilers.dynamicLibraries:
1079d310bb7SBarry Smith      self.addDefine('SERIALIZE_FUNCTIONS', 1)
1089d310bb7SBarry Smith
109184ce1a2SMatthew G. Knepley  def checkSymbolResolution(self):
110184ce1a2SMatthew G. Knepley    '''Checks that dladdr() works'''
111184ce1a2SMatthew G. Knepley    if self.headers.haveHeader('dlfcn.h') and self.functions.haveFunction('dlerror'):
112184ce1a2SMatthew G. Knepley      ftm = ''
113184ce1a2SMatthew G. Knepley      if self.ftm.defines.get('_GNU_SOURCE'): ftm = '#define _GNU_SOURCE\n'
11422164b4cSPierre Jolivet      if self.checkCompile('%s#include<stdlib.h>\n#include <dlfcn.h>\n' % ftm, 'Dl_info info;\nif (dladdr(*(void **)&exit, &info) == 0) return 1;\n'):
115184ce1a2SMatthew G. Knepley        self.addDefine('HAVE_DLADDR', 1)
116*1be94215SLisandro Dalcin        if hasattr(self.headers.setCompilers, 'CXX'):
117*1be94215SLisandro Dalcin          self.headers.pushLanguage('C++')
118258ec3d2SMatthew G. Knepley          self.headers.check('cxxabi.h')
119*1be94215SLisandro Dalcin          self.headers.popLanguage()
1209d310bb7SBarry Smith
1219d310bb7SBarry Smith  def configure(self):
1229d310bb7SBarry Smith    self.executeTest(self.checkSharedDynamicPicOptions)
1239d310bb7SBarry Smith    self.executeTest(self.configureSharedLibraries)
1249d310bb7SBarry Smith    self.executeTest(self.configureDynamicLibraries)
1259d310bb7SBarry Smith    self.executeTest(self.configureSerializedFunctions)
126184ce1a2SMatthew G. Knepley    self.executeTest(self.checkSymbolResolution)
1279d310bb7SBarry Smith    return
128