33import shutil
44from . import spawn
55from . import generate_manifest
6- from . import update_mphalport
6+ from . import update_mphalport as _update_mphalport
77from argparse import ArgumentParser
88
99
@@ -73,20 +73,22 @@ def build_commands(_, extra_args, script_dir, lv_cflags, board):
7373 if board :
7474 unix_cmd .append (f'VARIANT={ board } ' )
7575
76- unix_cmd .extend ([
77- f'LV_CFLAGS="{ lv_cflags } "' ,
78- f'LV_PORT=unix' ,
79- f'USER_C_MODULES="{ script_dir } /ext_mod"' ,
80- (
81- '"CFLAGS_EXTRA='
82- '-Wno-sign-compare '
83- '-Wno-unused-function '
84- '-Wno-double-promotion '
85- '-Wno-unused-command-line-argument '
86- '-Wno-missing-field-initializers"'
87- # 'export CPPFLAGS="-I/opt/homebrew/opt/libffi/include"'
88- )
89- ])
76+ unix_cmd .extend (
77+ [
78+ f'LV_CFLAGS="{ lv_cflags } "' ,
79+ f'LV_PORT=unix' ,
80+ f'USER_C_MODULES="{ script_dir } /ext_mod"' ,
81+ (
82+ '"CFLAGS_EXTRA='
83+ '-Wno-sign-compare '
84+ '-Wno-unused-function '
85+ '-Wno-double-promotion '
86+ '-Wno-unused-command-line-argument '
87+ '-Wno-missing-field-initializers"'
88+ # 'export CPPFLAGS="-I/opt/homebrew/opt/libffi/include"'
89+ )
90+ ]
91+ )
9092
9193 # unix_cmd.extend(extra_args)
9294
@@ -108,7 +110,7 @@ def build_manifest(
108110
109111 SCRIPT_PATH = script_dir
110112
111- update_mphalport (target )
113+ _update_mphalport (target )
112114
113115 manifest_path = 'lib/micropython/ports/unix/variants/manifest.py'
114116
@@ -226,10 +228,13 @@ def update_makefile():
226228 data = read_file (makefile_path )
227229
228230 if 'machine_timer.c' not in data :
229- data = data .replace (
230- 'SRC_C += \\ \n ' ,
231- 'SRC_C += \\ \n \t machine_timer.c\\ \n '
232- )
231+ code = [
232+ 'modjni.c \\ ' ,
233+ '\t machine_timer.c \\ ' ,
234+ '\t machine_sdl.c \\ ' ,
235+ ''
236+ ]
237+ data = data .replace ('modjni.c \\ \n ' , '\n ' .join (code ))
233238
234239 write_file (makefile_path , data )
235240
@@ -241,8 +246,9 @@ def update_modmachine():
241246
242247 if 'MICROPY_PY_MACHINE_EXTRA_GLOBALS' not in data :
243248 data += (
244- '\n #define MICROPY_PY_MACHINE_EXTRA_GLOBALS \\ \n '
245- ' { MP_ROM_QSTR(MP_QSTR_Timer), MP_ROM_PTR(&machine_timer_type) }, \\ \n ' # NOQA
249+ '\n #define MICROPY_PY_MACHINE_EXTRA_GLOBALS '
250+ ' { MP_ROM_QSTR(MP_QSTR_Timer), MP_ROM_PTR(&machine_timer_type) }, \n \n '
251+ # NOQA
246252 )
247253
248254 write_file (modmachine_path , data )
@@ -253,30 +259,67 @@ def update_main():
253259
254260 data = read_file (main_path )
255261
256- if 'modmachine .h' not in data :
257- data = data . replace (
262+ if 'machine_timer .h' not in data :
263+ code = [
258264 '#include "input.h"' ,
259- '#include "input.h"\n #include "modmachine.h"'
260- )
265+ '#include "machine_timer.h"'
266+ ]
267+ data = data .replace ('#include "input.h"' , '\n ' .join (code ))
268+
269+ if 'machine_sdl.h' not in data :
270+ code = [
271+ '#include "input.h"' ,
272+ '#include "machine_sdl.h"'
273+ ]
274+ data = data .replace ('#include "input.h"' , '\n ' .join (code ))
261275
262276 if 'machine_timer_deinit_all()' not in data :
263277 data = data .replace (
264278 '#if MICROPY_PY_SYS_ATEXIT' ,
265279 'machine_timer_deinit_all();\n #if MICROPY_PY_SYS_ATEXIT'
266280 )
267281
268- if 'lcd_bus_deinit_sdl ()' not in data :
282+ if 'deinit_sdl ()' not in data :
269283 data = data .replace (
270284 '#if MICROPY_PY_SYS_ATEXIT' ,
271- 'lcd_bus_deinit_sdl ();\n #if MICROPY_PY_SYS_ATEXIT'
285+ 'deinit_sdl ();\n #if MICROPY_PY_SYS_ATEXIT'
272286 )
273287
274- if 'lcd_bus_init_sdl ()' not in data :
288+ if 'init_sdl ()' not in data :
275289 data = data .replace (
276290 'mp_init();' ,
277- 'mp_init();\n lcd_bus_init_sdl ();'
291+ 'mp_init();\n init_sdl ();'
278292 )
279293
294+ if '*mp_repl_get_ps3' not in data :
295+ code = [
296+ 'char *mp_repl_get_ps3(void)' ,
297+ '{' ,
298+ ' return "";' ,
299+ '}' ,
300+ '' ,
301+ '' ,
302+ 'static int do_repl(void) {'
303+ ]
304+
305+ data = data .replace ('static int do_repl(void) {' , '\n ' .join (code ))
306+
307+ if 'EWOULDBLOCK' not in data :
308+ code = [
309+ 'if (errno != EWOULDBLOCK) {' ,
310+ ' return 0;' ,
311+ ' } else {' ,
312+ ' while (line == NULL && errno == EWOULDBLOCK) {' ,
313+ ' mp_handle_pending(true);' ,
314+ ' usleep(1000);' ,
315+ ' line = prompt(mp_repl_get_ps3());' ,
316+ ' }' ,
317+ ' if (line == NULL) return 0;' ,
318+ ' }' ,
319+ ]
320+
321+ data = data .replace ('// EOF\n return 0;' , '\n ' .join (code ))
322+
280323 data = data .split ('\n ' )
281324
282325 for i , line in enumerate (data ):
@@ -289,6 +332,24 @@ def update_main():
289332 write_file (main_path , data )
290333
291334
335+ def update_input ():
336+ input_path = 'lib/micropython/ports/unix/input.c'
337+
338+ data = read_file (input_path )
339+ if 'O_NONBLOCK' not in data :
340+ code = [
341+ 'char *prompt(char *p) {' ,
342+ ' int stdin_fd = fileno(stdin);' ,
343+ ' int flags = fcntl(stdin_fd, F_GETFL);' ,
344+ ' flags |= O_NONBLOCK;' ,
345+ ' fcntl(stdin_fd, F_SETFL, flags);' ,
346+ ''
347+ ]
348+
349+ data = data .replace ('char *prompt(char *p) {' , '\n ' .join (code ))
350+ write_file (input_path , data )
351+
352+
292353def update_mpconfigvariant_common ():
293354 mpconfigvariant_common_path = (
294355 'lib/micropython/ports/unix/variants/mpconfigvariant_common.h'
@@ -311,7 +372,7 @@ def update_mpconfigvariant_common():
311372 )
312373
313374 macros = (
314- '#define MICROPY_SCHEDULER_DEPTH (128)'
375+ '#define MICROPY_SCHEDULER_DEPTH (128)' ,
315376 '#define MICROPY_STACK_CHECK (0)'
316377 )
317378
@@ -323,12 +384,27 @@ def update_mpconfigvariant_common():
323384 write_file (mpconfigvariant_common_path , data )
324385
325386
387+ def update_mpconfigport_mk ():
388+ mpconfigport_path = 'lib/micropython/ports/unix/mpconfigport.mk'
389+
390+ data = read_file (mpconfigport_path )
391+ if 'MICROPY_USE_READLINE = 1' in data :
392+ data = data .replace (
393+ 'MICROPY_USE_READLINE = 1' ,
394+ 'MICROPY_USE_READLINE = 0'
395+ )
396+
397+ write_file (mpconfigport_path , data )
398+
399+
326400def compile (* args ): # NOQA
327401
328402 update_makefile ()
329403 update_modmachine ()
330404 update_main ()
331405 update_mpconfigvariant_common ()
406+ update_input ()
407+ update_mpconfigport_mk ()
332408 copy_updated_files ()
333409
334410 build_sdl ()
0 commit comments