~ubuntu-branches/ubuntu/oneiric/rpy/oneiric

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
"""
This file builds rpy.

It has been adapted to allow multiple versions of the rpy shared
library to be included in the build.  To enable this, set the
environment variable "RHOMES" to a colon-delimited
(semicolon-delimited on MS-Windows) list of R installation directories.

For linux csh derivatives (including tcsh) use something like

  setenv RHOMES  /usr/local/R-2.1.0/lib/R:/usr/local/R-2.1.1/lib/R
  python setup.py bdist 

For linux sh derivatives (including bash) use something like:

  RHOMES=/usr/local/R-2.1.0/lib/R:/usr/local/R-2.1.1/lib/R
  export RHOMES
  python setup.py bdist 

For windows, edit setup.32 to include the correct paths, then use
something like:

  copy setup.Win32 setup.cfg
  python setup.py build --compiler=mingw32 bdist_wininst  

setup.py will automatically look in C:\Program Files\R to determine
which versions of R are installed, and will build an installer that
can be used for each of these R versions.

See the files INSTALL.UNIX and INSTALL.WINDOWS for more details.
"""

DEBUG=False

import os, os.path, sys, shutil, re
from distutils.core import setup, Extension
from distutils.sysconfig import *
from distutils.errors import *
import rpy_tools

if sys.platform=="win32":
    import rpy_wintools

# Get list of R Home directories that we will be processing
try:
    if sys.platform=="win32":
        RHOMES = os.environ['RHOMES'].split(';')
    else:
        RHOMES = os.environ['RHOMES'].split(':')
except:
    RHOMES = []

print "RHOMES=", RHOMES
print "DEBUG=", DEBUG

if not RHOMES:
    if sys.platform=="win32":
        RHOMES=rpy_wintools.get_RHOMES()
    else:
        RHOMES = [rpy_tools.get_R_HOME(force_exec=False)]
    print "Setting RHOMES to ", RHOMES

# ensure RHOMES is in ascii, since most command line tools are unhappy with unicode...
RHOMES = map( lambda x: x.encode('ascii'), RHOMES )

# On windows, check for/create the python link library 
#if sys.platform=="win32":
#    rpy_wintools.CreatePythonWinLib()

# On Mac OS X ("darwin") it is necessary to change the symlink
#   /Library/Frameworks/R.framework/Versions/Current
# to point to the current version of R in
#  /Library/Frameworks/R.framework/Versions/X.Y
# for that version of R to execute.  So, we need to store the original
# symlink here so we can restore it when we are done
if sys.platform=="darwin":
  darwin_original_version = os.readlink("/Library/Frameworks/R.framework/Versions/Current")

modules = []
for RHOME in RHOMES:
   
    RHOME = RHOME.strip()

    if DEBUG:
        # to avoid strict prototypes errors from R includes
        get_config_vars()['OPT'] = '-g -Wall'
    else:
        # to avoid strict prototypes errors from R includes
        get_config_vars()['OPT'] = '-DNDEBUG -O3 -Wall'

    # get the Python version
    if sys.version[:3] >= '2.2':
        DEFINE = []
        UNDEF = ['PRE_2_2']
    else:
        DEFINE = [('PRE_2_2', None)]
        UNDEF = []

    # configure the R paths
    #if sys.platform=="darwin":
    #  version = 
    #  if os.symlink("/Library/Frameworks/R.framework/Versions/Current")
    #  if RHOME.endswith('/'):
    #    RHOME = RHOME[:-1]
    #  print "RHOME=",os.path.join(os.path.dirname(RHOME),
    #                              os.readlink(RHOME) )
    #  sys.exit()
      
    
    RVERSION = rpy_tools.get_R_VERSION(RHOME, force_exec=True)
    RVER     = rpy_tools.get_R_VERSION_CODE(RVERSION)

    print "### Using R verion %s installed at %s ###" % (RVERSION, RHOME)

    r_libs = [ # Different verisons of R put .so/.dll in different places
              os.path.join(RHOME, 'bin'),  # R 2.0.0+
              os.path.join(RHOME, 'lib'),  # Pre 2.0.0
             ]

    print "RHOME=",RHOME

    base_source_files = ["src/rpymodule.c", "src/R_eval.c",
                         "src/io.c"]

    exists = os.path.exists
    mtime = os.path.getmtime

    # Make one copy of the source files for this R version
    source_files = []
    for f in base_source_files:
        # file.c => file2010.c
        nfile = f[0:-2] + RVER + '.c'
        print "copying %s -> %s" % (f, nfile)
        if (not exists(nfile)) or  ( mtime(f) > mtime(nfile) ):
            shutil.copy(f, nfile)
        source_files.append(nfile)

    if sys.platform=='win32':
        include_dirs = [ os.path.join(RHOME.strip(), 'include'),
                         'src' ]
        libraries= ['R']
        library_dirs = r_libs
        runtime_libs = []
        extra_compile_args= [] 
        source_files = source_files + ["src\\setenv.c"]
    elif sys.platform=='darwin':
        include_dirs = [ os.path.join(RHOME.strip(), 'include'), 
                         'src' ]
        libraries=['R']
        library_dirs= r_libs
        runtime_libs = r_libs
        extra_compile_args=[]
        
    else: # unix-like systems, this is known to work for Linux and Solaris
        include_dirs = [ os.path.join(RHOME.strip(), 'include'), 
                         'src', '/usr/share/R/include' ]

        library_dirs = r_libs
        runtime_libs = r_libs
        extra_compile_args=["-shared"]
        source_files = source_files + ["src/setenv.c"]
        
        libraries=['R']
          
        # Ask R to tell us how to properly link against lapack
        extra_compile_args += [ rpy_tools.get_R_LAPACK_LIB_FLAGS( RHOME ) ]


    # Discover which array packages are present
    try: 
        import numpy
        DEFINE.append(('WITH_NUMERIC', '3'))
        DEFINE.append(('PY_ARRAY_TYPES_PREFIX', 'PyArray_'))
        include_dirs.append(numpy.get_include())
    except ImportError:
        # fall back to Numeric
        try:
            import Numeric
            DEFINE.append(('WITH_NUMERIC', '1'))
        except ImportError:
            UNDEF.append('WITH_NUMERIC')

    # get the RPy version
    from rpy_version import rpy_version

    LONG_DESC = """RPy provides a robust Python interface to the R
    programming language.  It can manage all kinds of R objects and can
    execute arbitrary R functions. All the errors from the R language are
    converted to Python exceptions."""

    # use R version specific shared library name
    shlib_name = "_rpy%s" % RVER
    DEFINE.append( ('RPY_SHNAME', shlib_name))
    DEFINE.append( ('INIT_RPY', 'init_rpy%s' % RVER ) )


    # add debugging 

    modules.append( Extension(
        shlib_name,
        source_files,
        include_dirs=include_dirs,
        libraries=libraries,
        library_dirs=library_dirs,
        define_macros=DEFINE,
        undef_macros=UNDEF,
        extra_compile_args=extra_compile_args,
        runtime_library_dirs = runtime_libs,
        ) )


setup(name="rpy",
      version=rpy_version,
      description="Python interface to the R language",
      maintainer="Gregory R. Warnes",
      maintainer_email="warnes@bst.rochester.edu",
      url="http://rpy.sourceforge.net",
      license="GPL",
      long_description=LONG_DESC,
      py_modules=['rpy', 'rpy_io', 'rpy_version', 'rpy_tools', 'rpy_options',
                  'rpy_wintools'],
      ext_modules=modules
      )