File:  [CENS] / python / pyGiNaC / setup4.py
Revision 1.7: download - view: text, annotated - select for diffs - revision graph
Tue Nov 20 17:11:13 2001 UTC (16 years ago) by pearu
Branches: MAIN
CVS tags: HEAD
Catching up latest GiNaC and BPL

#!/usr/bin/env python
#
#   Setup file for building/installing PyGiNaC
#
# Requirements:
#   See README.txt
#  
# Usage:
#   First read README.txt
#   > python setup4.py install        # build _ginac
#                                     # and install the package.
#   > cd test4 && python test_all.py  # run doctests.
#
# In Python `import ginac'. Module `ginac' extension classes expose
# almost all GiNaC classes to Python.
# See GiNaC documentation (http://www.ginac.de/tutorial/) for usage.
#
# Pearu Peterson <pearu@cens.ioc.ee>
# 30 March 2001
# 23 April 2001
# 11 August 2001
# 24 August 2001
# 20 November 2001

__credits__ = """\
GiNaC group (www.ginac.de), for the GiNaC library.
Boost.Python group (www.boost.org), for BPL that considerably
simplified exposure of GiNaC C++ classes to Python.
"""

import sys,os,string,time,commands,re

__today__ = time.asctime(time.localtime())
__author__ = 'Pearu Peterson <pearu@cens.ioc.ee>'

#++++++++++++++ Require Python 2.x +++++++++++++

if eval(sys.version[0])<2:
    print "You'll need Python 2.0 or higher to build PyGiNaC"
    sys.exit()
print 'Python Version',sys.version.split()[0]

#++++++++++++++ Rebuild ++++++++++++++++++++++++
rebuild = 0
disable_opt = 1 # disable optimization flags, useful when debugging
                # set 0 for final compilation

if 'rebuild' in sys.argv:
    sys.argv[sys.argv.index('rebuild')] = 'build'
    rebuild = 1
if 'reinstall' in sys.argv:
    sys.argv[sys.argv.index('reinstall')] = 'install'
    rebuild = 1

#+++++++++++++++++ arguments for Extension ++++++++++++++++
libraries = []
library_dirs = []
extra_objects = []
extra_link_args = []
extra_compile_args = []
include_dirs = [] #['tools']  # Uncomment if sstream cannot be found.
sources = []

#++++++++++++++++ GiNaC,CLN (,GMP) +++++++++++++++++++++++++++
gmp_so = '/usr/lib/libgmp.so' # fix this path if CLN uses gmp.
s,ginac_version=commands.getstatusoutput ('ginac-config --version')
if not s:
    print 'GiNaC Version',ginac_version
    ginac_prefix=commands.getoutput ('ginac-config --prefix')
    ginac_so = os.path.join (ginac_prefix,'lib','libginac.so')
    if os.path.isfile(ginac_so):
        extra_objects.append (ginac_so)
        s,cln_version=commands.getstatusoutput ('cln-config --version')
        if not s:
            print 'CLN Version',cln_version
            cln_prefix = commands.getoutput ('cln-config --prefix')
            cln_so = os.path.join (cln_prefix,'lib','libcln.so')
            cln_libs = commands.getoutput ('cln-config --libs')
            if os.path.isfile (cln_so):
                extra_objects.append(cln_so)
                cln_libs = cln_libs.replace ('-lcln ',' ')
            if re.match (r'.*-lgmp\b',cln_libs) and os.path.isfile (gmp_so):
                extra_objects.append(gmp_so)
                cln_libs = cln_libs.replace ('-lgmp',' ')
            if re.match (r'.*-l',cln_libs):
                extra_link_args.append(cln_libs)
            cln_flags = commands.getoutput ('cln-config --cppflags')
            if cln_flags:
                extra_compile_args.append (cln_flags)
        else:
            libraries += ['cln']
            print 'Failed to establish CLN using cln-config. Using defaults.'
    else:
        extra_link_args.append (commands.getoutput ('ginac-config --libs'))
    extra_compile_args.append (commands.getoutput ('ginac-config --cppflags'))
else:
    print 'Failed to establish GiNaC using ginac-config. Using defaults.'
    libraries += ['ginac','cln']

#++++++++++++++++++++ boost.python +++++++++++++++++++++

# Fix this path for Boost headers
boost_dir = 'boost'
if not os.path.isdir(boost_dir):
    print 'boost_dir=%s must be existing directory.'%(`boost_dir`)
    print '  Get Boost from http://www.boost.org/'
    print '  Fix its path in setup4.py'
    sys.exit()
else:
    include_dirs.append (boost_dir)

try:
    import _libboostpython
    libboost_so = _libboostpython.__file__
except ImportError:
    s = os.system ('cd libboostpython && ./setup.py install')
    if not s:
        try:
            import _libboostpython
            libboost_so = _libboostpython.__file__
            s = 0
        except ImportError: pass
    if s:
        # Fix this path if you have your own Boost.Python shared library
        libboost_so = 'XXX/usr/lib/libboost_python.so'

if not os.path.exists (libboost_so):
    print 'Using boost sources.'
    libboost = ''
    bpl_dir = os.path.join(boost_dir,'libs','python','src')
    bpl_src = map(lambda f:os.path.join(bpl_dir,f),
              ['classes.cpp',
               'conversions.cpp',
               'extension_class.cpp',
               #'cross_module.cpp',
               'functions.cpp',
               'init_function.cpp',
               'module_builder.cpp',
               'objects.cpp','types.cpp'])
    sources += bpl_src
else:
    extra_objects.append (libboost_so)

#++++++++++++++++++++++++++++++++++++++++++++++++
from distutils.core import setup, Extension

#+++HACK: replace linker gcc with g++ +++++++++++
#+++      turn off optimization if disable_opt +++++++++++
from distutils import sysconfig
save_init_posix = sysconfig._init_posix
def my_init_posix():
    save_init_posix()
    g = sysconfig._config_vars
    if g['LDSHARED'][:3]=='gcc':
        print 'my_init_posix: changing LDSHARED =',`g['LDSHARED']`,
        g['LDSHARED'] = 'g++'+g['LDSHARED'][3:]
        print 'to',`g['LDSHARED']`
    if disable_opt and g['OPT'][:6]=='-g -O2':
        print 'my_init_posix: changing OPT =',`g['OPT']`,
        g['OPT'] = '-g'+g['OPT'][6:]
        print 'to',`g['OPT']`
sysconfig._init_posix = my_init_posix

#+++++++++++++++++ PyGiNaC version ++++++++++++++

edition = 4
wrapper_dir = 'wrappers%d'%edition
lib_dir = 'lib%d'%edition
src_dir = 'src%d'%edition

major_version = 0
minor_version = 7
try: execfile(os.path.join('tools','get_revision.py'))
except: revision_version = 0
version='%d.%d.%d'%(major_version,minor_version,revision_version)
version_info = (major_version,minor_version,revision_version,'proto',edition)

print 'Creating %s/__version__.py'%lib_dir
f = open(os.path.join(lib_dir,'__version__.py'),'w')
f.write('__version__ = "%s"\n'%(version))
f.close()

include_dirs.append (wrapper_dir)

#+++++++++++++ print summary ++++++++++++++++++
print 'extra_link_args=',extra_link_args
print 'extra_compile_args=',extra_compile_args
print 'extra_objects=',extra_objects
print 'include_dirs=',include_dirs
print 'libraries=',libraries
print 'sources=',sources
if len(sys.argv)==1:
    print "PyGiNaC Version",version
    sys.exit()
#++++++++++++++++ ginac._ginac +++++++++++++++++++++

ginac_src = []
for n in ['_ginac.cpp',
          'ginac_enhancements.cpp'
          ]:
    ginac_src.append(os.path.join(wrapper_dir,n))

sources += ginac_src

if rebuild:
    for n in ['_ginac.cpp']:
        n = os.path.join(wrapper_dir,n)
        print 'touch '+n
        os.system('touch '+n)

_ginac_ext = Extension('ginac._ginac',
                       sources = sources,
                       include_dirs = include_dirs,
                       libraries = libraries,
                       library_dirs = library_dirs,
                       extra_objects = extra_objects,
                       extra_compile_args = extra_compile_args,
                       extra_link_args = extra_link_args,
                      )

#++++++++++++++ extension modules +++++++++++++++

ext_modules = [_ginac_ext]

#++++++++++++++++++++ setup +++++++++++++++++++++

setup (name = "PyGiNaC",
       version = version,
       description = "PyGiNaC --- the wrapper of C++ library GiNaC to Python",
       author = "Pearu Peterson",
       author_email = "pearu@cens.ioc.ee",
       maintainer = "Pearu Peterson",
       maintainer_email = "pearu@cens.ioc.ee",
       licence = "LGPL",
       long_description= """
GiNaC is an open framework for symbolic computation within the C++
programming language (http://www.ginac.de/).
PyGiNaC is a Python extension package that implements a complete
and transparent interface to the GiNaC library.
""",
       url = "http://cens.ioc.ee/projects/pyginac/",
       ext_modules = ext_modules,
       packages = ['ginac'],
       package_dir = {'ginac': lib_dir},
       )

print "PyGiNaC Version",version

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>