Skip to content

Commit 3926c72

Browse files
committed
unix: Add target to build "minimal" uPy interpreter.
1 parent 963a5a3 commit 3926c72

8 files changed

Lines changed: 160 additions & 4 deletions

File tree

unix/Makefile

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ ifeq ($(MICROPY_PY_TERMIOS),1)
6969
CFLAGS_MOD += -DMICROPY_PY_TERMIOS=1
7070
SRC_MOD += modtermios.c
7171
endif
72+
ifeq ($(MICROPY_PY_SOCKET),1)
73+
CFLAGS_MOD += -DMICROPY_PY_SOCKET=1
74+
SRC_MOD += modsocket.c
75+
endif
7276
ifeq ($(MICROPY_PY_FFI),1)
7377
LIBFFI_LDFLAGS_MOD := $(shell pkg-config --libs libffi)
7478
LIBFFI_CFLAGS_MOD := $(shell pkg-config --cflags libffi)
@@ -87,7 +91,6 @@ SRC_C = \
8791
gccollect.c \
8892
input.c \
8993
file.c \
90-
modsocket.c \
9194
modos.c \
9295
alloc.c \
9396
$(SRC_MOD)
@@ -122,4 +125,9 @@ uninstall:
122125
# build synthetically fast interpreter for benchmarking
123126
fast:
124127
@echo Make sure to run make -B
125-
$(MAKE) COPT="-O2 -DNDEBUG -fno-crossjumping" CFLAGS_EXTRA='-DMP_CONFIGFILE="<mpconfigport_fast.h>"' BUILD=build-fast
128+
$(MAKE) COPT="-O2 -DNDEBUG -fno-crossjumping" CFLAGS_EXTRA='-DMP_CONFIGFILE="<mpconfigport_fast.h>"' BUILD=build-fast PROG=micropython_fast
129+
130+
# build a minimal interpreter
131+
minimal:
132+
@echo Make sure to run make -B
133+
$(MAKE) COPT="-Os -DNDEBUG" CFLAGS_EXTRA='-DMP_CONFIGFILE="<mpconfigport_minimal.h>"' BUILD=build-minimal PROG=micropython_minimal MICROPY_PY_TIME=0 MICROPY_PY_TERMIOS=0 MICROPY_PY_SOCKET=0 MICROPY_PY_FFI=0

unix/alloc.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
#include "py/mpstate.h"
3434
#include "py/gc.h"
3535

36+
#if MICROPY_EMIT_NATIVE
37+
3638
#if defined(__OpenBSD__) || defined(__MACH__)
3739
#define MAP_ANONYMOUS MAP_ANON
3840
#endif
@@ -82,3 +84,5 @@ void mp_unix_mark_exec(void) {
8284
gc_collect_root(rg->ptr, rg->len / sizeof(mp_uint_t));
8385
}
8486
}
87+
88+
#endif // MICROPY_EMIT_NATIVE

unix/file.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
#include "py/stream.h"
3737
#include "py/builtin.h"
3838

39+
#if MICROPY_PY_IO
40+
3941
#ifdef _WIN32
4042
#define fsync _commit
4143
#endif
@@ -264,3 +266,5 @@ MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_open_obj, 1, mp_builtin_open);
264266
const mp_obj_fdfile_t mp_sys_stdin_obj = { .base = {&mp_type_textio}, .fd = STDIN_FILENO };
265267
const mp_obj_fdfile_t mp_sys_stdout_obj = { .base = {&mp_type_textio}, .fd = STDOUT_FILENO };
266268
const mp_obj_fdfile_t mp_sys_stderr_obj = { .base = {&mp_type_textio}, .fd = STDERR_FILENO };
269+
270+
#endif // MICROPY_PY_IO

unix/gccollect.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ void gc_collect(void) {
131131
// GC stack (and regs because we captured them)
132132
void **regs_ptr = (void**)(void*)&regs;
133133
gc_collect_root(regs_ptr, ((mp_uint_t)MP_STATE_VM(stack_top) - (mp_uint_t)&regs) / sizeof(mp_uint_t));
134-
#ifndef _WIN32
134+
#if MICROPY_EMIT_NATIVE
135135
mp_unix_mark_exec();
136136
#endif
137137
gc_collect_end();

unix/main.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,9 +481,11 @@ int main(int argc, char **argv) {
481481
ret = do_repl();
482482
}
483483

484+
#if MICROPY_PY_MICROPYTHON_MEM_INFO
484485
if (mp_verbose_flag) {
485486
mp_micropython_mem_info(0, NULL);
486487
}
488+
#endif
487489

488490
mp_deinit();
489491

unix/mpconfigport.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,16 @@ extern const struct _mp_obj_module_t mp_module_ffi;
114114
#else
115115
#define MICROPY_PY_TERMIOS_DEF
116116
#endif
117+
#if MICROPY_PY_SOCKET
118+
#define MICROPY_PY_SOCKET_DEF { MP_OBJ_NEW_QSTR(MP_QSTR_usocket), (mp_obj_t)&mp_module_socket },
119+
#else
120+
#define MICROPY_PY_SOCKET_DEF
121+
#endif
117122

118123
#define MICROPY_PORT_BUILTIN_MODULES \
119124
MICROPY_PY_FFI_DEF \
120125
MICROPY_PY_TIME_DEF \
121-
{ MP_OBJ_NEW_QSTR(MP_QSTR_usocket), (mp_obj_t)&mp_module_socket }, \
126+
MICROPY_PY_SOCKET_DEF \
122127
{ MP_OBJ_NEW_QSTR(MP_QSTR__os), (mp_obj_t)&mp_module_os }, \
123128
MICROPY_PY_TERMIOS_DEF \
124129

unix/mpconfigport.mk

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,8 @@ MICROPY_PY_TIME = 1
1212
# Subset of CPython termios module
1313
MICROPY_PY_TERMIOS = 1
1414

15+
# Subset of CPython socket module
16+
MICROPY_PY_SOCKET = 1
17+
1518
# ffi module requires libffi (libffi-dev Debian package)
1619
MICROPY_PY_FFI = 1

unix/mpconfigport_minimal.h

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
/*
2+
* This file is part of the Micro Python project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2015 Damien P. George
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
// options to control how Micro Python is built
28+
29+
#define MICROPY_ALLOC_PATH_MAX (PATH_MAX)
30+
#define MICROPY_ENABLE_GC (1)
31+
#define MICROPY_ENABLE_FINALISER (0)
32+
#define MICROPY_STACK_CHECK (0)
33+
#define MICROPY_COMP_CONST (0)
34+
#define MICROPY_MEM_STATS (0)
35+
#define MICROPY_DEBUG_PRINTERS (0)
36+
#define MICROPY_HELPER_REPL (1)
37+
#define MICROPY_HELPER_LEXER_UNIX (1)
38+
#define MICROPY_ENABLE_SOURCE_LINE (0)
39+
#define MICROPY_ERROR_REPORTING (MICROPY_ERROR_REPORTING_TERSE)
40+
#define MICROPY_WARNINGS (0)
41+
#define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_NONE)
42+
#define MICROPY_LONGINT_IMPL (MICROPY_LONGINT_IMPL_NONE)
43+
#define MICROPY_STREAMS_NON_BLOCK (0)
44+
#define MICROPY_OPT_COMPUTED_GOTO (0)
45+
#define MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE (0)
46+
#define MICROPY_CAN_OVERRIDE_BUILTINS (0)
47+
#define MICROPY_CPYTHON_COMPAT (0)
48+
#define MICROPY_PY_BUILTINS_BYTEARRAY (0)
49+
#define MICROPY_PY_BUILTINS_MEMORYVIEW (0)
50+
#define MICROPY_PY_BUILTINS_COMPILE (0)
51+
#define MICROPY_PY_BUILTINS_FROZENSET (0)
52+
#define MICROPY_PY_BUILTINS_SET (0)
53+
#define MICROPY_PY_BUILTINS_SLICE (0)
54+
#define MICROPY_PY_BUILTINS_STR_UNICODE (0)
55+
#define MICROPY_PY_BUILTINS_PROPERTY (0)
56+
#define MICROPY_PY___FILE__ (0)
57+
#define MICROPY_PY_MICROPYTHON_MEM_INFO (0)
58+
#define MICROPY_PY_GC (0)
59+
#define MICROPY_PY_GC_COLLECT_RETVAL (0)
60+
#define MICROPY_PY_ARRAY (0)
61+
#define MICROPY_PY_COLLECTIONS (0)
62+
#define MICROPY_PY_MATH (0)
63+
#define MICROPY_PY_CMATH (0)
64+
#define MICROPY_PY_IO (0)
65+
#define MICROPY_PY_IO_FILEIO (0)
66+
#define MICROPY_PY_STRUCT (0)
67+
#define MICROPY_PY_SYS (1)
68+
#define MICROPY_PY_SYS_EXIT (0)
69+
#define MICROPY_PY_SYS_PLATFORM "linux"
70+
#define MICROPY_PY_SYS_MAXSIZE (0)
71+
#define MICROPY_PY_SYS_STDFILES (0)
72+
#define MICROPY_PY_CMATH (0)
73+
#define MICROPY_PY_UCTYPES (0)
74+
#define MICROPY_PY_UZLIB (0)
75+
#define MICROPY_PY_UJSON (0)
76+
#define MICROPY_PY_URE (0)
77+
#define MICROPY_PY_UHEAPQ (0)
78+
#define MICROPY_PY_UHASHLIB (0)
79+
#define MICROPY_PY_UBINASCII (0)
80+
81+
// Define to 1 to use undertested inefficient GC helper implementation
82+
// (if more efficient arch-specific one is not available).
83+
#ifndef MICROPY_GCREGS_SETJMP
84+
#ifdef __mips__
85+
#define MICROPY_GCREGS_SETJMP (1)
86+
#else
87+
#define MICROPY_GCREGS_SETJMP (0)
88+
#endif
89+
#endif
90+
91+
#define MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF (0)
92+
93+
extern const struct _mp_obj_module_t mp_module_os;
94+
95+
#define MICROPY_PORT_BUILTIN_MODULES \
96+
{ MP_OBJ_NEW_QSTR(MP_QSTR__os), (mp_obj_t)&mp_module_os }, \
97+
98+
// type definitions for the specific machine
99+
100+
#ifdef __LP64__
101+
typedef long mp_int_t; // must be pointer size
102+
typedef unsigned long mp_uint_t; // must be pointer size
103+
#else
104+
// These are definitions for machines where sizeof(int) == sizeof(void*),
105+
// regardless for actual size.
106+
typedef int mp_int_t; // must be pointer size
107+
typedef unsigned int mp_uint_t; // must be pointer size
108+
#endif
109+
110+
#define BYTES_PER_WORD sizeof(mp_int_t)
111+
112+
// Cannot include <sys/types.h>, as it may lead to symbol name clashes
113+
#if _FILE_OFFSET_BITS == 64 && !defined(__LP64__)
114+
typedef long long mp_off_t;
115+
#else
116+
typedef long mp_off_t;
117+
#endif
118+
119+
typedef void *machine_ptr_t; // must be of pointer size
120+
typedef const void *machine_const_ptr_t; // must be of pointer size
121+
122+
#define MICROPY_PORT_ROOT_POINTERS \
123+
mp_obj_t keyboard_interrupt_obj;
124+
125+
// We need to provide a declaration/definition of alloca()
126+
#ifdef __FreeBSD__
127+
#include <stdlib.h>
128+
#else
129+
#include <alloca.h>
130+
#endif

0 commit comments

Comments
 (0)