1#
2# Copyright (c) 2016, ARM Limited and Contributors. All rights reserved.
3#
4# SPDX-License-Identifier: BSD-3-Clause
5#
6
7# Cross Compile
8M0_CROSS_COMPILE ?= arm-none-eabi-
9
10# Build architecture
11ARCH		:= cortex-m0
12
13# Build platform
14PLAT_M0		?= rk3399m0
15PLAT_M0_PMU	?= rk3399m0pmu
16
17ifeq (${V},0)
18	Q=@
19else
20	Q=
21endif
22export Q
23
24.SUFFIXES:
25
26INCLUDES		+= -Iinclude/ \
27			   -I../../include/shared/
28
29# NOTE: Add C source files here
30C_SOURCES_COMMON	:= src/startup.c
31C_SOURCES		:= src/dram.c	\
32			   src/stopwatch.c
33C_SOURCES_PMU		:= src/suspend.c
34
35# Flags definition
36COMMON_FLAGS		:= -g -mcpu=$(ARCH) -mthumb -Wall -O3 -nostdlib -mfloat-abi=soft
37CFLAGS			:= -ffunction-sections -fdata-sections -fomit-frame-pointer -fno-common
38ASFLAGS			:= -Wa,--gdwarf-2
39LDFLAGS			:= -Wl,--gc-sections -Wl,--build-id=none
40
41# Cross tool
42CC			:= ${M0_CROSS_COMPILE}gcc
43CPP			:= ${M0_CROSS_COMPILE}cpp
44AR			:= ${M0_CROSS_COMPILE}ar
45OC			:= ${M0_CROSS_COMPILE}objcopy
46OD			:= ${M0_CROSS_COMPILE}objdump
47NM			:= ${M0_CROSS_COMPILE}nm
48
49# NOTE: The line continuation '\' is required in the next define otherwise we
50# end up with a line-feed characer at the end of the last c filename.
51# Also bare this issue in mind if extending the list of supported filetypes.
52define SOURCES_TO_OBJS
53	$(notdir $(patsubst %.c,%.o,$(filter %.c,$(1)))) \
54	$(notdir $(patsubst %.S,%.o,$(filter %.S,$(1))))
55endef
56
57SOURCES_COMMON		:= $(C_SOURCES_COMMON)
58SOURCES 		:= $(C_SOURCES)
59SOURCES_PMU		:= $(C_SOURCES_PMU)
60OBJS_COMMON		:= $(addprefix $(BUILD)/,$(call SOURCES_TO_OBJS,$(SOURCES_COMMON)))
61OBJS 			:= $(addprefix $(BUILD)/,$(call SOURCES_TO_OBJS,$(SOURCES)))
62OBJS_PMU 		:= $(addprefix $(BUILD)/,$(call SOURCES_TO_OBJS,$(SOURCES_PMU)))
63LINKERFILE		:= $(BUILD)/$(PLAT_M0).ld
64MAPFILE			:= $(BUILD)/$(PLAT_M0).map
65MAPFILE_PMU		:= $(BUILD)/$(PLAT_M0_PMU).map
66ELF 			:= $(BUILD)/$(PLAT_M0).elf
67ELF_PMU			:= $(BUILD)/$(PLAT_M0_PMU).elf
68BIN 			:= $(BUILD)/$(PLAT_M0).bin
69BIN_PMU			:= $(BUILD)/$(PLAT_M0_PMU).bin
70LINKERFILE_SRC		:= src/$(PLAT_M0).ld.S
71
72# Function definition related compilation
73define MAKE_C
74$(eval OBJ := $(1)/$(patsubst %.c,%.o,$(notdir $(2))))
75-include $(patsubst %.o,%.d,$(OBJ))
76
77$(OBJ) : $(2)
78	@echo "  CC      $$<"
79	$$(Q)$$(CC) $$(COMMON_FLAGS) $$(CFLAGS) $$(INCLUDES) -MMD -MT $$@ -c $$< -o $$@
80endef
81
82define MAKE_S
83$(eval OBJ := $(1)/$(patsubst %.S,%.o,$(notdir $(2))))
84
85$(OBJ) : $(2)
86	@echo "  AS      $$<"
87	$$(Q)$$(CC) -x assembler-with-cpp $$(COMMON_FLAGS) $$(ASFLAGS) -c $$< -o $$@
88endef
89
90define MAKE_OBJS
91	$(eval C_OBJS := $(filter %.c,$(2)))
92	$(eval REMAIN := $(filter-out %.c,$(2)))
93	$(eval $(foreach obj,$(C_OBJS),$(call MAKE_C,$(1),$(obj),$(3))))
94
95	$(eval S_OBJS := $(filter %.S,$(REMAIN)))
96	$(eval REMAIN := $(filter-out %.S,$(REMAIN)))
97	$(eval $(foreach obj,$(S_OBJS),$(call MAKE_S,$(1),$(obj),$(3))))
98
99	$(and $(REMAIN),$(error Unexpected source files present: $(REMAIN)))
100endef
101
102.PHONY: all
103all: $(BIN) $(BIN_PMU)
104
105.DEFAULT_GOAL := all
106
107$(LINKERFILE): $(LINKERFILE_SRC)
108	$(CC) $(COMMON_FLAGS) $(INCLUDES) -P -E -D__LINKER__ -MMD -MF $@.d -MT $@ -o $@ $<
109-include $(LINKERFILE).d
110
111$(ELF) : $(OBJS) $(OBJS_COMMON) $(LINKERFILE)
112	@echo "  LD      $@"
113	$(Q)$(CC) -o $@ $(COMMON_FLAGS) $(LDFLAGS) -Wl,-Map=$(MAPFILE) -Wl,-T$(LINKERFILE) $(OBJS) $(OBJS_COMMON)
114
115%.bin : %.elf
116	@echo "  BIN     $@"
117	$(Q)$(OC) -O binary $< $@
118
119$(ELF_PMU) : $(OBJS_COMMON) $(OBJS_PMU) $(LINKERFILE)
120	@echo "  LD      $@"
121	$(Q)$(CC) -o $@ $(COMMON_FLAGS) $(LDFLAGS) -Wl,-Map=$(MAPFILE_PMU) -Wl,-T$(LINKERFILE) $(OBJS_PMU) $(OBJS_COMMON)
122
123$(eval $(call MAKE_OBJS,$(BUILD),$(SOURCES_COMMON),$(1)))
124$(eval $(call MAKE_OBJS,$(BUILD),$(SOURCES),$(1)))
125$(eval $(call MAKE_OBJS,$(BUILD),$(SOURCES_PMU),$(1)))
126