From 6d91a76fd3e8c02d807421ba4a22fa933f0927b7 Mon Sep 17 00:00:00 2001 From: Philipp Tomsich Date: Tue, 4 Apr 2017 15:33:55 +0200 Subject: Makefile: migrate to TSD guidelines for Cortex-M0 projects We rewrite the Makefile from scratch using our guidelines for embedded projects (e.g. used on the Cortex-M0 firmware for our module's companion controllers). The highlights of this commit include: - convert to our Makefile template for Cortex-M0 projects - switch to LTO - add support for an 'internal' toolchain build from crosstools-ng Signed-off-by: Philipp Tomsich --- Makefile | 151 +++++++++++++++++++++++++++++++++++++++------------------------ 1 file changed, 93 insertions(+), 58 deletions(-) diff --git a/Makefile b/Makefile index dcaa7ac..817f466 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,6 @@ # -# Copyright (c) 2016, ARM Limited and Contributors. All rights reserved. +# Copyright (c) 2017, Theobroma Systems Design und Consulting GmbH. +# All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: @@ -29,16 +30,23 @@ # # Cross Compile -# +USE_INTERNAL_TOOLCHAIN ?= 0 +ifneq (${USE_INTERNAL_TOOLCHAIN}, 0) + CROSS_COMPILE = crosstools/arm-cortex_m0-eabi/bin/arm-cortex_m0-eabi- +else # For a default cross-compile we use the naming convention from crosstools-ng # as this toolchain can be easily and quickly regenerated from source. -CROSS_COMPILE ?= arm-cortex_m0-eabi- + CROSS_COMPILE ?= arm-cortex_m0-eabi- +endif -# Build architecture -ARCH := cortex-m0 +CC = $(CROSS_COMPILE)gcc +CXX = $(CROSS_COMPILE)g++ +LD = $(CROSS_COMPILE)ld +OBJCOPY = $(CROSS_COMPILE)objcopy +OBJDUMP = $(CROSS_COMPILE)objdump # Build platform -PLAT_M0 ?= rk3399m0 +OUT ?= rk3399m0 V ?= 0 ifeq (${V},0) @@ -48,70 +56,97 @@ else endif export Q -.SUFFIXES: +ifneq (${USE_INTERNAL_TOOLCHAIN}, 0) + DEP_CC = $(CC) + DEP_CXX = $(CXX) + DEP_LD = $(LD) + DEP_OBJCOPY = $(OBJCOPY) + DEP_OBJDUMP = $(OBJDUMP) +endif -INCLUDES += -Iinclude/ \ - -Iinclude/shared/ +# C sources +CSRC = src/startup.c \ + src/main.c \ + src/suspend.c \ + src/dram.c \ + src/stopwatch.c -# NOTE: Add C source files here -C_SOURCES := src/startup.c \ - src/main.c \ - src/suspend.c \ - src/dram.c \ - src/stopwatch.c +# Assembly sources +SSRC = # Flags definition -COMMON_FLAGS := -g -mcpu=$(ARCH) -mthumb -Wall -Os -nostdlib -mfloat-abi=soft -CFLAGS := -ffunction-sections -fdata-sections -fomit-frame-pointer -fno-common -ASFLAGS := -Wa,--gdwarf-2 -LDFLAGS := -Wl,--gc-sections -Wl,--build-id=none - -# Cross tool -CC := ${CROSS_COMPILE}gcc -CPP := ${CROSS_COMPILE}cpp -AR := ${CROSS_COMPILE}ar -OC := ${CROSS_COMPILE}objcopy -OD := ${CROSS_COMPILE}objdump -NM := ${CROSS_COMPILE}nm - -define SOURCES_TO_OBJS - $(patsubst %.c,%.o,$(filter %.c,$(1))) \ - $(patsubst %.S,%.o,$(filter %.S,$(1))) -endef - -CSRC := $(C_SOURCES) -OBJS := $(CSRC:.c=.o) $(SSRC:.s=.o) - -LINKERFILE := $(PLAT_M0).ld -MAPFILE := $(PLAT_M0).map -ELF := $(PLAT_M0).elf -BIN := $(PLAT_M0).bin -LINKERFILE_SRC := src/$(PLAT_M0).ld.S - -%.o : %.c +OPTFLAGS = -Os -g -ffunction-sections -fdata-sections -flto -fno-fat-lto-objects +INCPATHS = -Iinclude/ \ + -Iinclude/shared/ +DEFINE = + +CFLAGS = $(INCPATHS) $(OPTFLAGS) $(DEFINE) -mcpu=cortex-m0 +LDFLAGS = -nostdlib -nostartfiles -Wl,--gc-sections $(OPTFLAGS) -Wl,--build-id=none $(CFLAGS) +LIBS = + +# Object defines +COBJ = $(CSRC:.c=.o) +SOBJ = $(SSRC:.s=.o) + +LDSCRIPT = $(OUT).ld +LDSCRIPT_SRC = src/$(OUT).ld.S + +OUTMAP = $(OUT).map +OUTELF = $(OUT).elf +OUTBIN = $(OUT).bin + +all: $(OUTBIN) + +$(COBJ) : %.o : %.c $(DEP_CC) @echo " CC $<" - $(Q)$(CC) $(COMMON_FLAGS) $(CFLAGS) $(INCLUDES) -MMD -MT $@ -c $< -o $@ + $(Q)$(CC) $(CFLAGS) -c $< -o $@ -$.o : %.s +$(SOBJ) : $.o : %.s $(DEP_CC) @echo " AS $<" - $(Q)$(CC) -x assembler-with-cpp $(COMMON_FLAGS) $(ASFLAGS) -c $< -o $@ - -.DEFAULT_GOAL := $(BIN) + $(Q)$(CC) -x assembler-with-cpp $(CFLAGS) -c $< -o $@ -$(LINKERFILE): $(LINKERFILE_SRC) - $(CC) $(COMMON_FLAGS) $(INCLUDES) -P -E -D__LINKER__ -MMD -MF $@.d -MT $@ -o $@ $< --include $(LINKERFILE).d +$(LDSCRIPT): $(LDSCRIPT_SRC) $(DEP_CC) + @echo " LDSCRIPT " + $(Q)$(CC) $(CFLAGS) $(LDFLAGS) -P -E -D__LINKER__ -o $@ $< -$(ELF) : $(OBJS) $(LINKERFILE) - @echo " LD $@" - $(Q)$(CC) -o $@ $(COMMON_FLAGS) $(LDFLAGS) -Wl,-Map=$(MAPFILE) -Wl,-T$(LINKERFILE) $(OBJS) +$(OUTMAP) $(OUTELF) : $(COBJ) $(SOBJ) $(LDSCRIPT) $(DEP_CC) + @echo " LINK $@" + $(Q)$(CC) -o $@ $(CFLAGS) $(LDFLAGS) -Wl,-Map=$(OUTMAP) -Wl,-T$(LDSCRIPT) $(COBJ) $(SOBJ) $(LIBS) -$(BIN) : $(ELF) +$(OUTBIN) : $(OUTELF) $(DEP_OBJDUMP) @echo " BIN $@" - $(Q)$(OC) -O binary $< $@ + $(Q)$(OBJCOPY) -O binary $(OUTELF) $(OUTBIN) clean: @echo " CLEAN" - $(Q)rm -f $(OBJS) $(ELF) $(BIN) $(LINKERFILE) + $(Q)rm -f $(SOBJ) $(COBJ) $(OUTMAP) $(OUTELF) $(OUTBIN) $(LDSCRIPT) + +# Incantation to use an 'internal toolchain' via cosstool-ng + +crosstools/crosstool-ng/bootstrap: + @echo " GIT [submodule-update] crosstool-ng" + $(Q)git submodule update --init --recursive + +crosstools/crosstool-ng/configure: crosstools/crosstool-ng/bootstrap + @echo " GEN $@" + $(Q)(cd crosstools/crosstool-ng; ./bootstrap;) + +crosstools/crosstool-ng/Makefile: crosstools/crosstool-ng/configure + $(Q)(cd crosstools/crosstool-ng; ./configure --enable-local) + +crosstools/crosstool-ng/ct-ng: crosstools/crosstool-ng/Makefile + $(Q)make -C crosstools/crosstool-ng install MAKELEVEL=0 + +TOOLPATH = `pwd`/crosstools/arm-cortex_m0-eabi +CTNG_DEFCONFIG = crosstools/defconfig + +crosstools/arm-cortex_m0-eabi/bin/arm-cortex_m0-eabi-gcc: $(CTNG_DEFCONFIG) crosstools/crosstool-ng/ct-ng + @echo " CT-NG defconfig" + $(Q)(cd crosstools/crosstool-ng; ./ct-ng defconfig DEFCONFIG=../defconfig) + $(Q)sed -r -i.org s%CT_PREFIX_DIR=.*%CT_PREFIX_DIR=$(TOOLPATH)% crosstools/crosstool-ng/.config + @echo " CT-NG build" + $(Q)(cd crosstools/crosstool-ng; ./ct-ng build) -$(eval $(call MAKE_OBJS,$(BUILD),$(SOURCES),$(1))) +crosstools/arm-cortex_m0-eabi: + @echo " MKDIR $@" + $(Q)mkdir $@ -- cgit v1.2.3