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#define OPAQUE_THREADING
22#include <mgba-util/common.h>
23#include <mgba/core/core.h>
24#include <mgba/core/log.h>
25#include <mgba/core/mem-search.h>
26#include <mgba/core/thread.h>
27#include <mgba/core/tile-cache.h>
28#include <mgba/core/version.h>
29#include <mgba/debugger/debugger.h>
30#include <mgba/internal/arm/arm.h>
31#include <mgba/internal/gba/gba.h>
32#include <mgba/internal/gba/input.h>
33#include <mgba/internal/gba/renderers/tile-cache.h>
34#include <mgba/internal/lr35902/lr35902.h>
35#include <mgba/internal/gb/gb.h>
36#include <mgba/internal/gb/renderers/tile-cache.h>
37#include <mgba-util/png-io.h>
38#include <mgba-util/vfs.h>
39
40#define PYEXPORT
41#include "platform/python/log.h"
42#include "platform/python/sio.h"
43#include "platform/python/vfs-py.h"
44#undef PYEXPORT
45""", include_dirs=[incdir, srcdir],
46 extra_compile_args=cppflags,
47 libraries=["mgba"],
48 library_dirs=[bindir],
49 sources=[os.path.join(pydir, path) for path in ["vfs-py.c", "log.c", "sio.c"]])
50
51preprocessed = subprocess.check_output(cpp + ["-fno-inline", "-P"] + cppflags + [os.path.join(pydir, "_builder.h")], universal_newlines=True)
52
53lines = []
54for line in preprocessed.splitlines():
55 line = line.strip()
56 if line.startswith('#'):
57 continue
58 lines.append(line)
59ffi.cdef('\n'.join(lines))
60
61preprocessed = subprocess.check_output(cpp + ["-fno-inline", "-P"] + cppflags + [os.path.join(pydir, "lib.h")], universal_newlines=True)
62
63lines = []
64for line in preprocessed.splitlines():
65 line = line.strip()
66 if line.startswith('#'):
67 continue
68 lines.append(line)
69ffi.embedding_api('\n'.join(lines))
70
71ffi.embedding_init_code("""
72 from mgba._pylib import ffi
73 debugger = None
74 pendingCode = []
75
76 @ffi.def_extern()
77 def mPythonSetDebugger(_debugger):
78 from mgba.debugger import NativeDebugger
79 global debugger
80 debugger = _debugger and NativeDebugger(_debugger)
81
82 @ffi.def_extern()
83 def mPythonLoadScript(name, vf):
84 from mgba.vfs import VFile
85 vf = VFile(vf)
86 name = ffi.string(name)
87 source = vf.readAll().decode('utf-8')
88 try:
89 code = compile(source, name, 'exec')
90 pendingCode.append(code)
91 except:
92 return False
93 return True
94
95 @ffi.def_extern()
96 def mPythonRunPending():
97 global pendingCode
98 for code in pendingCode:
99 exec(code)
100 pendingCode = []
101""")
102
103if __name__ == "__main__":
104 ffi.emit_c_code("lib.c")