| 1 |
import os |
| 2 |
import sys |
| 3 |
import py |
| 4 |
from pypy import pypydir |
| 5 |
|
| 6 |
if os.name != 'nt': |
| 7 |
so_ext = 'so' |
| 8 |
else: |
| 9 |
so_ext = 'dll' |
| 10 |
|
| 11 |
HERE = py.path.local(pypydir) / 'module' / 'cpyext' / 'test' |
| 12 |
|
| 13 |
class SystemCompilationInfo(object): |
| 14 |
"""Bundles all the generic information required to compile extensions. |
| 15 |
|
| 16 |
Note: here, 'system' means OS + target interpreter + test config + ... |
| 17 |
""" |
| 18 |
def __init__(self, builddir_base, include_extra=None, compile_extra=None, |
| 19 |
link_extra=None, extra_libs=None, ext=None): |
| 20 |
self.builddir_base = builddir_base |
| 21 |
self.include_extra = include_extra or [] |
| 22 |
self.compile_extra = compile_extra |
| 23 |
self.link_extra = link_extra |
| 24 |
self.extra_libs = extra_libs |
| 25 |
self.ext = ext |
| 26 |
|
| 27 |
def get_builddir(self, name): |
| 28 |
builddir = py.path.local.make_numbered_dir( |
| 29 |
rootdir=py.path.local(self.builddir_base), |
| 30 |
prefix=name + '-', |
| 31 |
keep=0) |
| 32 |
return builddir |
| 33 |
|
| 34 |
def compile_extension_module(self, name, include_dirs=None, |
| 35 |
source_files=None, source_strings=None): |
| 36 |
""" |
| 37 |
Build an extension module and return the filename of the resulting |
| 38 |
native code file. |
| 39 |
|
| 40 |
name is the name of the module, possibly including dots if it is a |
| 41 |
module inside a package. |
| 42 |
""" |
| 43 |
include_dirs = include_dirs or [] |
| 44 |
modname = name.split('.')[-1] |
| 45 |
dirname = self.get_builddir(name=modname) |
| 46 |
if source_strings: |
| 47 |
assert not source_files |
| 48 |
files = convert_sources_to_files(source_strings, dirname) |
| 49 |
source_files = files |
| 50 |
soname = c_compile(source_files, outputfilename=str(dirname / modname), |
| 51 |
compile_extra=self.compile_extra, |
| 52 |
link_extra=self.link_extra, |
| 53 |
include_dirs=self.include_extra + include_dirs, |
| 54 |
libraries=self.extra_libs) |
| 55 |
pydname = soname.new(purebasename=modname, ext=self.ext) |
| 56 |
soname.rename(pydname) |
| 57 |
return str(pydname) |
| 58 |
|
| 59 |
def import_module(self, name, init=None, body='', filename=None, |
| 60 |
include_dirs=None, PY_SSIZE_T_CLEAN=False, use_imp=False): |
| 61 |
""" |
| 62 |
init specifies the overall template of the module. |
| 63 |
|
| 64 |
if init is None, the module source will be loaded from a file in this |
| 65 |
test directory, give a name given by the filename parameter. |
| 66 |
|
| 67 |
if filename is None, the module name will be used to construct the |
| 68 |
filename. |
| 69 |
""" |
| 70 |
if body or init: |
| 71 |
if init is None: |
| 72 |
init = "return PyModule_Create(&moduledef);" |
| 73 |
if init is not None: |
| 74 |
code = make_source(name, init, body, PY_SSIZE_T_CLEAN) |
| 75 |
kwds = dict(source_strings=[code]) |
| 76 |
else: |
| 77 |
assert not PY_SSIZE_T_CLEAN |
| 78 |
if filename is None: |
| 79 |
filename = name |
| 80 |
filename = HERE / (filename + ".c") |
| 81 |
kwds = dict(source_files=[filename]) |
| 82 |
mod = self.compile_extension_module( |
| 83 |
name, include_dirs=include_dirs, **kwds) |
| 84 |
return self.load_module(mod, name, use_imp) |
| 85 |
|
| 86 |
def import_extension(self, modname, functions, prologue="", |
| 87 |
include_dirs=None, more_init="", PY_SSIZE_T_CLEAN=False): |
| 88 |
body = prologue + make_methods(functions, modname) |
| 89 |
init = """PyObject *mod = PyModule_Create(&moduledef); |
| 90 |
""" |
| 91 |
if more_init: |
| 92 |
init += """#define INITERROR return NULL |
| 93 |
""" |
| 94 |
init += more_init |
| 95 |
init += "\nreturn mod;" |
| 96 |
return self.import_module( |
| 97 |
name=modname, init=init, body=body, include_dirs=include_dirs, |
| 98 |
PY_SSIZE_T_CLEAN=PY_SSIZE_T_CLEAN) |
| 99 |
|
| 100 |
class ExtensionCompiler(SystemCompilationInfo): |
| 101 |
"""Extension compiler for appdirect mode""" |
| 102 |
def load_module(space, mod, name, use_imp=False): |
| 103 |
|
| 104 |
import imp |
| 105 |
return imp.load_dynamic(name, mod) |
| 106 |
|
| 107 |
def convert_sources_to_files(sources, dirname): |
| 108 |
files = [] |
| 109 |
for i, source in enumerate(sources): |
| 110 |
filename = dirname / ('source_%d.c' % i) |
| 111 |
with filename.open('w') as f: |
| 112 |
f.write(str(source)) |
| 113 |
files.append(filename) |
| 114 |
return files |
| 115 |
|
| 116 |
|
| 117 |
def make_methods(functions, modname): |
| 118 |
methods_table = [] |
| 119 |
codes = [] |
| 120 |
for funcname, flags, code in functions: |
| 121 |
cfuncname = "%s_%s" % (modname, funcname) |
| 122 |
if 'METH_FASTCALL' in flags and 'METH_KEYWORDS' in flags: |
| 123 |
signature = ('(PyObject *self, PyObject *const *args, ' |
| 124 |
'Py_ssize_t len_args, PyObject *kwnames)') |
| 125 |
elif 'METH_KEYWORDS' in flags: |
| 126 |
signature = '(PyObject *self, PyObject *args, PyObject *kwargs)' |
| 127 |
elif 'METH_FASTCALL' in flags: |
| 128 |
signature = ('(PyObject *self, PyObject *const *args, ' |
| 129 |
'Py_ssize_t len_args)') |
| 130 |
else: |
| 131 |
signature = '(PyObject *self, PyObject *args)' |
| 132 |
methods_table.append( |
| 133 |
"{\"%s\", (PyCFunction)%s, %s}," % (funcname, cfuncname, flags)) |
| 134 |
func_code = """ |
| 135 |
static PyObject* {cfuncname}{signature} |
| 136 |
{{ |
| 137 |
{code} |
| 138 |
}} |
| 139 |
""".format(cfuncname=cfuncname, signature=signature, code=code) |
| 140 |
codes.append(func_code) |
| 141 |
|
| 142 |
body = "\n".join(codes) + """ |
| 143 |
static PyMethodDef methods[] = { |
| 144 |
%(methods)s |
| 145 |
{ NULL } |
| 146 |
}; |
| 147 |
static struct PyModuleDef moduledef = { |
| 148 |
PyModuleDef_HEAD_INIT, |
| 149 |
"%(modname)s", /* m_name */ |
| 150 |
NULL, /* m_doc */ |
| 151 |
-1, /* m_size */ |
| 152 |
methods, /* m_methods */ |
| 153 |
}; |
| 154 |
""" % dict(methods='\n'.join(methods_table), modname=modname) |
| 155 |
return body |
| 156 |
|
| 157 |
def make_source(name, init, body, PY_SSIZE_T_CLEAN): |
| 158 |
code = """ |
| 159 |
%(PY_SSIZE_T_CLEAN)s |
| 160 |
#include <Python.h> |
| 161 |
|
| 162 |
%(body)s |
| 163 |
|
| 164 |
PyMODINIT_FUNC |
| 165 |
PyInit_%(name)s(void) { |
| 166 |
%(init)s |
| 167 |
} |
| 168 |
""" % dict( |
| 169 |
name=name, init=init, body=body, |
| 170 |
PY_SSIZE_T_CLEAN='#define PY_SSIZE_T_CLEAN' |
| 171 |
if PY_SSIZE_T_CLEAN else '') |
| 172 |
return code |
| 173 |
|
| 174 |
|
| 175 |
def c_compile(cfilenames, outputfilename, |
| 176 |
compile_extra=None, link_extra=None, |
| 177 |
include_dirs=None, libraries=None, library_dirs=None): |
| 178 |
compile_extra = compile_extra or [] |
| 179 |
link_extra = link_extra or [] |
| 180 |
include_dirs = include_dirs or [] |
| 181 |
libraries = libraries or [] |
| 182 |
library_dirs = library_dirs or [] |
| 183 |
if sys.platform == 'win32': |
| 184 |
link_extra = link_extra + ['/DEBUG'] |
| 185 |
if sys.platform == 'darwin': |
| 186 |
|
| 187 |
for s in ('/sw/', '/opt/local/'): |
| 188 |
if (s + 'include' not in include_dirs |
| 189 |
and os.path.exists(s + 'include')): |
| 190 |
include_dirs.append(s + 'include') |
| 191 |
if s + 'lib' not in library_dirs and os.path.exists(s + 'lib'): |
| 192 |
library_dirs.append(s + 'lib') |
| 193 |
|
| 194 |
outputfilename = py.path.local(outputfilename).new(ext=so_ext) |
| 195 |
saved_environ = os.environ.copy() |
| 196 |
try: |
| 197 |
_build( |
| 198 |
cfilenames, outputfilename, |
| 199 |
compile_extra, link_extra, |
| 200 |
include_dirs, libraries, library_dirs) |
| 201 |
finally: |
| 202 |
|
| 203 |
|
| 204 |
for key, value in saved_environ.items(): |
| 205 |
if os.environ.get(key) != value: |
| 206 |
os.environ[key] = value |
| 207 |
return outputfilename |
| 208 |
|
| 209 |
def _build(cfilenames, outputfilename, compile_extra, link_extra, |
| 210 |
include_dirs, libraries, library_dirs): |
| 211 |
try: |
| 212 |
|
| 213 |
import setuptools |
| 214 |
except ImportError: |
| 215 |
|
| 216 |
|
| 217 |
pass |
| 218 |
from distutils.ccompiler import new_compiler |
| 219 |
from distutils import sysconfig |
| 220 |
|
| 221 |
|
| 222 |
sysconfig.get_config_vars() |
| 223 |
|
| 224 |
compiler = new_compiler(force=1) |
| 225 |
sysconfig.customize_compiler(compiler) |
| 226 |
objects = [] |
| 227 |
for cfile in cfilenames: |
| 228 |
cfile = py.path.local(cfile) |
| 229 |
old = cfile.dirpath().chdir() |
| 230 |
try: |
| 231 |
res = compiler.compile([cfile.basename], |
| 232 |
include_dirs=include_dirs, extra_preargs=compile_extra) |
| 233 |
assert len(res) == 1 |
| 234 |
cobjfile = py.path.local(res[0]) |
| 235 |
assert cobjfile.check() |
| 236 |
objects.append(str(cobjfile)) |
| 237 |
finally: |
| 238 |
old.chdir() |
| 239 |
|
| 240 |
compiler.link_shared_object( |
| 241 |
objects, str(outputfilename), |
| 242 |
libraries=libraries, |
| 243 |
extra_preargs=link_extra, |
| 244 |
library_dirs=library_dirs) |
| 245 |
|
| 246 |
def get_so_suffix(): |
| 247 |
from imp import get_suffixes, C_EXTENSION |
| 248 |
for suffix, mode, typ in get_suffixes(): |
| 249 |
if typ == C_EXTENSION: |
| 250 |
return suffix |
| 251 |
else: |
| 252 |
raise RuntimeError("This interpreter does not define a filename " |
| 253 |
"suffix for C extensions!") |
| 254 |
|
| 255 |
def get_sys_info_app(base_dir): |
| 256 |
from distutils.sysconfig import get_python_inc |
| 257 |
if sys.platform == 'win32': |
| 258 |
compile_extra = ["/we4013"] |
| 259 |
link_extra = ["/LIBPATH:" + os.path.join(sys.exec_prefix, 'libs')] |
| 260 |
elif sys.platform.startswith('linux'): |
| 261 |
compile_extra = [ |
| 262 |
"-O0", "-g", "-Werror=implicit-function-declaration", "-fPIC"] |
| 263 |
link_extra = None |
| 264 |
else: |
| 265 |
compile_extra = link_extra = None |
| 266 |
pass |
| 267 |
return ExtensionCompiler( |
| 268 |
builddir_base=base_dir, |
| 269 |
include_extra=[get_python_inc()], |
| 270 |
compile_extra=compile_extra, |
| 271 |
link_extra=link_extra, |
| 272 |
ext=get_so_suffix()) |