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(), ".."))
12libdir = os.environ.get("LIBDIR")
13
14cpp = shlex.split(os.environ.get("CPP", "cc -E"))
15cppflags = shlex.split(os.environ.get("CPPFLAGS", ""))
16cppflags.extend(["-I" + incdir, "-I" + srcdir, "-I" + bindir])
17
18ffi.set_source("mgba._pylib", """
19#define static
20#define inline
21#include <mgba/flags.h>
22#define OPAQUE_THREADING
23#include <mgba/core/blip_buf.h>
24#include <mgba/core/cache-set.h>
25#include <mgba-util/common.h>
26#include <mgba/core/core.h>
27#include <mgba/core/map-cache.h>
28#include <mgba/core/log.h>
29#include <mgba/core/mem-search.h>
30#include <mgba/core/thread.h>
31#include <mgba/core/version.h>
32#include <mgba/debugger/debugger.h>
33#include <mgba/gba/interface.h>
34#include <mgba/internal/arm/arm.h>
35#include <mgba/internal/debugger/cli-debugger.h>
36#include <mgba/internal/gba/gba.h>
37#include <mgba/internal/gba/input.h>
38#include <mgba/internal/gba/renderers/cache-set.h>
39#include <mgba/internal/lr35902/lr35902.h>
40#include <mgba/internal/gb/gb.h>
41#include <mgba/internal/gb/renderers/cache-set.h>
42#include <mgba-util/png-io.h>
43#include <mgba-util/vfs.h>
44
45#define PYEXPORT
46#include "platform/python/core.h"
47#include "platform/python/log.h"
48#include "platform/python/sio.h"
49#include "platform/python/vfs-py.h"
50#undef PYEXPORT
51""", include_dirs=[incdir, srcdir],
52 extra_compile_args=cppflags,
53 libraries=["mgba"],
54 library_dirs=[bindir],
55 runtime_library_dirs=[libdir],
56 sources=[os.path.join(pydir, path) for path in ["vfs-py.c", "core.c", "log.c", "sio.c"]])
57
58preprocessed = subprocess.check_output(cpp + ["-fno-inline", "-P"] + cppflags + [os.path.join(pydir, "_builder.h")], universal_newlines=True)
59
60lines = []
61for line in preprocessed.splitlines():
62 line = line.strip()
63 if line.startswith('#'):
64 continue
65 lines.append(line)
66ffi.cdef('\n'.join(lines))
67
68ffi.cdef("""
69struct GBARTC {
70 int32_t bytesRemaining;
71 int32_t transferStep;
72 int32_t bitsRead;
73 int32_t bits;
74 int32_t commandActive;
75 RTCCommandData command;
76 RTCControl control;
77 uint8_t time[7];
78};""", packed=True)
79
80preprocessed = subprocess.check_output(cpp + ["-fno-inline", "-P"] + cppflags + [os.path.join(pydir, "lib.h")], universal_newlines=True)
81
82lines = []
83for line in preprocessed.splitlines():
84 line = line.strip()
85 if line.startswith('#'):
86 continue
87 lines.append(line)
88ffi.embedding_api('\n'.join(lines))
89
90ffi.embedding_init_code("""
91 import os, os.path
92 from mgba._pylib import ffi, lib
93 symbols = {}
94 globalSyms = {
95 'symbols': symbols
96 }
97 pendingCode = []
98
99 @ffi.def_extern()
100 def mPythonSetDebugger(debugger):
101 from mgba.debugger import NativeDebugger, CLIDebugger
102 oldDebugger = globalSyms.get('debugger')
103 if oldDebugger and oldDebugger._native == debugger:
104 return
105 if oldDebugger and not debugger:
106 del globalSyms['debugger']
107 return
108 if debugger.type == lib.DEBUGGER_CLI:
109 debugger = CLIDebugger(debugger)
110 else:
111 debugger = NativeDebugger(debugger)
112 globalSyms['debugger'] = debugger
113
114 @ffi.def_extern()
115 def mPythonLoadScript(name, vf):
116 from mgba.vfs import VFile
117 vf = VFile(vf)
118 name = ffi.string(name)
119 source = vf.read_all().decode('utf-8')
120 try:
121 code = compile(source, name, 'exec')
122 pendingCode.append(code)
123 except:
124 return False
125 return True
126
127 @ffi.def_extern()
128 def mPythonRunPending():
129 global pendingCode
130 for code in pendingCode:
131 exec(code, globalSyms, {})
132 pendingCode = []
133
134 @ffi.def_extern()
135 def mPythonDebuggerEntered(reason, info):
136 debugger = globalSyms['debugger']
137 if not debugger:
138 return
139 if info == ffi.NULL:
140 info = None
141 for cb in debugger._cbs:
142 cb(reason, info)
143
144 @ffi.def_extern()
145 def mPythonLookupSymbol(name, outptr):
146 name = ffi.string(name).decode('utf-8')
147 if name not in symbols:
148 return False
149 sym = symbols[name]
150 val = None
151 try:
152 val = int(sym)
153 except:
154 try:
155 val = sym()
156 except:
157 pass
158 if val is None:
159 return False
160 try:
161 outptr[0] = ffi.cast('int32_t', val)
162 return True
163 except:
164 return False
165""")
166
167if __name__ == "__main__":
168 ffi.emit_c_code("lib.c")