From 220afa2f55d52ae049bd77329e38ff1572777f0a Mon Sep 17 00:00:00 2001 From: David Lechner Date: Tue, 5 Jan 2021 15:37:30 -0600 Subject: [PATCH] pybricks: fix non-extern variables in header files In C, function definitions are implicitly extern, so the extern keyword is not needed. But variables are not implicitly extern, so the extern keyword is always needed. Prior to GCC 10, the `-fcommon` flag was set by default which silently ignored this mistake. Fixed by adding extern everywhere. This also uncovered a bug where `pybricks.parameters.Icon` was included on all hubs but would have resulted in a crash when used on hubs where it should have been omitted. Issue: https://github.com/pybricks/support/discussions/194 --- bricks/ev3dev/pb_ev3dev_types.h | 4 +- lib/pbio/include/pbio/control.h | 10 +-- pybricks/common.h | 14 ++-- pybricks/ev3devices.h | 12 +-- pybricks/experimental.h | 2 +- pybricks/geometry.h | 4 +- pybricks/hubs.h | 16 ++-- pybricks/iodevices.h | 14 ++-- pybricks/media.h | 2 +- pybricks/nxtdevices.h | 18 ++-- pybricks/parameters.h | 98 +++++++++++----------- pybricks/parameters/pb_module_parameters.c | 2 + pybricks/pupdevices.h | 20 ++--- pybricks/robotics.h | 4 +- pybricks/tools.h | 4 +- 15 files changed, 114 insertions(+), 110 deletions(-) diff --git a/bricks/ev3dev/pb_ev3dev_types.h b/bricks/ev3dev/pb_ev3dev_types.h index e32a50d85..d8664ff83 100644 --- a/bricks/ev3dev/pb_ev3dev_types.h +++ b/bricks/ev3dev/pb_ev3dev_types.h @@ -22,10 +22,10 @@ GrxFont *pb_ev3dev_Font_obj_get_font(mp_const_obj_t obj); // class Image -const mp_obj_type_t pb_type_ev3dev_Image; +extern const mp_obj_type_t pb_type_ev3dev_Image; // class Speaker -const mp_obj_type_t pb_type_ev3dev_Speaker; +extern const mp_obj_type_t pb_type_ev3dev_Speaker; #endif // PYBRICKS_INCLUDED_BRICKS_EV3DEV_TYPES_H diff --git a/lib/pbio/include/pbio/control.h b/lib/pbio/include/pbio/control.h index 2e8127146..09723deba 100644 --- a/lib/pbio/include/pbio/control.h +++ b/lib/pbio/include/pbio/control.h @@ -61,11 +61,11 @@ typedef bool (*pbio_control_on_target_t)(pbio_trajectory_t *trajectory, bool stalled); // Functions to check whether motion is done -pbio_control_on_target_t pbio_control_on_target_always; -pbio_control_on_target_t pbio_control_on_target_never; -pbio_control_on_target_t pbio_control_on_target_angle; -pbio_control_on_target_t pbio_control_on_target_time; -pbio_control_on_target_t pbio_control_on_target_stalled; +extern pbio_control_on_target_t pbio_control_on_target_always; +extern pbio_control_on_target_t pbio_control_on_target_never; +extern pbio_control_on_target_t pbio_control_on_target_angle; +extern pbio_control_on_target_t pbio_control_on_target_time; +extern pbio_control_on_target_t pbio_control_on_target_stalled; typedef enum { PBIO_CONTROL_NONE, /**< No control */ diff --git a/pybricks/common.h b/pybricks/common.h index e4301a217..805ad645e 100644 --- a/pybricks/common.h +++ b/pybricks/common.h @@ -28,8 +28,8 @@ mp_obj_t common_LightArray_obj_make_new(pb_device_t *pbdev, uint8_t light_mode, #ifdef PYBRICKS_PY_COMMON_LIGHT_MATRIX #include // pybricks._common.LightMatrix() -const uint8_t pb_digits_5x2[10][5]; -const uint8_t pb_font_5x5[95][5]; +extern const uint8_t pb_digits_5x2[10][5]; +extern const uint8_t pb_font_5x5[95][5]; mp_obj_t pb_type_Lightmatrix_obj_new(pbio_light_matrix_t *light_matrix); #endif @@ -39,7 +39,7 @@ mp_obj_t pb_type_Keypad_obj_new(uint8_t number_of_buttons, const pb_obj_enum_mem #endif // pybricks._common.Battery() -const mp_obj_module_t pb_module_battery; +extern const mp_obj_module_t pb_module_battery; #if PYBRICKS_PY_COMMON_MOTORS @@ -48,7 +48,7 @@ const mp_obj_module_t pb_module_battery; #include // pybricks._common.Control() -const mp_obj_type_t pb_type_Control; +extern const mp_obj_type_t pb_type_Control; mp_obj_t common_Control_obj_make_new(pbio_control_t *control); // pybricks._common.Logger() @@ -62,7 +62,7 @@ typedef struct _common_Motor_obj_t { mp_obj_t logger; } common_Motor_obj_t; -const mp_obj_type_t pb_type_Motor; +extern const mp_obj_type_t pb_type_Motor; // pybricks._common.DCMotor() typedef struct _common_DCMotor_obj_t { @@ -70,7 +70,7 @@ typedef struct _common_DCMotor_obj_t { pbio_dcmotor_t *dcmotor; } common_DCMotor_obj_t; -const mp_obj_type_t pb_type_DCMotor; +extern const mp_obj_type_t pb_type_DCMotor; // Nonstatic objects shared between Motor and DCMotor void common_DCMotor_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind); @@ -82,7 +82,7 @@ MP_DECLARE_CONST_FUN_OBJ_1(common_DCMotor_brake_obj); #if PYBRICKS_PY_COMMON_SPEAKER -const mp_obj_type_t pb_type_Speaker; +extern const mp_obj_type_t pb_type_Speaker; #endif // PYBRICKS_PY_COMMON_SPEAKER diff --git a/pybricks/ev3devices.h b/pybricks/ev3devices.h index 6c5e176f9..03bcdedc8 100644 --- a/pybricks/ev3devices.h +++ b/pybricks/ev3devices.h @@ -10,13 +10,13 @@ #include "py/obj.h" -const mp_obj_module_t pb_module_ev3devices; +extern const mp_obj_module_t pb_module_ev3devices; -const mp_obj_type_t pb_type_ev3devices_ColorSensor; -const mp_obj_type_t pb_type_ev3devices_InfraredSensor; -const mp_obj_type_t pb_type_ev3devices_GyroSensor; -const mp_obj_type_t pb_type_ev3devices_TouchSensor; -const mp_obj_type_t pb_type_ev3devices_UltrasonicSensor; +extern const mp_obj_type_t pb_type_ev3devices_ColorSensor; +extern const mp_obj_type_t pb_type_ev3devices_InfraredSensor; +extern const mp_obj_type_t pb_type_ev3devices_GyroSensor; +extern const mp_obj_type_t pb_type_ev3devices_TouchSensor; +extern const mp_obj_type_t pb_type_ev3devices_UltrasonicSensor; #endif // PYBRICKS_PY_EV3DEVICES diff --git a/pybricks/experimental.h b/pybricks/experimental.h index 5e360d190..bf99d4023 100644 --- a/pybricks/experimental.h +++ b/pybricks/experimental.h @@ -10,7 +10,7 @@ #include "py/obj.h" -const mp_obj_module_t pb_module_experimental; +extern const mp_obj_module_t pb_module_experimental; #endif // PYBRICKS_PY_EXPERIMENTAL diff --git a/pybricks/geometry.h b/pybricks/geometry.h index 62e14155b..bab57fdf6 100644 --- a/pybricks/geometry.h +++ b/pybricks/geometry.h @@ -12,9 +12,9 @@ #include "py/obj.h" -const mp_obj_module_t pb_module_geometry; +extern const mp_obj_module_t pb_module_geometry; -const mp_obj_type_t pb_type_Matrix; +extern const mp_obj_type_t pb_type_Matrix; typedef struct _pb_type_Matrix_obj_t { mp_obj_base_t base; diff --git a/pybricks/hubs.h b/pybricks/hubs.h index 266eb8e94..d5bb3e7a0 100644 --- a/pybricks/hubs.h +++ b/pybricks/hubs.h @@ -11,26 +11,26 @@ #include "py/obj.h" #if PYBRICKS_HUB_MOVEHUB -const mp_obj_type_t pb_type_MoveHub; +extern const mp_obj_type_t pb_type_MoveHub; #endif #if PYBRICKS_HUB_CITYHUB -const mp_obj_type_t pb_type_CityHub; +extern const mp_obj_type_t pb_type_CityHub; #endif #if PYBRICKS_HUB_TECHNICHUB -const mp_obj_type_t pb_type_TechnicHub; +extern const mp_obj_type_t pb_type_TechnicHub; #endif #ifdef PYBRICKS_HUB_PRIMEHUB -const mp_obj_type_t pb_type_PrimeHub; -const mp_obj_type_t pb_type_InventorHub; +extern const mp_obj_type_t pb_type_PrimeHub; +extern const mp_obj_type_t pb_type_InventorHub; #endif #if PYBRICKS_HUB_NXTBRICK -const mp_obj_type_t pb_type_NXTBrick; +extern const mp_obj_type_t pb_type_NXTBrick; #endif #if PYBRICKS_HUB_EV3BRICK -const mp_obj_type_t pb_type_EV3Brick; +extern const mp_obj_type_t pb_type_EV3Brick; #endif -const mp_obj_module_t pb_module_hubs; +extern const mp_obj_module_t pb_module_hubs; #endif // PYBRICKS_PY_HUBS diff --git a/pybricks/iodevices.h b/pybricks/iodevices.h index b8bffa31e..a2d3b504b 100644 --- a/pybricks/iodevices.h +++ b/pybricks/iodevices.h @@ -10,21 +10,21 @@ #include "py/obj.h" -const mp_obj_module_t pb_module_iodevices; +extern const mp_obj_module_t pb_module_iodevices; #if PYBRICKS_PY_PUPDEVICES -const mp_obj_type_t pb_type_iodevices_PUPDevice; +extern const mp_obj_type_t pb_type_iodevices_PUPDevice; #endif // PYBRICKS_PY_PUPDEVICES #if PYBRICKS_PY_EV3DEVICES -const mp_obj_type_t pb_type_iodevices_LUMPDevice; -const mp_obj_type_t pb_type_iodevices_AnalogSensor; -const mp_obj_type_t pb_type_iodevices_Ev3devSensor; -const mp_obj_type_t pb_type_iodevices_I2CDevice; -const mp_obj_type_t pb_type_iodevices_UARTDevice; +extern const mp_obj_type_t pb_type_iodevices_LUMPDevice; +extern const mp_obj_type_t pb_type_iodevices_AnalogSensor; +extern const mp_obj_type_t pb_type_iodevices_Ev3devSensor; +extern const mp_obj_type_t pb_type_iodevices_I2CDevice; +extern const mp_obj_type_t pb_type_iodevices_UARTDevice; #endif // PYBRICKS_PY_EV3DEVICES diff --git a/pybricks/media.h b/pybricks/media.h index 952aac4cd..4d73268d8 100644 --- a/pybricks/media.h +++ b/pybricks/media.h @@ -10,7 +10,7 @@ #include "py/obj.h" -const mp_obj_module_t pb_module_media; +extern const mp_obj_module_t pb_module_media; #endif // PYBRICKS_PY_MEDIA diff --git a/pybricks/nxtdevices.h b/pybricks/nxtdevices.h index 9cae3811c..2d6f6cb66 100644 --- a/pybricks/nxtdevices.h +++ b/pybricks/nxtdevices.h @@ -10,15 +10,15 @@ #include "py/obj.h" -const mp_obj_module_t pb_module_nxtdevices; - -const mp_obj_type_t pb_type_nxtdevices_ColorSensor; -const mp_obj_type_t pb_type_nxtdevices_EnergyMeter; -const mp_obj_type_t pb_type_nxtdevices_LightSensor; -const mp_obj_type_t pb_type_nxtdevices_SoundSensor; -const mp_obj_type_t pb_type_nxtdevices_TemperatureSensor; -const mp_obj_type_t pb_type_nxtdevices_TouchSensor; -const mp_obj_type_t pb_type_nxtdevices_UltrasonicSensor; +extern const mp_obj_module_t pb_module_nxtdevices; + +extern const mp_obj_type_t pb_type_nxtdevices_ColorSensor; +extern const mp_obj_type_t pb_type_nxtdevices_EnergyMeter; +extern const mp_obj_type_t pb_type_nxtdevices_LightSensor; +extern const mp_obj_type_t pb_type_nxtdevices_SoundSensor; +extern const mp_obj_type_t pb_type_nxtdevices_TemperatureSensor; +extern const mp_obj_type_t pb_type_nxtdevices_TouchSensor; +extern const mp_obj_type_t pb_type_nxtdevices_UltrasonicSensor; int32_t analog_scale(int32_t mvolts, int32_t mvolts_min, int32_t mvolts_max, bool invert); diff --git a/pybricks/parameters.h b/pybricks/parameters.h index 6f3470605..0b4ad22ca 100644 --- a/pybricks/parameters.h +++ b/pybricks/parameters.h @@ -16,27 +16,27 @@ #if PYBRICKS_PY_PARAMETERS_BUTTON -const mp_obj_type_t pb_enum_type_Button; - -const pb_obj_enum_member_t pb_Button_UP_obj; -const pb_obj_enum_member_t pb_Button_DOWN_obj; -const pb_obj_enum_member_t pb_Button_LEFT_obj; -const pb_obj_enum_member_t pb_Button_RIGHT_obj; -const pb_obj_enum_member_t pb_Button_RIGHT_PLUS_obj; -const pb_obj_enum_member_t pb_Button_RIGHT_MINUS_obj; -const pb_obj_enum_member_t pb_Button_CENTER_obj; -const pb_obj_enum_member_t pb_Button_LEFT_UP_obj; -const pb_obj_enum_member_t pb_Button_LEFT_DOWN_obj; -const pb_obj_enum_member_t pb_Button_LEFT_PLUS_obj; -const pb_obj_enum_member_t pb_Button_LEFT_MINUS_obj; -const pb_obj_enum_member_t pb_Button_RIGHT_UP_obj; -const pb_obj_enum_member_t pb_Button_RIGHT_DOWN_obj; -const pb_obj_enum_member_t pb_Button_BEACON_obj; -const pb_obj_enum_member_t pb_Button_BLUETOOTH_obj; +extern const mp_obj_type_t pb_enum_type_Button; + +extern const pb_obj_enum_member_t pb_Button_UP_obj; +extern const pb_obj_enum_member_t pb_Button_DOWN_obj; +extern const pb_obj_enum_member_t pb_Button_LEFT_obj; +extern const pb_obj_enum_member_t pb_Button_RIGHT_obj; +extern const pb_obj_enum_member_t pb_Button_RIGHT_PLUS_obj; +extern const pb_obj_enum_member_t pb_Button_RIGHT_MINUS_obj; +extern const pb_obj_enum_member_t pb_Button_CENTER_obj; +extern const pb_obj_enum_member_t pb_Button_LEFT_UP_obj; +extern const pb_obj_enum_member_t pb_Button_LEFT_DOWN_obj; +extern const pb_obj_enum_member_t pb_Button_LEFT_PLUS_obj; +extern const pb_obj_enum_member_t pb_Button_LEFT_MINUS_obj; +extern const pb_obj_enum_member_t pb_Button_RIGHT_UP_obj; +extern const pb_obj_enum_member_t pb_Button_RIGHT_DOWN_obj; +extern const pb_obj_enum_member_t pb_Button_BEACON_obj; +extern const pb_obj_enum_member_t pb_Button_BLUETOOTH_obj; #endif // PYBRICKS_PY_PARAMETERS_BUTTON -const mp_obj_type_t pb_type_Color; +extern const mp_obj_type_t pb_type_Color; typedef struct _pb_type_Color_obj_t { mp_obj_base_t base; @@ -47,45 +47,47 @@ typedef struct _pb_type_Color_obj_t { pb_type_Color_obj_t *pb_type_Color_new_empty(void); const pbio_color_hsv_t *pb_type_Color_get_hsv(mp_obj_t obj); -const pb_type_Color_obj_t pb_Color_RED_obj; -const pb_type_Color_obj_t pb_Color_BROWN_obj; -const pb_type_Color_obj_t pb_Color_ORANGE_obj; -const pb_type_Color_obj_t pb_Color_YELLOW_obj; -const pb_type_Color_obj_t pb_Color_GREEN_obj; -const pb_type_Color_obj_t pb_Color_CYAN_obj; -const pb_type_Color_obj_t pb_Color_BLUE_obj; -const pb_type_Color_obj_t pb_Color_VIOLET_obj; -const pb_type_Color_obj_t pb_Color_MAGENTA_obj; -const pb_type_Color_obj_t pb_Color_NONE_obj; -const pb_type_Color_obj_t pb_Color_BLACK_obj; -const pb_type_Color_obj_t pb_Color_GRAY_obj; -const pb_type_Color_obj_t pb_Color_WHITE_obj; +extern const pb_type_Color_obj_t pb_Color_RED_obj; +extern const pb_type_Color_obj_t pb_Color_BROWN_obj; +extern const pb_type_Color_obj_t pb_Color_ORANGE_obj; +extern const pb_type_Color_obj_t pb_Color_YELLOW_obj; +extern const pb_type_Color_obj_t pb_Color_GREEN_obj; +extern const pb_type_Color_obj_t pb_Color_CYAN_obj; +extern const pb_type_Color_obj_t pb_Color_BLUE_obj; +extern const pb_type_Color_obj_t pb_Color_VIOLET_obj; +extern const pb_type_Color_obj_t pb_Color_MAGENTA_obj; +extern const pb_type_Color_obj_t pb_Color_NONE_obj; +extern const pb_type_Color_obj_t pb_Color_BLACK_obj; +extern const pb_type_Color_obj_t pb_Color_GRAY_obj; +extern const pb_type_Color_obj_t pb_Color_WHITE_obj; -const mp_obj_type_t pb_enum_type_Direction; +extern const mp_obj_type_t pb_enum_type_Direction; -const pb_obj_enum_member_t pb_Direction_CLOCKWISE_obj; -const pb_obj_enum_member_t pb_Direction_COUNTERCLOCKWISE_obj; +extern const pb_obj_enum_member_t pb_Direction_CLOCKWISE_obj; +extern const pb_obj_enum_member_t pb_Direction_COUNTERCLOCKWISE_obj; -const mp_obj_base_t pb_Icon_obj; +#if PYBRICKS_PY_PARAMETERS_ICON +extern const mp_obj_base_t pb_Icon_obj; +#endif -const mp_obj_type_t pb_enum_type_Port; +extern const mp_obj_type_t pb_enum_type_Port; -const mp_obj_type_t pb_enum_type_Stop; +extern const mp_obj_type_t pb_enum_type_Stop; -const pb_obj_enum_member_t pb_Stop_COAST_obj; -const pb_obj_enum_member_t pb_Stop_BRAKE_obj; -const pb_obj_enum_member_t pb_Stop_HOLD_obj; +extern const pb_obj_enum_member_t pb_Stop_COAST_obj; +extern const pb_obj_enum_member_t pb_Stop_BRAKE_obj; +extern const pb_obj_enum_member_t pb_Stop_HOLD_obj; -const mp_obj_type_t pb_enum_type_Side; +extern const mp_obj_type_t pb_enum_type_Side; -const pb_obj_enum_member_t pb_Side_BACK_obj; -const pb_obj_enum_member_t pb_Side_BOTTOM_obj; -const pb_obj_enum_member_t pb_Side_FRONT_obj; -const pb_obj_enum_member_t pb_Side_LEFT_obj; -const pb_obj_enum_member_t pb_Side_RIGHT_obj; -const pb_obj_enum_member_t pb_Side_TOP_obj; +extern const pb_obj_enum_member_t pb_Side_BACK_obj; +extern const pb_obj_enum_member_t pb_Side_BOTTOM_obj; +extern const pb_obj_enum_member_t pb_Side_FRONT_obj; +extern const pb_obj_enum_member_t pb_Side_LEFT_obj; +extern const pb_obj_enum_member_t pb_Side_RIGHT_obj; +extern const pb_obj_enum_member_t pb_Side_TOP_obj; -const mp_obj_module_t pb_module_parameters; +extern const mp_obj_module_t pb_module_parameters; #endif // PYBRICKS_PY_PARAMETERS diff --git a/pybricks/parameters/pb_module_parameters.c b/pybricks/parameters/pb_module_parameters.c index f3da8b888..ae88b1cd2 100644 --- a/pybricks/parameters/pb_module_parameters.c +++ b/pybricks/parameters/pb_module_parameters.c @@ -14,7 +14,9 @@ STATIC const mp_rom_map_elem_t parameters_globals_table[] = { #endif { MP_ROM_QSTR(MP_QSTR_Color), MP_ROM_PTR(&pb_type_Color) }, { MP_ROM_QSTR(MP_QSTR_Direction), MP_ROM_PTR(&pb_enum_type_Direction) }, + #if PYBRICKS_PY_PARAMETERS_ICON { MP_ROM_QSTR(MP_QSTR_Icon), MP_ROM_PTR(&pb_Icon_obj) }, + #endif { MP_ROM_QSTR(MP_QSTR_Port), MP_ROM_PTR(&pb_enum_type_Port) }, { MP_ROM_QSTR(MP_QSTR_Side), MP_ROM_PTR(&pb_enum_type_Side) }, { MP_ROM_QSTR(MP_QSTR_Stop), MP_ROM_PTR(&pb_enum_type_Stop) }, diff --git a/pybricks/pupdevices.h b/pybricks/pupdevices.h index ab0cf1836..bd064f4fd 100644 --- a/pybricks/pupdevices.h +++ b/pybricks/pupdevices.h @@ -12,16 +12,16 @@ #include -const mp_obj_module_t pb_module_pupdevices; - -const mp_obj_type_t pb_type_pupdevices_ColorDistanceSensor; -const mp_obj_type_t pb_type_pupdevices_ColorSensor; -const mp_obj_type_t pb_type_pupdevices_ForceSensor; -const mp_obj_type_t pb_type_pupdevices_InfraredSensor; -const mp_obj_type_t pb_type_pupdevices_Light; -const mp_obj_type_t pb_type_pupdevices_PFMotor; -const mp_obj_type_t pb_type_pupdevices_TiltSensor; -const mp_obj_type_t pb_type_pupdevices_UltrasonicSensor; +extern const mp_obj_module_t pb_module_pupdevices; + +extern const mp_obj_type_t pb_type_pupdevices_ColorDistanceSensor; +extern const mp_obj_type_t pb_type_pupdevices_ColorSensor; +extern const mp_obj_type_t pb_type_pupdevices_ForceSensor; +extern const mp_obj_type_t pb_type_pupdevices_InfraredSensor; +extern const mp_obj_type_t pb_type_pupdevices_Light; +extern const mp_obj_type_t pb_type_pupdevices_PFMotor; +extern const mp_obj_type_t pb_type_pupdevices_TiltSensor; +extern const mp_obj_type_t pb_type_pupdevices_UltrasonicSensor; pb_device_t *pupdevices_ColorDistanceSensor__get_device(mp_obj_t obj); diff --git a/pybricks/robotics.h b/pybricks/robotics.h index 57605a6cc..aaf4b9873 100644 --- a/pybricks/robotics.h +++ b/pybricks/robotics.h @@ -13,9 +13,9 @@ #include "py/obj.h" -const mp_obj_type_t pb_type_drivebase; +extern const mp_obj_type_t pb_type_drivebase; -const mp_obj_module_t pb_module_robotics; +extern const mp_obj_module_t pb_module_robotics; #endif // PYBRICKS_PY_ROBOTICS diff --git a/pybricks/tools.h b/pybricks/tools.h index 487af2ec6..6d980c944 100644 --- a/pybricks/tools.h +++ b/pybricks/tools.h @@ -10,9 +10,9 @@ #include "py/obj.h" -const mp_obj_type_t pb_type_StopWatch; +extern const mp_obj_type_t pb_type_StopWatch; -const mp_obj_module_t pb_module_tools; +extern const mp_obj_module_t pb_module_tools; #endif // PYBRICKS_PY_TOOLS