setup.py.in 4.4 KB
from distutils.core import setup, Extension
from distutils import sysconfig
import glob
import os
import sys

# Removes list of bad prefixes from list
def remove_prefixes(optlist, bad_prefixes):
	for bad_prefix in bad_prefixes:
		for i, flag in enumerate(optlist):
			if flag.startswith(bad_prefix):
				optlist.pop(i)
				break
	return optlist

# Adjust compile options for darwin
if sys.platform.lower().startswith("darwin"):

	# Remove some time consuming options
	cvars = sysconfig.get_config_vars()
	cflags = cvars.get('OPT')
	if cflags:
		cflags = remove_prefixes(cflags.split(), 
		         ['-g', '-O', '-Wstrict-prototypes', '-DNDEBUG'])
	cvars['OPT'] = str.join(' ', cflags)
	cvars['CFLAGS'] = cvars['BASECFLAGS'] + ' ' + cvars['OPT']
	
	# Remove -arch flags. This prevents building a fat library
	cvars['ARCHFLAGS'] = ''
	
	# Signal darwin compiler adjustments
	print 'Adjust default distutils compiler options for darwin.'

# Set GammaLib library name. If we're about to make the test build then
# create a symbolic link to the static library to enforce static linking.
# This is required on some system (such as Mac OS X) to avoid problems
# with finding the shared library during unit testing.
libgamma='gamma'
if (len(sys.argv) > 3):
	if (sys.argv[3] == 'build'):
		
		# Remove any existing symbolic link to libgamma.a. This will
		# prevent any exception in creating the symbolic link due to
		# an already existing link.
		try:
			os.remove('../src/.libs/libgamma_python.a')
			print 'Removed ../src/.libs/libgamma_python.a'
		except:
			pass
		
		# Now create a symbolic link and set the library name to this
		# symbolic link
		try:
			os.symlink('libgamma.a', '../src/.libs/libgamma_python.a')
			print 'Created ../src/.libs/libgamma_python.a'
			libgamma='gamma_python'
		except:
			print 'WARNING: Failed to create symbolic link to static library. Link dynamically.'

# Set default include directories and data files
include_dirs         = ['@top_srcdir@/include']
data_files           = glob.glob('@top_srcdir@/pyext/*.i')
library_dirs         = ['../src/.libs']
runtime_library_dirs = ['@libdir@']
libraries            = [libgamma]

# Add cfitsio library only if available
if '@WITH_CFITSIO_TRUE@' != '#':
	print "Link cfitsio into Python bindings."
	libraries.append('cfitsio')

# Add readline library only if available
if '@WITH_READLINE_TRUE@' != '#':
	print "Link readline into Python bindings."
	libraries.append('readline')
	libraries.append('ncurses')

# Add extra libraries
extra_libs = ('@PYTHON_EXTRA_LIBS@').split(',')
for extra_lib in extra_libs:
	if len(extra_lib) > 0:
		libraries.append(extra_lib)

# Get extra library paths
extra_lib_paths = ('@LDFLAGS@').split(' ')
for extra_lib_path in extra_lib_paths:
	dir = extra_lib_path.lstrip('-L')
	if len(dir) > 0:
		library_dirs.append(extra_lib_path.lstrip('-L'))
		runtime_library_dirs(extra_lib_path.lstrip('-L'))

# Get extra include directories
extra_inc_paths = ('@CPPFLAGS@').split(' ')
for extra_inc_path in extra_inc_paths:
	dir = extra_inc_path.lstrip('-I')
	if len(dir) > 0:
		include_dirs.append(extra_inc_path.lstrip('-I'))

# Add instrument specific include directories and data files
if '@WITH_INST_MWL_TRUE@' != '#':
	print "Include multi-wavelength interface in gammalib Python bindings."
	include_dirs.append('@top_srcdir@/inst/mwl/include')
	data_files.extend(glob.glob('@top_srcdir@/inst/mwl/pyext/*.i'))
if '@WITH_INST_CTA_TRUE@' != '#':
	print "Include CTA instrument interface in gammalib Python bindings."
	include_dirs.append('@top_srcdir@/inst/cta/include')
	data_files.extend(glob.glob('@top_srcdir@/inst/cta/pyext/*.i'))
if '@WITH_INST_LAT_TRUE@' != '#':
	print "Include Fermi/LAT instrument interface in gammalib Python bindings."
	include_dirs.append('@top_srcdir@/inst/lat/include')
	data_files.extend(glob.glob('@top_srcdir@/inst/lat/pyext/*.i'))

# Setup
setup(name='@PACKAGE_NAME@',
      version='@PACKAGE_VERSION@',
      description='gammalib Python bindings',
      author='Jurgen Knodlseder',
      author_email='@PACKAGE_BUGREPORT@',
      url='http://gammalib.sourceforge.net/',
      py_modules=['gammalib'],
      ext_modules=[Extension('_gammalib', ['gammalib_wrap.cpp'],
                             include_dirs=include_dirs,
                             library_dirs=library_dirs,
                             libraries=libraries,
                             runtime_library_dirs=runtime_library_dirs)],
      data_files=[('@prefix@/share/gammalib/swig', data_files)],
      )