Skip to content

Commit

Permalink
build: removed recursive make
Browse files Browse the repository at this point in the history
generate targets separately, avoiding the need to recursively call
make

This also fixes disabling quiet, like this:

  make FOXEER_F421 QUIET=

also see "Recursive Make Considered Harmful" paper by Peter Miller
  • Loading branch information
tridge committed Jul 28, 2024
1 parent 1b9dcb1 commit d71bf22
Showing 1 changed file with 28 additions and 36 deletions.
64 changes: 28 additions & 36 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,14 @@ FIRMWARE_VERSION := $(VERSION_MAJOR).$(VERSION_MINOR)
CFLAGS_BASE := -DUSE_MAKE -fsingle-precision-constant -fomit-frame-pointer -ffast-math
CFLAGS_BASE += -I$(MAIN_INC_DIR) -g3 -O3 -Wall -ffunction-sections

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

# Linker options
LDFLAGS_COMMON := -specs=nano.specs $(LIBS) -Wl,--gc-sections -Wl,--print-memory-usage

# Search source files
SRC_COMMON := $(foreach dir,$(SRC_DIRS_COMMON),$(wildcard $(dir)/*.[cs]))

TARGET_FNAME = $(IDENTIFIER)_$(TARGET)_$(FIRMWARE_VERSION)
TARGET_BASENAME = $(BIN_DIR)/$(TARGET_FNAME)

# configure some directories that are relative to wherever ROOT_DIR is located
OBJ := obj
BIN_DIR := $(ROOT)/$(OBJ)
Expand All @@ -79,45 +76,37 @@ clean :
@echo Removing $(OBJ) directory
@$(RM) -rf $(OBJ)

#####################
# main firmware build
define CREATE_BUILD_TARGET
$(2)_BASENAME = $(BIN_DIR)/$(IDENTIFIER)_$(2)_$(FIRMWARE_VERSION)

$(2) : $$($(2)_BASENAME).bin

# Generate bin and hex files from elf
$$($(2)_BASENAME).bin: $$($(2)_BASENAME).elf
echo building BIN $$@
@$(ECHO) Generating $$(notdir $$@)
$(QUIET)$(OBJCOPY) -O binary $$(<) $$@
$(QUIET)$(OBJCOPY) $$(<) -O ihex $$(@:.bin=.hex)

binary : $(TARGET_BASENAME).bin
CFLAGS_$(2) = $(MCU_$(1)) -D$(2) $(CFLAGS_$(1)) $(CFLAGS_COMMON)
LDFLAGS_$(2) = $(LDFLAGS_COMMON) $(LDFLAGS_$(1)) -T$(LDSCRIPT_$(1))

$$($(2)_BASENAME).elf: $(SRC_COMMON) $$(SRC_$(1))
@$(ECHO) Compiling $$(notdir $$@)
$(QUIET)$(MKDIR) -p $(OBJ)
$(QUIET)$(CC) $$(CFLAGS_$(2)) $$(LDFLAGS_$(2)) -MMD -MP -MF $$(@:.elf=.d) -o $$(@) $(SRC_COMMON) $$(SRC_$(1))
# 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)
@$(CP) -f $$(@) $(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)$(call lc,$(MCU_TYPE))$(DSEP)openocd.cfg $(OBJ)$(DSEP)openocd.cfg > $(NUL)
@$(ECHO) done $(TARGET)

# 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))))


# Compile target
$(TARGET_BASENAME).elf: CFLAGS := $(MCU_$(MCU_TYPE)) $(CFLAGS_$(MCU_TYPE)) $(CFLAGS_COMMON)
$(TARGET_BASENAME).elf: LDFLAGS := $(LDFLAGS_COMMON) $(LDFLAGS_$(MCU_TYPE)) -T$(LDSCRIPT_$(MCU_TYPE))
$(TARGET_BASENAME).elf: $(SRC_COMMON) $(SRC_$(MCU_TYPE))
@$(ECHO) Compiling $(notdir $@)
$(QUIRT)$(MKDIR) -p $(OBJ)
$(QUIET)$(CC) $(CFLAGS) $(LDFLAGS) -MMD -MP -MF $(@:.elf=.d) -o $(@) $(SRC_COMMON) $(SRC_$(MCU_TYPE))

# Generate bin and hex files
$(TARGET_BASENAME).bin: $(TARGET_BASENAME).elf
@$(ECHO) Generating $(notdir $@)
$(QUIET)$(OBJCOPY) -O binary $(<) $@
$(QUIET)$(OBJCOPY) $(<) -O ihex $(@:.bin=.hex)

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

define SHOW_MCU_TARGETS
$(1) Targets $(TARGETS_$(1))\n
@$(CP) -f Mcu$(DSEP)$(call lc,$(1))$(DSEP)openocd.cfg $(OBJ)$(DSEP)openocd.cfg > $(NUL)
endef
$(foreach MCU,$(MCU_TYPES),$(foreach TARGET,$(TARGETS_$(MCU)), $(eval $(call CREATE_BUILD_TARGET,$(MCU),$(TARGET)))))

##################
# bootloader build
BOOTLOADER_VERSION := $(shell $(FGREP) "define BOOTLOADER_VERSION" $(MAIN_INC_DIR)/version.h | $(CUT) -d" " -f3 )

Expand Down Expand Up @@ -163,6 +152,9 @@ ALL_BOOTLOADERS := $(foreach MCU,$(BL_MCU_TYPES),$(foreach PIN,$(BOOTLOADER_PINS

bootloaders: $(ALL_BOOTLOADERS)

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

# useful target to list all of the board targets so you can see what
# make target to use for your board
targets:
Expand Down

0 comments on commit d71bf22

Please sign in to comment.