Skip to content

Commit

Permalink
build: reduce the number of places we list the MCU types
Browse files Browse the repository at this point in the history
this is a step towards removing the recursive make, and making it
easier to add new MCUs
  • Loading branch information
tridge committed Jul 25, 2024
1 parent b502e36 commit 2e04461
Showing 1 changed file with 35 additions and 59 deletions.
94 changes: 35 additions & 59 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,15 @@ ROOT := $(patsubst %/,%,$(dir $(lastword $(MAKEFILE_LIST))))
# include the rules for OS independence
include $(ROOT)/make/tools.mk

# Include processor specific makefiles
include f051makefile.mk
include g071makefile.mk
include f031makefile.mk
include f421makefile.mk
include e230makefile.mk
include f415makefile.mk
include gd32makefile.mk

# Default MCU type to F051
MCU_TYPE ?= F051
# supported MCU types
MCU_TYPES := E230 F031 F051 F415 F421 G071
MCU_TYPE := NONE

# Function to include makefile for each MCU type
define INCLUDE_MCU_MAKEFILES
$(foreach MCU_TYPE,$(MCU_TYPES),$(eval include $(call lc,$(MCU_TYPE))makefile.mk))
endef
$(call INCLUDE_MCU_MAKEFILES)

# additional libs
LIBS := -lnosys
Expand All @@ -44,9 +42,11 @@ VERSION_MINOR := $(shell $(FGREP) "define VERSION_MINOR" $(MAIN_INC_DIR)/version
FIRMWARE_VERSION := $(VERSION_MAJOR).$(VERSION_MINOR)

# Compiler options
CFLAGS_COMMON := -DUSE_MAKE -fsingle-precision-constant -fomit-frame-pointer -ffast-math
CFLAGS_COMMON += -I$(MAIN_INC_DIR) -g -O3 -Wall -ffunction-sections
CFLAGS_COMMON += -D$(TARGET)

CFLAGS_BASE := -DUSE_MAKE -fsingle-precision-constant -fomit-frame-pointer -ffast-math
CFLAGS_BASE += -I$(MAIN_INC_DIR) -g -O3 -Wall -ffunction-sections

CFLAGS_COMMON := $(CFLAGS_BASE) -D$(TARGET)

# Linker options
LDFLAGS_COMMON := -specs=nano.specs $(LIBS) -Wl,--gc-sections -Wl,--print-memory-usage
Expand All @@ -61,52 +61,37 @@ TARGET_BASENAME = $(BIN_DIR)/$(TARGET_FNAME)
OBJ := obj
BIN_DIR := $(ROOT)/$(OBJ)

TOOLS_DIR ?= $(ROOT)/tools
DL_DIR := $(ROOT)/downloads

.PHONY : clean all binary f051 g071 f031 e230 f421 f415
ALL_TARGETS := $(TARGETS_F051) $(TARGETS_G071) $(TARGETS_F031) $(TARGETS_E230) $(TARGETS_F421) $(TARGETS_F415)
.PHONY : clean all binary $(foreach MCU,$(MCU_TYPES),$(call lc,$(MCU)))
ALL_TARGETS := $(foreach MCU,$(MCU_TYPES),$(TARGETS_$(MCU)))
all : $(ALL_TARGETS)
f051 : $(TARGETS_F051)
g071 : $(TARGETS_G071)
f031 : $(TARGETS_F031)
e230 : $(TARGETS_E230)
f421 : $(TARGETS_F421)
f415 : $(TARGETS_F415)

# create targets for compiling one mcu type, eg "make f421"
define CREATE_TARGET
$(call lc,$(1)) : $$(TARGETS_$(1))
endef
$(foreach MCU,$(MCU_TYPES),$(eval $(call CREATE_TARGET,$(MCU))))

clean :
@echo Removing $(OBJ) directory
@$(RM) -rf $(OBJ)

# lowercase version of MCU_TYPE
MCU_LOWER = $(call lc,$(MCU_TYPE))

binary : $(TARGET_BASENAME).bin
# we copy debug.elf to give us a constant debug target for vscode
# this means the debug button will always debug the last target built
@$(CP) -f $(OBJ)$(DSEP)$(TARGET_FNAME).elf $(OBJ)$(DSEP)debug.elf > $(NUL)
# also copy the openocd.cfg from the MCU directory to obj/openocd.cfg for auto config of Cortex-Debug
# in vscode
@$(CP) -f Mcu$(DSEP)$(MCU_LOWER)$(DSEP)openocd.cfg $(OBJ)$(DSEP)openocd.cfg > $(NUL)
@$(CP) -f Mcu$(DSEP)$(call lc,$(MCU_TYPE))$(DSEP)openocd.cfg $(OBJ)$(DSEP)openocd.cfg > $(NUL)
@$(ECHO) done $(TARGET)

$(TARGETS_F051) :
@$(MAKE) -s MCU_TYPE=F051 TARGET=$@ binary

$(TARGETS_G071) :
@$(MAKE) -s MCU_TYPE=G071 TARGET=$@ binary
# create targets compiling each MCU types targets
define CREATE_MCU_TARGETS
$$(TARGETS_$(1)) :
@$$(MAKE) -s MCU_TYPE=$(1) TARGET=$$@ binary
endef
$(foreach MCU,$(MCU_TYPES),$(eval $(call CREATE_MCU_TARGETS,$(MCU))))

$(TARGETS_F031) :
@$(MAKE) -s MCU_TYPE=F031 TARGET=$@ binary

$(TARGETS_E230) :
@$(MAKE) -s MCU_TYPE=E230 TARGET=$@ binary

$(TARGETS_F421) :
@$(MAKE) -s MCU_TYPE=F421 TARGET=$@ binary

$(TARGETS_F415) :
@$(MAKE) -s MCU_TYPE=F415 TARGET=$@ binary

# Compile target
$(TARGET_BASENAME).elf: CFLAGS := $(MCU_$(MCU_TYPE)) $(CFLAGS_$(MCU_TYPE)) $(CFLAGS_COMMON)
Expand All @@ -122,22 +107,13 @@ $(TARGET_BASENAME).bin: $(TARGET_BASENAME).elf
$(QUIET)$(OBJCOPY) -O binary $(<) $@
$(QUIET)$(OBJCOPY) $(<) -O ihex $(@:.bin=.hex)

# mkdirs
$(DL_DIR):
$(QUIET)$(MKDIR) -p $@

$(TOOLS_DIR):
$(QUIET)$(MKDIR) -p $@

# include the targets for installing tools
include $(ROOT)/make/tools_install.mk

define SHOW_MCU_TARGETS
$(1) Targets $(TARGETS_$(1))\n
endef

targets:
$(QUIET)echo Targets for each MCU. To build a target use 'make TARGETNAME'
$(QUIET)echo F051 Targets: $(TARGETS_F051)
$(QUIET)echo G071 Targets: $(TARGETS_G071)
$(QUIET)echo F031 Targets: $(TARGETS_F031)
$(QUIET)echo E230 Targets: $(TARGETS_E230)
$(QUIET)echo F421 Targets: $(TARGETS_F421)
$(QUIET)echo F415 Targets: $(TARGETS_F415)
$(QUIET)echo GD32 Targets: $(TARGETS_GD32)
$(QUIET)echo List of targets. To build a target use 'make TARGETNAME'
$(QUIET)echo $(ALL_TARGETS)

0 comments on commit 2e04461

Please sign in to comment.