2013-08-03 09:58:19 -04:00
|
|
|
# Top-level Makefile
|
|
|
|
|
2013-10-27 08:12:48 -04:00
|
|
|
# Current development directory path
|
|
|
|
|
2013-10-30 11:01:27 -04:00
|
|
|
-include Makefile.opts
|
2013-10-27 08:12:48 -04:00
|
|
|
|
2020-01-01 23:25:55 -05:00
|
|
|
ifeq ($(MCU),)
|
|
|
|
$(error Must set 'MCU' in Makefile.opts; see 'BUILD' file)
|
|
|
|
endif
|
|
|
|
|
2013-10-27 08:12:48 -04:00
|
|
|
# Toolchain - Don't change this unless you are sure what you're doing!
|
|
|
|
TOOLCHAIN_CONFIG=toolchain-$(MCU_CORE).config
|
|
|
|
TOOLCHAIN_SRC_DIR=crosstool-ng
|
2019-11-29 19:07:28 -05:00
|
|
|
CROSS_DIR ?= ${HOME}/x-tools/arm-$(MCU_CORE)_v3a-eabi
|
2016-07-12 20:28:33 -04:00
|
|
|
CROSS_COMPILE ?= ${CROSS_DIR}/bin/arm-$(MCU_CORE)_v3a-eabi-
|
2013-10-27 08:12:48 -04:00
|
|
|
CC=$(CROSS_COMPILE)gcc
|
2019-11-29 19:29:16 -05:00
|
|
|
CXX=$(CROSS_COMPILE)g++
|
2013-10-27 08:12:48 -04:00
|
|
|
CPP=$(CROSS_COMPILE)cpp
|
|
|
|
LD=$(CROSS_COMPILE)gcc
|
|
|
|
OBJCOPY=$(CROSS_COMPILE)objcopy
|
2015-07-22 09:49:50 -04:00
|
|
|
AR=$(CROSS_COMPILE)gcc-ar
|
|
|
|
RANLIB=$(CROSS_COMPILE)gcc-ranlib
|
2013-10-27 08:12:48 -04:00
|
|
|
AS=$(CROSS_COMPILE)as
|
|
|
|
GDB=$(CROSS_COMPILE)gdb
|
|
|
|
SIZE=$(CROSS_COMPILE)size
|
2015-07-22 09:49:50 -04:00
|
|
|
NM=$(CROSS_COMPILE)nm
|
2013-10-27 08:12:48 -04:00
|
|
|
RM=rm
|
|
|
|
ECHO=echo
|
|
|
|
HOSTCC ?= gcc
|
2013-10-27 12:53:54 -04:00
|
|
|
MAKE ?= make
|
2013-10-27 08:12:48 -04:00
|
|
|
|
|
|
|
# How many processor cores we can use for builds
|
|
|
|
JLEVEL:=${shell cat /proc/cpuinfo |grep processor |wc -l}
|
|
|
|
|
|
|
|
# Libraries
|
|
|
|
LIBDIR=libs
|
|
|
|
|
|
|
|
# Generic CMSIS include:
|
|
|
|
MCU_INCLUDE += -I$(LIBDIR)/CMSIS/Include
|
|
|
|
|
|
|
|
# Compiler flags
|
2015-08-01 12:23:37 -04:00
|
|
|
CFLAGS += -pipe -Wall -Wextra -g -Os -std=c99 $(MCU_CFLAGS) $(TARGET_CFLAGS)
|
2019-11-29 19:29:16 -05:00
|
|
|
CXXFLAGS += -pipe -Wall -Wextra -g -Os -std=c++03 $(MCU_CXXFLAGS) $(TARGET_CXXFLAGS)
|
2013-10-27 08:12:48 -04:00
|
|
|
CPPFLAGS += $(MCU_INCLUDE) $(INCLUDE) $(MCU_CPPFLAGS) $(TARGET_CPPFLAGS)
|
|
|
|
LDCPPFLAGS += -DMCU=$(MCU) -DMCU_SUBTYPE=$(MCU_SUBTYPE) $(TARGET_LDCPPFLAGS)
|
|
|
|
|
|
|
|
# Linker Flags
|
2019-11-29 19:07:28 -05:00
|
|
|
LDFLAGS += -nostartfiles -fno-exceptions -ffunction-sections -fdata-sections -specs=nosys.specs
|
2013-10-27 08:12:48 -04:00
|
|
|
|
|
|
|
# Use LTO?
|
|
|
|
ifneq ($(USE_LTO),)
|
2015-07-22 09:49:50 -04:00
|
|
|
CFLAGS += -flto # -ffat-lto-objects
|
2019-11-29 19:29:16 -05:00
|
|
|
CXXFLAGS += -flto # -ffat-lto-objects
|
2015-07-22 09:49:50 -04:00
|
|
|
LDFLAGS += -fuse-linker-plugin -flto=$(JLEVEL) $(CFLAGS)
|
2013-10-27 08:12:48 -04:00
|
|
|
else
|
2015-07-22 09:49:50 -04:00
|
|
|
CFLAGS += -ffunction-sections
|
2019-11-29 19:29:16 -05:00
|
|
|
CXXFLAGS += -ffunction-sections
|
2013-10-27 08:12:48 -04:00
|
|
|
LDFLAGS += -fwhole-program
|
|
|
|
endif
|
|
|
|
|
|
|
|
# Try and pretty things up a bit.
|
|
|
|
Q=@
|
|
|
|
E=echo
|
|
|
|
ifeq ($(V), 1)
|
|
|
|
Q=
|
|
|
|
E=true
|
|
|
|
endif
|
2013-08-03 09:58:19 -04:00
|
|
|
|
2013-08-03 20:13:14 -04:00
|
|
|
# Now for the image definition
|
2013-08-04 08:47:31 -04:00
|
|
|
IMAGE = $(MCU)-main
|
2013-08-03 20:13:14 -04:00
|
|
|
|
2013-10-27 08:12:48 -04:00
|
|
|
# Initial Makefile targets
|
2013-08-04 08:47:31 -04:00
|
|
|
all: .toolchain version $(IMAGE).bin
|
2013-08-03 09:58:19 -04:00
|
|
|
|
2013-08-29 23:59:38 -04:00
|
|
|
.PHONY: clean libclean distclean version dep
|
2013-08-03 09:58:19 -04:00
|
|
|
|
2013-12-18 19:39:30 -05:00
|
|
|
.PRECIOUS: %.elf %.ld %.a
|
2013-08-04 08:47:31 -04:00
|
|
|
|
2013-11-10 00:23:03 -05:00
|
|
|
# Include library targets
|
2013-10-27 08:12:48 -04:00
|
|
|
include $(LIBDIR)/$(MCU).mk
|
|
|
|
include $(LIBDIR)/math.mk
|
2013-12-19 10:51:41 -05:00
|
|
|
include $(LIBDIR)/freertos.mk
|
2013-11-10 00:23:03 -05:00
|
|
|
# Include application targets
|
2013-08-03 09:58:19 -04:00
|
|
|
include src/build.mk
|
|
|
|
|
|
|
|
# Bookeeping
|
|
|
|
VERSION_H = inc/version.h
|
|
|
|
VERSION = `./git-version-gen /dev/null`
|
|
|
|
|
|
|
|
# Set up the libraries we need
|
2013-11-10 00:23:03 -05:00
|
|
|
LIBRARIES += $(LIBDIR)/lib$(MCU).a
|
|
|
|
LIBS_OBJS += $(MCU_LIBS_OBJS)
|
2013-08-03 09:58:19 -04:00
|
|
|
|
2013-12-18 19:39:30 -05:00
|
|
|
$(LIBDIR)/lib$(MCU).a: $(MCU_LIBS_OBJS)
|
2013-08-05 09:18:08 -04:00
|
|
|
@$(E) " AR " $@
|
2013-12-18 19:39:30 -05:00
|
|
|
$(Q)$(AR) cr $@ $^
|
2013-08-05 09:18:08 -04:00
|
|
|
|
2013-12-18 19:39:30 -05:00
|
|
|
# Generic compilation rules
|
2013-10-27 12:46:56 -04:00
|
|
|
%.ld: libs/$(MCU).ld.in inc/$(MCU)_hw.h
|
2013-08-03 10:56:57 -04:00
|
|
|
@$(E) " GENERATE " $@
|
2013-10-24 11:27:45 -04:00
|
|
|
$(Q)$(CPP) $(LDCPPFLAGS) -include inc/$(MCU)_hw.h libs/$(MCU).ld.in -P -o $@
|
2013-08-03 10:56:57 -04:00
|
|
|
|
2013-08-03 09:58:19 -04:00
|
|
|
%.bin: %.elf
|
|
|
|
@$(E) " OBJCOPY " $@
|
2013-12-18 19:39:30 -05:00
|
|
|
$(Q)$(OBJCOPY) -O binary $< --remove-section .note.gnu.build-id $@
|
2013-08-03 09:58:19 -04:00
|
|
|
|
2014-02-18 10:18:37 -05:00
|
|
|
# Dependency tracking
|
|
|
|
-include $(SRC_OBJS:.o=.d)
|
|
|
|
-include $(LIBS_OBJS:.o=.d)
|
|
|
|
|
|
|
|
%.o: %.c Makefile.opts $(VERSION_H) # These last two are ... special
|
2013-08-03 09:58:19 -04:00
|
|
|
@$(E) " CC " $<
|
2014-02-18 10:18:37 -05:00
|
|
|
$(Q)$(CC) $(CFLAGS) $(CPPFLAGS) -MM $< > $(@:.o=.dt)
|
|
|
|
$(Q)sed -e 's|.*:|$*.o:|' < $*.dt > $*.d
|
|
|
|
$(Q)sed -e 's/.*://' -e 's/\\$$//' < $*.dt | fmt -1 | sed -e 's/^ *//' -e 's/$$/:/' >> $*.d
|
|
|
|
@$(RM) -f $*.dt
|
2013-12-18 19:39:30 -05:00
|
|
|
$(Q)$(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $@
|
2013-08-03 09:58:19 -04:00
|
|
|
|
2019-11-29 19:29:16 -05:00
|
|
|
%.o: %.cpp Makefile.opts $(VERSION_H) # These last two are ... special
|
|
|
|
@$(E) " CX " $<
|
|
|
|
$(Q)$(CXX) $(CXXFLAGS) $(CPPFLAGS) -MM $< > $(@:.o=.dt)
|
|
|
|
$(Q)sed -e 's|.*:|$*.o:|' < $*.dt > $*.d
|
|
|
|
$(Q)sed -e 's/.*://' -e 's/\\$$//' < $*.dt | fmt -1 | sed -e 's/^ *//' -e 's/$$/:/' >> $*.d
|
|
|
|
@$(RM) -f $*.dt
|
|
|
|
$(Q)$(CC) $(CXXFLAGS) $(CPPFLAGS) -c $< -o $@
|
|
|
|
|
2013-10-27 12:46:56 -04:00
|
|
|
%.elf: %.ld $(LIBRARIES) $(PROGRAM_LIBS)
|
2013-08-03 09:58:19 -04:00
|
|
|
@$(E) " LINK " $@
|
|
|
|
$(Q)$(CC) -o $@ $(LDFLAGS) \
|
2013-08-04 18:45:47 -04:00
|
|
|
-Wl,--gc-sections \
|
2013-10-27 12:46:56 -04:00
|
|
|
-Wl,-T$(@:.elf=.ld) \
|
2014-02-18 10:18:37 -05:00
|
|
|
-Wl,--no-whole-archive \
|
|
|
|
$(PROGRAM_LIBS) $(LIBRARIES) \
|
2013-10-23 11:54:43 -04:00
|
|
|
-Wl,--build-id=none \
|
|
|
|
-Wl,-Map=$(@:.elf=.map)
|
2013-08-04 08:47:31 -04:00
|
|
|
$(Q)$(SIZE) $@
|
2013-08-03 09:58:19 -04:00
|
|
|
|
2014-02-18 10:18:37 -05:00
|
|
|
cppcheck:
|
|
|
|
cppcheck --std=c99 -q -v --enable=all $(CPPFLAGS) $(SRC_OBJS:.o=.c) $(LIBS_OBJS:.o=.c)
|
|
|
|
|
2013-10-27 08:12:48 -04:00
|
|
|
# Cleanup
|
2013-08-03 09:58:19 -04:00
|
|
|
clean:
|
2013-08-03 20:16:29 -04:00
|
|
|
@$(E) " CLEAN "
|
2013-10-27 12:46:56 -04:00
|
|
|
$(Q)$(RM) -f $(SRC_OBJS) src/*.a $(IMAGE).{bin,elf,ld,map}
|
2013-08-03 09:58:19 -04:00
|
|
|
|
2013-08-29 23:59:38 -04:00
|
|
|
libclean:
|
|
|
|
@$(E) " LIBCLEAN "
|
|
|
|
$(Q)$(RM) -f $(LIBS_OBJS) $(LIBRARIES)
|
|
|
|
|
|
|
|
distclean: clean libclean
|
2013-08-03 20:16:29 -04:00
|
|
|
@$(E) " DISTCLEAN"
|
2013-08-26 09:16:37 -04:00
|
|
|
$(Q)$(RM) -f $(DEPFILE) $(VERSION_H)
|
|
|
|
$(Q)$(RM) -Rf $(TOOLCHAIN_SRC_DIR)/.build
|
|
|
|
$(Q)if [ -r $(TOOLCHAIN_SRC_DIR)/Makefile ] ; then \
|
|
|
|
cd $(TOOLCHAIN_SRC_DIR) ; \
|
2013-10-27 12:53:54 -04:00
|
|
|
MAKELEVEL='' $(MAKE) clean ; \
|
|
|
|
MAKELEVEL='' $(MAKE) distclean ; \
|
2013-08-03 09:58:19 -04:00
|
|
|
fi
|
2013-08-26 09:16:37 -04:00
|
|
|
@$(RM) -f .toolchain-$(MCU_CORE)
|
|
|
|
$(Q)$(RM) -f tags TAGS
|
2013-08-03 09:58:19 -04:00
|
|
|
|
|
|
|
# Toolchain
|
2013-08-03 20:43:28 -04:00
|
|
|
.toolchain: .toolchain-$(MCU_CORE)
|
|
|
|
|
|
|
|
.toolchain-$(MCU_CORE):
|
2013-08-26 09:16:37 -04:00
|
|
|
$(Q)if [ ! -x $(CROSS_COMPILE)gcc ] ; then \
|
2013-08-03 13:08:13 -04:00
|
|
|
( unset AS LD LDD CC CPP AR NM STRIP STRIPTOOL OBJCOPY OBJDUMP RANLIB LD_LIBRARY_PATH MAKELEVEL ; \
|
2013-10-27 07:58:33 -04:00
|
|
|
if [ -r $(TOOLCHAIN_SRC_DIR)/$(TOOLCHAIN_CONFIG) ] ; then \
|
2013-10-25 23:24:13 -04:00
|
|
|
cp $(TOOLCHAIN_SRC_DIR)/$(TOOLCHAIN_CONFIG) $(TOOLCHAIN_SRC_DIR)/.config ; \
|
2013-08-03 09:58:19 -04:00
|
|
|
else \
|
2013-08-26 09:16:37 -04:00
|
|
|
$(ECHO) "$(TOOLCHAIN_CONFIG) missing! Exiting." ; \
|
2013-08-03 09:58:19 -04:00
|
|
|
exit 1 ;\
|
|
|
|
fi ; \
|
2013-08-26 09:16:37 -04:00
|
|
|
$(RM) -Rf $(TOOLCHAIN_SRC_DIR)/.build ; \
|
|
|
|
cd $(TOOLCHAIN_SRC_DIR) ; \
|
2013-08-03 09:58:19 -04:00
|
|
|
./configure --enable-local && \
|
2013-10-27 12:53:54 -04:00
|
|
|
$(MAKE) && \
|
2013-08-03 09:58:19 -04:00
|
|
|
./ct-ng build ; \
|
|
|
|
cd - ) ; \
|
2019-11-29 19:29:16 -05:00
|
|
|
fi
|
2013-08-26 09:16:37 -04:00
|
|
|
@if [ ! -x $(CROSS_COMPILE)gcc ] ; then \
|
|
|
|
$(ECHO) "Toolchain Build failed!" ; \
|
2013-08-03 09:58:19 -04:00
|
|
|
exit 1 ;\
|
|
|
|
fi
|
2013-08-03 20:43:28 -04:00
|
|
|
@touch .toolchain-$(MCU_CORE)
|
2013-08-03 09:58:19 -04:00
|
|
|
|
|
|
|
toolclean:
|
2013-08-26 09:16:37 -04:00
|
|
|
$(Q)if [ -d $(CROSS_DIR) ] ; then \
|
|
|
|
chmod -R +w $(CROSS_DIR) ; \
|
|
|
|
$(RM) -fr $(CROSS_DIR) ; \
|
2013-08-03 09:58:19 -04:00
|
|
|
fi
|
2013-08-26 09:16:37 -04:00
|
|
|
@$(RM) -f .toolchain-$(MCU_CORE)
|
2013-08-03 09:58:19 -04:00
|
|
|
|
2013-10-27 08:12:48 -04:00
|
|
|
# For saner developers
|
2013-08-03 09:58:19 -04:00
|
|
|
tags:
|
|
|
|
find . -path '*crosstool*' -prune \
|
|
|
|
-o -name '*.[CHch]' -type f -print \
|
|
|
|
| etags -
|
|
|
|
|
2013-10-27 08:12:48 -04:00
|
|
|
# Automagic version strings
|
2013-08-03 09:58:19 -04:00
|
|
|
version:
|
|
|
|
# @$(E) " VERSION " $(VERSION_H)
|
|
|
|
$(Q)./git-version-gen $(VERSION_H) $(APP) > /dev/null
|