src/platform/python/_builder.py (view raw)
1import cffi
2import os, os.path
3import shlex
4import subprocess
5import sys
6
7ffi = cffi.FFI()
8pydir = os.path.dirname(os.path.abspath(__file__))
9srcdir = os.path.join(pydir, "..", "..")
10incdir = os.path.join(pydir, "..", "..", "..", "include")
11bindir = os.environ.get("BINDIR", os.path.join(os.getcwd(), ".."))
12
13cpp = shlex.split(os.environ.get("CPP", "cc -E"))
14cppflags = shlex.split(os.environ.get("CPPFLAGS", ""))
15if __name__ == "__main__":
16 cppflags.extend(sys.argv[1:])
17cppflags.extend(["-I" + incdir, "-I" + srcdir, "-I" + bindir])
18
19ffi.set_source("mgba._pylib", """
20#include "flags.h"
21#include <mgba-util/common.h>
22#include <mgba/core/core.h>
23#include <mgba/core/log.h>
24#include <mgba/core/mem-search.h>
25#include <mgba/core/tile-cache.h>
26#include <mgba/internal/arm/arm.h>
27#include <mgba/internal/gba/gba.h>
28#include <mgba/internal/gba/input.h>
29#include <mgba/internal/gba/renderers/tile-cache.h>
30#include <mgba/internal/lr35902/lr35902.h>
31#include <mgba/internal/gb/gb.h>
32#include <mgba/internal/gb/renderers/tile-cache.h>
33#include <mgba-util/png-io.h>
34#include <mgba-util/vfs.h>
35
36#define PYEXPORT
37#include "platform/python/log.h"
38#include "platform/python/sio.h"
39#include "platform/python/vfs-py.h"
40#undef PYEXPORT
41""", include_dirs=[incdir, srcdir],
42 extra_compile_args=cppflags,
43 libraries=["mgba"],
44 library_dirs=[bindir],
45 sources=[os.path.join(pydir, path) for path in ["vfs-py.c", "log.c", "sio.c"]])
46
47preprocessed = subprocess.check_output(cpp + ["-fno-inline", "-P"] + cppflags + [os.path.join(pydir, "_builder.h")], universal_newlines=True)
48
49lines = []
50for line in preprocessed.splitlines():
51 line = line.strip()
52 if line.startswith('#'):
53 continue
54 lines.append(line)
55ffi.cdef('\n'.join(lines))
56
57if __name__ == "__main__":
58 ffi.compile()