make: restructure build scripts

tags/0.1.0
S.J.R. van Schaik 7 years ago
parent 99ab4bae75
commit 3873f68817
  1. 76
      Makefile
  2. 0
      include/macros.h
  3. 0
      include/shell.h
  4. 0
      include/spi_flash.h
  5. 0
      include/usart.h
  6. 0
      source/debug.c
  7. 0
      source/main.c
  8. 0
      source/shell.c
  9. 0
      source/spi_flash.c
  10. 0
      source/usart.c
  11. 183
      support/libopencm3.rules.mk
  12. 6
      support/libopencm3.target.mk

@ -1,13 +1,73 @@
BINARY = tbm
BUILD ?= build
PREFIX ?= arm-none-eabi
all: $(BUILD)/tbm
LDSCRIPT = support/stm32f0-discovery.ld
OPENCM3_DIR = libopencm3
CFLAGS += -Iinclude
CFLAGS += -Wall -Wundef -Wextra -Wshadow -Wimplicit-function-declaration
CFLAGS += -Wredundant-decls -Wmissing-prototypes -Wstrict-prototypes
CFLAGS += -fno-common --function-sections -fdata-sections
LDFLAGS += -static -nostartfiles
LDFLAGS += -Wl,--gc-sections
LIBS += -Wl,--start-group -lc -lgcc -lnosys -Wl,--end-group
# Set up libopencm3.
OPENCM3_DIR ?= libopencm3
OPENCM3_LIBNAME = opencm3_stm32f0
CFLAGS += -I$(OPENCM3_DIR)/include
CFLAGS += -DSTM32F0 -msoft-float -mthumb -mcpu=cortex-m0
LDFLAGS += -L$(OPENCM3_DIR)/lib
LIBS += -l$(OPENCM3_LIBNAME)
STLINK_PORT ?= :4242
obj-y += source/main.o
obj-y += source/shell.o
obj-y += source/spi_flash.o
obj-y += source/usart.o
obj = $(addprefix $(BUILD)/, $(obj-y))
# Include the dependencies.
-include $(obj:.o=.d)
# Set up the toolchain.
CC := $(PREFIX)-gcc
LD := $(PREFIX)-gcc
GDB := $(PREFIX)-gdb
MAKE := make
.SECONDARY:
clean:
@echo "CLEAN"
@rm -rf $(BUILD)
$(OPENCM3_DIR)/lib/lib$(OPENCM3_LIBNAME).a:
@if [ ! -e libopencm3/.git ]; then \
git submodule init; \
git submodule update; \
fi
@$(MAKE) -C $(OPENCM3_DIR)
# Rule to compile C source code.
$(BUILD)/%.o: %.c
@echo "CC $<"
@mkdir -p $(dir $@)
@$(CC) -c $< -o $@ $(CFLAGS) -MT $@ -MMD -MP -MF $(@:.o=.d)
$(BUILD)/tbm: $(obj) $(LDSCRIPT) $(OPENCM3_DIR)/lib/lib$(OPENCM3_LIBNAME).a
@echo "LD $@"
@mkdir -p $(dir $@)
@$(LD) -o $@ $(CFLAGS) $(LDFLAGS) -T $(LDSCRIPT) $(obj) $(LIBS)
OBJS += main.o
OBJS += shell.o
OBJS += spi_flash.o
OBJS += usart.o
run: $(BUILD)/tbm
@echo "GDB $<"
@$(GDB) --batch \
-ex 'target extended-remote $(STLINK_PORT)' \
-x flash.scr \
$<
include support/libopencm3.target.mk
include support/libopencm3.rules.mk
.PHONY: clean run

@ -1,183 +0,0 @@
##
## This file is part of the libopencm3 project.
##
## Copyright (C) 2009 Uwe Hermann <uwe@hermann-uwe.de>
## Copyright (C) 2010 Piotr Esden-Tempski <piotr@esden.net>
## Copyright (C) 2013 Frantisek Burian <BuFran@seznam.cz>
##
## This library is free software: you can redistribute it and/or modify
## it under the terms of the GNU Lesser General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## This library is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU Lesser General Public License for more details.
##
## You should have received a copy of the GNU Lesser General Public License
## along with this library. If not, see <http://www.gnu.org/licenses/>.
##
# Be silent per default, but 'make V=1' will show all compiler calls.
ifneq ($(V),1)
Q := @
NULL := 2>/dev/null
endif
###############################################################################
# Executables
PREFIX ?= arm-none-eabi
CC := $(PREFIX)-gcc
LD := $(PREFIX)-gcc
AR := $(PREFIX)-ar
AS := $(PREFIX)-as
OBJCOPY := $(PREFIX)-objcopy
OBJDUMP := $(PREFIX)-objdump
GDB := $(PREFIX)-gdb
STFLASH = $(shell which st-flash)
STYLECHECK := /checkpatch.pl
STYLECHECKFLAGS := --no-tree -f --terse --mailback
STYLECHECKFILES := $(shell find . -name '*.[ch]')
###############################################################################
# Source files
LDSCRIPT ?= $(BINARY).ld
ifeq ($(strip $(OPENCM3_DIR)),)
# user has not specified the library path, so we try to detect it
# where we search for the library
LIBPATHS := ./libopencm3 ../../../../libopencm3 ../../../../../libopencm3
OPENCM3_DIR := $(wildcard $(LIBPATHS:=/locm3.sublime-project))
OPENCM3_DIR := $(firstword $(dir $(OPENCM3_DIR)))
ifeq ($(strip $(OPENCM3_DIR)),)
$(warning Cannot find libopencm3 library in the standard search paths.)
$(error Please specify it through OPENCM3_DIR variable!)
endif
endif
ifeq ($(V),1)
$(info Using $(OPENCM3_DIR) path to library)
endif
INCLUDE_DIR = $(OPENCM3_DIR)/include
LIB_DIR = $(OPENCM3_DIR)/lib
SCRIPT_DIR = $(OPENCM3_DIR)/scripts
###############################################################################
# C flags
CFLAGS += -Os -g
CFLAGS += -Wextra -Wshadow -Wimplicit-function-declaration
CFLAGS += -Wredundant-decls -Wmissing-prototypes -Wstrict-prototypes
CFLAGS += -fno-common -ffunction-sections -fdata-sections
CPPFLAGS += -MD
CPPFLAGS += -Wall -Wundef
CPPFLAGS += -I$(INCLUDE_DIR) $(DEFS)
###############################################################################
# Linker flags
LDFLAGS += --static -nostartfiles
LDFLAGS += -L$(LIB_DIR)
LDFLAGS += -T$(LDSCRIPT)
LDFLAGS += -Wl,-Map=$(*).map
LDFLAGS += -Wl,--gc-sections
ifeq ($(V),99)
LDFLAGS += -Wl,--print-gc-sections
endif
###############################################################################
# Used libraries
LDLIBS += -l$(LIBNAME)
LDLIBS += -Wl,--start-group -lc -lgcc -lnosys -Wl,--end-group
###############################################################################
###############################################################################
###############################################################################
.SUFFIXES: .elf .bin .hex .srec .list .map .images
.SECONDEXPANSION:
.SECONDARY:
all: elf
elf: $(BINARY).elf
bin: $(BINARY).bin
hex: $(BINARY).hex
srec: $(BINARY).srec
list: $(BINARY).list
images: $(BINARY).images
flash: $(BINARY).flash
$(LDSCRIPT):
ifeq (,$(wildcard $(LDSCRIPT)))
$(error Unable to find specified linker script: $(LDSCRIPT))
endif
%.images: %.bin %.hex %.srec %.list %.map
@#printf "*** $* images generated ***\n"
%.bin: %.elf
@#printf " OBJCOPY $(*).bin\n"
$(Q)$(OBJCOPY) -Obinary $(*).elf $(*).bin
%.hex: %.elf
@#printf " OBJCOPY $(*).hex\n"
$(Q)$(OBJCOPY) -Oihex $(*).elf $(*).hex
%.srec: %.elf
@#printf " OBJCOPY $(*).srec\n"
$(Q)$(OBJCOPY) -Osrec $(*).elf $(*).srec
%.list: %.elf
@#printf " OBJDUMP $(*).list\n"
$(Q)$(OBJDUMP) -S $(*).elf > $(*).list
%.elf %.map: $(OBJS) $(LDSCRIPT) $(LIB_DIR)/lib$(LIBNAME).a
@#printf " LD $(*).elf\n"
$(Q)$(LD) $(LDFLAGS) $(ARCH_FLAGS) $(OBJS) $(LDLIBS) -o $(*).elf
%.o: %.c
@#printf " CC $(*).c\n"
$(Q)$(CC) $(CFLAGS) $(CPPFLAGS) $(ARCH_FLAGS) -o $(*).o -c $(*).c
clean:
@#printf " CLEAN\n"
$(Q)$(RM) *.o *.d *.elf *.bin *.hex *.srec *.list *.map
stylecheck: $(STYLECHECKFILES:=.stylecheck)
styleclean: $(STYLECHECKFILES:=.styleclean)
# the cat is due to multithreaded nature - we like to have consistent chunks of text on the output
%.stylecheck: %
$(Q)$(SCRIPT_DIR)$(STYLECHECK) $(STYLECHECKFLAGS) $* > $*.stylecheck; \
if [ -s $*.stylecheck ]; then \
cat $*.stylecheck; \
else \
rm -f $*.stylecheck; \
fi;
%.styleclean:
$(Q)rm -f $*.stylecheck;
%.flash: %.elf
@printf " GDB $(*).elf (flash)\n"
$(Q)$(GDB) --batch \
-ex 'target extended-remote $(STLINK_PORT)' \
-x flash.scr \
$(*).elf
.PHONY: images clean stylecheck styleclean elf bin hex srec list
-include $(OBJS:.o=.d)

@ -1,6 +0,0 @@
LIBNAME = opencm3_stm32f0
DEFS += -DSTM32F0
FP_FLAGS ?= -msoft-float
ARCH_FLAGS = -mthumb -mcpu=cortex-m0 $(FP_FLAGS)
STLINK_PORT ?= :4242
Loading…
Cancel
Save