Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "buildtool" to generate C source for cffi-based modules. #76

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,9 @@ def has_ext_modules(self):
"distutils.setup_keywords": [
"cffi_modules = cffi.setuptools_ext:cffi_modules",
],
'console_scripts': [
'cffi-buildtool = cffi.buildtool:main',
]
},

classifiers=[
Expand Down
71 changes: 71 additions & 0 deletions src/cffi/buildtool.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import argparse
import pathlib

from cffi.api import FFI
from cffi.recompiler import Recompiler, NativeIO

parser = argparse.ArgumentParser(
description="Generate source for a C module from the cffi build script."
)
parser.add_argument("--ffi_var_name", default="ffibuilder")
parser.add_argument("infile", type=pathlib.Path)
parser.add_argument("outfile", type=pathlib.Path)


class BuildError(Exception):
__module__ = "cffi"


def execfile(filename, glob):
# We use execfile() (here rewritten for Python 3) instead of
# __import__() to load the build script. The problem with
# a normal import is that in some packages, the intermediate
# __init__.py files may already try to import the file that
# we are generating.
with open(filename) as f:
src = f.read()
src += "\n" # Python 2.6 compatibility
code = compile(src, filename, "exec")
exec(code, glob, glob)


def get_ffi(filename, ffi_var_name):
globs = {}
execfile(filename, globs)
if ffi_var_name not in globs:
raise BuildError("%r: object %r not found in module" % (filename, ffi_var_name))
ffi = globs[ffi_var_name]
if not isinstance(ffi, FFI) and callable(ffi):
# Maybe it's a callable that returns a FFI
ffi = ffi()
if not isinstance(ffi, FFI):
raise TypeError(
"%r is not an FFI instance (got %r)" % (filename, type(ffi).__name__)
)
return ffi


def generate_c_source(ffi):
"""Generate C module source from a FFI instance.

Example of use:
if __name__ == "__main__":
from cffi.buildtool import generate_c_source
print(generate_c_source(ffibuilder))
"""
# TODO: improve this; https://github.com/python-cffi/cffi/issues/47
module_name, source, source_extension, kwds = ffi._assigned_source
recompiler = Recompiler(ffi, module_name)
recompiler.collect_type_table()
recompiler.collect_step_tables()
f = NativeIO()
recompiler.write_source_to_f(f, source)
return f.getvalue()


def main():
args = parser.parse_args()
ffi = get_ffi(args.infile, args.ffi_var_name)
output = generate_c_source(ffi)
with args.outfile.open("w") as f:
f.write(output)