Skip to content

Commit

Permalink
Allow the build system to use the system spidermonkey library.
Browse files Browse the repository at this point in the history
Update spidermonkey-related #includes.
Update documentation.
Add a global option to setup.py, --system-library, which enables linking to the
system library.
Use pkg-config instead of nspr-config for the purposes of code reusability.
Clean up the library flag filtering and find sources code.
  • Loading branch information
malept committed Apr 14, 2009
1 parent 9890c7b commit ef996cd
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 26 deletions.
34 changes: 32 additions & 2 deletions README.md
Expand Up @@ -37,8 +37,32 @@ Debian/Ubuntu:
$ sudo apt-get install libnspr4-dev

Alternatively you can build from [source][nspr]. If you choose this route make
sure that the nspr-config command is on your $PATH when running the install
commands below.
sure that the pkg-config command is on your `$PATH` when running the install
commands below. Additionally, make sure that `$PKG_CONFIG_PATH` is properly
set.

XULRunner (optional)
--------------------
You can optionally build the extension linked to your system's spidermonkey
library, which is installed with XULRunner. You should be able to grab it from
your package manager of choice with something like the following:

Mac OS X:

$ sudo port install xulrunner

Debian/Ubuntu:

$ sudo apt-get install xulrunner-1.9-dev

Gentoo:

$ sudo emerge xulrunner

Alternatively you can build from [source][xulrunner]. If you choose this route,
make sure that the pkg-config command is on your $PATH when running the
install commands below. Additionally, make sure that `$PKG_CONFIG_PATH` is
properly set.

Installation
============
Expand All @@ -54,6 +78,11 @@ Installation

$ sudo python setup.py develop

If you want to build with the system spidermonkey library, replace the build
command with the following:

$ python setup.py --system-library build

Having Issues?
==============

Expand Down Expand Up @@ -117,3 +146,4 @@ Previous Authors

[lh]: http://davisp.lighthouseapp.com/projects/26898-python-spidermonkey/overview
[nspr]: ftp://ftp.mozilla.org/pub/mozilla.org/nspr/releases
[xulrunner]: ftp://ftp.mozilla.org/pub/mozilla.org/xulrunner/releases
64 changes: 44 additions & 20 deletions setup.py
Expand Up @@ -13,53 +13,74 @@
Javascript Perl module, in turn based on Mozilla's 'PerlConnect' Perl binding.
""",

from glob import glob
import os
import subprocess as sp
import sys
from distutils.dist import Distribution
import ez_setup
ez_setup.use_setuptools()
from setuptools import setup, Extension

use_system_library = '--system-library' in sys.argv

def find_sources(extensions=[".c", ".cpp"]):
ret = []
for dpath, dnames, fnames in os.walk("./spidermonkey"):
for fname in fnames:
if os.path.splitext(fname)[1] in extensions:
ret.append(os.path.join(dpath, fname))
return ret
if use_system_library:
return reduce(lambda x, y: x + y,
[glob('spidermonkey/*' + x) for x in extensions])
else:
sources = []
for dpath, dnames, fnames in os.walk('./spidermonkey'):
sources += [os.path.join(dpath, f)
for f in fnames
if os.path.splitext(f)[1] in extensions]
return sources

def nspr_config():
pipe = sp.Popen("nspr-config --cflags --libs",
def pkg_config(package):
pipe = sp.Popen("pkg-config --cflags --libs %s" % package,
shell=True, stdout=sp.PIPE, stderr=sp.PIPE)
(stdout, stderr) = pipe.communicate()
if pipe.wait() != 0:
raise RuntimeError("Failed to get nspr config.")
bits = stdout.split()
ret = {
raise RuntimeError("Failed to get package config flags for '%s'." %
package)
flags = {
"include_dirs": [],
"library_dirs": [],
"libraries": [],
"extra_compile_args": [],
"extra_link_args": []
}
prfx = {
prefixes = {
"-I": ("include_dirs", 2),
"-L": ("library_dirs", 2),
"-l": ("libraries", 2),
"-Wl": ("extra_link_args", 0)
}
for b in bits:
for p in prfx:
if b.startswith(p):
name, trim = prfx[p]
ret[name].append(b[trim:])
return ret
for flag in stdout.split():
for prefix in prefixes:
if flag.startswith(prefix):
# hack for xulrunner
if flag.endswith('/stable'):
flag = flag[:-6] + 'unstable'
name, trim = prefixes[prefix]
flags[name].append(flag[trim:])
return flags

def nspr_config():
return pkg_config('nspr')

def js_config(prefix='mozilla'):
return pkg_config(prefix + '-js')

def platform_config():
sysname = os.uname()[0]
machine = os.uname()[-1]

config = nspr_config()

if use_system_library:
config = js_config()
else:
config = nspr_config()
config['include_dirs'].append('spidermonkey/libjs')
config["include_dirs"].append("spidermonkey/%s-%s" % (sysname, machine))
config["extra_compile_args"].extend([
"-DJS_THREADSAFE",
Expand All @@ -81,6 +102,9 @@ def platform_config():

return config

Distribution.global_options.append(('system-library', None,
'Use system JS library instead of bundled'))

setup(
name = "python-spidermonkey",
version = "0.0.5",
Expand Down
2 changes: 1 addition & 1 deletion spidermonkey/context.c
Expand Up @@ -7,7 +7,7 @@
*/

#include "spidermonkey.h"
#include "libjs/jsobj.h"
#include <jsobj.h>

static JSClass
js_global_class = {
Expand Down
2 changes: 1 addition & 1 deletion spidermonkey/jsobject.c
Expand Up @@ -7,7 +7,7 @@
*/

#include "spidermonkey.h"
#include "libjs/jsobj.h"
#include <jsobj.h>

PyObject*
make_object(PyTypeObject* type, Context* cx, jsval val)
Expand Down
2 changes: 1 addition & 1 deletion spidermonkey/runtime.h
Expand Up @@ -12,7 +12,7 @@
#include <Python.h>
#include "structmember.h"

#include "libjs/jsapi.h"
#include <jsapi.h>

typedef struct {
PyObject_HEAD
Expand Down
2 changes: 1 addition & 1 deletion spidermonkey/spidermonkey.h
Expand Up @@ -12,7 +12,7 @@
#include <Python.h>
#include "structmember.h"

#include "libjs/jsapi.h"
#include <jsapi.h>

#include "runtime.h"
#include "context.h"
Expand Down

0 comments on commit ef996cd

Please sign in to comment.