blob: b8e3cd410b343da17b3a4c00ae194cc0ee69fdad [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
49INCLUDES += -Iinclude/
50
51# NOTE: Add C source files here
52C_SOURCES := src/startup.c \
53 src/main.c
54
55# Flags definition
56CFLAGS := -g
57ASFLAGS := -g -Wa,--gdwarf-2
58
59ASFLAGS += -mcpu=$(ARCH) -mthumb -Wall -ffunction-sections -O3
60CFLAGS += -mcpu=$(ARCH) -mthumb -Wall -ffunction-sections -O3
61
62LDFLAGS := -mcpu=$(ARCH) -mthumb -g -nostartfiles -O3
63LDFLAGS += -Wl,--gc-sections -Wl,--build-id=none
64
65# Cross tool
66CC := ${M0_CROSS_COMPILE}gcc
67CPP := ${M0_CROSS_COMPILE}cpp
68AS := ${M0_CROSS_COMPILE}gcc
69AR := ${M0_CROSS_COMPILE}ar
70LD := ${M0_CROSS_COMPILE}ld
71OC := ${M0_CROSS_COMPILE}objcopy
72OD := ${M0_CROSS_COMPILE}objdump
73NM := ${M0_CROSS_COMPILE}nm
74PP := ${M0_CROSS_COMPILE}gcc -E ${CFLAGS}
75
76# NOTE: The line continuation '\' is required in the next define otherwise we
77# end up with a line-feed characer at the end of the last c filename.
78# Also bare this issue in mind if extending the list of supported filetypes.
79define SOURCES_TO_OBJS
80 $(notdir $(patsubst %.c,%.o,$(filter %.c,$(1)))) \
81 $(notdir $(patsubst %.S,%.o,$(filter %.S,$(1))))
82endef
83
Caesar Wangb4003742016-10-12 08:10:12 +080084SOURCES := $(C_SOURCES)
Julius Werner705aeeb2016-10-31 19:18:47 -070085OBJS := $(addprefix $(BUILD)/,$(call SOURCES_TO_OBJS,$(SOURCES)))
Caesar Wangb4003742016-10-12 08:10:12 +080086LINKERFILE := src/rk3399m0.ld
Julius Werner705aeeb2016-10-31 19:18:47 -070087MAPFILE := $(BUILD)/$(PLAT_M0).map
88ELF := $(BUILD)/$(PLAT_M0).elf
89BIN := $(BUILD)/$(PLAT_M0).bin
Caesar Wangb4003742016-10-12 08:10:12 +080090
91# Function definition related compilation
92define MAKE_C
93$(eval OBJ := $(1)/$(patsubst %.c,%.o,$(notdir $(2))))
Julius Werner844782d2016-11-03 12:25:48 -070094-include $(patsubst %.o,%.d,$(OBJ))
Caesar Wangb4003742016-10-12 08:10:12 +080095
96$(OBJ) : $(2)
97 @echo " CC $$<"
Julius Werner844782d2016-11-03 12:25:48 -070098 $$(Q)$$(CC) $$(CFLAGS) $$(INCLUDES) -MMD -MT $$@ -c $$< -o $$@
Caesar Wangb4003742016-10-12 08:10:12 +080099endef
100
101define MAKE_S
102$(eval OBJ := $(1)/$(patsubst %.S,%.o,$(notdir $(2))))
103
104$(OBJ) : $(2)
105 @echo " AS $$<"
106 $$(Q)$$(AS) $$(ASFLAGS) -c $$< -o $$@
107endef
108
109define MAKE_OBJS
110 $(eval C_OBJS := $(filter %.c,$(2)))
111 $(eval REMAIN := $(filter-out %.c,$(2)))
112 $(eval $(foreach obj,$(C_OBJS),$(call MAKE_C,$(1),$(obj),$(3))))
113
114 $(eval S_OBJS := $(filter %.S,$(REMAIN)))
115 $(eval REMAIN := $(filter-out %.S,$(REMAIN)))
116 $(eval $(foreach obj,$(S_OBJS),$(call MAKE_S,$(1),$(obj),$(3))))
117
118 $(and $(REMAIN),$(error Unexpected source files present: $(REMAIN)))
119endef
120
Julius Werner705aeeb2016-10-31 19:18:47 -0700121.PHONY: all
122all: $(BIN)
Caesar Wangb4003742016-10-12 08:10:12 +0800123
124$(ELF) : $(OBJS) $(LINKERFILE)
125 @echo " LD $@"
126 $(Q)$(CC) -o $@ $(LDFLAGS) -Wl,-Map=$(MAPFILE) -Wl,-T$(LINKERFILE) \
127 $(OBJS)
128
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)))