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