• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
15
16ifeq (${V},0)
17	Q=@
18else
19	Q=
20endif
21export Q
22
23.SUFFIXES:
24
25INCLUDES		+= -Iinclude/ \
26			   -I../../include/shared/
27
28# NOTE: Add C source files here
29C_SOURCES		:= src/startup.c \
30			   src/main.c	\
31			   src/suspend.c \
32			   src/dram.c	\
33			   src/stopwatch.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 		:= $(C_SOURCES)
58OBJS 			:= $(addprefix $(BUILD)/,$(call SOURCES_TO_OBJS,$(SOURCES)))
59LINKERFILE		:= $(BUILD)/$(PLAT_M0).ld
60MAPFILE			:= $(BUILD)/$(PLAT_M0).map
61ELF 			:= $(BUILD)/$(PLAT_M0).elf
62BIN 			:= $(BUILD)/$(PLAT_M0).bin
63LINKERFILE_SRC		:= src/$(PLAT_M0).ld.S
64
65# Function definition related compilation
66define MAKE_C
67$(eval OBJ := $(1)/$(patsubst %.c,%.o,$(notdir $(2))))
68-include $(patsubst %.o,%.d,$(OBJ))
69
70$(OBJ) : $(2)
71	@echo "  CC      $$<"
72	$$(Q)$$(CC) $$(COMMON_FLAGS) $$(CFLAGS) $$(INCLUDES) -MMD -MT $$@ -c $$< -o $$@
73endef
74
75define MAKE_S
76$(eval OBJ := $(1)/$(patsubst %.S,%.o,$(notdir $(2))))
77
78$(OBJ) : $(2)
79	@echo "  AS      $$<"
80	$$(Q)$$(CC) -x assembler-with-cpp $$(COMMON_FLAGS) $$(ASFLAGS) -c $$< -o $$@
81endef
82
83define MAKE_OBJS
84	$(eval C_OBJS := $(filter %.c,$(2)))
85	$(eval REMAIN := $(filter-out %.c,$(2)))
86	$(eval $(foreach obj,$(C_OBJS),$(call MAKE_C,$(1),$(obj),$(3))))
87
88	$(eval S_OBJS := $(filter %.S,$(REMAIN)))
89	$(eval REMAIN := $(filter-out %.S,$(REMAIN)))
90	$(eval $(foreach obj,$(S_OBJS),$(call MAKE_S,$(1),$(obj),$(3))))
91
92	$(and $(REMAIN),$(error Unexpected source files present: $(REMAIN)))
93endef
94
95.DEFAULT_GOAL := $(BIN)
96
97$(LINKERFILE): $(LINKERFILE_SRC)
98	$(CC) $(COMMON_FLAGS) $(INCLUDES) -P -E -D__LINKER__ -MMD -MF $@.d -MT $@ -o $@ $<
99-include $(LINKERFILE).d
100
101$(ELF) : $(OBJS) $(LINKERFILE)
102	@echo "  LD      $@"
103	$(Q)$(CC) -o $@ $(COMMON_FLAGS) $(LDFLAGS) -Wl,-Map=$(MAPFILE) -Wl,-T$(LINKERFILE) $(OBJS)
104
105$(BIN) : $(ELF)
106	@echo "  BIN     $@"
107	$(Q)$(OC) -O binary $< $@
108
109$(eval $(call MAKE_OBJS,$(BUILD),$(SOURCES),$(1)))
110