From 3bf70f16e964370475114ff9658f58870ffaf768 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Tue, 10 Oct 2023 16:50:28 +1100 Subject: [PATCH] py/mkrules.mk: Add MICROPY_PREVIEW_VERSION_2. This provides a way to enable features and changes slated for MicroPython 2.x, by running `make MICROPY_PREVIEW_VERSION_2=1`. Also supported for the cmake ports (except Zephyr). This is an alternative to having a 2.x development branch (or equivalently, keeping a 1.x release branch). Any feature or change that needs to be "hidden" until 2.x can use this flag (either in the Makefile or the preprocessor). A good example is changing function arguments or other public API features, in particular to aid in improving consistency between ports. When `MICROPY_PREVIEW_VERSION_2` is enabled, the REPL banner is amended to say "MicroPython (with v2.0 preview) vX.Y.Z", and sys.implementation gets a new field `_v2` set to `True`. This work was funded through GitHub Sponsors. Signed-off-by: Jim Mussared --- .github/workflows/ports_unix.yml | 12 ++++++++++ ports/esp32/Makefile | 4 ++++ ports/rp2/Makefile | 4 ++++ py/mkrules.cmake | 10 ++++++++ py/mkrules.mk | 7 ++++++ py/modsys.c | 40 +++++++++++++++++++++++--------- py/mpconfig.h | 10 ++++++++ tools/ci.sh | 9 +++++++ 8 files changed, 85 insertions(+), 11 deletions(-) diff --git a/.github/workflows/ports_unix.yml b/.github/workflows/ports_unix.yml index 04dfd78525..25dd52f31d 100644 --- a/.github/workflows/ports_unix.yml +++ b/.github/workflows/ports_unix.yml @@ -55,6 +55,18 @@ jobs: if: failure() run: tests/run-tests.py --print-failures + standard_v2: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: Build + run: source tools/ci.sh && ci_unix_standard_v2_build + - name: Run main test suite + run: source tools/ci.sh && ci_unix_standard_v2_run_tests + - name: Print failures + if: failure() + run: tests/run-tests.py --print-failures + coverage: runs-on: ubuntu-latest steps: diff --git a/ports/esp32/Makefile b/ports/esp32/Makefile index bf275ffe66..ce574ce799 100644 --- a/ports/esp32/Makefile +++ b/ports/esp32/Makefile @@ -52,6 +52,10 @@ ifdef BOARD_VARIANT IDFPY_FLAGS += -D MICROPY_BOARD_VARIANT=$(BOARD_VARIANT) endif +ifdef MICROPY_PREVIEW_VERSION_2 + IDFPY_FLAGS += -D MICROPY_PREVIEW_VERSION_2=1 +endif + HELP_BUILD_ERROR ?= "See \033[1;31mhttps://github.com/micropython/micropython/wiki/Build-Troubleshooting\033[0m" define RUN_IDF_PY diff --git a/ports/rp2/Makefile b/ports/rp2/Makefile index 8399b9e98a..11d2115a2f 100644 --- a/ports/rp2/Makefile +++ b/ports/rp2/Makefile @@ -48,6 +48,10 @@ ifdef BOARD_VARIANT CMAKE_ARGS += -DMICROPY_BOARD_VARIANT=$(BOARD_VARIANT) endif +ifdef MICROPY_PREVIEW_VERSION_2 +CMAKE_ARGS += -DMICROPY_PREVIEW_VERSION_2=1 +endif + HELP_BUILD_ERROR ?= "See \033[1;31mhttps://github.com/micropython/micropython/wiki/Build-Troubleshooting\033[0m" all: diff --git a/py/mkrules.cmake b/py/mkrules.cmake index 02e3148f2b..6415b549aa 100644 --- a/py/mkrules.cmake +++ b/py/mkrules.cmake @@ -15,6 +15,10 @@ set(MICROPY_ROOT_POINTERS_SPLIT "${MICROPY_GENHDR_DIR}/root_pointers.split") set(MICROPY_ROOT_POINTERS_COLLECTED "${MICROPY_GENHDR_DIR}/root_pointers.collected") set(MICROPY_ROOT_POINTERS "${MICROPY_GENHDR_DIR}/root_pointers.h") +if(NOT MICROPY_PREVIEW_VERSION_2) + set(MICROPY_PREVIEW_VERSION_2 0) +endif() + # Need to do this before extracting MICROPY_CPP_DEF below. Rest of frozen # manifest handling is at the end of this file. if(MICROPY_FROZEN_MANIFEST) @@ -24,6 +28,12 @@ if(MICROPY_FROZEN_MANIFEST) ) endif() +if(MICROPY_PREVIEW_VERSION_2) + target_compile_definitions(${MICROPY_TARGET} PUBLIC + MICROPY_PREVIEW_VERSION_2=\(1\) + ) +endif() + # Provide defaults for preprocessor flags if not already defined if(NOT MICROPY_CPP_FLAGS) get_target_property(MICROPY_CPP_INC ${MICROPY_TARGET} INCLUDE_DIRECTORIES) diff --git a/py/mkrules.mk b/py/mkrules.mk index 421e638c22..8b21c7974d 100644 --- a/py/mkrules.mk +++ b/py/mkrules.mk @@ -4,6 +4,13 @@ THIS_MAKEFILE = $(lastword $(MAKEFILE_LIST)) include $(dir $(THIS_MAKEFILE))mkenv.mk endif +# Enable in-progress/breaking changes that are slated for MicroPython 2.x. +MICROPY_PREVIEW_VERSION_2 ?= 0 + +ifeq ($(MICROPY_PREVIEW_VERSION_2),1) +CFLAGS += -DMICROPY_PREVIEW_VERSION_2=1 +endif + HELP_BUILD_ERROR ?= "See \033[1;31mhttps://github.com/micropython/micropython/wiki/Build-Troubleshooting\033[0m" HELP_MPY_LIB_SUBMODULE ?= "\033[1;31mError: micropython-lib submodule is not initialized.\033[0m Run 'make submodules'" diff --git a/py/modsys.c b/py/modsys.c index e40542467b..12fa1101b9 100644 --- a/py/modsys.c +++ b/py/modsys.c @@ -79,19 +79,26 @@ STATIC const mp_rom_obj_tuple_t mp_sys_implementation_version_info_obj = { } }; STATIC const MP_DEFINE_STR_OBJ(mp_sys_implementation_machine_obj, MICROPY_BANNER_MACHINE); -#if MICROPY_PERSISTENT_CODE_LOAD -#define SYS_IMPLEMENTATION_ELEMS \ - MP_ROM_QSTR(MP_QSTR_micropython), \ - MP_ROM_PTR(&mp_sys_implementation_version_info_obj), \ - MP_ROM_PTR(&mp_sys_implementation_machine_obj), \ - MP_ROM_INT(MPY_FILE_HEADER_INT) -#else -#define SYS_IMPLEMENTATION_ELEMS \ +#define SYS_IMPLEMENTATION_ELEMS_BASE \ MP_ROM_QSTR(MP_QSTR_micropython), \ MP_ROM_PTR(&mp_sys_implementation_version_info_obj), \ MP_ROM_PTR(&mp_sys_implementation_machine_obj) + +#if MICROPY_PERSISTENT_CODE_LOAD +#define SYS_IMPLEMENTATION_ELEMS__MPY \ + , MP_ROM_INT(MPY_FILE_HEADER_INT) +#else +#define SYS_IMPLEMENTATION_ELEMS__MPY #endif + #if MICROPY_PY_ATTRTUPLE +#if MICROPY_PREVIEW_VERSION_2 +#define SYS_IMPLEMENTATION_ELEMS__V2 \ + , MP_ROM_TRUE +#else +#define SYS_IMPLEMENTATION_ELEMS__V2 +#endif + STATIC const qstr impl_fields[] = { MP_QSTR_name, MP_QSTR_version, @@ -99,19 +106,30 @@ STATIC const qstr impl_fields[] = { #if MICROPY_PERSISTENT_CODE_LOAD MP_QSTR__mpy, #endif + #if MICROPY_PREVIEW_VERSION_2 + MP_QSTR__v2, + #endif }; STATIC MP_DEFINE_ATTRTUPLE( mp_sys_implementation_obj, impl_fields, - 3 + MICROPY_PERSISTENT_CODE_LOAD, - SYS_IMPLEMENTATION_ELEMS + 3 + MICROPY_PERSISTENT_CODE_LOAD + MICROPY_PREVIEW_VERSION_2, + SYS_IMPLEMENTATION_ELEMS_BASE + SYS_IMPLEMENTATION_ELEMS__MPY + SYS_IMPLEMENTATION_ELEMS__V2 ); #else STATIC const mp_rom_obj_tuple_t mp_sys_implementation_obj = { {&mp_type_tuple}, 3 + MICROPY_PERSISTENT_CODE_LOAD, + // Do not include SYS_IMPLEMENTATION_ELEMS__V2 because + // SYS_IMPLEMENTATION_ELEMS__MPY may be empty if + // MICROPY_PERSISTENT_CODE_LOAD is disabled, which means they'll share + // the same index. Cannot query _v2 if MICROPY_PY_ATTRTUPLE is + // disabled. { - SYS_IMPLEMENTATION_ELEMS + SYS_IMPLEMENTATION_ELEMS_BASE + SYS_IMPLEMENTATION_ELEMS__MPY } }; #endif diff --git a/py/mpconfig.h b/py/mpconfig.h index a36f9658fb..6995600abe 100644 --- a/py/mpconfig.h +++ b/py/mpconfig.h @@ -53,6 +53,12 @@ #define MICROPY_VERSION_STRING MICROPY_VERSION_STRING_BASE #endif +// If this is enabled, then in-progress/breaking changes slated for the 2.x +// release will be enabled. +#ifndef MICROPY_PREVIEW_VERSION_2 +#define MICROPY_PREVIEW_VERSION_2 (0) +#endif + // This file contains default configuration settings for MicroPython. // You can override any of the options below using mpconfigport.h file // located in a directory of your port. @@ -1828,8 +1834,12 @@ typedef double mp_float_t; // String used for the banner, and sys.version additional information #ifndef MICROPY_BANNER_NAME_AND_VERSION +#if MICROPY_PREVIEW_VERSION_2 +#define MICROPY_BANNER_NAME_AND_VERSION "MicroPython (with v2.0 preview) " MICROPY_GIT_TAG " on " MICROPY_BUILD_DATE +#else #define MICROPY_BANNER_NAME_AND_VERSION "MicroPython " MICROPY_GIT_TAG " on " MICROPY_BUILD_DATE #endif +#endif // String used for the second part of the banner, and sys.implementation._machine #ifndef MICROPY_BANNER_MACHINE diff --git a/tools/ci.sh b/tools/ci.sh index 975ce86172..a4cbb045a4 100755 --- a/tools/ci.sh +++ b/tools/ci.sh @@ -462,6 +462,15 @@ function ci_unix_standard_run_tests { ci_unix_run_tests_full_helper standard } +function ci_unix_standard_v2_build { + ci_unix_build_helper VARIANT=standard MICROPY_PREVIEW_VERSION_2=1 + ci_unix_build_ffi_lib_helper gcc +} + +function ci_unix_standard_v2_run_tests { + ci_unix_run_tests_full_helper standard +} + function ci_unix_coverage_setup { sudo pip3 install setuptools sudo pip3 install pyelftools