Skip to content

Commit 584ba6f

Browse files
author
Paul Davis
committed
Updates to the build system.
Linking against the library works, but importing spidermonkey kills the Python VM because of an unresolved symbol. Can't figure out if this is just OS X's build of libmozjs or if I'm missing a flag. [#12]
1 parent a245d0a commit 584ba6f

File tree

6 files changed

+88
-46
lines changed

6 files changed

+88
-46
lines changed

setup.py

Lines changed: 82 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
# libc = ctypes.CDLL("libc.dylib")
2525
# libc.siginterrupt(signal.SIGCHLD, 0)
2626

27+
import glob
2728
import os
2829
import subprocess as sp
2930
import sys
@@ -33,77 +34,118 @@
3334
from setuptools import setup, Extension
3435

3536
DEBUG = "--debug" in sys.argv
36-
37-
def find_sources(extensions=[".c", ".cpp"]):
38-
ret = []
39-
for dpath, dnames, fnames in os.walk("./spidermonkey"):
40-
for fname in fnames:
41-
if os.path.splitext(fname)[1] in extensions:
42-
ret.append(os.path.join(dpath, fname))
43-
return ret
44-
45-
def nspr_config():
46-
pipe = sp.Popen("nspr-config --cflags --libs",
37+
USE_SYSTEM_LIB = "--system-library" in sys.argv
38+
39+
def find_sources(extensions=(".c", ".cpp")):
40+
if USE_SYSTEM_LIB:
41+
return [
42+
fname
43+
for ext in extensions
44+
for fname in glob.glob("spidermonkey/*" + ext)
45+
]
46+
else:
47+
return [
48+
os.path.join(dpath, fname)
49+
for (dpath, dnames, fnames) in os.walk("./spidermonkey")
50+
for fname in fnames
51+
if fname.endswith(extensions)
52+
]
53+
54+
def pkg_config(pkg_name, config=None):
55+
pipe = sp.Popen("pkg-config --cflags --libs %s" % pkg_name,
4756
shell=True, stdout=sp.PIPE, stderr=sp.PIPE)
4857
(stdout, stderr) = pipe.communicate()
4958
if pipe.wait() != 0:
50-
raise RuntimeError("Failed to get nspr config.")
51-
bits = stdout.split()
52-
ret = {
53-
"include_dirs": [],
54-
"library_dirs": [],
55-
"libraries": [],
56-
"extra_compile_args": [],
57-
"extra_link_args": []
58-
}
59-
prfx = {
59+
raise RuntimeError("No package configuration found for: %s" % pkg_name)
60+
if config is None:
61+
config = {
62+
"include_dirs": [],
63+
"library_dirs": [],
64+
"libraries": [],
65+
"extra_compile_args": [],
66+
"extra_link_args": []
67+
}
68+
prefixes = {
6069
"-I": ("include_dirs", 2),
6170
"-L": ("library_dirs", 2),
6271
"-l": ("libraries", 2),
72+
"-D": ("extra_compile_args", 0),
6373
"-Wl": ("extra_link_args", 0)
6474
}
65-
for b in bits:
66-
for p in prfx:
67-
if b.startswith(p):
68-
name, trim = prfx[p]
69-
ret[name].append(b[trim:])
70-
return ret
75+
for flag in stdout.split():
76+
for prefix in prefixes:
77+
if not flag.startswith(prefix):
78+
continue
79+
# Hack for xulrunner
80+
if flag.endswith("/stable"):
81+
flag = flag[:-6] + "unstable"
82+
name, trim = prefixes[prefix]
83+
config[name].append(flag[trim:])
84+
return config
85+
86+
def nspr_config(config=None):
87+
return pkg_config("nspr", config)
88+
89+
def js_config(config=None):
90+
config = pkg_config("mozilla-js", config)
91+
if "-DJS_THREADSAFE" not in config["extra_compile_args"]:
92+
raise SystemError("Unable to link against a library that was "
93+
"compiled without -DJS_THREADSAFE");
94+
return config
7195

7296
def platform_config():
7397
sysname = os.uname()[0]
7498
machine = os.uname()[-1]
75-
76-
config = nspr_config()
77-
config["include_dirs"].append("spidermonkey/%s-%s" % (sysname, machine))
78-
config["extra_compile_args"].extend([
79-
"-DJS_THREADSAFE",
80-
"-DPOSIX_SOURCE",
81-
"-D_BSD_SOURCE",
82-
"-Wno-strict-prototypes"
83-
])
99+
100+
# If we're linking against a system library it should give
101+
# us all the information we need.
102+
if USE_SYSTEM_LIB:
103+
return js_config()
104+
105+
# Build our configuration
106+
config = {
107+
"extra_compile_args": [
108+
"-DJS_THREADSAFE",
109+
"-DPOSIX_SOURCE",
110+
"-D_BSD_SOURCE",
111+
"-Wno-strict-prototypes" # Disable copius JS warnings
112+
],
113+
"include_dirs": [
114+
"spidermonkey/libjs",
115+
"spidermonkey/%s-%s" % (sysname, machine)
116+
],
117+
"library_dirs": [],
118+
"libraries": [],
119+
"extra_link_args": []
120+
}
121+
122+
# Debug builds are useful for finding errors in
123+
# the request counting semantics for Spidermonkey
84124
if DEBUG:
85125
config["extra_compile_args"].extend([
86126
"-UNDEBG",
87127
"-DDEBUG",
88128
"-DJS_PARANOID_REQUEST"
89129
])
90130

91-
92131
if sysname in ["Linux", "FreeBSD"]:
93132
config["extra_compile_args"].extend([
94133
"-DHAVE_VA_COPY",
95134
"-DVA_COPY=va_copy"
96-
])
135+
])
97136

137+
# Currently no suppot for Win32, patches welcome.
98138
if sysname in ["Darwin", "Linux", "FreeBSD"]:
99139
config["extra_compile_args"].append("-DXP_UNIX")
100140
else:
101141
raise RuntimeError("Unknown system name: %s" % sysname)
102142

103-
return config
143+
return nspr_config(config=config)
104144

105145
Distribution.global_options.append(("debug", None,
106-
"Build a DEBUG version of spidermonkey"))
146+
"Build a DEBUG version of spidermonkey."))
147+
Distribution.global_options.append(("system-library", None,
148+
"Link against an installed system library."))
107149

108150
setup(
109151
name = "python-spidermonkey",

spidermonkey/context.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010

1111
#include <time.h> // After spidermonkey.h so after Python.h
1212

13-
#include "libjs/jsobj.h"
14-
#include "libjs/jscntxt.h"
13+
#include <jsobj.h>
14+
#include <jscntxt.h>
1515

1616
JSBool
1717
get_prop(JSContext* jscx, JSObject* jsobj, jsval key, jsval* rval)

spidermonkey/jsobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88

99
#include "spidermonkey.h"
10-
#include "libjs/jsobj.h"
10+
#include <jsobj.h>
1111

1212
PyObject*
1313
make_object(PyTypeObject* type, Context* cx, jsval val)

spidermonkey/pyobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88

99
#include "spidermonkey.h"
10-
#include "libjs/jsobj.h"
10+
#include <jsobj.h>
1111

1212
/*
1313
This is a fairly unsafe operation in so much as

spidermonkey/runtime.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#include <Python.h>
1313
#include "structmember.h"
1414

15-
#include "libjs/jsapi.h"
15+
#include <jsapi.h>
1616

1717
typedef struct {
1818
PyObject_HEAD

spidermonkey/spidermonkey.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#include <Python.h>
1313
#include "structmember.h"
1414

15-
#include "libjs/jsapi.h"
15+
#include <jsapi.h>
1616

1717
#include "runtime.h"
1818
#include "context.h"

0 commit comments

Comments
 (0)