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
35struct VFile* VFileFromPython(void* fileobj);
36
37struct VFilePy {
38 struct VFile d;
39 void* fileobj;
40};
41
42struct mLogger* mLoggerPythonCreate(void* pyobj);
43
44struct mLoggerPy {
45 struct mLogger d;
46 void* pyobj;
47};
48""", include_dirs=[incdir, srcdir],
49 extra_compile_args=cppflags,
50 libraries=["mgba"],
51 library_dirs=[bindir],
52 sources=[os.path.join(pydir, path) for path in ["vfs-py.c", "log.c"]])
53
54preprocessed = subprocess.check_output(cpp + ["-fno-inline", "-P"] + cppflags + [os.path.join(pydir, "_builder.h")], universal_newlines=True)
55
56lines = []
57for line in preprocessed.splitlines():
58 line = line.strip()
59 if line.startswith('#'):
60 continue
61 lines.append(line)
62ffi.cdef('\n'.join(lines))
63
64if __name__ == "__main__":
65 ffi.compile()