Skip to content

Commit f5b4545

Browse files
committed
py/modsys: Add sys.implementation._build entry.
For a given MicroPython firmware/executable it can be sometimes important to know how it was built, which variant/board configuration it came from. This commit adds a new field `sys.implementation._build` that can help identify the configuration that MicroPython was built with. For now it's either: * <VARIANT> for unix, webassembly and windows ports * <BOARD>-<VARIANT> for microcontroller ports (the variant is optional) In the future additional elements may be added to this string, separated by a hyphen. Resolves issue micropython#16498. Signed-off-by: Damien George <damien@micropython.org>
1 parent b4cf82b commit f5b4545

File tree

5 files changed

+61
-4
lines changed

5 files changed

+61
-4
lines changed

docs/library/sys.rst

+12
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ Constants
7575
* *version* - tuple (major, minor, micro, releaselevel), e.g. (1, 22, 0, '')
7676
* *_machine* - string describing the underlying machine
7777
* *_mpy* - supported mpy file-format version (optional attribute)
78+
* *_build* - string that can help identify the configuration that
79+
MicroPython was built with
7880

7981
This object is the recommended way to distinguish MicroPython from other
8082
Python implementations (note that it still may not exist in the very
@@ -83,6 +85,16 @@ Constants
8385
Starting with version 1.22.0-preview, the fourth node *releaselevel* in
8486
*implementation.version* is either an empty string or ``"preview"``.
8587

88+
The *_build* entry was added in version 1.25.0 and is a hyphen-separated
89+
set of elements. New elements may be appended in the future so it's best to
90+
access this field using ``sys.implementation._build.split("-")``. The
91+
elements that are currently used are:
92+
93+
* On the unix, webassembly and windows ports the first element is the variant
94+
name, for example ``'standard'``.
95+
* On microcontroller targets, the first element is the board name and the second
96+
element (if present) is the board variant, for example ``'RPI_PICO2-RISCV'``
97+
8698
.. admonition:: Difference to CPython
8799
:class: attention
88100

py/mkrules.cmake

+13
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,19 @@ if(NOT MICROPY_PREVIEW_VERSION_2)
1919
set(MICROPY_PREVIEW_VERSION_2 0)
2020
endif()
2121

22+
# Set the board name.
23+
if(MICROPY_BOARD)
24+
if(MICROPY_BOARD_VARIANT)
25+
set(MICROPY_BOARD_BUILD_NAME ${MICROPY_BOARD}-${MICROPY_BOARD_VARIANT})
26+
else()
27+
set(MICROPY_BOARD_BUILD_NAME ${MICROPY_BOARD})
28+
endif()
29+
30+
target_compile_definitions(${MICROPY_TARGET} PRIVATE
31+
MICROPY_BOARD_BUILD_NAME="${MICROPY_BOARD_BUILD_NAME}"
32+
)
33+
endif()
34+
2235
# Need to do this before extracting MICROPY_CPP_DEF below. Rest of frozen
2336
# manifest handling is at the end of this file.
2437
if(MICROPY_FROZEN_MANIFEST)

py/mkrules.mk

+11
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,17 @@ OBJ_EXTRA_ORDER_DEPS += $(HEADER_BUILD)/compressed.data.h
2727
CFLAGS += -DMICROPY_ROM_TEXT_COMPRESSION=1
2828
endif
2929

30+
# Set the variant or board name.
31+
ifneq ($(VARIANT),)
32+
CFLAGS += -DMICROPY_BOARD_BUILD_NAME=\"$(VARIANT)\"
33+
else ifneq ($(BOARD),)
34+
ifeq ($(BOARD_VARIANT),)
35+
CFLAGS += -DMICROPY_BOARD_BUILD_NAME=\"$(BOARD)\"
36+
else
37+
CFLAGS += -DMICROPY_BOARD_BUILD_NAME=\"$(BOARD)-$(BOARD_VARIANT)\"
38+
endif
39+
endif
40+
3041
# QSTR generation uses the same CFLAGS, with these modifications.
3142
QSTR_GEN_FLAGS = -DNO_QSTR
3243
# Note: := to force evaluation immediately.

py/modsys.c

+19-4
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,17 @@ static const MP_DEFINE_STR_OBJ(mp_sys_implementation_machine_obj, MICROPY_BANNER
9292
#endif
9393

9494
#if MICROPY_PY_ATTRTUPLE
95+
96+
#if defined(MICROPY_BOARD_BUILD_NAME)
97+
static const MP_DEFINE_STR_OBJ(mp_sys_implementation__build_obj, MICROPY_BOARD_BUILD_NAME);
98+
#define MICROPY_BOARD_BUILD (1)
99+
#define SYS_IMPLEMENTATION_ELEMS__BUILD \
100+
, MP_ROM_PTR(&mp_sys_implementation__build_obj)
101+
#else
102+
#define MICROPY_BOARD_BUILD (0)
103+
#define SYS_IMPLEMENTATION_ELEMS__BUILD
104+
#endif
105+
95106
#if MICROPY_PREVIEW_VERSION_2
96107
#define SYS_IMPLEMENTATION_ELEMS__V2 \
97108
, MP_ROM_TRUE
@@ -106,26 +117,30 @@ static const qstr impl_fields[] = {
106117
#if MICROPY_PERSISTENT_CODE_LOAD
107118
MP_QSTR__mpy,
108119
#endif
120+
#if defined(MICROPY_BOARD_BUILD_NAME)
121+
MP_QSTR__build,
122+
#endif
109123
#if MICROPY_PREVIEW_VERSION_2
110124
MP_QSTR__v2,
111125
#endif
112126
};
113127
static MP_DEFINE_ATTRTUPLE(
114128
mp_sys_implementation_obj,
115129
impl_fields,
116-
3 + MICROPY_PERSISTENT_CODE_LOAD + MICROPY_PREVIEW_VERSION_2,
130+
3 + MICROPY_PERSISTENT_CODE_LOAD + MICROPY_BOARD_BUILD + MICROPY_PREVIEW_VERSION_2,
117131
SYS_IMPLEMENTATION_ELEMS_BASE
118132
SYS_IMPLEMENTATION_ELEMS__MPY
133+
SYS_IMPLEMENTATION_ELEMS__BUILD
119134
SYS_IMPLEMENTATION_ELEMS__V2
120135
);
121136
#else
122137
static const mp_rom_obj_tuple_t mp_sys_implementation_obj = {
123138
{&mp_type_tuple},
124139
3 + MICROPY_PERSISTENT_CODE_LOAD,
125-
// Do not include SYS_IMPLEMENTATION_ELEMS__V2 because
126-
// SYS_IMPLEMENTATION_ELEMS__MPY may be empty if
140+
// Do not include SYS_IMPLEMENTATION_ELEMS__BUILD or SYS_IMPLEMENTATION_ELEMS__V2
141+
// because SYS_IMPLEMENTATION_ELEMS__MPY may be empty if
127142
// MICROPY_PERSISTENT_CODE_LOAD is disabled, which means they'll share
128-
// the same index. Cannot query _v2 if MICROPY_PY_ATTRTUPLE is
143+
// the same index. Cannot query _build or _v2 if MICROPY_PY_ATTRTUPLE is
129144
// disabled.
130145
{
131146
SYS_IMPLEMENTATION_ELEMS_BASE

tests/basics/sys1.py

+6
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@
2424
# Effectively skip subtests
2525
print(int)
2626

27+
if hasattr(sys.implementation, '_build'):
28+
print(type(sys.implementation._build))
29+
else:
30+
# Effectively skip subtests
31+
print(str)
32+
2733
try:
2834
print(sys.intern('micropython') == 'micropython')
2935
has_intern = True

0 commit comments

Comments
 (0)