pyobjc / pyobjc / setup.py

  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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
#!/usr/bin/env python

# If true we use libffi instead of register.m
USE_FFI = 1

# It true we use libffi for normal invocations, instead of NSInvocation
USE_FFI_SHORTCUTS = 1

# If true we adjust the reference count for copy/alloc, otherwise users
# have to do that manually. (Experimental)
USE_ADJUST_REFCOUNTS = 1

# Set this to the path to an extracted tree of libffi to automaticly build
# a compatible version of libffi
LIBFFI_SOURCES=None
LIBFFI_SOURCES='libffi-src'

import sys
import os

if sys.platform == 'darwin': 
    # Apple has used build options that don't work with a 'normal' system.
    # Remove '-arch i386' from the LDFLAGS.
    import distutils.sysconfig
    distutils.sysconfig.get_config_vars()
    x = distutils.sysconfig._config_vars['LDSHARED']
    y = x.replace('-arch i386', '')
    if y != x:
        print "Fixing Apple strangeness in Python configuration"
        distutils.sysconfig._config_vars['LDSHARED'] = y

from distutils.core import setup, Extension
import os

# We need at least Python 2.2
req_ver = [ 2, 2]

if sys.version_info[0] < req_ver[0] or (
        sys.version_info[0] == req_ver[0] 
        and sys.version_info[1] < req_ver[1]):

    sys.stderr.write('PyObjC: Need at least Python %s\n'%('.'.join(req_ver)))
    sys.exit(1)

if USE_FFI:
    if LIBFFI_SOURCES is not None:
        if not os.path.isdir(LIBFFI_SOURCES):
            sys.stderr.write(
                'LIBFFI_SOURCES is not a directory: %s\n'%LIBFFI_SOURCES)
            sys.stderr.write('\tSee Install.txt or Install.html for more information.\n')
            sys.exit(1)
        
        if not os.path.exists('build'):
            os.mkdir('build')

        if not os.path.exists('build/libffi'):
            os.mkdir('build/libffi')

        if not os.path.exists('build/libffi/BLD'):
            os.mkdir('build/libffi/BLD')

        if not os.path.exists('build/libffi/lib/libffi.a'):
            # No pre-build version available, build it now.
            print "Building local copy of libffi"

            # Do not use a relative path for the build-tree, libtool on
            # MacOS X doesn't like that.
            inst_dir = os.path.join(os.getcwd(), 'build/libffi')
            src_path = os.path.abspath(LIBFFI_SOURCES)

            inst_dir = inst_dir.replace("'", "'\"'\"'")
            src_path = src_path.replace("'", "'\"'\"'")

            fd = os.popen(
                "cd build/libffi/BLD && '%s/configure' --prefix='%s' --disable-shared --enable-static && make install"%(src_path, inst_dir), 'r')
            for ln in fd.xreadlines():
                sys.stdout.write(ln)

            res = fd.close()
            if res is not None:
                sys.stderr.write('Building libffi failed [%d]\n'%(res,))
                sys.exit(1)
            
        LIBFFI_BASE='build/libffi'

    elif os.environ.has_key('LIBFFI_BASE'):
        LIBFFI_BASE=os.environ['LIBFFI_BASE']
    else:
        LIBFFI_BASE='libffi'
    LIBFFI_CFLAGS=[ 
        "-DOC_WITH_LIBFFI", 
        #"-I%s/include"%LIBFFI_BASE, 
        "-isystem", "%s/include"%LIBFFI_BASE, 
    ]
    LIBFFI_LDFLAGS=[ 
        '-read_only_relocs','warning',
        '-L%s/lib'%LIBFFI_BASE, '-lffi', 
    ]
    LIBFFI_SOURCEFILES=[
        'Modules/objc/libffi_support.m',
    ]

    if USE_FFI_SHORTCUTS:
        LIBFFI_CFLAGS.append("-DOC_USE_FFI_SHORTCUTS")
else:
    LIBFFI_CFLAGS=[]
    LIBFFI_LDFLAGS=[]
    LIBFFI_SOURCEFILES=[]

sourceFiles = [
        "Modules/objc/objc_util.m",
        "Modules/objc/objc_support.m",
        "Modules/objc/class-builder.m",
        "Modules/objc/class-list.m",
        "Modules/objc/ObjCPointer.m",
        "Modules/objc/py2.2bool.c",
        "Modules/objc/objc-class.m",
        "Modules/objc/unicode-object.m",
        "Modules/objc/informal-protocol.m",
        "Modules/objc/objc-object.m",
        "Modules/objc/super-call.m",
        "Modules/objc/selector.m",
        "Modules/objc/method-accessor.m",
        "Modules/objc/instance-var.m",
        "Modules/objc/OC_PythonObject.m",
        "Modules/objc/OC_PythonArray.m",
        "Modules/objc/OC_PythonDictionary.m",
        "Modules/objc/register.m",
        "Modules/objc/pyobjc-api.m",
        "Modules/objc/alloc_hack.m",
        "Modules/objc/module.m",
]

# On GNUstep we can read some configuration from the environment.
gs_root = os.environ.get('GNUSTEP_SYSTEM_ROOT')

if gs_root is None:
    #
    # MacOS X 
    #
    CFLAGS=[
        "-DMACOSX",
        "-no-cpp-precomp",
        "-Wno-long-double",
        "-g",
        #"-O0", "-g",
        #"-Werror",
        ]

    OBJC_LDFLAGS=[
        '-framework', 'Foundation',
        ]

    FND_LDFLAGS=[
        '-framework', 'Foundation',
        ]

    APPKIT_LDFLAGS=[
        '-framework', 'AppKit',
        ]

    FNDMAP_LDFLAGS=[
        '-framework', 'Foundation',
        ]

    APPMAP_LDFLAGS=[
        '-framework', 'AppKit',
        ]

    ADDRESSBOOK_LDFLAGS=[
        '-framework', 'AddressBook', '-framework', 'Foundation',
    ]

    PREFPANES_LDFLAGS=[
        '-framework', 'PreferencePanes', '-framework', 'Foundation',
    ]

else:
    #
    # GNUstep
    #
    # NOTE: We add '-g' to the compile flags to make development easier
    # on systems where the installed python hasn't been build with debugging
    # support.

    gs_cpu = os.environ.get('GNUSTEP_HOST_CPU')
    gs_os = os.environ.get('GNUSTEP_HOST_OS')
    gs_combo = os.environ.get('LIBRARY_COMBO')

    gs_lib_dir = gs_cpu + '/' + gs_os
    gs_flib_dir = gs_cpu + '/' + gs_os + '/' + gs_combo

    CFLAGS=[
        '-g',
        '-Wno-import',
        '-DGNU_RUNTIME',
        '-DGNUSTEP',
        '-I' + gs_root + '/Headers',
        '-I' + gs_root + '/Headers/gnustep',
        '-I' + gs_root + '/Headers/' + gs_lib_dir
        ]

    OBJC_LDFLAGS=[
        '-g',
        '-L', gs_root + '/Libraries/' + gs_flib_dir,
        '-l', 'gnustep-base',
        '-L', gs_root + '/Libraries/' + gs_lib_dir,
        '-l', 'objc'
        ]

    FND_LDFLAGS=[
        '-g',
        '-L', gs_root + '/Libraries/' + gs_flib_dir,
        '-l', 'gnustep-base',
        '-L', gs_root + '/Libraries/' + gs_lib_dir,
        '-l', 'objc'
        ]

    APPKIT_LDFLAGS=[
        '-g',
        '-L', gs_root + '/Libraries/' + gs_flib_dir,
        '-l', 'gnustep-base',
        '-l', 'gnustep-gui',
        '-L', gs_root + '/Libraries/' + gs_lib_dir,
        '-l', 'objc'
        ]

    FNDMAP_LDFLAGS=[
        '-g',
        '-L', gs_root + '/Libraries/' + gs_flib_dir,
        '-l', 'gnustep-base',
        '-L', gs_root + '/Libraries/' + gs_lib_dir,
        '-l', 'objc'
        ]

    APPMAP_LDFLAGS=[
        '-g',
        '-L', gs_root + '/Libraries/' + gs_flib_dir,
        '-l', 'gnustep-base',
        '-l', 'gnustep-gui',
        '-L', gs_root + '/Libraries/' + gs_lib_dir,
        '-l', 'objc'
        ]

    ADDRESSBOOK_LDFLAGS=[]
    PREFPANES_LDFLAGS=[]

if USE_ADJUST_REFCOUNTS:
    CFLAGS.append('-DOC_ADJUST_REFCOUNTS')

CFLAGS.append('-IInclude/')

def IfFrameWork(name, packages, extensions):
    """
    Return the packages and extensions if the framework exists, or
    two empty lists if not.
    """
    if os.path.exists(os.path.join('/System/Library/Frameworks/', name)):
        return packages, extensions
    if os.path.exists(os.path.join('/Library/Frameworks/', name)):
        return packages, extensions
    return [], []


CorePackages = [ 'objc' ]
CoreExtensions =  [
    Extension("objc._objc", 
              sourceFiles + LIBFFI_SOURCEFILES,
              extra_compile_args=[
                    "-DOBJC_PARANOIA_MODE",
              ] + LIBFFI_CFLAGS + CFLAGS,
              extra_link_args=LIBFFI_LDFLAGS + OBJC_LDFLAGS),
    Extension("objc.test.testbndl",
              ["Lib/objc/test/testbndl.m"],
              extra_compile_args=["-IModules/objc" ] + CFLAGS,
              extra_link_args=OBJC_LDFLAGS),
    Extension("autoGIL", 
              ["Modules/autoGIL.c"],
              extra_compile_args = CFLAGS,
              extra_link_args = ['-framework', 'CoreFoundation']),
    ]
CocoaPackages = [ 'Foundation', 'AppKit' ]
CocoaExtensions = [
          Extension("Foundation._Foundation", 
                    [
                        "Modules/Foundation/_Foundation.m",
                        "Modules/Foundation/NSAutoreleasePoolSupport.m"
                    ],
                    extra_compile_args=[
                        "-IModules/objc",  
                    ] + CFLAGS,
                    extra_link_args=[
                    ] + FND_LDFLAGS),
          Extension("AppKit._AppKit", 
                    ["Modules/AppKit/_AppKit.m"],
                    extra_compile_args=[
                        "-IModules/objc", 
                    ] + CFLAGS,
                    extra_link_args=[
                    ] + APPKIT_LDFLAGS),
          Extension("AppKit._AppKitMapping",
                    ["Modules/AppKit/_AppKitMapping.m"],
                    extra_compile_args=[ "-IModules/objc",] + CFLAGS,
                    extra_link_args=[] + APPMAP_LDFLAGS),
          Extension("objc._FoundationMapping", 
                    ["Modules/Foundation/_FoundationMapping.m"],
                    extra_compile_args=[
                        "-IModules/objc", 
                    ] + CFLAGS,
                    extra_link_args=[
                    ] + FNDMAP_LDFLAGS),
      ]

# The AdressBook module is only installed when the user actually has the
# AddressBook framework.
AddressBookPackages, AddressBookExtensions = \
        IfFrameWork('AddressBook.framework', [ 'AddressBook' ], [
            Extension('AddressBook._AddressBook',
                      [ 'Modules/AddressBook/_AddressBook.m' ],
                      extra_compile_args=[
                        '-IModules/objc',
                      ] + CFLAGS,
                      extra_link_args=[
                      ] + ADDRESSBOOK_LDFLAGS),
        ])

PrefPanesPackages, PrefPanesExtensions = \
        IfFrameWork('PreferencePanes.framework', [ 'PreferencePanes' ], [
            Extension('PreferencePanes._PreferencePanes',
                      [ 'Modules/PreferencePanes/_PreferencePanes.m' ],
                      extra_compile_args=[
                        '-IModules/objc',
                      ] + CFLAGS,
                      extra_link_args=[
                      ] + PREFPANES_LDFLAGS),
        ])

InterfaceBuilderPackages, InterfaceBuilderExtensions = \
        IfFrameWork('InterfaceBuilder.framework', [ 'InterfaceBuilder' ], [
            Extension('InterfaceBuilder._InterfaceBuilder',
                      [ 'Modules/InterfaceBuilder/_InterfaceBuilder.m' ],
                      extra_compile_args=[
                        '-IModules/objc',
                      ] + CFLAGS,
                      extra_link_args=[
                        '-framework', 'InterfaceBuilder', 
                        '-framework', 'Foundation'
                      ]),
        ])


def package_version():
    fp = open('Modules/objc/pyobjc.h', 'r')
    for ln in fp.readlines():
        if ln.startswith('#define OBJC_VERSION'):
            fp.close()
            return ln.split()[-1][1:-1]

    raise ValueError, "Version not found"


packages = CorePackages + CocoaPackages + AddressBookPackages + PrefPanesPackages + InterfaceBuilderPackages + [ 'PyObjCTools' ] 
# The following line is needed to allow separate flat modules
# to be installed from a different folder (needed for the 
# bundlebuilder test below).
package_dir = dict([(pkg, 'Lib/' + pkg) for pkg in packages])

for aPackage in package_dir.keys():
    testDir = os.path.join(package_dir[aPackage], 'test')
    if os.path.isdir(testDir):
        packageName = '%s.test' % aPackage
        package_dir[packageName] = testDir
        packages.append(packageName)

try:
    import bundlebuilder

    if sys.version_info[:2] == (2,2):
        raise ImportError, "We're the only provider of these modules ATM."
except ImportError:
    # bundlebuilder.py and plistlib.py shipped with newer versions of
    # Python but are included with pyobjc be independent. The following
    # magic makes distutils install the contents of MPCompat.
    packages.append('')
    package_dir[''] = 'MPCompat'

dist = setup(name = "pyobjc",
	     version = package_version(),
	     description = "Python<->ObjC Interoperability Module",
	     author = "bbum, RonaldO, SteveM, LeleG, many others stretching back through the reaches of time...",
	     author_email = "pyobjc-dev@lists.sourceforge.net",
	     url = "http://pyobjc.sourceforge.net/",
	     ext_modules = (
			     CoreExtensions 
			   + CocoaExtensions 
			   + AddressBookExtensions 
                           + PrefPanesExtensions
                           + InterfaceBuilderExtensions
			   ),
	     packages = packages,
	     package_dir = package_dir,
	     scripts = [ 'Scripts/nibclassbuilder', ],
	     extra_path = "PyObjC",
)

if "install" in sys.argv:
    # Hack to remove a previous version that may have been installed
    # directly into site-packages (pre 0.9) as opposed to a new subdir.
    import shutil
    inst = dist.get_command_obj("install")
    install_dir = inst.install_platlib
    if install_dir is not None:
        for name in ("objc", "Foundation", "AppKit", "AddressBook", "autoGIL"):
            path = os.path.join(install_dir, name)
            if os.path.isdir(path):
                print "(removing old version: %s)" % path
                shutil.rmtree(path)
            elif os.path.isfile(path):
                print "(removing old version: %s)" % path
                os.remove(path)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.