unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
blob e9c446f600d2a534e88c95c6a8e795f641d403b1 2380 bytes (raw)
name: bindings/python-cffi/notmuch2/_build.py 	 # note: path name is non-authoritative(*)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
 
import distutils.ccompiler
import distutils.log
import distutils.sysconfig
import os
import shutil
import tempfile

import cffi


# Also look in these directories for include files, i.e. notmuch.h (-I)
INCLUDE_DIRS = ["../../lib"]

# Also look in these directories for libraries, i.e. libnotmuch.so.5 (-L)
LIBRARY_DIRS = ["../../lib"]

# Add extra linker flags, e.g. -Wl,--enable-new-dtags,-R/path/to/notmuch/libdir
EXTRA_LINK_ARGS = ["-Wl,--enable-new-dtags,-R/home/flub/Projects/notmuch/lib"]


def extract_functions():
    """Extract the function definitions from notmuch.h.

    This creates a .h file with a single `#include <notmuch.h>` line
    in it.  It then runs the C preprocessor with the PY_CFFI symbol
    defined to create an output file which contains all function
    definitions found in `notmuch.h`.
    """
    distutils.log.set_verbosity(distutils.log.INFO)
    cc = distutils.ccompiler.new_compiler(force=True)
    distutils.sysconfig.customize_compiler(cc)
    tmpdir = tempfile.mkdtemp()
    try:
        src_name = os.path.join(tmpdir, "include.h")
        dst_name = os.path.join(tmpdir, "expanded.h")
        with open(src_name, "w") as src_fp:
            src_fp.write("#include <notmuch.h>")
        cc.preprocess(
            source=src_name,
            output_file=dst_name,
            include_dirs=INCLUDE_DIRS,
            macros=[("PY_CFFI", "1")],
        )
        with open(dst_name, "r") as dst_fp:
            return dst_fp.read()
    finally:
        shutil.rmtree(tmpdir)


ffibuilder = cffi.FFI()
ffibuilder.set_source(
    "notmuch2._capi",
    r"""
    #include <stdlib.h>
    #include <time.h>
    #include <notmuch.h>

    #if LIBNOTMUCH_MAJOR_VERSION < 5
        #error libnotmuch version not supported by notmuch2 python bindings
    #endif
    #if LIBNOTMUCH_MINOR_VERSION < 1
        #ERROR libnotmuch  version < 5.1 not supported
    #endif
    """,
    include_dirs=INCLUDE_DIRS,
    library_dirs=LIBRARY_DIRS,
    extra_link_args=EXTRA_LINK_ARGS,
    libraries=["notmuch"],
)
ffibuilder.cdef(
    r"""
    void free(void *ptr);
    typedef int... time_t;

    #define LIBNOTMUCH_MAJOR_VERSION ...
    #define LIBNOTMUCH_MINOR_VERSION ...
    #define LIBNOTMUCH_MICRO_VERSION ...

    #define NOTMUCH_TAG_MAX ...
    """
)
ffibuilder.cdef(extract_functions())


if __name__ == "__main__":
    ffibuilder.compile(verbose=True)

debug log:

solving e9c446f6 ...
found e9c446f6 in https://yhetil.org/notmuch/20201122162353.63824-2-flub@devork.be/
found f269f2a1 in https://yhetil.org/notmuch.git/
preparing index
index prepared:
100644 f269f2a195b501437cbc70e932dea48fe1f5e5fa	bindings/python-cffi/notmuch2/_build.py

applying [1/1] https://yhetil.org/notmuch/20201122162353.63824-2-flub@devork.be/
diff --git a/bindings/python-cffi/notmuch2/_build.py b/bindings/python-cffi/notmuch2/_build.py
index f269f2a1..e9c446f6 100644

Checking patch bindings/python-cffi/notmuch2/_build.py...
Applied patch bindings/python-cffi/notmuch2/_build.py cleanly.

index at:
100644 e9c446f600d2a534e88c95c6a8e795f641d403b1	bindings/python-cffi/notmuch2/_build.py

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

	https://yhetil.org/notmuch.git/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).