|
24 | 24 | # libc = ctypes.CDLL("libc.dylib")
|
25 | 25 | # libc.siginterrupt(signal.SIGCHLD, 0)
|
26 | 26 |
|
| 27 | +import glob |
27 | 28 | import os
|
28 | 29 | import subprocess as sp
|
29 | 30 | import sys
|
|
33 | 34 | from setuptools import setup, Extension
|
34 | 35 |
|
35 | 36 | 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, |
47 | 56 | shell=True, stdout=sp.PIPE, stderr=sp.PIPE)
|
48 | 57 | (stdout, stderr) = pipe.communicate()
|
49 | 58 | 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 = { |
60 | 69 | "-I": ("include_dirs", 2),
|
61 | 70 | "-L": ("library_dirs", 2),
|
62 | 71 | "-l": ("libraries", 2),
|
| 72 | + "-D": ("extra_compile_args", 0), |
63 | 73 | "-Wl": ("extra_link_args", 0)
|
64 | 74 | }
|
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 |
71 | 95 |
|
72 | 96 | def platform_config():
|
73 | 97 | sysname = os.uname()[0]
|
74 | 98 | 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 |
84 | 124 | if DEBUG:
|
85 | 125 | config["extra_compile_args"].extend([
|
86 | 126 | "-UNDEBG",
|
87 | 127 | "-DDEBUG",
|
88 | 128 | "-DJS_PARANOID_REQUEST"
|
89 | 129 | ])
|
90 | 130 |
|
91 |
| - |
92 | 131 | if sysname in ["Linux", "FreeBSD"]:
|
93 | 132 | config["extra_compile_args"].extend([
|
94 | 133 | "-DHAVE_VA_COPY",
|
95 | 134 | "-DVA_COPY=va_copy"
|
96 |
| - ]) |
| 135 | + ]) |
97 | 136 |
|
| 137 | + # Currently no suppot for Win32, patches welcome. |
98 | 138 | if sysname in ["Darwin", "Linux", "FreeBSD"]:
|
99 | 139 | config["extra_compile_args"].append("-DXP_UNIX")
|
100 | 140 | else:
|
101 | 141 | raise RuntimeError("Unknown system name: %s" % sysname)
|
102 | 142 |
|
103 |
| - return config |
| 143 | + return nspr_config(config=config) |
104 | 144 |
|
105 | 145 | 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.")) |
107 | 149 |
|
108 | 150 | setup(
|
109 | 151 | name = "python-spidermonkey",
|
|
0 commit comments