blob: f6bdbf271fe19701e0d7941c1fa0ea852f91871c [file] [log] [blame]
Caesar Wangb4003742016-10-12 08:10:12 +08001#
2# Copyright (c) 2016, ARM Limited and Contributors. All rights reserved.
3#
dp-armfa3cf0b2017-05-03 09:38:09 +01004# SPDX-License-Identifier: BSD-3-Clause
Caesar Wangb4003742016-10-12 08:10:12 +08005#
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=@
Caesar Wangb4003742016-10-12 08:10:12 +080018else
19 Q=
20endif
21export Q
22
Caesar Wangb4003742016-10-12 08:10:12 +080023.SUFFIXES:
24
Xing Zheng93280b72016-10-26 21:25:26 +080025INCLUDES += -Iinclude/ \
26 -I../../include/shared/
Caesar Wangb4003742016-10-12 08:10:12 +080027
28# NOTE: Add C source files here
29C_SOURCES := src/startup.c \
Xing Zheng93280b72016-10-26 21:25:26 +080030 src/main.c \
31 src/suspend.c \
Lin Huange7c24222016-11-30 16:57:08 +080032 src/dram.c \
33 src/stopwatch.c
Caesar Wangb4003742016-10-12 08:10:12 +080034
35# Flags definition
Julius Wernerf5852462017-01-30 16:13:21 -080036COMMON_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
Caesar Wangb4003742016-10-12 08:10:12 +080040
41# Cross tool
42CC := ${M0_CROSS_COMPILE}gcc
43CPP := ${M0_CROSS_COMPILE}cpp
Caesar Wangb4003742016-10-12 08:10:12 +080044AR := ${M0_CROSS_COMPILE}ar
Caesar Wangb4003742016-10-12 08:10:12 +080045OC := ${M0_CROSS_COMPILE}objcopy
46OD := ${M0_CROSS_COMPILE}objdump
47NM := ${M0_CROSS_COMPILE}nm
Caesar Wangb4003742016-10-12 08:10:12 +080048
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
Caesar Wangb4003742016-10-12 08:10:12 +080057SOURCES := $(C_SOURCES)
Julius Werner705aeeb2016-10-31 19:18:47 -070058OBJS := $(addprefix $(BUILD)/,$(call SOURCES_TO_OBJS,$(SOURCES)))
Xing Zheng93280b72016-10-26 21:25:26 +080059LINKERFILE := $(BUILD)/$(PLAT_M0).ld
Julius Werner705aeeb2016-10-31 19:18:47 -070060MAPFILE := $(BUILD)/$(PLAT_M0).map
61ELF := $(BUILD)/$(PLAT_M0).elf
62BIN := $(BUILD)/$(PLAT_M0).bin
Xing Zheng93280b72016-10-26 21:25:26 +080063LINKERFILE_SRC := src/$(PLAT_M0).ld.S
Caesar Wangb4003742016-10-12 08:10:12 +080064
65# Function definition related compilation
66define MAKE_C
67$(eval OBJ := $(1)/$(patsubst %.c,%.o,$(notdir $(2))))
Julius Werner844782d2016-11-03 12:25:48 -070068-include $(patsubst %.o,%.d,$(OBJ))
Caesar Wangb4003742016-10-12 08:10:12 +080069
70$(OBJ) : $(2)
71 @echo " CC $$<"
Julius Wernerf5852462017-01-30 16:13:21 -080072 $$(Q)$$(CC) $$(COMMON_FLAGS) $$(CFLAGS) $$(INCLUDES) -MMD -MT $$@ -c $$< -o $$@
Caesar Wangb4003742016-10-12 08:10:12 +080073endef
74
75define MAKE_S
76$(eval OBJ := $(1)/$(patsubst %.S,%.o,$(notdir $(2))))
77
78$(OBJ) : $(2)
79 @echo " AS $$<"
Julius Wernerf5852462017-01-30 16:13:21 -080080 $$(Q)$$(CC) -x assembler-with-cpp $$(COMMON_FLAGS) $$(ASFLAGS) -c $$< -o $$@
Caesar Wangb4003742016-10-12 08:10:12 +080081endef
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
Xing Zheng93280b72016-10-26 21:25:26 +080095.DEFAULT_GOAL := $(BIN)
96
97$(LINKERFILE): $(LINKERFILE_SRC)
Julius Wernerf5852462017-01-30 16:13:21 -080098 $(CC) $(COMMON_FLAGS) $(INCLUDES) -P -E -D__LINKER__ -MMD -MF $@.d -MT $@ -o $@ $<
Xing Zheng93280b72016-10-26 21:25:26 +080099-include $(LINKERFILE).d
Caesar Wangb4003742016-10-12 08:10:12 +0800100
101$(ELF) : $(OBJS) $(LINKERFILE)
102 @echo " LD $@"
Julius Wernerf5852462017-01-30 16:13:21 -0800103 $(Q)$(CC) -o $@ $(COMMON_FLAGS) $(LDFLAGS) -Wl,-Map=$(MAPFILE) -Wl,-T$(LINKERFILE) $(OBJS)
Caesar Wangb4003742016-10-12 08:10:12 +0800104
105$(BIN) : $(ELF)
106 @echo " BIN $@"
107 $(Q)$(OC) -O binary $< $@
108
Julius Werner705aeeb2016-10-31 19:18:47 -0700109$(eval $(call MAKE_OBJS,$(BUILD),$(SOURCES),$(1)))