blob: b2dac4a877d50df91daa20634d008c8b3c121b4b [file] [log] [blame]
Caesar Wangb4003742016-10-12 08:10:12 +08001#
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=@
Caesar Wangb4003742016-10-12 08:10:12 +080042else
43 Q=
44endif
45export Q
46
Caesar Wangb4003742016-10-12 08:10:12 +080047.SUFFIXES:
48
Xing Zheng93280b72016-10-26 21:25:26 +080049INCLUDES += -Iinclude/ \
50 -I../../include/shared/
Caesar Wangb4003742016-10-12 08:10:12 +080051
52# NOTE: Add C source files here
53C_SOURCES := src/startup.c \
Xing Zheng93280b72016-10-26 21:25:26 +080054 src/main.c \
55 src/suspend.c \
Lin Huange7c24222016-11-30 16:57:08 +080056 src/dram.c \
57 src/stopwatch.c
Caesar Wangb4003742016-10-12 08:10:12 +080058
59# Flags definition
Julius Wernerf5852462017-01-30 16:13:21 -080060COMMON_FLAGS := -g -mcpu=$(ARCH) -mthumb -Wall -O3 -nostdlib -mfloat-abi=soft
61CFLAGS := -ffunction-sections -fdata-sections -fomit-frame-pointer -fno-common
62ASFLAGS := -Wa,--gdwarf-2
63LDFLAGS := -Wl,--gc-sections -Wl,--build-id=none
Caesar Wangb4003742016-10-12 08:10:12 +080064
65# Cross tool
66CC := ${M0_CROSS_COMPILE}gcc
67CPP := ${M0_CROSS_COMPILE}cpp
Caesar Wangb4003742016-10-12 08:10:12 +080068AR := ${M0_CROSS_COMPILE}ar
Caesar Wangb4003742016-10-12 08:10:12 +080069OC := ${M0_CROSS_COMPILE}objcopy
70OD := ${M0_CROSS_COMPILE}objdump
71NM := ${M0_CROSS_COMPILE}nm
Caesar Wangb4003742016-10-12 08:10:12 +080072
73# NOTE: The line continuation '\' is required in the next define otherwise we
74# end up with a line-feed characer at the end of the last c filename.
75# Also bare this issue in mind if extending the list of supported filetypes.
76define SOURCES_TO_OBJS
77 $(notdir $(patsubst %.c,%.o,$(filter %.c,$(1)))) \
78 $(notdir $(patsubst %.S,%.o,$(filter %.S,$(1))))
79endef
80
Caesar Wangb4003742016-10-12 08:10:12 +080081SOURCES := $(C_SOURCES)
Julius Werner705aeeb2016-10-31 19:18:47 -070082OBJS := $(addprefix $(BUILD)/,$(call SOURCES_TO_OBJS,$(SOURCES)))
Xing Zheng93280b72016-10-26 21:25:26 +080083LINKERFILE := $(BUILD)/$(PLAT_M0).ld
Julius Werner705aeeb2016-10-31 19:18:47 -070084MAPFILE := $(BUILD)/$(PLAT_M0).map
85ELF := $(BUILD)/$(PLAT_M0).elf
86BIN := $(BUILD)/$(PLAT_M0).bin
Xing Zheng93280b72016-10-26 21:25:26 +080087LINKERFILE_SRC := src/$(PLAT_M0).ld.S
Caesar Wangb4003742016-10-12 08:10:12 +080088
89# Function definition related compilation
90define MAKE_C
91$(eval OBJ := $(1)/$(patsubst %.c,%.o,$(notdir $(2))))
Julius Werner844782d2016-11-03 12:25:48 -070092-include $(patsubst %.o,%.d,$(OBJ))
Caesar Wangb4003742016-10-12 08:10:12 +080093
94$(OBJ) : $(2)
95 @echo " CC $$<"
Julius Wernerf5852462017-01-30 16:13:21 -080096 $$(Q)$$(CC) $$(COMMON_FLAGS) $$(CFLAGS) $$(INCLUDES) -MMD -MT $$@ -c $$< -o $$@
Caesar Wangb4003742016-10-12 08:10:12 +080097endef
98
99define MAKE_S
100$(eval OBJ := $(1)/$(patsubst %.S,%.o,$(notdir $(2))))
101
102$(OBJ) : $(2)
103 @echo " AS $$<"
Julius Wernerf5852462017-01-30 16:13:21 -0800104 $$(Q)$$(CC) -x assembler-with-cpp $$(COMMON_FLAGS) $$(ASFLAGS) -c $$< -o $$@
Caesar Wangb4003742016-10-12 08:10:12 +0800105endef
106
107define MAKE_OBJS
108 $(eval C_OBJS := $(filter %.c,$(2)))
109 $(eval REMAIN := $(filter-out %.c,$(2)))
110 $(eval $(foreach obj,$(C_OBJS),$(call MAKE_C,$(1),$(obj),$(3))))
111
112 $(eval S_OBJS := $(filter %.S,$(REMAIN)))
113 $(eval REMAIN := $(filter-out %.S,$(REMAIN)))
114 $(eval $(foreach obj,$(S_OBJS),$(call MAKE_S,$(1),$(obj),$(3))))
115
116 $(and $(REMAIN),$(error Unexpected source files present: $(REMAIN)))
117endef
118
Xing Zheng93280b72016-10-26 21:25:26 +0800119.DEFAULT_GOAL := $(BIN)
120
121$(LINKERFILE): $(LINKERFILE_SRC)
Julius Wernerf5852462017-01-30 16:13:21 -0800122 $(CC) $(COMMON_FLAGS) $(INCLUDES) -P -E -D__LINKER__ -MMD -MF $@.d -MT $@ -o $@ $<
Xing Zheng93280b72016-10-26 21:25:26 +0800123-include $(LINKERFILE).d
Caesar Wangb4003742016-10-12 08:10:12 +0800124
125$(ELF) : $(OBJS) $(LINKERFILE)
126 @echo " LD $@"
Julius Wernerf5852462017-01-30 16:13:21 -0800127 $(Q)$(CC) -o $@ $(COMMON_FLAGS) $(LDFLAGS) -Wl,-Map=$(MAPFILE) -Wl,-T$(LINKERFILE) $(OBJS)
Caesar Wangb4003742016-10-12 08:10:12 +0800128
129$(BIN) : $(ELF)
130 @echo " BIN $@"
131 $(Q)$(OC) -O binary $< $@
132
Julius Werner705aeeb2016-10-31 19:18:47 -0700133$(eval $(call MAKE_OBJS,$(BUILD),$(SOURCES),$(1)))