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/tile-cache.h>
25#include <mgba/internal/arm/arm.h>
26#include <mgba/internal/gba/gba.h>
27#include <mgba/internal/gba/input.h>
28#include <mgba/internal/gba/renderers/tile-cache.h>
29#include <mgba/internal/lr35902/lr35902.h>
30#include <mgba/internal/gb/gb.h>
31#include <mgba/internal/gb/renderers/tile-cache.h>
32#include <mgba-util/png-io.h>
33#include <mgba-util/vfs.h>
34
35#define PYEXPORT
36#include "platform/python/log.h"
37#include "platform/python/sio.h"
38#include "platform/python/vfs-py.h"
39#undef PYEXPORT
40""", include_dirs=[incdir, srcdir],
41 extra_compile_args=cppflags,
42 libraries=["mgba"],
43 library_dirs=[bindir],
44 sources=[os.path.join(pydir, path) for path in ["vfs-py.c", "log.c", "sio.c"]])
45
46preprocessed = subprocess.check_output(cpp + ["-fno-inline", "-P"] + cppflags + [os.path.join(pydir, "_builder.h")], universal_newlines=True)
47
48lines = []
49for line in preprocessed.splitlines():
50 line = line.strip()
51 if line.startswith('#'):
52 continue
53 lines.append(line)
54ffi.cdef('\n'.join(lines))
55
56if __name__ == "__main__":
57 ffi.compile()