Skip to content

Commit 49934fc

Browse files
stinosdpgeorge
authored andcommitted
extmod/moduplatform: Move platform PP definitions into a header.
These are more generally useful than just for the module so make them globally available, prefixed consistently with MICROPY_PLATFORM_.
1 parent f30b32e commit 49934fc

File tree

2 files changed

+112
-75
lines changed

2 files changed

+112
-75
lines changed

extmod/moduplatform.c

Lines changed: 7 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -29,87 +29,19 @@
2929
#include "py/objtuple.h"
3030
#include "py/objstr.h"
3131
#include "py/mphal.h"
32+
#include "extmod/moduplatform.h"
3233
#include "genhdr/mpversion.h"
3334

3435
#if MICROPY_PY_UPLATFORM
3536

3637
// platform - Access to underlying platform's identifying data
3738

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__) || defined(_WIN64)
44-
#define PLATFORM_ARCH "x86_64"
45-
#elif defined(__i386__) || defined(_M_IX86)
46-
#define PLATFORM_ARCH "x86"
47-
#elif defined(__xtensa__) || defined(_M_IX86)
48-
#define PLATFORM_ARCH "xtensa"
49-
#else
50-
#define PLATFORM_ARCH ""
51-
#endif
52-
53-
#if defined(__GNUC__)
54-
#define PLATFORM_COMPILER \
55-
"GCC " \
56-
MP_STRINGIFY(__GNUC__) "." \
57-
MP_STRINGIFY(__GNUC_MINOR__) "." \
58-
MP_STRINGIFY(__GNUC_PATCHLEVEL__)
59-
#elif defined(__ARMCC_VERSION)
60-
#define PLATFORM_COMPILER \
61-
"ARMCC " \
62-
MP_STRINGIFY((__ARMCC_VERSION / 1000000)) "." \
63-
MP_STRINGIFY((__ARMCC_VERSION / 10000 % 100)) "." \
64-
MP_STRINGIFY((__ARMCC_VERSION % 10000))
65-
#elif defined(_MSC_VER)
66-
#if defined(_WIN64)
67-
#define COMPILER_BITS "64 bit"
68-
#elif defined(_M_IX86)
69-
#define COMPILER_BITS "32 bit"
70-
#else
71-
#define COMPILER_BITS ""
72-
#endif
73-
#define PLATFORM_COMPILER \
74-
"MSC v." MP_STRINGIFY(_MSC_VER) " " COMPILER_BITS
75-
#else
76-
#define PLATFORM_COMPILER ""
77-
#endif
78-
79-
#if defined(__GLIBC__)
80-
#define PLATFORM_LIBC_LIB "glibc"
81-
#define PLATFORM_LIBC_VER \
82-
MP_STRINGIFY(__GLIBC__) "." \
83-
MP_STRINGIFY(__GLIBC_MINOR__)
84-
#elif defined(__NEWLIB__)
85-
#define PLATFORM_LIBC_LIB "newlib"
86-
#define PLATFORM_LIBC_VER _NEWLIB_VERSION
87-
#else
88-
#define PLATFORM_LIBC_LIB ""
89-
#define PLATFORM_LIBC_VER ""
90-
#endif
91-
92-
#if defined(__linux)
93-
#define PLATFORM_SYSTEM "Linux"
94-
#elif defined(__unix__)
95-
#define PLATFORM_SYSTEM "Unix"
96-
#elif defined(__CYGWIN__)
97-
#define PLATFORM_SYSTEM "Cygwin"
98-
#elif defined(_WIN32)
99-
#define PLATFORM_SYSTEM "Windows"
100-
#else
101-
#define PLATFORM_SYSTEM "MicroPython"
102-
#endif
103-
104-
#ifndef MICROPY_PLATFORM_VERSION
105-
#define MICROPY_PLATFORM_VERSION ""
106-
#endif
107-
108-
STATIC const MP_DEFINE_STR_OBJ(info_platform_obj, PLATFORM_SYSTEM "-" MICROPY_VERSION_STRING "-" \
109-
PLATFORM_ARCH "-" MICROPY_PLATFORM_VERSION "-with-" PLATFORM_LIBC_LIB "" PLATFORM_LIBC_VER);
110-
STATIC const MP_DEFINE_STR_OBJ(info_python_compiler_obj, PLATFORM_COMPILER);
111-
STATIC const MP_DEFINE_STR_OBJ(info_libc_lib_obj, PLATFORM_LIBC_LIB);
112-
STATIC const MP_DEFINE_STR_OBJ(info_libc_ver_obj, PLATFORM_LIBC_VER);
39+
STATIC const MP_DEFINE_STR_OBJ(info_platform_obj, MICROPY_PLATFORM_SYSTEM "-" \
40+
MICROPY_VERSION_STRING "-" MICROPY_PLATFORM_ARCH "-" MICROPY_PLATFORM_VERSION "-with-" \
41+
MICROPY_PLATFORM_LIBC_LIB "" MICROPY_PLATFORM_LIBC_VER);
42+
STATIC const MP_DEFINE_STR_OBJ(info_python_compiler_obj, MICROPY_PLATFORM_COMPILER);
43+
STATIC const MP_DEFINE_STR_OBJ(info_libc_lib_obj, MICROPY_PLATFORM_LIBC_LIB);
44+
STATIC const MP_DEFINE_STR_OBJ(info_libc_ver_obj, MICROPY_PLATFORM_LIBC_VER);
11345
STATIC const mp_rom_obj_tuple_t info_libc_tuple_obj = {
11446
{&mp_type_tuple}, 2, {MP_ROM_PTR(&info_libc_lib_obj), MP_ROM_PTR(&info_libc_ver_obj)}
11547
};

extmod/moduplatform.h

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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+
#ifndef MICROPY_INCLUDED_MODUPLATFORM_H
27+
#define MICROPY_INCLUDED_MODUPLATFORM_H
28+
29+
#include "py/misc.h" // For MP_STRINGIFY.
30+
#include "py/mpconfig.h"
31+
32+
// Preprocessor directives indentifying the platform.
33+
// The (u)platform module itself is guarded by MICROPY_PY_UPLATFORM, see the
34+
// .c file, but these are made available because they're generally usable.
35+
// TODO: Add more architectures, compilers and libraries.
36+
// See: https://sourceforge.net/p/predef/wiki/Home/
37+
38+
#if defined(__ARM_ARCH)
39+
#define MICROPY_PLATFORM_ARCH "arm"
40+
#elif defined(__x86_64__) || defined(_WIN64)
41+
#define MICROPY_PLATFORM_ARCH "x86_64"
42+
#elif defined(__i386__) || defined(_M_IX86)
43+
#define MICROPY_PLATFORM_ARCH "x86"
44+
#elif defined(__xtensa__) || defined(_M_IX86)
45+
#define MICROPY_PLATFORM_ARCH "xtensa"
46+
#else
47+
#define MICROPY_PLATFORM_ARCH ""
48+
#endif
49+
50+
#if defined(__GNUC__)
51+
#define MICROPY_PLATFORM_COMPILER \
52+
"GCC " \
53+
MP_STRINGIFY(__GNUC__) "." \
54+
MP_STRINGIFY(__GNUC_MINOR__) "." \
55+
MP_STRINGIFY(__GNUC_PATCHLEVEL__)
56+
#elif defined(__ARMCC_VERSION)
57+
#define MICROPY_PLATFORM_COMPILER \
58+
"ARMCC " \
59+
MP_STRINGIFY((__ARMCC_VERSION / 1000000)) "." \
60+
MP_STRINGIFY((__ARMCC_VERSION / 10000 % 100)) "." \
61+
MP_STRINGIFY((__ARMCC_VERSION % 10000))
62+
#elif defined(_MSC_VER)
63+
#if defined(_WIN64)
64+
#define MICROPY_PLATFORM_COMPILER_BITS "64 bit"
65+
#elif defined(_M_IX86)
66+
#define MICROPY_PLATFORM_COMPILER_BITS "32 bit"
67+
#else
68+
#define MICROPY_PLATFORM_COMPILER_BITS ""
69+
#endif
70+
#define MICROPY_PLATFORM_COMPILER \
71+
"MSC v." MP_STRINGIFY(_MSC_VER) " " MICROPY_PLATFORM_COMPILER_BITS
72+
#else
73+
#define MICROPY_PLATFORM_COMPILER ""
74+
#endif
75+
76+
#if defined(__GLIBC__)
77+
#define MICROPY_PLATFORM_LIBC_LIB "glibc"
78+
#define MICROPY_PLATFORM_LIBC_VER \
79+
MP_STRINGIFY(__GLIBC__) "." \
80+
MP_STRINGIFY(__GLIBC_MINOR__)
81+
#elif defined(__NEWLIB__)
82+
#define MICROPY_PLATFORM_LIBC_LIB "newlib"
83+
#define MICROPY_PLATFORM_LIBC_VER _NEWLIB_VERSION
84+
#else
85+
#define MICROPY_PLATFORM_LIBC_LIB ""
86+
#define MICROPY_PLATFORM_LIBC_VER ""
87+
#endif
88+
89+
#if defined(__linux)
90+
#define MICROPY_PLATFORM_SYSTEM "Linux"
91+
#elif defined(__unix__)
92+
#define MICROPY_PLATFORM_SYSTEM "Unix"
93+
#elif defined(__CYGWIN__)
94+
#define MICROPY_PLATFORM_SYSTEM "Cygwin"
95+
#elif defined(_WIN32)
96+
#define MICROPY_PLATFORM_SYSTEM "Windows"
97+
#else
98+
#define MICROPY_PLATFORM_SYSTEM "MicroPython"
99+
#endif
100+
101+
#ifndef MICROPY_PLATFORM_VERSION
102+
#define MICROPY_PLATFORM_VERSION ""
103+
#endif
104+
105+
#endif // MICROPY_INCLUDED_MODUPLATFORM_H

0 commit comments

Comments
 (0)