summaryrefslogtreecommitdiffstats
path: root/ffi/lang/python/sequoia/sequoia_build.py
blob: ea0ad9df8c388270e9b0ea9b22ac3960e672eebd (plain)
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
from os.path import join, dirname
from cffi import FFI, error
from itertools import chain

sq_inc = join(dirname(__file__), '../../../include/sequoia')
pgp_inc = join(dirname(__file__), '../../../../openpgp-ffi/include/sequoia')
defs = "".join(l
               for l in chain(open(join(pgp_inc, "openpgp/error.h")).readlines(),
                              open(join(pgp_inc, "io.h")).readlines(),
                              open(join(pgp_inc, "openpgp/crypto.h")).readlines(),
                              open(join(pgp_inc, "openpgp.h")).readlines(),
                              open(join(sq_inc, "core.h")).readlines(),
                              open(join(sq_inc, "net.h")).readlines(),
                              open(join(sq_inc, "store.h")).readlines())
               if not l.startswith('#'))

defs = defs.replace("INT_MAX", "{}".format(1<<31))

ffibuilder = FFI()
ffibuilder.set_source('_sequoia',
                      '#include <sequoia.h>',
                      libraries=['sequoia_ffi'])

# cffi magic to make time_t work.
ffibuilder.cdef('typedef int... time_t;')

# free(3)
ffibuilder.cdef('void free (void *ptr);')

try:
    ffibuilder.cdef(defs, override=True)
except error.CDefError as e:
    try:
        current_decl = e.args[1]
        linenum = current_decl.coord.line

        print("ATTENTION:  Line numbers are not very reliable :(")
        print()
        print("{}: {}".format(linenum, e.args[0]))
        print()

        for i, l in enumerate(defs.split("\n")):
            if i < linenum - 5 or i > linenum + 5:
                continue
            print("{}: {}".format(i + 1, l))
        print()

        print("AST:", e.args[1])
        print()
    except (AttributeError, TypeError, IndexError):
        pass

    raise

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