Skip to content

Commit 2c5e9bb

Browse files
iabdalkaderdpgeorge
authored andcommitted
extmod: Add platform module.
It contains the compiler version, and underlying system HAL/SDK version.
1 parent 38f8e85 commit 2c5e9bb

File tree

5 files changed

+142
-0
lines changed

5 files changed

+142
-0
lines changed

extmod/extmod.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ set(MICROPY_SOURCE_EXTMOD
2525
${MICROPY_EXTMOD_DIR}/moduhashlib.c
2626
${MICROPY_EXTMOD_DIR}/moduheapq.c
2727
${MICROPY_EXTMOD_DIR}/modujson.c
28+
${MICROPY_EXTMOD_DIR}/moduplatform.c
2829
${MICROPY_EXTMOD_DIR}/modurandom.c
2930
${MICROPY_EXTMOD_DIR}/modure.c
3031
${MICROPY_EXTMOD_DIR}/moduselect.c

extmod/moduplatform.c

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2013-2021 Ibrahim Abdelkader <[email protected]>
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+
28+
#include "py/runtime.h"
29+
#include "py/objtuple.h"
30+
#include "py/objstr.h"
31+
#include "py/mphal.h"
32+
#include "genhdr/mpversion.h"
33+
34+
#if MICROPY_PY_UPLATFORM
35+
36+
// platform - Access to underlying platform's identifying data
37+
38+
// TODO: Add more architectures, compilers and libraries.
39+
// See: https://sourceforge.net/p/predef/wiki/Home/
40+
41+
#if defined(__ARM_ARCH)
42+
#define PLATFORM_ARCH "arm"
43+
#elif defined(__x86_64__)
44+
#define PLATFORM_ARCH "x86_64"
45+
#else
46+
#define PLATFORM_ARCH ""
47+
#endif
48+
49+
#if defined(__GNUC__)
50+
#define PLATFORM_COMPILER \
51+
"GCC " \
52+
MP_STRINGIFY(__GNUC__) "." \
53+
MP_STRINGIFY(__GNUC_MINOR__) "." \
54+
MP_STRINGIFY(__GNUC_PATCHLEVEL__)
55+
#elif defined(__ARMCC_VERSION)
56+
#define PLATFORM_COMPILER \
57+
"ARMCC " \
58+
MP_STRINGIFY((__ARMCC_VERSION / 1000000)) "." \
59+
MP_STRINGIFY((__ARMCC_VERSION / 10000 % 100)) "." \
60+
MP_STRINGIFY((__ARMCC_VERSION % 10000))
61+
#else
62+
#define PLATFORM_COMPILER ""
63+
#endif
64+
65+
#if defined(__GLIBC__)
66+
#define PLATFORM_LIBC_LIB "glibc"
67+
#define PLATFORM_LIBC_VER \
68+
MP_STRINGIFY(__GLIBC__) "." \
69+
MP_STRINGIFY(__GLIBC_MINOR__)
70+
#elif defined(__NEWLIB__)
71+
#define PLATFORM_LIBC_LIB "newlib"
72+
#define PLATFORM_LIBC_VER _NEWLIB_VERSION
73+
#else
74+
#define PLATFORM_LIBC_LIB ""
75+
#define PLATFORM_LIBC_VER ""
76+
#endif
77+
78+
#if defined(__linux)
79+
#define PLATFORM_SYSTEM "Linux"
80+
#elif defined(__unix__)
81+
#define PLATFORM_SYSTEM "Unix"
82+
#elif defined(__CYGWIN__)
83+
#define PLATFORM_SYSTEM "Cygwin"
84+
#elif defined(__WIN32__)
85+
#define PLATFORM_SYSTEM "Windows"
86+
#else
87+
#define PLATFORM_SYSTEM "MicroPython"
88+
#endif
89+
90+
#ifndef MICROPY_HW_BOARD_NAME
91+
#define MICROPY_HW_BOARD_NAME PLATFORM_ARCH
92+
#endif
93+
94+
#ifndef MICROPY_HW_MCU_NAME
95+
#define MICROPY_HW_MCU_NAME ""
96+
#endif
97+
98+
STATIC const MP_DEFINE_STR_OBJ(info_platform_obj, PLATFORM_SYSTEM "-" MICROPY_VERSION_STRING "-HAL" \
99+
MICROPY_HAL_VERSION "-" PLATFORM_ARCH "-with-" PLATFORM_LIBC_LIB "" PLATFORM_LIBC_VER);
100+
STATIC const MP_DEFINE_STR_OBJ(info_python_compiler_obj, PLATFORM_COMPILER);
101+
STATIC const MP_DEFINE_STR_OBJ(info_libc_lib_obj, PLATFORM_LIBC_LIB);
102+
STATIC const MP_DEFINE_STR_OBJ(info_libc_ver_obj, PLATFORM_LIBC_VER);
103+
STATIC const mp_rom_obj_tuple_t info_libc_tuple_obj = {
104+
{&mp_type_tuple}, 2, {MP_ROM_PTR(&info_libc_lib_obj), MP_ROM_PTR(&info_libc_ver_obj)}
105+
};
106+
107+
STATIC mp_obj_t platform_platform(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
108+
return MP_OBJ_FROM_PTR(&info_platform_obj);
109+
}
110+
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(platform_platform_obj, 0, platform_platform);
111+
112+
STATIC mp_obj_t platform_python_compiler(void) {
113+
return MP_OBJ_FROM_PTR(&info_python_compiler_obj);
114+
}
115+
STATIC MP_DEFINE_CONST_FUN_OBJ_0(platform_python_compiler_obj, platform_python_compiler);
116+
117+
STATIC mp_obj_t platform_libc_ver(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
118+
return MP_OBJ_FROM_PTR(&info_libc_tuple_obj);
119+
}
120+
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(platform_libc_ver_obj, 0, platform_libc_ver);
121+
122+
STATIC const mp_rom_map_elem_t modplatform_globals_table[] = {
123+
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_uplatform) },
124+
{ MP_ROM_QSTR(MP_QSTR_platform), MP_ROM_PTR(&platform_platform_obj) },
125+
{ MP_ROM_QSTR(MP_QSTR_python_compiler), MP_ROM_PTR(&platform_python_compiler_obj) },
126+
{ MP_ROM_QSTR(MP_QSTR_libc_ver), MP_ROM_PTR(&platform_libc_ver_obj) },
127+
};
128+
129+
STATIC MP_DEFINE_CONST_DICT(modplatform_globals, modplatform_globals_table);
130+
131+
const mp_obj_module_t mp_module_uplatform = {
132+
.base = { &mp_type_module },
133+
.globals = (mp_obj_dict_t *)&modplatform_globals,
134+
};
135+
136+
#endif // MICROPY_PY_UPLATFORM

py/builtin.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ extern const mp_obj_module_t mp_module_webrepl;
124124
extern const mp_obj_module_t mp_module_framebuf;
125125
extern const mp_obj_module_t mp_module_btree;
126126
extern const mp_obj_module_t mp_module_ubluetooth;
127+
extern const mp_obj_module_t mp_module_uplatform;
127128

128129
extern const char MICROPY_PY_BUILTINS_HELP_TEXT[];
129130

py/objmodule.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,9 @@ STATIC const mp_rom_map_elem_t mp_builtin_module_table[] = {
230230
#if MICROPY_PY_BLUETOOTH
231231
{ MP_ROM_QSTR(MP_QSTR_ubluetooth), MP_ROM_PTR(&mp_module_ubluetooth) },
232232
#endif
233+
#if MICROPY_PY_UPLATFORM
234+
{ MP_ROM_QSTR(MP_QSTR_uplatform), MP_ROM_PTR(&mp_module_uplatform) },
235+
#endif
233236

234237
// extra builtin modules as defined by a port
235238
MICROPY_PORT_BUILTIN_MODULES

py/py.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ PY_EXTMOD_O_BASENAME = \
195195
extmod/modbluetooth.o \
196196
extmod/modussl_axtls.o \
197197
extmod/modussl_mbedtls.o \
198+
extmod/moduplatform.o\
198199
extmod/modurandom.o \
199200
extmod/moduselect.o \
200201
extmod/moduwebsocket.o \

0 commit comments

Comments
 (0)