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 "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
68preprocessed = subprocess.check_output(cpp + ["-fno-inline", "-P"] + cppflags + [os.path.join(pydir, "lib.h")], universal_newlines=True)
69
70lines = []
71for line in preprocessed.splitlines():
72 line = line.strip()
73 if line.startswith('#'):
74 continue
75 lines.append(line)
76ffi.embedding_api('\n'.join(lines))
77
78ffi.embedding_init_code("""
79 import os, os.path
80 from mgba._pylib import ffi, lib
81 symbols = {}
82 globalSyms = {
83 'symbols': symbols
84 }
85 pendingCode = []
86
87 @ffi.def_extern()
88 def mPythonSetDebugger(debugger):
89 from mgba.debugger import NativeDebugger, CLIDebugger
90 oldDebugger = globalSyms.get('debugger')
91 if oldDebugger and oldDebugger._native == debugger:
92 return
93 if oldDebugger and not debugger:
94 del globalSyms['debugger']
95 return
96 if debugger.type == lib.DEBUGGER_CLI:
97 debugger = CLIDebugger(debugger)
98 else:
99 debugger = NativeDebugger(debugger)
100 globalSyms['debugger'] = debugger
101
102 @ffi.def_extern()
103 def mPythonLoadScript(name, vf):
104 from mgba.vfs import VFile
105 vf = VFile(vf)
106 name = ffi.string(name)
107 source = vf.read_all().decode('utf-8')
108 try:
109 code = compile(source, name, 'exec')
110 pendingCode.append(code)
111 except:
112 return False
113 return True
114
115 @ffi.def_extern()
116 def mPythonRunPending():
117 global pendingCode
118 for code in pendingCode:
119 exec(code, globalSyms, {})
120 pendingCode = []
121
122 @ffi.def_extern()
123 def mPythonDebuggerEntered(reason, info):
124 debugger = globalSyms['debugger']
125 if not debugger:
126 return
127 if info == ffi.NULL:
128 info = None
129 for cb in debugger._cbs:
130 cb(reason, info)
131
132 @ffi.def_extern()
133 def mPythonLookupSymbol(name, outptr):
134 name = ffi.string(name).decode('utf-8')
135 if name not in symbols:
136 return False
137 sym = symbols[name]
138 val = None
139 try:
140 val = int(sym)
141 except:
142 try:
143 val = sym()
144 except:
145 pass
146 if val is None:
147 return False
148 try:
149 outptr[0] = ffi.cast('int32_t', val)
150 return True
151 except:
152 return False
153""")
154
155if __name__ == "__main__":
156 ffi.emit_c_code("lib.c")