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