Skip to content

Instantly share code, notes, and snippets.

@fabiovila
Last active October 1, 2023 21:28
Show Gist options
  • Save fabiovila/d4b1d353ef23d39db677ac1a8e26343f to your computer and use it in GitHub Desktop.
Save fabiovila/d4b1d353ef23d39db677ac1a8e26343f to your computer and use it in GitHub Desktop.
Makefile that find any *.c *.s and folders with *.h. Target is ARM Devices. For use with VSCode or command line.
#Part two of two files that recursivaly find and compile *.c *.s files
# and automatically adds folders with *.h files to gcc include option.
# Made for STM32F103 ARM devices and for use with VSCode
# with few changes these files will work in any toolchain
include Makefile.inc
.SECONDEXPANSION:
DEFINES = -DSTM32F207xx -DARM_MATH_CM3
OUTOFCOMPILER = "./nocompile"
#INCLUDES := $(filter-out $(OUTOFCOMPILER) ,$(INCLUDES))
#C := $(filter-out $(OUTOFCOMPILER), $(C))
#S := $(filter-out $(OUTOFCOMPILER) , $(S))
CC_FLAGS += $(INCLUDES) $(DEFINES) -mcpu=cortex-m3 -mthumb -mlittle-endian -Wall -fdata-sections -ffunction-sections
CC_FLAGS += -Wstrict-prototypes -fverbose-asm -nostdlib -pipe
LD_FLAGS += -lm
LD_FLAGS += -mthumb -mcpu=cortex-m3 -Wl,-Map=project.map,--cref,--no-warn-mismatch
LD_FLAGS += -nostartfiles -specs=nano.specs -TSTM32F217IGHx_FLASH.ld -Wl,--gc-sections
LIBPATH = /usr/arm-none-eabi/lib/thumb/
LIBS = $(LIBPATH)libm.a $(LIBPATH)libc_nano.a
.PHONY: clean flash
O = $(C:%.c=%.o)
O += $(S:%.s=%.o)
O += $(Supper:%.S=%.o)
ifeq ($(TARGET), release)
ELF = bin/release.elf
CC_FLAGS += -O3 -DRELEASE
OBJPATH = obj/release
else
ELF = bin/debug.elf
CC_FLAGS += -g -O0 -DDEBUG
OBJPATH = obj/debug
endif
OBJ = $(addprefix $(OBJPATH)/, $(O))
all: makepath build
build: $(OBJ)
@echo ---- LINKING ----
@$(CC) $(OBJ) $(LIBS) $(LD_FLAGS) -o $(ELF)
@$(OBJCOPY) -O ihex $(ELF) $(ELF:%.elf=%.hex)
@$(OBJCOPY) -O srec $(ELF) $(ELF:%.elf=%.s19)
@$(OBJCOPY) -O binary $(ELF) $(ELF:%.elf=%.bin)
@$(NM) $(ELF)
@echo
@$(SIZE) --format=Berkeley $(ELF)
@echo ---- FINISHED ----
makepath:
@mkdir -p $(dir $(OBJ))
$(OBJPATH)/%.o:%.c
@echo ---- C ----
$(CC) $(CC_FLAGS) -c $< -o $@
$(OBJPATH)/%.o:%.s
@echo ---- S ----
$(CC) $(CC_FLAGS) -c $< -o $@
$(OBJPATH)/%.o:%.S
@echo ---- S ----
$(CC) $(CC_FLAGS) -c $< -o $@
flash:
@echo ---- FLASHING ----
st-flash erase
st-flash --reset write $(ELF:%.elf=%.bin) 0x08000000
$(SIZE) -A -d $(ELF)
@$(OBJDUMP) -d -M force-thumb -S $(ELF) > $(ELF:%.elf=%.lst)
clean:
@echo ---- CLEANING ----
@rm -R ./obj/debug/*
@rm -R ./obj/release/*
@rm ./bin/*
@find -name *.elf -delete
rebuild: clean
@echo ---- REBUILD ALL ----
@make TARGET=release
@make TARGET=debug
debugmake:
@echo $(O)
@echo $(Supper)
make TARGET=release
make TARGET=debug
Features:
All objects files will be located in release/obj or debug/obj
elf, hex, srec, bin files located in bin folder
Project structured as folder. Simple put files in any folder and make it!
Generate map file.
Shows size info of sections and ELF's symbols (functions)
# Head make file include
# This file is one of two files that:
# - Find folders with *.h files and add this path do INCLUDES
# - Find *.c and *.s files and add its to C and S variables
# Part two do:
# - Compile any *.c and *.s file in own folder in subfolder obj/release or obj/debug
# - Include folders ( folders with *.h files) are added to gcc automatically
# - These files targets ARM devices however with few changes it will be work on any toolchain
H = $(shell find . -name "*.h" -exec dirname {} \; | sort -u)
C = $(subst ./,,$(shell find . -name "*.c"))
S = $(subst ./,,$(shell find . -name "*.s"))
Supper = $(subst ./,,$(shell find . -name "*.S")) # sporadic upper 's' extensions
INCLUDES = $(addprefix -I,$(H))
export CROSS_COMPILE ?= arm-none-eabi-
export CC = $(CROSS_COMPILE)gcc
export LD = $(CROSS_COMPILE)ld
export OBJDUMP = $(CROSS_COMPILE)objdump
export OBJCOPY = $(CROSS_COMPILE)objcopy
export SIZE = $(CROSS_COMPILE)size
export NM = $(CROSS_COMPILE)nm
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment