Skip to content

Commit

Permalink
ports/Makefile: Assign C optimization options conditionally
Browse files Browse the repository at this point in the history
-Og introduced in for example 9ffb1ad and c5dbbf7 results in a binary
with debug symbols, but essentially breaks debugging with gdb since
it still allows the compiler to optimize temporaries for instance,
so gdb will report 'optimized out' for a lot of variables and will
often halt on other lines than where breakpoints are defined.
-O0 has none of these problems, but also makes for a large binary, so
allow specifying it via COPT=-O0 by making all Makefiles consistently
use this flag and related CSUPEROPT as well.

Signed-off-by: stijn <stijn@ignitron.net>
  • Loading branch information
stinos committed Mar 19, 2024
1 parent 77f08b7 commit fa5994b
Show file tree
Hide file tree
Showing 14 changed files with 28 additions and 25 deletions.
4 changes: 2 additions & 2 deletions mpy-cross/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ CFLAGS += -fdata-sections -ffunction-sections -fno-asynchronous-unwind-tables
# Debugging/Optimization
ifdef DEBUG
CFLAGS += -g
COPT = -O0
COPT ?= -O0
else
COPT = -Os #-DNDEBUG
COPT ?= -Os #-DNDEBUG
endif

# On OSX, 'gcc' is a symlink to clang unless a real gcc is installed.
Expand Down
7 changes: 4 additions & 3 deletions ports/bare-arm/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ PYDFU ?= $(TOP)/tools/pydfu.py
CFLAGS += -I. -I$(TOP) -I$(BUILD)
CFLAGS += -Wall -Werror -std=c99 -nostdlib
CFLAGS += -mthumb -mtune=cortex-m4 -mcpu=cortex-m4 -msoft-float
CSUPEROPT = -Os # save some code space for performance-critical code
CFLAGS += $(COPT)
CSUPEROPT ?= -Os # save some code space for performance-critical code

# Select debugging or optimisation build.
ifeq ($(DEBUG), 1)
CFLAGS += -Og
COPT ?= -Og
else
CFLAGS += -Os -DNDEBUG
COPT ?= -Os -DNDEBUG
CFLAGS += -fdata-sections -ffunction-sections
endif

Expand Down
4 changes: 2 additions & 2 deletions ports/esp8266/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,10 @@ LIBS += -L$(dir $(LIBGCC_FILE_NAME)) -lgcc
# Debugging/Optimization
CFLAGS += -g # always include debug info in the ELF
ifeq ($(DEBUG), 1)
COPT = -O0
COPT ?= -O0
else
CFLAGS += -fdata-sections -ffunction-sections
COPT += -Os -DNDEBUG
COPT ?= -Os -DNDEBUG
LDFLAGS += --gc-sections
endif

Expand Down
5 changes: 3 additions & 2 deletions ports/mimxrt/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -309,11 +309,11 @@ SRC_QSTR += $(SRC_C) $(SHARED_SRC_C) $(GEN_PINS_SRC)

CFLAGS += -g # always include debug info in the ELF
ifeq ($(DEBUG),1)
CFLAGS += -Og
COPT ?= -Og
# Disable text compression in debug builds
MICROPY_ROM_TEXT_COMPRESSION = 0
else
CFLAGS += -Os -DNDEBUG
COPT ?= -Os -DNDEBUG
endif

# Set default values for optional variables
Expand All @@ -323,6 +323,7 @@ MICROPY_HW_SDRAM_SIZE ?= 0
# Configure default compiler flags
CFLAGS += \
$(INC) \
$(COPT) \
-D__START=main \
-D__STARTUP_CLEAR_BSS \
-D__STARTUP_INITIALIZE_RAMFUNCTION \
Expand Down
2 changes: 1 addition & 1 deletion ports/minimal/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ LDFLAGS += -Wl,-map,$@.map -Wl,-dead_strip
endif
endif

CSUPEROPT = -Os # save some code space
CSUPEROPT ?= -Os # save some code space

# Tune for Debugging or Optimization
CFLAGS += -g # always include debug info in the ELF
Expand Down
5 changes: 3 additions & 2 deletions ports/pic16bit/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ CFLAGS += $(INC) -Wall -Werror -std=gnu99 -nostdlib $(CFLAGS_PIC16BIT) $(COPT)

#Debugging/Optimization
ifeq ($(DEBUG), 1)
CFLAGS += -O0 -ggdb
COPT ?= -O0
CFLAGS += -ggdb
else
CFLAGS += -O1 -DNDEBUG
COPT ?= -O1 -DNDEBUG
endif

LDFLAGS += --heap=0 -nostdlib -T $(XC16)/support/$(PARTFAMILY)/gld/p$(PART).gld -Map=$@.map --cref -p$(PART)
Expand Down
2 changes: 1 addition & 1 deletion ports/powerpc/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ INC += -I$(BUILD)
CFLAGS += $(INC) -g -Wall -Wdouble-promotion -Wfloat-conversion -std=c99 $(COPT)
CFLAGS += -mno-string -mno-multiple -mno-vsx -mno-altivec -nostdlib
CFLAGS += -mlittle-endian -mstrict-align -msoft-float
CFLAGS += -Os
CFLAGS += -fdata-sections -ffunction-sections -fno-stack-protector -ffreestanding
CFLAGS += -U_FORTIFY_SOURCE
COPT ?= -Os

LDFLAGS += -N -T powerpc.lds -nostdlib

Expand Down
4 changes: 2 additions & 2 deletions ports/qemu-arm/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ CFLAGS += $(CFLAGS_EXTRA)
# Debugging/Optimization
ifeq ($(DEBUG), 1)
CFLAGS += -g
COPT = -O0
COPT ?= -O0
else
COPT += -Os -DNDEBUG
COPT ?= -Os -DNDEBUG
endif

## With CoudeSourcery it's actually a little different, you just need `-T generic-m-hosted.ld`.
Expand Down
5 changes: 2 additions & 3 deletions ports/renesas-ra/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,11 @@ LDFLAGS += --gc-sections
CFLAGS += -g # always include debug info in the ELF
ifeq ($(DEBUG), 1)
CFLAGS += -DPENDSV_DEBUG
#COPT = -Og
COPT = -Os
COPT ?= -Os
# Disable text compression in debug builds
MICROPY_ROM_TEXT_COMPRESSION = 0
else
COPT += -Os -DNDEBUG
COPT ?= -Os -DNDEBUG
endif

# Flags for optional C++ source code
Expand Down
2 changes: 1 addition & 1 deletion ports/stm32/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ SRC_O += \
$(SYSTEM_FILE)

ifeq ($(MCU_SERIES),$(filter $(MCU_SERIES),f0 g0 l0))
CSUPEROPT = -Os # save some code space
CSUPEROPT ?= -Os # save some code space
SRC_O += \
resethandler_m0.o \
shared/runtime/gchelper_thumb1.o
Expand Down
4 changes: 2 additions & 2 deletions ports/stm32/mboot/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ LDFLAGS += --gc-sections
# Debugging/Optimization
ifeq ($(DEBUG), 1)
CFLAGS += -g -DPENDSV_DEBUG
COPT = -Og
COPT ?= -Og
else
COPT += -Os -DNDEBUG
COPT ?= -Os -DNDEBUG
endif

$(BUILD)/shared/libc/string0.o: CFLAGS += $(CFLAGS_BUILTIN)
Expand Down
3 changes: 2 additions & 1 deletion ports/unix/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ To build a debuggable version of the Unix port, there are two options

1. Run `make [other arguments] DEBUG=1`. Note setting `DEBUG` also reduces the
optimisation level, so it's not a good option for builds that also want the
best performance.
best performance. When using gdb and facing 'optimized out' values or stepping
through the source not halting at the correct location, also pass `COPT=-O0`.
2. Run `make [other arguments] STRIP=`. Note that the value of `STRIP` is
empty. This will skip the build step that strips symbols and debug
information, but changes nothing else in the build configuration.
4 changes: 2 additions & 2 deletions ports/windows/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ LDFLAGS += -lm -lbcrypt $(LDFLAGS_EXTRA)
# Debugging/Optimization
ifdef DEBUG
CFLAGS += -g
COPT = -O0
COPT ?= -O0
else
COPT = -Os #-DNDEBUG
COPT ?= -Os #-DNDEBUG
endif

# source files
Expand Down
2 changes: 1 addition & 1 deletion py/py.mk
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ QSTR_GLOBAL_DEPENDENCIES += $(PY_SRC)/mpconfig.h mpconfigport.h
QSTR_GLOBAL_REQUIREMENTS += $(HEADER_BUILD)/mpversion.h

# some code is performance bottleneck and compiled with other optimization options
CSUPEROPT = -O3
CSUPEROPT ?= -O3

# Enable building 32-bit code on 64-bit host.
ifeq ($(MICROPY_FORCE_32BIT),1)
Expand Down

0 comments on commit fa5994b

Please sign in to comment.