@@ -2,11 +2,13 @@ $(info "Rules.mk")
22
33PREFIX ?= arm-none-eabi-
44
5- CC = $(PREFIX ) gcc
6- CPP = $(PREFIX ) g++
7- AS = $(CC )
8- LD = $(PREFIX ) ld
9- AR = $(PREFIX ) ar
5+ CC = $(PREFIX ) gcc
6+ CPP = $(PREFIX ) g++
7+ AS = $(CC )
8+ LD = $(PREFIX ) gcc
9+ AR = $(PREFIX ) gcc-ar
10+ RANLIB = $(PREFIX ) gcc-ranlib
11+ NM = $(PREFIX ) gcc-nm
1012
1113BOARD? =BOARD_GD32F407RE
1214ENET_PHY? =DP83848
@@ -54,14 +56,10 @@ COPS+=-fstack-usage
5456COPS+ =-ffunction-sections -fdata-sections
5557COPS+ =-Wall -Werror -Wpedantic -Wextra -Wunused -Wsign-conversion -Wconversion -Wduplicated-cond -Wlogical-op
5658COPS+=--specs =nosys.specs
59+ COPS+=-flto =auto
5760
5861include ../common/make/CppOps.mk
59-
60- LDOPS =--gc-sections --print-gc-sections --print-memory-usage
61-
62- PLATFORM_LIBGCC+ = -L $(shell dirname `$(CC ) $(COPS ) -print-libgcc-file-name`)
63-
64- $(info $$PLATFORM_LIBGCC [${PLATFORM_LIBGCC}])
62+ include ../common/make/LdOps.mk
6563
6664C_OBJECTS =$(foreach sdir,$(SRCDIR ) ,$(patsubst $(sdir ) /% .c,$(BUILD )$(sdir ) /% .o,$(wildcard $(sdir ) /* .c) ) )
6765CPP_OBJECTS+ =$(foreach sdir,$(SRCDIR ) ,$(patsubst $(sdir ) /% .cpp,$(BUILD )$(sdir ) /% .o,$(wildcard $(sdir ) /* .cpp) ) )
@@ -109,24 +107,70 @@ $(LIBDEP):
109107 $(MAKE ) -f Makefile.GD32 $(MAKECMDGOALS ) ' PROJECT=${PROJECT}' ' FAMILY=${FAMILY}' ' MCU=${MCU}' ' BOARD=${BOARD}' ' ENET_PHY=${ENET_PHY}' ' MAKE_FLAGS=$(DEFINES)' -C $@
110108
111109#
112- # Build bin
110+ # Startup and support objects
113111#
114112
113+ # Assemble the MCU startup code.
115114$(BUILD ) startup_$(LINE ) .o : $(FIRMWARE_DIR ) /startup_$(LINE ) .S
116115 $(AS ) $(COPS ) -D__ASSEMBLY__ -c $(FIRMWARE_DIR ) /startup_$(LINE ) .S -o $(BUILD ) startup_$(LINE ) .o
117116
117+ # Compile the common HardFault handler.
118118$(BUILD ) hardfault_handler.o : $(FIRMWARE_DIR ) /hardfault_handler.cpp
119119 $(CPP ) $(COPS ) $(CPPOPS ) -c $(FIRMWARE_DIR ) /hardfault_handler.cpp -o $(BUILD ) hardfault_handler.o
120120
121+ # Compile the common debug Stack handler.
121122$(BUILD ) stack_debug_init.o : $(FIRMWARE_DIR ) /stack_debug_init.cpp
122123 $(CPP ) $(COPS ) $(CPPOPS ) -c $(FIRMWARE_DIR ) /stack_debug_init.cpp -o $(BUILD ) stack_debug_init.o
123124
124- $(BUILD ) main.elf : Makefile.GD32 $(LINKER ) $(BUILD ) startup_$(LINE ) .o $(BUILD ) hardfault_handler.o $(BUILD ) stack_debug_init.o $(OBJECTS ) $(LIBDEP )
125- $(LD ) $(BUILD ) startup_$(LINE ) .o $(BUILD ) hardfault_handler.o $(BUILD ) stack_debug_init.o $(OBJECTS ) -Map $(MAP ) -T $(LINKER ) $(LDOPS ) -o $(BUILD ) main.elf $(LIBGD32 ) $(LDLIBS ) $(PLATFORM_LIBGCC ) -lgcc
126- $(PREFIX ) objdump -D $(BUILD ) main.elf | $(PREFIX ) c++filt > $(LIST )
127- $(PREFIX ) size -A -x $(BUILD ) main.elf
125+ #
126+ # Link the ELF image
127+ #
128+
129+ # Link all object files together with the dependent libraries.
130+ # A linker map and a demangled disassembly listing are generated
131+ # for debugging and analysis.
132+ $(BUILD ) main.elf : \
133+ Makefile.GD32 \
134+ $(LINKER ) \
135+ $(BUILD ) startup_$(LINE ) .o \
136+ $(BUILD ) hardfault_handler.o \
137+ $(BUILD ) stack_debug_init.o \
138+ $(OBJECTS ) \
139+ $(LIBDEP ) \
140+ | builddirs
141+ $(LD ) \
142+ $(BUILD ) startup_$(LINE ) .o \
143+ $(BUILD ) hardfault_handler.o \
144+ $(BUILD ) stack_debug_init.o \
145+ $(OBJECTS ) \
146+ -T $(LINKER ) \
147+ $(LDOPS ) \
148+ -o $@ \
149+ $(LIBGD32 ) \
150+ $(LDLIBS ) \
151+ -lgcc
152+
153+ # Generate a demangled disassembly listing.
154+ $(PREFIX)objdump -D $@ | $(PREFIX)c++filt > $(LIST)
155+
156+ # Display the memory usage by section.
157+ $(PREFIX)size -A -x $@
158+
159+ #
160+ # Create the binary firmware image
161+ #
128162
129- $(TARGET ) : $(BUILD ) main.elf
130- $(PREFIX ) objcopy $(BUILD ) main.elf -O binary $(TARGET ) --remove-section=.tcmsram* --remove-section=.sram1* --remove-section=.sram2* --remove-section=.ramadd* --remove-section=.bkpsram*
163+ # Convert the ELF image into a binary image. RAM-only sections are
164+ # removed because they are initialized at runtime rather than stored
165+ # in flash.
166+ $(TARGET ) : $(BUILD ) main.elf
167+ $(PREFIX ) objcopy $< \
168+ -O binary \
169+ $@ \
170+ --remove-section=.tcmsram* \
171+ --remove-section=.sram1* \
172+ --remove-section=.sram2* \
173+ --remove-section=.ramadd* \
174+ --remove-section=.bkpsram*
131175
132176$(foreach bdir,$(SRCDIR),$(eval $(call compile-objects,$(bdir))))
0 commit comments