Skip to content

Commit

Permalink
Fix several build errors in CircuitPython (#533)
Browse files Browse the repository at this point in the history
* Properly register submodules of ulab

This is related to
 * adafruit/circuitpython#6066

in which, after the merge of 1.18 into CircuitPython, we lost the ability
to import submodules of built-in modules.

While reconstructing the changes we had made locally to enable this,
I discovered that there was an easier way: simply register the dotted
module names via MP_REGISTER_MODULE.

* Fix finding processor count when no `python` executable is installed

debian likes to install only `python3`, and not `python` (which was,
for many decades, python2).

This was previously done for `build.sh` but not for `build-cp.sh`.

* Only use this submodule feature in CircuitPython

.. as it does not work properly in MicroPython.

Also, modules to be const. This saves a small amount of RAM

* Fix -Werror=undef diagnostic

Most CircuitPython ports build with -Werror=undef, so that use of an
undefined preprocessor flag is an error. Also, CircuitPython's micropython
version is old enough that MICROPY_VERSION is not (ever) defined.

Defensively check for this macro being defined, and use the older style
of MP_REGISTER_MODULE when it is not.

* Fix -Werror=discarded-qualifiers diagnostics

Most CircuitPython ports build with -Werror=discarded-qualifiers.
This detected a problem where string constants were passed to functions
with non-constant parameter types.

* bump version number

* Use MicroPython-compatible registration of submodules

* straggler

* Remove spurious casts

these were build errors for micropython

* Run tests for both nanbox and regular variant during CI
  • Loading branch information
jepler committed Jul 7, 2022
1 parent 1347694 commit 308627c
Show file tree
Hide file tree
Showing 24 changed files with 100 additions and 30 deletions.
2 changes: 1 addition & 1 deletion build-cp.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ readlinkf_posix() {
done
return 1
}
NPROC=$(python -c 'import multiprocessing; print(multiprocessing.cpu_count())')
NPROC=$(python3 -c 'import multiprocessing; print(multiprocessing.cpu_count())')
HERE="$(dirname -- "$(readlinkf_posix -- "${0}")" )"
[ -e circuitpython/py/py.mk ] || (git clone --branch main https://github.com/adafruit/circuitpython && cd circuitpython && make fetch-submodules && git submodule update --init lib/uzlib tools)
rm -rf circuitpython/extmod/ulab; ln -s "$HERE" circuitpython/extmod/ulab
Expand Down
5 changes: 3 additions & 2 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,12 @@ make -C micropython/mpy-cross -j${NPROC}
make -C micropython/ports/unix -j${NPROC} axtls
make -C micropython/ports/unix -j${NPROC} USER_C_MODULES="${HERE}" DEBUG=1 STRIP=: MICROPY_PY_FFI=0 MICROPY_PY_BTREE=0 CFLAGS_EXTRA=-DULAB_MAX_DIMS=$dims CFLAGS_EXTRA+=-DULAB_HASH=$GIT_HASH BUILD=build-$dims PROG=micropython-$dims

bash test-common.sh "${dims}" "micropython/ports/unix/micropython-$dims"

# The unix nanbox variant builds as a 32-bit executable and requires gcc-multilib.
# macOS doesn't support i386 builds so only build on linux.
if [ $PLATFORM = linux ]; then
make -C micropython/ports/unix -j${NPROC} VARIANT=nanbox USER_C_MODULES="${HERE}" DEBUG=1 STRIP=: MICROPY_PY_FFI=0 MICROPY_PY_BTREE=0 CFLAGS_EXTRA=-DULAB_MAX_DIMS=$dims CFLAGS_EXTRA+=-DULAB_HASH=$GIT_HASH BUILD=build-nanbox-$dims PROG=micropython-nanbox-$dims
bash test-common.sh "${dims}" "micropython/ports/unix/micropython-$dims"
fi

bash test-common.sh "${dims}" "micropython/ports/unix/micropython-$dims"

9 changes: 8 additions & 1 deletion code/numpy/fft/fft.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,14 @@ STATIC const mp_rom_map_elem_t ulab_fft_globals_table[] = {

STATIC MP_DEFINE_CONST_DICT(mp_module_ulab_fft_globals, ulab_fft_globals_table);

mp_obj_module_t ulab_fft_module = {
const mp_obj_module_t ulab_fft_module = {
.base = { &mp_type_module },
.globals = (mp_obj_dict_t*)&mp_module_ulab_fft_globals,
};
#if CIRCUITPY_ULAB
#if !defined(MICROPY_VERSION) || MICROPY_VERSION <= 70144
MP_REGISTER_MODULE(MP_QSTR_ulab_dot_numpy_dot_fft, ulab_fft_module, MODULE_ULAB_ENABLED);
#else
MP_REGISTER_MODULE(MP_QSTR_ulab_dot_numpy_dot_fft, ulab_fft_module);
#endif
#endif
2 changes: 1 addition & 1 deletion code/numpy/fft/fft.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#include "../../ndarray.h"
#include "fft_tools.h"

extern mp_obj_module_t ulab_fft_module;
extern const mp_obj_module_t ulab_fft_module;

#if ULAB_SUPPORTS_COMPLEX & ULAB_FFT_IS_NUMPY_COMPATIBLE
MP_DECLARE_CONST_FUN_OBJ_3(fft_fft_obj);
Expand Down
6 changes: 3 additions & 3 deletions code/numpy/io/io.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
#define ULAB_IO_BIG_ENDIAN 2

#if ULAB_NUMPY_HAS_LOAD
static void io_read_(mp_obj_t stream, const mp_stream_p_t *stream_p, char *buffer, char *string, uint16_t len, int *error) {
static void io_read_(mp_obj_t stream, const mp_stream_p_t *stream_p, char *buffer, const char *string, uint16_t len, int *error) {
size_t read = stream_p->read(stream, buffer, len, error);
bool fail = false;
if(read == len) {
Expand Down Expand Up @@ -668,7 +668,7 @@ MP_DEFINE_CONST_FUN_OBJ_2(io_save_obj, io_save);
#endif /* ULAB_NUMPY_HAS_SAVE */

#if ULAB_NUMPY_HAS_SAVETXT
static int8_t io_format_float(ndarray_obj_t *ndarray, mp_float_t (*func)(void *), uint8_t *array, char *buffer, char *delimiter) {
static int8_t io_format_float(ndarray_obj_t *ndarray, mp_float_t (*func)(void *), uint8_t *array, char *buffer, const char *delimiter) {
// own implementation of float formatting for platforms that don't have sprintf
int8_t offset = 0;

Expand Down Expand Up @@ -814,4 +814,4 @@ static mp_obj_t io_savetxt(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw
}

MP_DEFINE_CONST_FUN_OBJ_KW(io_savetxt_obj, 2, io_savetxt);
#endif /* ULAB_NUMPY_HAS_SAVETXT */
#endif /* ULAB_NUMPY_HAS_SAVETXT */
10 changes: 8 additions & 2 deletions code/numpy/linalg/linalg.c
Original file line number Diff line number Diff line change
Expand Up @@ -532,9 +532,15 @@ STATIC const mp_rom_map_elem_t ulab_linalg_globals_table[] = {

STATIC MP_DEFINE_CONST_DICT(mp_module_ulab_linalg_globals, ulab_linalg_globals_table);

mp_obj_module_t ulab_linalg_module = {
const mp_obj_module_t ulab_linalg_module = {
.base = { &mp_type_module },
.globals = (mp_obj_dict_t*)&mp_module_ulab_linalg_globals,
};

#if CIRCUITPY_ULAB
#if !defined(MICROPY_VERSION) || MICROPY_VERSION <= 70144
MP_REGISTER_MODULE(MP_QSTR_ulab_dot_numpy_dot_linalg, ulab_linalg_module, MODULE_ULAB_ENABLED);
#else
MP_REGISTER_MODULE(MP_QSTR_ulab_dot_numpy_dot_linalg, ulab_linalg_module);
#endif
#endif
#endif
2 changes: 1 addition & 1 deletion code/numpy/linalg/linalg.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#include "../../ndarray.h"
#include "linalg_tools.h"

extern mp_obj_module_t ulab_linalg_module;
extern const mp_obj_module_t ulab_linalg_module;

MP_DECLARE_CONST_FUN_OBJ_1(linalg_cholesky_obj);
MP_DECLARE_CONST_FUN_OBJ_1(linalg_det_obj);
Expand Down
10 changes: 9 additions & 1 deletion code/numpy/numpy.c
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,15 @@ static const mp_rom_map_elem_t ulab_numpy_globals_table[] = {

static MP_DEFINE_CONST_DICT(mp_module_ulab_numpy_globals, ulab_numpy_globals_table);

mp_obj_module_t ulab_numpy_module = {
const mp_obj_module_t ulab_numpy_module = {
.base = { &mp_type_module },
.globals = (mp_obj_dict_t*)&mp_module_ulab_numpy_globals,
};

#if CIRCUITPY_ULAB
#if !defined(MICROPY_VERSION) || MICROPY_VERSION <= 70144
MP_REGISTER_MODULE(MP_QSTR_ulab_dot_numpy, ulab_numpy_module, MODULE_ULAB_ENABLED);
#else
MP_REGISTER_MODULE(MP_QSTR_ulab_dot_numpy, ulab_numpy_module);
#endif
#endif
2 changes: 1 addition & 1 deletion code/numpy/numpy.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@
#include "../ulab.h"
#include "../ndarray.h"

extern mp_obj_module_t ulab_numpy_module;
extern const mp_obj_module_t ulab_numpy_module;

#endif /* _NUMPY_ */
10 changes: 8 additions & 2 deletions code/scipy/linalg/linalg.c
Original file line number Diff line number Diff line change
Expand Up @@ -271,9 +271,15 @@ static const mp_rom_map_elem_t ulab_scipy_linalg_globals_table[] = {

static MP_DEFINE_CONST_DICT(mp_module_ulab_scipy_linalg_globals, ulab_scipy_linalg_globals_table);

mp_obj_module_t ulab_scipy_linalg_module = {
const mp_obj_module_t ulab_scipy_linalg_module = {
.base = { &mp_type_module },
.globals = (mp_obj_dict_t*)&mp_module_ulab_scipy_linalg_globals,
};

#if CIRCUITPY_ULAB
#if !defined(MICROPY_VERSION) || MICROPY_VERSION <= 70144
MP_REGISTER_MODULE(MP_QSTR_ulab_dot_scipy_dot_linalg, ulab_scipy_linalg_module, MODULE_ULAB_ENABLED);
#else
MP_REGISTER_MODULE(MP_QSTR_ulab_dot_scipy_dot_linalg, ulab_scipy_linalg_module);
#endif
#endif
#endif
2 changes: 1 addition & 1 deletion code/scipy/linalg/linalg.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#ifndef _SCIPY_LINALG_
#define _SCIPY_LINALG_

extern mp_obj_module_t ulab_scipy_linalg_module;
extern const mp_obj_module_t ulab_scipy_linalg_module;

MP_DECLARE_CONST_FUN_OBJ_KW(linalg_solve_triangular_obj);
MP_DECLARE_CONST_FUN_OBJ_2(linalg_cho_solve_obj);
Expand Down
9 changes: 8 additions & 1 deletion code/scipy/optimize/optimize.c
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,14 @@ static const mp_rom_map_elem_t ulab_scipy_optimize_globals_table[] = {

static MP_DEFINE_CONST_DICT(mp_module_ulab_scipy_optimize_globals, ulab_scipy_optimize_globals_table);

mp_obj_module_t ulab_scipy_optimize_module = {
const mp_obj_module_t ulab_scipy_optimize_module = {
.base = { &mp_type_module },
.globals = (mp_obj_dict_t*)&mp_module_ulab_scipy_optimize_globals,
};
#if CIRCUITPY_ULAB
#if !defined(MICROPY_VERSION) || MICROPY_VERSION <= 70144
MP_REGISTER_MODULE(MP_QSTR_ulab_dot_scipy_dot_optimize, ulab_scipy_optimize_module, MODULE_ULAB_ENABLED);
#else
MP_REGISTER_MODULE(MP_QSTR_ulab_dot_scipy_dot_optimize, ulab_scipy_optimize_module);
#endif
#endif
2 changes: 1 addition & 1 deletion code/scipy/optimize/optimize.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
#define OPTIMIZE_GAMMA MICROPY_FLOAT_CONST(0.5)
#define OPTIMIZE_DELTA MICROPY_FLOAT_CONST(0.5)

extern mp_obj_module_t ulab_scipy_optimize_module;
extern const mp_obj_module_t ulab_scipy_optimize_module;

MP_DECLARE_CONST_FUN_OBJ_KW(optimize_bisect_obj);
MP_DECLARE_CONST_FUN_OBJ_KW(optimize_curve_fit_obj);
Expand Down
9 changes: 8 additions & 1 deletion code/scipy/scipy.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,15 @@ static const mp_rom_map_elem_t ulab_scipy_globals_table[] = {

static MP_DEFINE_CONST_DICT(mp_module_ulab_scipy_globals, ulab_scipy_globals_table);

mp_obj_module_t ulab_scipy_module = {
const mp_obj_module_t ulab_scipy_module = {
.base = { &mp_type_module },
.globals = (mp_obj_dict_t*)&mp_module_ulab_scipy_globals,
};
#if CIRCUITPY_ULAB
#if !defined(MICROPY_VERSION) || MICROPY_VERSION <= 70144
MP_REGISTER_MODULE(MP_QSTR_ulab_dot_scipy, ulab_scipy_module, MODULE_ULAB_ENABLED);
#else
MP_REGISTER_MODULE(MP_QSTR_ulab_dot_scipy, ulab_scipy_module);
#endif
#endif
#endif /* ULAB_HAS_SCIPY */
2 changes: 1 addition & 1 deletion code/scipy/scipy.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@
#include "../ulab.h"
#include "../ndarray.h"

extern mp_obj_module_t ulab_scipy_module;
extern const mp_obj_module_t ulab_scipy_module;

#endif /* _SCIPY_ */
9 changes: 8 additions & 1 deletion code/scipy/signal/signal.c
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,14 @@ static const mp_rom_map_elem_t ulab_scipy_signal_globals_table[] = {

static MP_DEFINE_CONST_DICT(mp_module_ulab_scipy_signal_globals, ulab_scipy_signal_globals_table);

mp_obj_module_t ulab_scipy_signal_module = {
const mp_obj_module_t ulab_scipy_signal_module = {
.base = { &mp_type_module },
.globals = (mp_obj_dict_t*)&mp_module_ulab_scipy_signal_globals,
};
#if CIRCUITPY_ULAB
#if !defined(MICROPY_VERSION) || MICROPY_VERSION <= 70144
MP_REGISTER_MODULE(MP_QSTR_ulab_dot_scipy_dot_signal, ulab_scipy_signal_module, MODULE_ULAB_ENABLED);
#else
MP_REGISTER_MODULE(MP_QSTR_ulab_dot_scipy_dot_signal, ulab_scipy_signal_module);
#endif
#endif
2 changes: 1 addition & 1 deletion code/scipy/signal/signal.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#include "../../ulab.h"
#include "../../ndarray.h"

extern mp_obj_module_t ulab_scipy_signal_module;
extern const mp_obj_module_t ulab_scipy_signal_module;

MP_DECLARE_CONST_FUN_OBJ_KW(signal_sosfilt_obj);

Expand Down
9 changes: 8 additions & 1 deletion code/scipy/special/special.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,14 @@ static const mp_rom_map_elem_t ulab_scipy_special_globals_table[] = {

static MP_DEFINE_CONST_DICT(mp_module_ulab_scipy_special_globals, ulab_scipy_special_globals_table);

mp_obj_module_t ulab_scipy_special_module = {
const mp_obj_module_t ulab_scipy_special_module = {
.base = { &mp_type_module },
.globals = (mp_obj_dict_t*)&mp_module_ulab_scipy_special_globals,
};
#if CIRCUITPY_ULAB
#if !defined(MICROPY_VERSION) || MICROPY_VERSION <= 70144
MP_REGISTER_MODULE(MP_QSTR_ulab_dot_scipy_dot_special, ulab_scipy_special_module, MODULE_ULAB_ENABLED);
#else
MP_REGISTER_MODULE(MP_QSTR_ulab_dot_scipy_dot_special, ulab_scipy_special_module);
#endif
#endif
2 changes: 1 addition & 1 deletion code/scipy/special/special.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@
#include "../../ulab.h"
#include "../../ndarray.h"

extern mp_obj_module_t ulab_scipy_special_module;
extern const mp_obj_module_t ulab_scipy_special_module;

#endif /* _SCIPY_SPECIAL_ */
4 changes: 2 additions & 2 deletions code/ulab.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
#include "user/user.h"
#include "utils/utils.h"

#define ULAB_VERSION 5.0.8
#define ULAB_VERSION 5.0.9
#define xstr(s) str(s)
#define str(s) #s

Expand Down Expand Up @@ -190,7 +190,7 @@ const mp_obj_module_t ulab_user_cmodule = {

// Use old three-argument MP_REGISTER_MODULE for
// MicroPython <= v1.18.0: (1 << 16) | (18 << 8) | 0
#if MICROPY_VERSION <= 70144
#if !defined(MICROPY_VERSION) || MICROPY_VERSION <= 70144
MP_REGISTER_MODULE(MP_QSTR_ulab, ulab_user_cmodule, MODULE_ULAB_ENABLED);
#else
MP_REGISTER_MODULE(MP_QSTR_ulab, ulab_user_cmodule);
Expand Down
9 changes: 8 additions & 1 deletion code/user/user.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,16 @@ static const mp_rom_map_elem_t ulab_user_globals_table[] = {

static MP_DEFINE_CONST_DICT(mp_module_ulab_user_globals, ulab_user_globals_table);

mp_obj_module_t ulab_user_module = {
const mp_obj_module_t ulab_user_module = {
.base = { &mp_type_module },
.globals = (mp_obj_dict_t*)&mp_module_ulab_user_globals,
};
#if CIRCUITPY_ULAB
#if !defined(MICROPY_VERSION) || MICROPY_VERSION <= 70144
MP_REGISTER_MODULE(MP_QSTR_ulab_dot_user, ulab_user_module, ULAB_HAS_USER_MODULE);
#else
MP_REGISTER_MODULE(MP_QSTR_ulab_dot_user, ulab_user_module);
#endif
#endif

#endif
2 changes: 1 addition & 1 deletion code/user/user.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@
#include "../ulab.h"
#include "../ndarray.h"

extern mp_obj_module_t ulab_user_module;
extern const mp_obj_module_t ulab_user_module;

#endif
9 changes: 8 additions & 1 deletion code/utils/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,16 @@ static const mp_rom_map_elem_t ulab_utils_globals_table[] = {

static MP_DEFINE_CONST_DICT(mp_module_ulab_utils_globals, ulab_utils_globals_table);

mp_obj_module_t ulab_utils_module = {
const mp_obj_module_t ulab_utils_module = {
.base = { &mp_type_module },
.globals = (mp_obj_dict_t*)&mp_module_ulab_utils_globals,
};
#if CIRCUITPY_ULAB
#if !defined(MICROPY_VERSION) || MICROPY_VERSION <= 70144
MP_REGISTER_MODULE(MP_QSTR_ulab_dot_utils, ulab_utils_module, MODULE_ULAB_ENABLED);
#else
MP_REGISTER_MODULE(MP_QSTR_ulab_dot_utils, ulab_utils_module);
#endif
#endif

#endif /* ULAB_HAS_UTILS_MODULE */
2 changes: 1 addition & 1 deletion code/utils/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@
#include "../ulab.h"
#include "../ndarray.h"

extern mp_obj_module_t ulab_utils_module;
extern const mp_obj_module_t ulab_utils_module;

#endif

0 comments on commit 308627c

Please sign in to comment.