@@ -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
@@ -63,14 +65,10 @@ COPS+=-fstack-usage
6365COPS+ =-ffunction-sections -fdata-sections
6466COPS+ =-Wall -Werror -Wpedantic -Wextra -Wunused -Wsign-conversion -Wconversion -Wduplicated-cond -Wlogical-op
6567COPS+=--specs =nosys.specs
68+ COPS+=-flto =auto
6669
6770include ../common/make/CppOps.mk
68-
69- LDOPS =--gc-sections --print-gc-sections --print-memory-usage
70-
71- PLATFORM_LIBGCC+ = -L $(shell dirname `$(CC ) $(COPS ) -print-libgcc-file-name`)
72-
73- $(info $$PLATFORM_LIBGCC [${PLATFORM_LIBGCC}])
71+ include ../common/make/LdOps.mk
7472
7573C_OBJECTS =$(foreach sdir,$(SRCDIR ) ,$(patsubst $(sdir ) /% .c,$(BUILD )$(sdir ) /% .o,$(wildcard $(sdir ) /* .c) ) )
7674CPP_OBJECTS+ =$(foreach sdir,$(SRCDIR ) ,$(patsubst $(sdir ) /% .cpp,$(BUILD )$(sdir ) /% .o,$(wildcard $(sdir ) /* .cpp) ) )
@@ -118,24 +116,70 @@ $(LIBDEP):
118116 $(MAKE ) -f Makefile.GD32 $(MAKECMDGOALS ) ' PROJECT=${PROJECT}' ' FAMILY=${FAMILY}' ' MCU=${MCU}' ' BOARD=${BOARD}' ' ENET_PHY=${ENET_PHY}' ' MAKE_FLAGS=$(DEFINES)' -C $@
119117
120118#
121- # Build bin
119+ # Startup and support objects
122120#
123121
122+ # Assemble the MCU startup code.
124123$(BUILD ) startup_$(LINE ) .o : $(FIRMWARE_DIR ) /startup_$(LINE ) .S
125124 $(AS ) $(COPS ) -D__ASSEMBLY__ -c $(FIRMWARE_DIR ) /startup_$(LINE ) .S -o $(BUILD ) startup_$(LINE ) .o
126125
126+ # Compile the common HardFault handler.
127127$(BUILD ) hardfault_handler.o : $(FIRMWARE_DIR ) /hardfault_handler.cpp
128128 $(CPP ) $(COPS ) $(CPPOPS ) -c $(FIRMWARE_DIR ) /hardfault_handler.cpp -o $(BUILD ) hardfault_handler.o
129129
130+ # Compile the common debug Stack handler.
130131$(BUILD ) stack_debug_init.o : $(FIRMWARE_DIR ) /stack_debug_init.cpp
131132 $(CPP ) $(COPS ) $(CPPOPS ) -c $(FIRMWARE_DIR ) /stack_debug_init.cpp -o $(BUILD ) stack_debug_init.o
132133
133- $(BUILD ) main.elf : Makefile.GD32 $(LINKER ) $(BUILD ) startup_$(LINE ) .o $(BUILD ) hardfault_handler.o $(BUILD ) stack_debug_init.o $(OBJECTS ) $(LIBDEP )
134- $(LD ) $(BUILD ) startup_$(LINE ) .o $(BUILD ) hardfault_handler.o $(OBJECTS ) $(BUILD ) stack_debug_init.o -Map $(MAP ) -T $(LINKER ) $(LDOPS ) -o $(BUILD ) main.elf $(LIBGD32 ) $(LDLIBS ) $(PLATFORM_LIBGCC ) -lgcc
135- $(PREFIX ) objdump -D $(BUILD ) main.elf | $(PREFIX ) c++filt > $(LIST )
136- $(PREFIX ) size -A -x $(BUILD ) main.elf
134+ #
135+ # Link the ELF image
136+ #
137+
138+ # Link all object files together with the dependent libraries.
139+ # A linker map and a demangled disassembly listing are generated
140+ # for debugging and analysis.
141+ $(BUILD ) main.elf : \
142+ Makefile.GD32 \
143+ $(LINKER ) \
144+ $(BUILD ) startup_$(LINE ) .o \
145+ $(BUILD ) hardfault_handler.o \
146+ $(BUILD ) stack_debug_init.o \
147+ $(OBJECTS ) \
148+ $(LIBDEP ) \
149+ | builddirs
150+ $(LD ) \
151+ $(BUILD ) startup_$(LINE ) .o \
152+ $(BUILD ) hardfault_handler.o \
153+ $(BUILD ) stack_debug_init.o \
154+ $(OBJECTS ) \
155+ -T $(LINKER ) \
156+ $(LDOPS ) \
157+ -o $@ \
158+ $(LIBGD32 ) \
159+ $(LDLIBS ) \
160+ -lgcc
161+
162+ # Generate a demangled disassembly listing.
163+ $(PREFIX)objdump -D $@ | $(PREFIX)c++filt > $(LIST)
164+
165+ # Display the memory usage by section.
166+ $(PREFIX)size -A -x $@
167+
168+ #
169+ # Create the binary firmware image
170+ #
137171
138- $(TARGET ) : $(BUILD ) main.elf
139- $(PREFIX ) objcopy $(BUILD ) main.elf -O binary $(TARGET ) --remove-section=.tcmsram* --remove-section=.sram1* --remove-section=.sram2* --remove-section=.ramadd* --remove-section=.bkpsram*
172+ # Convert the ELF image into a binary image. RAM-only sections are
173+ # removed because they are initialized at runtime rather than stored
174+ # in flash.
175+ $(TARGET ) : $(BUILD ) main.elf
176+ $(PREFIX ) objcopy $< \
177+ -O binary \
178+ $@ \
179+ --remove-section=.tcmsram* \
180+ --remove-section=.sram1* \
181+ --remove-section=.sram2* \
182+ --remove-section=.ramadd* \
183+ --remove-section=.bkpsram*
140184
141185$(foreach bdir,$(SRCDIR),$(eval $(call compile-objects,$(bdir))))
0 commit comments