1
0
Fork 0
mirror of https://github.com/deltachat/deltachat-core.git synced 2025-10-06 03:50:08 +02:00
deltachat-core/python/src/deltachat/_build.py
Floris Bruynooghe 0fd55137f6 Improve building of extension a little
- This renames deltachat.types to deltachat.props.  Not re-using a
  stdlib name is somewhat nice and solves a weird edge-case for me where
  the deltachat.types module can be hiding stdlib types by the deltachat
  directory appearing before the stlib on sys.path.

- When manually invoking the build script (rather then via
  pip/setup.py) it figures out where the package actually lives and
  makes sure the .so file ends up in that directory.  This is better
  than the current version which depends on the current directory and
  thus sometimes creates an alternate deltachat/ directory tree.

- Add some common python artefact files to gitignore.
2018-11-01 18:12:06 +02:00

63 lines
1.9 KiB
Python

import distutils.ccompiler
import distutils.sysconfig
import tempfile
import cffi
def ffibuilder():
builder = cffi.FFI()
builder.set_source(
'deltachat.capi',
"""
#include <deltachat/deltachat.h>
const char * dupstring_helper(const char* string)
{
return strdup(string);
}
int dc_get_event_signature_types(int e)
{
int result = 0;
if (DC_EVENT_DATA1_IS_STRING(e))
result |= 1;
if (DC_EVENT_DATA2_IS_STRING(e))
result |= 2;
if (DC_EVENT_RETURNS_STRING(e))
result |= 4;
if (DC_EVENT_RETURNS_INT(e))
result |= 8;
return result;
}
""",
libraries=['deltachat'],
)
builder.cdef("""
typedef int... time_t;
void free(void *ptr);
extern const char * dupstring_helper(const char* string);
extern int dc_get_event_signature_types(int);
""")
cc = distutils.ccompiler.new_compiler(force=True)
distutils.sysconfig.customize_compiler(cc)
with tempfile.NamedTemporaryFile(mode='w', suffix='.h') as src_fp:
src_fp.write('#include <deltachat/deltachat.h>')
src_fp.flush()
with tempfile.NamedTemporaryFile(mode='r') as dst_fp:
cc.preprocess(source=src_fp.name,
output_file=dst_fp.name,
macros=[('PY_CFFI', '1')])
builder.cdef(dst_fp.read())
builder.cdef("""
extern "Python" uintptr_t py_dc_callback(
dc_context_t* context,
int event,
uintptr_t data1,
uintptr_t data2);
""")
return builder
if __name__ == '__main__':
import os.path
pkgdir = os.path.join(os.path.dirname(__file__), '..')
builder = ffibuilder()
builder.compile(tmpdir=pkgdir, verbose=True)