1# 2# Copyright (c) 2016, ARM Limited and Contributors. All rights reserved. 3# 4# Redistribution and use in source and binary forms, with or without 5# modification, are permitted provided that the following conditions are met: 6# 7# Redistributions of source code must retain the above copyright notice, this 8# list of conditions and the following disclaimer. 9# 10# Redistributions in binary form must reproduce the above copyright notice, 11# this list of conditions and the following disclaimer in the documentation 12# and/or other materials provided with the distribution. 13# 14# Neither the name of ARM nor the names of its contributors may be used 15# to endorse or promote products derived from this software without specific 16# prior written permission. 17# 18# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 22# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28# POSSIBILITY OF SUCH DAMAGE. 29# 30 31# Cross Compile 32M0_CROSS_COMPILE ?= arm-none-eabi- 33 34# Build architecture 35ARCH := cortex-m0 36 37# Build platform 38PLAT_M0 ?= rk3399m0 39 40ifeq (${V},0) 41 Q=@ 42 CHECKCODE_ARGS += --no-summary --terse 43else 44 Q= 45endif 46export Q 47 48# All PHONY definition 49.PHONY: all clean distclean ${ARCH} 50all: ${ARCH} 51 52.SUFFIXES: 53 54INCLUDES += -Iinclude/ 55 56# NOTE: Add C source files here 57C_SOURCES := src/startup.c \ 58 src/main.c 59 60# Flags definition 61CFLAGS := -g 62ASFLAGS := -g -Wa,--gdwarf-2 63 64ASFLAGS += -mcpu=$(ARCH) -mthumb -Wall -ffunction-sections -O3 65CFLAGS += -mcpu=$(ARCH) -mthumb -Wall -ffunction-sections -O3 66 67LDFLAGS := -mcpu=$(ARCH) -mthumb -g -nostartfiles -O3 68LDFLAGS += -Wl,--gc-sections -Wl,--build-id=none 69 70# Cross tool 71CC := ${M0_CROSS_COMPILE}gcc 72CPP := ${M0_CROSS_COMPILE}cpp 73AS := ${M0_CROSS_COMPILE}gcc 74AR := ${M0_CROSS_COMPILE}ar 75LD := ${M0_CROSS_COMPILE}ld 76OC := ${M0_CROSS_COMPILE}objcopy 77OD := ${M0_CROSS_COMPILE}objdump 78NM := ${M0_CROSS_COMPILE}nm 79PP := ${M0_CROSS_COMPILE}gcc -E ${CFLAGS} 80 81# NOTE: The line continuation '\' is required in the next define otherwise we 82# end up with a line-feed characer at the end of the last c filename. 83# Also bare this issue in mind if extending the list of supported filetypes. 84define SOURCES_TO_OBJS 85 $(notdir $(patsubst %.c,%.o,$(filter %.c,$(1)))) \ 86 $(notdir $(patsubst %.S,%.o,$(filter %.S,$(1)))) 87endef 88 89BUILD_DIR := ${BUILD_PLAT}/obj 90BIN_DIR := ${BUILD_PLAT}/bin 91SOURCES := $(C_SOURCES) 92OBJS := $(addprefix $(BUILD_DIR)/,$(call SOURCES_TO_OBJS,$(SOURCES))) 93LINKERFILE := src/rk3399m0.ld 94MAPFILE := $(BIN_DIR)/$(PLAT_M0).map 95ELF := $(BIN_DIR)/$(PLAT_M0).elf 96BIN := $(BIN_DIR)/$(PLAT_M0).bin 97 98# Function definition related compilation 99define MAKE_C 100$(eval OBJ := $(1)/$(patsubst %.c,%.o,$(notdir $(2)))) 101 102$(OBJ) : $(2) 103 @echo " CC $$<" 104 $$(Q)$$(CC) $$(CFLAGS) $$(INCLUDES) -c $$< -o $$@ 105endef 106 107define MAKE_S 108$(eval OBJ := $(1)/$(patsubst %.S,%.o,$(notdir $(2)))) 109 110$(OBJ) : $(2) 111 @echo " AS $$<" 112 $$(Q)$$(AS) $$(ASFLAGS) -c $$< -o $$@ 113endef 114 115define MAKE_OBJS 116 $(eval C_OBJS := $(filter %.c,$(2))) 117 $(eval REMAIN := $(filter-out %.c,$(2))) 118 $(eval $(foreach obj,$(C_OBJS),$(call MAKE_C,$(1),$(obj),$(3)))) 119 120 $(eval S_OBJS := $(filter %.S,$(REMAIN))) 121 $(eval REMAIN := $(filter-out %.S,$(REMAIN))) 122 $(eval $(foreach obj,$(S_OBJS),$(call MAKE_S,$(1),$(obj),$(3)))) 123 124 $(and $(REMAIN),$(error Unexpected source files present: $(REMAIN))) 125endef 126 127$(BIN_DIR) : 128 $(Q)mkdir -p "$@" 129 130$(BUILD_DIR) : $(BIN_DIR) 131 $(Q)mkdir -p "$@" 132 133$(ELF) : $(OBJS) $(LINKERFILE) 134 @echo " LD $@" 135 $(Q)$(CC) -o $@ $(LDFLAGS) -Wl,-Map=$(MAPFILE) -Wl,-T$(LINKERFILE) \ 136 $(OBJS) 137 138$(BIN) : $(ELF) 139 @echo " BIN $@" 140 $(Q)$(OC) -O binary $< $@ 141 142.PHONY : ${ARCH} 143${ARCH} : $(BUILD_DIR) $(BIN) 144 145$(eval $(call MAKE_OBJS,$(BUILD_DIR),$(SOURCES),$(1))) 146 147# Other common compilation entries 148clean: 149 @echo " CLEAN" 150 ${Q}rm -rf ${BUILD_BASE}/${PLAT_M0} 151 ${Q}rm -rf ${VER_BIN_DIR}/$(PLAT_M0)* 152 153distclean: 154 @echo " DISTCLEAN" 155 ${Q}rm -rf ${BUILD_BASE}/${PLAT_M0} 156 ${Q}rm -rf ${VER_BIN_DIR}/$(PLAT_M0)* 157