Skip to content

Commit 0411841

Browse files
committed
bricks/ev3dev: merge changes from upstream unix port
This merges some changes to makefiles and config files from the upstream unix port.
1 parent dbbbbc7 commit 0411841

File tree

6 files changed

+114
-65
lines changed

6 files changed

+114
-65
lines changed

bricks/ev3dev/Makefile

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,17 @@ USER_C_MODULES = ../..
3939
-include mpconfigport.mk
4040
include ../../micropython/py/mkenv.mk
4141

42-
FROZEN_DIR = scripts
43-
FROZEN_MPY_DIR = modules
44-
QSTR_GLOBAL_DEPENDENCIES = brickconfig.h
42+
# use FROZEN_MANIFEST for new projects, others are legacy
43+
FROZEN_MANIFEST ?= manifest.py
44+
FROZEN_DIR =
45+
FROZEN_MPY_DIR =
4546

4647
# define main target
4748
PROG = pybricks-micropython
4849

4950
# qstr definitions (must come before including py.mk)
5051
#QSTR_DEFS = qstrdefsport.h
52+
QSTR_GLOBAL_DEPENDENCIES =
5153

5254
# OS name, for simple autoconfig
5355
UNAME_S := $(shell uname -s)
@@ -72,15 +74,23 @@ INC += -I$(TOP)/ports/unix
7274

7375
# compiler settings
7476
CWARN = -Wall -Werror
75-
CWARN += -Wpointer-arith -Wuninitialized
76-
CFLAGS = $(INC) $(CWARN) -std=gnu99 -DUNIX $(CFLAGS_MOD) $(COPT) $(CFLAGS_EXTRA)
77+
CWARN += -Wpointer-arith -Wuninitialized -Wdouble-promotion -Wsign-compare -Wfloat-conversion
78+
CFLAGS += $(INC) $(CWARN) -std=gnu99 -DUNIX $(CFLAGS_MOD) $(COPT) $(CFLAGS_EXTRA)
7779

7880
# Debugging/Optimization
7981
ifdef DEBUG
80-
CFLAGS += -g
81-
COPT = -O0
82+
COPT ?= -O0
8283
else
83-
COPT = -Os -fdata-sections -ffunction-sections -DNDEBUG
84+
COPT ?= -Os
85+
COPT += -fdata-sections -ffunction-sections
86+
COPT += -DNDEBUG
87+
endif
88+
89+
# Always enable symbols -- They're occasionally useful, and don't make it into the
90+
# final .bin/.hex/.dfu so the extra size doesn't matter.
91+
CFLAGS += -g
92+
93+
ifndef DEBUG
8494
# _FORTIFY_SOURCE is a feature in gcc/glibc which is intended to provide extra
8595
# security for detecting buffer overflows. Some distros (Ubuntu at the very least)
8696
# have it enabled by default.
@@ -124,7 +134,7 @@ else
124134
# Use gcc syntax for map file
125135
LDFLAGS_ARCH = -Wl,-Map=$@.map,--cref -Wl,--gc-sections
126136
endif
127-
LDFLAGS = $(LDFLAGS_MOD) $(LDFLAGS_ARCH) -lm $(LDFLAGS_EXTRA)
137+
LDFLAGS += $(LDFLAGS_MOD) $(LDFLAGS_ARCH) -lm $(LDFLAGS_EXTRA)
128138

129139
# Flags to link with pthread library
130140
LDFLAGS += -lpthread
@@ -183,20 +193,19 @@ endif
183193

184194
# source files
185195
SRC_C = $(addprefix micropython/ports/unix/,\
186-
main.c \
196+
alloc.c \
197+
coverage.c \
187198
gccollect.c \
188-
mpthreadport.c \
189199
input.c \
190-
file.c \
200+
main.c \
191201
modmachine.c \
192202
modos.c \
193-
modmmap.c \
194-
moduos_vfs.c \
195203
modtime.c \
196204
modufcntl.c \
205+
modummap.c \
206+
moduos_vfs.c \
197207
moduselect.c \
198-
alloc.c \
199-
coverage.c \
208+
mpthreadport.c \
200209
)
201210

202211
SRC_C += $(SRC_MOD)
@@ -216,6 +225,7 @@ PYBRICKS_SRC_C += \
216225
LIB_SRC_C = $(addprefix micropython/lib/,\
217226
$(LIB_SRC_C_EXTRA) \
218227
timeutils/timeutils.c \
228+
utils/gchelper_generic.c \
219229
utils/pyexec.c \
220230
)
221231

@@ -288,26 +298,32 @@ PYBRICKS_LIB_SRC_C = $(addprefix lib/,\
288298
OBJ = $(PY_O)
289299
OBJ += $(addprefix $(BUILD)/, $(SRC_C:.c=.o))
290300
OBJ += $(addprefix $(BUILD)/, $(LIB_SRC_C:.c=.o))
301+
OBJ += $(addprefix $(BUILD)/, $(EXTMOD_SRC_C:.c=.o))
291302
OBJ += $(addprefix $(BUILD)/, $(PYBRICKS_SRC_C:.c=.o))
292303
OBJ += $(addprefix $(BUILD)/, $(PYBRICKS_PY_SRC_C:.c=.o))
293304
OBJ += $(addprefix $(BUILD)/, $(PYBRICKS_EXTMOD_SRC_C:.c=.o))
294305
OBJ += $(addprefix $(BUILD)/, $(PYBRICKS_LIB_SRC_C:.c=.o))
295306

296307
# List of sources for qstr extraction
297-
SRC_QSTR += $(SRC_C) $(LIB_SRC_C) $(PYBRICKS_SRC_C) $(PYBRICKS_PY_SRC_C) $(PYBRICKS_EXTMOD_SRC_C)
308+
SRC_QSTR += $(SRC_C) $(LIB_SRC_C) $(EXTMOD_SRC_C) $(PYBRICKS_SRC_C) $(PYBRICKS_PY_SRC_C) $(PYBRICKS_EXTMOD_SRC_C)
298309
# Append any auto-generated sources that are needed by sources listed in
299310
# SRC_QSTR
300311
SRC_QSTR_AUTO_DEPS +=
301312

302-
ifneq ($(FROZEN_MPY_DIR),)
303-
# To use frozen bytecode, put your .py files in a subdirectory (eg frozen/) and
304-
# then invoke make with FROZEN_MPY_DIR=frozen (be sure to build from scratch).
313+
ifneq ($(FROZEN_MANIFEST)$(FROZEN_MPY_DIR),)
314+
# To use frozen code create a manifest.py file with a description of files to
315+
# freeze, then invoke make with FROZEN_MANIFEST=manifest.py (be sure to build from scratch).
305316
CFLAGS += -DMICROPY_QSTR_EXTRA_POOL=mp_qstr_frozen_const_pool
306317
CFLAGS += -DMICROPY_MODULE_FROZEN_MPY
307318
CFLAGS += -DMPZ_DIG_SIZE=16 # force 16 bits to work on both 32 and 64 bit archs
308319
MPY_CROSS_FLAGS += -mcache-lookup-bc
309320
endif
310321

322+
ifneq ($(FROZEN_MANIFEST)$(FROZEN_DIR),)
323+
CFLAGS += -DMICROPY_MODULE_FROZEN_STR
324+
endif
325+
326+
RUN_TESTS_MPY_CROSS_FLAGS = --mpy-cross-flags='-mcache-lookup-bc'
311327

312328
include $(TOP)/py/mkrules.mk
313329

@@ -342,15 +358,13 @@ test: $(PROG) $(TOP)/tests/run-tests $(GRX_TEST_PLUGIN_LIB)
342358
GRX_PLUGIN_PATH=$(realpath $(BUILD)) GRX_DRIVER=test \
343359
./run-tests
344360

345-
# install micropython in /usr/local/bin
346361
PREFIX = /usr/local
347362
BINDIR = $(DESTDIR)$(PREFIX)/bin
348363

349364
install: $(PROG)
350365
install -d $(BINDIR)
351366
install $(PROG) $(BINDIR)/$(PROG)
352367

353-
# uninstall micropython
354368
uninstall:
355369
-rm $(BINDIR)/$(PROG)
356370

bricks/ev3dev/brickconfig.h

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// SPDX-License-Identifier: MIT
22
// Copyright (c) 2018-2020 The Pybricks Authors
33

4-
#include <glib.h>
5-
64
#include <pbdrv/config.h>
75
#include "pbinit.h"
86

@@ -25,20 +23,6 @@
2523
#define MICROPY_PORT_INIT_FUNC pybricks_init()
2624
#define MICROPY_PORT_DEINIT_FUNC pybricks_deinit()
2725
#define MICROPY_MPHALPORT_H "ev3dev_mphal.h"
28-
#define MICROPY_VM_HOOK_LOOP do { \
29-
extern int pbio_do_one_event(void); \
30-
pbio_do_one_event(); \
31-
} while (0);
32-
#define MICROPY_EVENT_POLL_HOOK do { \
33-
extern void mp_handle_pending(bool); \
34-
mp_handle_pending(true); \
35-
extern int pbio_do_one_event(void); \
36-
while (pbio_do_one_event()) { } \
37-
MP_THREAD_GIL_EXIT(); \
38-
g_main_context_iteration(g_main_context_get_thread_default(), TRUE); \
39-
MP_THREAD_GIL_ENTER(); \
40-
} while (0);
41-
4226
#define MICROPY_PY_SYS_PATH_DEFAULT (":~/.pybricks-micropython/lib:/usr/lib/pybricks-micropython")
4327

4428
extern const struct _mp_obj_module_t pb_module_bluetooth;

bricks/ev3dev/ev3dev_mphal.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,21 @@ static inline void mp_hal_delay_us(mp_uint_t us) {
5757
#define RAISE_ERRNO(err_flag, error_val) \
5858
{ if (err_flag == -1) \
5959
{ mp_raise_OSError(error_val); } }
60+
61+
// This macro is used to implement PEP 475 to retry specified syscalls on EINTR
62+
#define MP_HAL_RETRY_SYSCALL(ret, syscall, raise) { \
63+
for (;;) { \
64+
MP_THREAD_GIL_EXIT(); \
65+
ret = syscall; \
66+
MP_THREAD_GIL_ENTER(); \
67+
if (ret == -1) { \
68+
int err = errno; \
69+
if (err == EINTR) { \
70+
mp_handle_pending(true); \
71+
continue; \
72+
} \
73+
raise; \
74+
} \
75+
break; \
76+
} \
77+
}

bricks/ev3dev/manifest.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
freeze_as_mpy("./modules")
2+
freeze_as_mpy("$(MPY_DIR)/tools", "upip.py")
3+
freeze_as_mpy("$(MPY_DIR)/tools", "upip_utarfile.py", opt=3)

bricks/ev3dev/mpconfigport.h

Lines changed: 54 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// SPDX-License-Identifier: MIT
22
// Copyright (c) 2013, 2014 Damien P. George
3+
// Copyright (c) 2018-2020 The Pybricks Authors
34

45
// Pybricks brick specific definitions
56
#include "brickconfig.h"
@@ -64,7 +65,9 @@ static const char pybricks_ev3dev_help_text[] =
6465
#ifndef MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE
6566
#define MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE (1)
6667
#endif
68+
#define MICROPY_MODULE_WEAK_LINKS (1)
6769
#define MICROPY_CAN_OVERRIDE_BUILTINS (1)
70+
#define MICROPY_VFS_POSIX_FILE (1)
6871
#define MICROPY_PY_FUNCTION_ATTRS (1)
6972
#define MICROPY_PY_DESCRIPTORS (1)
7073
#define MICROPY_PY_BUILTINS_STR_UNICODE (1)
@@ -86,13 +89,21 @@ static const char pybricks_ev3dev_help_text[] =
8689
#define MICROPY_PY_REVERSE_SPECIAL_METHODS (1)
8790
#define MICROPY_PY_ARRAY_SLICE_ASSIGN (1)
8891
#define MICROPY_PY_BUILTINS_SLICE_ATTRS (1)
92+
#define MICROPY_PY_BUILTINS_SLICE_INDICES (1)
8993
#define MICROPY_PY_INSTANCE_ATTRS (1)
9094
#define MICROPY_PY_SYS_EXIT (1)
95+
#define MICROPY_PY_SYS_ATEXIT (1)
96+
#if MICROPY_PY_SYS_SETTRACE
97+
#define MICROPY_PERSISTENT_CODE_SAVE (1)
98+
#define MICROPY_COMP_CONST (0)
99+
#endif
100+
#ifndef MICROPY_PY_SYS_PLATFORM
91101
#if defined(__APPLE__) && defined(__MACH__)
92102
#define MICROPY_PY_SYS_PLATFORM "darwin"
93103
#else
94104
#define MICROPY_PY_SYS_PLATFORM "linux"
95105
#endif
106+
#endif
96107
#define MICROPY_PY_SYS_MAXSIZE (1)
97108
#define MICROPY_PY_SYS_STDFILES (1)
98109
#define MICROPY_PY_SYS_EXC_INFO (0)
@@ -105,7 +116,6 @@ static const char pybricks_ev3dev_help_text[] =
105116
#define MICROPY_PY_IO_IOBASE (1)
106117
#define MICROPY_PY_IO_FILEIO (1)
107118
#define MICROPY_PY_GC_COLLECT_RETVAL (1)
108-
#define MICROPY_MODULE_FROZEN_STR (1)
109119
#define MICROPY_MODULE_BUILTIN_INIT (1)
110120

111121
#define MICROPY_PY_THREAD (1)
@@ -200,6 +210,7 @@ static const char pybricks_ev3dev_help_text[] =
200210
#define MICROPY_PY_UTIMEQ (1)
201211
#define MICROPY_PY_UHASHLIB (1)
202212
#if MICROPY_PY_USSL
213+
#define MICROPY_PY_UHASHLIB_MD5 (1)
203214
#define MICROPY_PY_UHASHLIB_SHA1 (1)
204215
#define MICROPY_PY_UCRYPTOLIB (1)
205216
#endif
@@ -233,23 +244,20 @@ static const char pybricks_ev3dev_help_text[] =
233244

234245
extern const struct _mp_print_t mp_stderr_print;
235246

236-
// Define to 1 to use undertested inefficient GC helper implementation
237-
// (if more efficient arch-specific one is not available).
238-
#ifndef MICROPY_GCREGS_SETJMP
239-
#ifdef __mips__
240-
#define MICROPY_GCREGS_SETJMP (1)
241-
#else
242-
#define MICROPY_GCREGS_SETJMP (0)
243-
#endif
247+
#if !(defined(MICROPY_GCREGS_SETJMP) || defined(__x86_64__) || defined(__i386__) || defined(__thumb2__) || defined(__thumb__) || defined(__arm__))
248+
// Fall back to setjmp() implementation for discovery of GC pointers in registers.
249+
#define MICROPY_GCREGS_SETJMP (1)
244250
#endif
245251

246252
#define MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF (1)
247253
#define MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE (256)
248254
#define MICROPY_KBD_EXCEPTION (1)
249255
#define MICROPY_ASYNC_KBD_INTR (0)
250256

257+
#define mp_type_fileio mp_type_vfs_posix_fileio
258+
#define mp_type_textio mp_type_vfs_posix_textio
259+
251260
extern const struct _mp_obj_module_t mp_module_machine;
252-
extern const struct _mp_obj_module_t mp_module_mmap;
253261
extern const struct _mp_obj_module_t mp_module_os;
254262
extern const struct _mp_obj_module_t mp_module_uos_vfs;
255263
extern const struct _mp_obj_module_t mp_module_uselect;
@@ -259,6 +267,7 @@ extern const struct _mp_obj_module_t mp_module_socket;
259267
extern const struct _mp_obj_module_t mp_module_ffi;
260268
extern const struct _mp_obj_module_t mp_module_jni;
261269
extern const struct _mp_obj_module_t mp_module_ufcntl;
270+
extern const struct _mp_obj_module_t mp_module_ummap;
262271

263272
#if MICROPY_PY_UOS_VFS
264273
#define MICROPY_PY_UOS_DEF { MP_ROM_QSTR(MP_QSTR_uos), MP_ROM_PTR(&mp_module_uos_vfs) },
@@ -306,8 +315,8 @@ extern const struct _mp_obj_module_t mp_module_ufcntl;
306315
MICROPY_PY_UOS_DEF \
307316
MICROPY_PY_USELECT_DEF \
308317
MICROPY_PY_TERMIOS_DEF \
309-
{ MP_ROM_QSTR(MP_QSTR_mmap), MP_ROM_PTR(&mp_module_mmap) }, \
310318
{ MP_ROM_QSTR(MP_QSTR_ufcntl), MP_ROM_PTR(&mp_module_ufcntl) }, \
319+
{ MP_ROM_QSTR(MP_QSTR_ummap), MP_ROM_PTR(&mp_module_ummap) }, \
311320

312321
// type definitions for the specific machine
313322

@@ -345,17 +354,6 @@ void mp_unix_mark_exec(void);
345354
#define MICROPY_FORCE_PLAT_ALLOC_EXEC (1)
346355
#endif
347356

348-
#if MICROPY_PY_OS_DUPTERM
349-
#define MP_PLAT_PRINT_STRN(str, len) mp_hal_stdout_tx_strn_cooked(str, len)
350-
#else
351-
#define MP_PLAT_PRINT_STRN(str, len) do { \
352-
MP_THREAD_GIL_EXIT(); \
353-
ssize_t ret = write(1, str, len); \
354-
MP_THREAD_GIL_ENTER(); \
355-
(void)ret; \
356-
} while (0)
357-
#endif
358-
359357
#ifdef __linux__
360358
// Can access physical memory using /dev/mem
361359
#define MICROPY_PLAT_DEV_MEM (1)
@@ -380,9 +378,17 @@ void mp_unix_mark_exec(void);
380378

381379
#define MP_STATE_PORT MP_STATE_VM
382380

381+
#if MICROPY_PY_BLUETOOTH && MICROPY_BLUETOOTH_BTSTACK
382+
struct _mp_bluetooth_btstack_root_pointers_t;
383+
#define MICROPY_BLUETOOTH_ROOT_POINTERS struct _mp_bluetooth_btstack_root_pointers_t *bluetooth_btstack_root_pointers;
384+
#else
385+
#define MICROPY_BLUETOOTH_ROOT_POINTERS
386+
#endif
387+
383388
#define MICROPY_PORT_ROOT_POINTERS \
384389
const char *readline_hist[50]; \
385390
void *mmap_region_head; \
391+
MICROPY_BLUETOOTH_ROOT_POINTERS \
386392

387393
// We need to provide a declaration/definition of alloca()
388394
// unless support for it is disabled.
@@ -413,4 +419,30 @@ void mp_unix_mark_exec(void);
413419
#include <stdio.h>
414420
#endif
415421

422+
#if MICROPY_PY_THREAD
423+
#define MICROPY_BEGIN_ATOMIC_SECTION() (mp_thread_unix_begin_atomic_section(), 0)
424+
#define MICROPY_END_ATOMIC_SECTION(x) (void)x; mp_thread_unix_end_atomic_section()
425+
#endif
426+
427+
#define MICROPY_VM_HOOK_LOOP do { \
428+
extern int pbio_do_one_event(void); \
429+
pbio_do_one_event(); \
430+
} while (0);
431+
432+
#include <glib.h>
433+
434+
#define MICROPY_EVENT_POLL_HOOK do { \
435+
extern void mp_handle_pending(bool); \
436+
mp_handle_pending(true); \
437+
extern int pbio_do_one_event(void); \
438+
while (pbio_do_one_event()) { } \
439+
MP_THREAD_GIL_EXIT(); \
440+
g_main_context_iteration(g_main_context_get_thread_default(), TRUE); \
441+
MP_THREAD_GIL_ENTER(); \
442+
} while (0);
443+
444+
445+
#include <sched.h>
446+
#define MICROPY_UNIX_MACHINE_IDLE sched_yield();
447+
416448
#include "../pybricks_config.h"

bricks/ev3dev/mpconfigport.mk

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,11 @@ MICROPY_PY_FFI = 1
2525

2626
# ussl module requires one of the TLS libraries below
2727
MICROPY_PY_USSL = 1
28-
# axTLS has minimal size and fully integrated with MicroPython, but
29-
# implements only a subset of modern TLS functionality, so may have
30-
# problems with some servers.
28+
# axTLS has minimal size but implements only a subset of modern TLS
29+
# functionality, so may have problems with some servers.
3130
MICROPY_SSL_AXTLS = 1
3231
# mbedTLS is more up to date and complete implementation, but also
33-
# more bloated. Configuring and building of mbedTLS should be done
34-
# outside of MicroPython, it can just link with mbedTLS library.
32+
# more bloated.
3533
MICROPY_SSL_MBEDTLS = 0
3634

3735
# jni module requires JVM/JNI

0 commit comments

Comments
 (0)