CC = arm-none-eabi-gcc CFLAGS = -mcpu=cortex-m4 -mthumb -mfloat-abi=hard -mfpu=fpv4-sp-d16 CFLAGS += -Wall -Werror -Wpedantic -Wextra # Remove unused code and data; https://gcc.gnu.org/onlinedocs/gnat_ugn/Compilation-options.html CFLAGS += -ffunction-sections -fdata-sections # Don't include memcpy CFLAGS += -fno-builtin # "-fno-common ... specifies that the compiler places uninitialized global variables in the BSS section of the object file." CFLAGS += -fno-common # Use experimental binary literal gcc extension (to be able to use binary literals; e.g. 0b0101) CFLAGS += -std=c2x DEBUG ?= 1 ifeq ($(DEBUG), 0) # Optimize for size CFLAGS += -Os else CFLAGS += -O0 -g3 -ggdb endif LDFLAGS = -T link.ld -nostartfiles -nostdlib --specs nano.specs -lc -lgcc -Wl,--gc-sections -Wl,-Map=$@.map SRC_DIR = src BUILD_DIR = build TARGET = final SRC_FILES := $(wildcard $(SRC_DIR)/*.c) PREP_FILES := $(patsubst $(SRC_DIR)/%.c,$(BUILD_DIR)/%.i,$(SRC_FILES)) ASM_FILES := $(patsubst $(BUILD_DIR)/%.i,$(BUILD_DIR)/%.S,$(PREP_FILES)) OBJ_FILES := $(patsubst $(BUILD_DIR)/%.S,$(BUILD_DIR)/%.o,$(ASM_FILES)) .PHONY: build build: clean builddir preprocess compile assemble $(BUILD_DIR)/$(TARGET).elf $(BUILD_DIR)/$(TARGET).bin: $(BUILD_DIR)/$(TARGET).elf arm-none-eabi-objcopy -O binary $< $@ $(BUILD_DIR)/$(TARGET).elf: $(OBJ_FILES) $(CC) $(LDFLAGS) $^ -o $@ $(BUILD_DIR)/%.i: $(SRC_DIR)/%.c $(CC) $(CFLAGS) -E $^ -o $@ $(BUILD_DIR)/%.S: $(BUILD_DIR)/%.i $(CC) $(CFLAGS) -S $^ -o $@ $(BUILD_DIR)/%.o: $(BUILD_DIR)/%.S $(CC) $(CFLAGS) -c $^ -o $@ .PHONY: builddir builddir: mkdir $(BUILD_DIR) .PHONY: preprocess preprocess: $(PREP_FILES) .PHONY: compile compile: preprocess $(ASM_FILES) .PHONY: assemble assemble: compile $(OBJ_FILES) .PHONY: flash flash: $(BUILD_DIR)/$(TARGET).bin st-flash --reset write $< 0x8000000 .PHONY: gdb-server gdb-server: sudo openocd -f stlink.cfg -f stm32f4x.cfg .PHONY: gdb-client gdb-client: gdb --symbols $(BUILD_DIR)/$(TARGET).elf --init-eval-command="target extended-remote localhost:3333" .PHONY: gdb-guiclient gdb-guiclient: gdbgui --gdb-cmd="gdb --init-eval-command='target extended-remote localhost:3333'" $(BUILD_DIR)/$(TARGET).elf .PHONY: clean clean: rm -rf $(BUILD_DIR)