blob: 2d07d24489f4312d0a2b5cd33c94d8d736203939 [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
60CFLAGS := -g
61ASFLAGS := -g -Wa,--gdwarf-2
62
63ASFLAGS += -mcpu=$(ARCH) -mthumb -Wall -ffunction-sections -O3
64CFLAGS += -mcpu=$(ARCH) -mthumb -Wall -ffunction-sections -O3
65
Patrick Georgi5c979f12017-01-04 19:06:14 +010066LDFLAGS := -mcpu=$(ARCH) -mthumb -g -nostartfiles -nostdlib -O3
Caesar Wangb4003742016-10-12 08:10:12 +080067LDFLAGS += -Wl,--gc-sections -Wl,--build-id=none
68
69# Cross tool
70CC := ${M0_CROSS_COMPILE}gcc
71CPP := ${M0_CROSS_COMPILE}cpp
72AS := ${M0_CROSS_COMPILE}gcc
73AR := ${M0_CROSS_COMPILE}ar
74LD := ${M0_CROSS_COMPILE}ld
75OC := ${M0_CROSS_COMPILE}objcopy
76OD := ${M0_CROSS_COMPILE}objdump
77NM := ${M0_CROSS_COMPILE}nm
78PP := ${M0_CROSS_COMPILE}gcc -E ${CFLAGS}
79
80# NOTE: The line continuation '\' is required in the next define otherwise we
81# end up with a line-feed characer at the end of the last c filename.
82# Also bare this issue in mind if extending the list of supported filetypes.
83define SOURCES_TO_OBJS
84 $(notdir $(patsubst %.c,%.o,$(filter %.c,$(1)))) \
85 $(notdir $(patsubst %.S,%.o,$(filter %.S,$(1))))
86endef
87
Caesar Wangb4003742016-10-12 08:10:12 +080088SOURCES := $(C_SOURCES)
Julius Werner705aeeb2016-10-31 19:18:47 -070089OBJS := $(addprefix $(BUILD)/,$(call SOURCES_TO_OBJS,$(SOURCES)))
Xing Zheng93280b72016-10-26 21:25:26 +080090LINKERFILE := $(BUILD)/$(PLAT_M0).ld
Julius Werner705aeeb2016-10-31 19:18:47 -070091MAPFILE := $(BUILD)/$(PLAT_M0).map
92ELF := $(BUILD)/$(PLAT_M0).elf
93BIN := $(BUILD)/$(PLAT_M0).bin
Xing Zheng93280b72016-10-26 21:25:26 +080094LINKERFILE_SRC := src/$(PLAT_M0).ld.S
Caesar Wangb4003742016-10-12 08:10:12 +080095
96# Function definition related compilation
97define MAKE_C
98$(eval OBJ := $(1)/$(patsubst %.c,%.o,$(notdir $(2))))
Julius Werner844782d2016-11-03 12:25:48 -070099-include $(patsubst %.o,%.d,$(OBJ))
Caesar Wangb4003742016-10-12 08:10:12 +0800100
101$(OBJ) : $(2)
102 @echo " CC $$<"
Julius Werner844782d2016-11-03 12:25:48 -0700103 $$(Q)$$(CC) $$(CFLAGS) $$(INCLUDES) -MMD -MT $$@ -c $$< -o $$@
Caesar Wangb4003742016-10-12 08:10:12 +0800104endef
105
106define MAKE_S
107$(eval OBJ := $(1)/$(patsubst %.S,%.o,$(notdir $(2))))
108
109$(OBJ) : $(2)
110 @echo " AS $$<"
111 $$(Q)$$(AS) $$(ASFLAGS) -c $$< -o $$@
112endef
113
114define MAKE_OBJS
115 $(eval C_OBJS := $(filter %.c,$(2)))
116 $(eval REMAIN := $(filter-out %.c,$(2)))
117 $(eval $(foreach obj,$(C_OBJS),$(call MAKE_C,$(1),$(obj),$(3))))
118
119 $(eval S_OBJS := $(filter %.S,$(REMAIN)))
120 $(eval REMAIN := $(filter-out %.S,$(REMAIN)))
121 $(eval $(foreach obj,$(S_OBJS),$(call MAKE_S,$(1),$(obj),$(3))))
122
123 $(and $(REMAIN),$(error Unexpected source files present: $(REMAIN)))
124endef
125
Xing Zheng93280b72016-10-26 21:25:26 +0800126.DEFAULT_GOAL := $(BIN)
127
128$(LINKERFILE): $(LINKERFILE_SRC)
129 $(CC) $(CFLAGS) $(INCLUDES) -P -E -D__LINKER__ -MMD -MF $@.d -MT $@ -o $@ $<
130-include $(LINKERFILE).d
Caesar Wangb4003742016-10-12 08:10:12 +0800131
132$(ELF) : $(OBJS) $(LINKERFILE)
133 @echo " LD $@"
134 $(Q)$(CC) -o $@ $(LDFLAGS) -Wl,-Map=$(MAPFILE) -Wl,-T$(LINKERFILE) \
135 $(OBJS)
136
137$(BIN) : $(ELF)
138 @echo " BIN $@"
139 $(Q)$(OC) -O binary $< $@
140
Julius Werner705aeeb2016-10-31 19:18:47 -0700141$(eval $(call MAKE_OBJS,$(BUILD),$(SOURCES),$(1)))