blob: 79e09f0ef71192c72c600ba32bdd09c6f9dc2efe [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
Lin Huang00960ba2018-04-20 15:55:21 +080015PLAT_M0_PMU ?= rk3399m0pmu
Caesar Wangb4003742016-10-12 08:10:12 +080016
17ifeq (${V},0)
18 Q=@
Caesar Wangb4003742016-10-12 08:10:12 +080019else
20 Q=
21endif
22export Q
23
Caesar Wangb4003742016-10-12 08:10:12 +080024.SUFFIXES:
25
Xing Zheng93280b72016-10-26 21:25:26 +080026INCLUDES += -Iinclude/ \
27 -I../../include/shared/
Caesar Wangb4003742016-10-12 08:10:12 +080028
29# NOTE: Add C source files here
Lin Huang00960ba2018-04-20 15:55:21 +080030C_SOURCES_COMMON := src/startup.c
31C_SOURCES := src/dram.c \
Lin Huange7c24222016-11-30 16:57:08 +080032 src/stopwatch.c
Lin Huang00960ba2018-04-20 15:55:21 +080033C_SOURCES_PMU := src/suspend.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
Lin Huang00960ba2018-04-20 15:55:21 +080057SOURCES_COMMON := $(C_SOURCES_COMMON)
Caesar Wangb4003742016-10-12 08:10:12 +080058SOURCES := $(C_SOURCES)
Lin Huang00960ba2018-04-20 15:55:21 +080059SOURCES_PMU := $(C_SOURCES_PMU)
60OBJS_COMMON := $(addprefix $(BUILD)/,$(call SOURCES_TO_OBJS,$(SOURCES_COMMON)))
Julius Werner705aeeb2016-10-31 19:18:47 -070061OBJS := $(addprefix $(BUILD)/,$(call SOURCES_TO_OBJS,$(SOURCES)))
Lin Huang00960ba2018-04-20 15:55:21 +080062OBJS_PMU := $(addprefix $(BUILD)/,$(call SOURCES_TO_OBJS,$(SOURCES_PMU)))
Xing Zheng93280b72016-10-26 21:25:26 +080063LINKERFILE := $(BUILD)/$(PLAT_M0).ld
Julius Werner705aeeb2016-10-31 19:18:47 -070064MAPFILE := $(BUILD)/$(PLAT_M0).map
Lin Huang00960ba2018-04-20 15:55:21 +080065MAPFILE_PMU := $(BUILD)/$(PLAT_M0_PMU).map
Julius Werner705aeeb2016-10-31 19:18:47 -070066ELF := $(BUILD)/$(PLAT_M0).elf
Lin Huang00960ba2018-04-20 15:55:21 +080067ELF_PMU := $(BUILD)/$(PLAT_M0_PMU).elf
Julius Werner705aeeb2016-10-31 19:18:47 -070068BIN := $(BUILD)/$(PLAT_M0).bin
Lin Huang00960ba2018-04-20 15:55:21 +080069BIN_PMU := $(BUILD)/$(PLAT_M0_PMU).bin
Xing Zheng93280b72016-10-26 21:25:26 +080070LINKERFILE_SRC := src/$(PLAT_M0).ld.S
Caesar Wangb4003742016-10-12 08:10:12 +080071
72# Function definition related compilation
73define MAKE_C
74$(eval OBJ := $(1)/$(patsubst %.c,%.o,$(notdir $(2))))
Julius Werner844782d2016-11-03 12:25:48 -070075-include $(patsubst %.o,%.d,$(OBJ))
Caesar Wangb4003742016-10-12 08:10:12 +080076
77$(OBJ) : $(2)
78 @echo " CC $$<"
Julius Wernerf5852462017-01-30 16:13:21 -080079 $$(Q)$$(CC) $$(COMMON_FLAGS) $$(CFLAGS) $$(INCLUDES) -MMD -MT $$@ -c $$< -o $$@
Caesar Wangb4003742016-10-12 08:10:12 +080080endef
81
82define MAKE_S
83$(eval OBJ := $(1)/$(patsubst %.S,%.o,$(notdir $(2))))
84
85$(OBJ) : $(2)
86 @echo " AS $$<"
Julius Wernerf5852462017-01-30 16:13:21 -080087 $$(Q)$$(CC) -x assembler-with-cpp $$(COMMON_FLAGS) $$(ASFLAGS) -c $$< -o $$@
Caesar Wangb4003742016-10-12 08:10:12 +080088endef
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
Lin Huang00960ba2018-04-20 15:55:21 +0800102.PHONY: all
103all: $(BIN) $(BIN_PMU)
104
105.DEFAULT_GOAL := all
Xing Zheng93280b72016-10-26 21:25:26 +0800106
107$(LINKERFILE): $(LINKERFILE_SRC)
Julius Wernerf5852462017-01-30 16:13:21 -0800108 $(CC) $(COMMON_FLAGS) $(INCLUDES) -P -E -D__LINKER__ -MMD -MF $@.d -MT $@ -o $@ $<
Xing Zheng93280b72016-10-26 21:25:26 +0800109-include $(LINKERFILE).d
Caesar Wangb4003742016-10-12 08:10:12 +0800110
Lin Huang00960ba2018-04-20 15:55:21 +0800111$(ELF) : $(OBJS) $(OBJS_COMMON) $(LINKERFILE)
Caesar Wangb4003742016-10-12 08:10:12 +0800112 @echo " LD $@"
Lin Huang00960ba2018-04-20 15:55:21 +0800113 $(Q)$(CC) -o $@ $(COMMON_FLAGS) $(LDFLAGS) -Wl,-Map=$(MAPFILE) -Wl,-T$(LINKERFILE) $(OBJS) $(OBJS_COMMON)
Caesar Wangb4003742016-10-12 08:10:12 +0800114
Lin Huang00960ba2018-04-20 15:55:21 +0800115%.bin : %.elf
Caesar Wangb4003742016-10-12 08:10:12 +0800116 @echo " BIN $@"
117 $(Q)$(OC) -O binary $< $@
118
Lin Huang00960ba2018-04-20 15:55:21 +0800119$(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)))
Julius Werner705aeeb2016-10-31 19:18:47 -0700124$(eval $(call MAKE_OBJS,$(BUILD),$(SOURCES),$(1)))
Lin Huang00960ba2018-04-20 15:55:21 +0800125$(eval $(call MAKE_OBJS,$(BUILD),$(SOURCES_PMU),$(1)))