setup.py.in 6.04 KB
#############################################################################
# Disutils setup file for ctools Python extension module                    #
# ------------------------------------------------------------------------- #
# Copyright (C) 2011 Jurgen Knodlseder                                      #
# ------------------------------------------------------------------------- #
#                                                                           #
#  This program is free software: you can redistribute it and/or modify     #
#  it under the terms of the GNU General Public License as published by     #
#  the Free Software Foundation, either version 3 of the License, or        #
#  (at your option) any later version.                                      #
#                                                                           #
#  This program is distributed in the hope that it will be useful,          #
#  but WITHOUT ANY WARRANTY; without even the implied warranty of           #
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            #
#  GNU General Public License for more details.                             #
#                                                                           #
#  You should have received a copy of the GNU General Public License        #
#  along with this program.  If not, see <http://www.gnu.org/licenses/>.    #
#                                                                           #
#############################################################################

from distutils.core import setup, Extension
from distutils import sysconfig
import glob
import os
import sys

# Detect execution mode
is_unit_test = False
if (len(sys.argv) >= 3):
	if (sys.argv[2] == '--inplace'):
		print "This build is for unit testing."
		is_unit_test = True

# Darwin MacPorts kluge: Remove -L/opt/local/lib path from LDSHARED as this
# overrides the GammaLib settings. Otherwise, MacPorts libraries would be
# used instead of the libraries selected by GammaLib
if sys.platform.lower().startswith("darwin"):
	
	# Get configuration variables
	cvars = sysconfig.get_config_vars()
	
	# Extract LDSHARED
	ldshared_old = cvars.get('LDSHARED')
	ldshared_new = ''
	ldshared_rm  = ''
	for option in ldshared_old.split(' '):
		if option.lstrip().find('-L/opt/local/lib') != 0:
			ldshared_new += option + ' '
		else:
			ldshared_rm += option + ' '
	if (len(ldshared_rm) > 0):
		print 'Removed following option(s) from LDSHARED: '+ldshared_rm
	
	# Set LDSHARED
	cvars['LDSHARED'] = ldshared_new

# Set ctools 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.
libcta='ctools'
#if is_unit_test:
if sys.platform.lower().startswith("darwin") and is_unit_test:
	
	# Remove any existing symbolic link to libctools.a. This will
	# prevent any exception in creating the symbolic link due to
	# an already existing link.
	try:
		os.remove('../src/.libs/libctools_python.a')
		print 'Removed ../src/.libs/libctools_python.a'
	except:
		pass
	
	# Now create a symbolic link and set the library name to this
	# symbolic link
	try:
		os.symlink('libctools.a', '../src/.libs/libctools_python.a')
		print 'Created ../src/.libs/libctools_python.a'
		libcta='ctools_python'
	except:
		print 'WARNING: Failed to create symbolic link to static library. Link dynamically.'

# Set default include directories and data files
include_dirs         = ['@prefix@/include/gammalib']
install_files        = glob.glob('@top_srcdir@/pyext/*.i')
library_dirs         = ['../src/.libs']
runtime_library_dirs = ['../src/.libs']
libraries            = [libcta,'gamma']

# Add install directory to runtime library path
if os.path.exists('@prefix@/lib'):
	library_dirs.append('@prefix@/lib')
	runtime_library_dirs.append('@prefix@/lib')

# Add application include files. The method we use here is not very
# intelligent as it simple will add all names that start with 'ct'
# in the src directory. Once we added the ctools script we had to
# add an explicit filter to avoid appending those.
dirs = glob.glob('../src/ct*')
for dir in dirs:
	if len(dir) > 0 and 'ctools' not in dir:
		path = os.path.abspath(dir)
		include_dirs.append(path)

# 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:
	if extra_lib_path.lstrip().find('-L') == 0:
		dir = extra_lib_path.lstrip().lstrip('-L')
		if len(dir) > 0:
			library_dirs.append(dir)
			runtime_library_dirs.append(dir)

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

# Set data files to install. Do not install data files in unit testing mode.
if is_unit_test:
	data_files=[]
else:
	data_files=[('@prefix@/share/ctools/swig', install_files)]

if '@OPENMP_TRUE@' != '#':
		extra_compile_args=['-fopenmp']
		extra_link_args=['-fopenmp']
		
# Setup
setup(name='@PACKAGE_NAME@',
      version='@PACKAGE_VERSION@',
      description='ctools Python bindings',
      author='Juergen Knoedlseder',
      author_email='@PACKAGE_BUGREPORT@',
      url='http://gammalib.sourceforge.net/',
      py_modules=['ctools'],
      ext_modules=[Extension('_ctools', ['ctools_wrap.cpp'],
                             include_dirs=include_dirs,
                             library_dirs=library_dirs,
                             libraries=libraries,
                             runtime_library_dirs=runtime_library_dirs,
			     extra_compile_args=extra_compile_args,
			     extra_link_args=extra_link_args)],
	  data_files=data_files,
	  license="GNU GPL v3",
	  long_description="CTA science analysis tools",
	  platforms=["Linux", "Solaris", "Mac OS-X", "Unix", "BSD"],
      )