Caesar Wang | b400374 | 2016-10-12 08:10:12 +0800 | [diff] [blame] | 1 | # |
| 2 | # Copyright (c) 2016, ARM Limited and Contributors. All rights reserved. |
| 3 | # |
dp-arm | fa3cf0b | 2017-05-03 09:38:09 +0100 | [diff] [blame] | 4 | # SPDX-License-Identifier: BSD-3-Clause |
Caesar Wang | b400374 | 2016-10-12 08:10:12 +0800 | [diff] [blame] | 5 | # |
| 6 | |
| 7 | # Cross Compile |
| 8 | M0_CROSS_COMPILE ?= arm-none-eabi- |
| 9 | |
| 10 | # Build architecture |
| 11 | ARCH := cortex-m0 |
| 12 | |
| 13 | # Build platform |
| 14 | PLAT_M0 ?= rk3399m0 |
| 15 | |
| 16 | ifeq (${V},0) |
| 17 | Q=@ |
Caesar Wang | b400374 | 2016-10-12 08:10:12 +0800 | [diff] [blame] | 18 | else |
| 19 | Q= |
| 20 | endif |
| 21 | export Q |
| 22 | |
Caesar Wang | b400374 | 2016-10-12 08:10:12 +0800 | [diff] [blame] | 23 | .SUFFIXES: |
| 24 | |
Xing Zheng | 93280b7 | 2016-10-26 21:25:26 +0800 | [diff] [blame] | 25 | INCLUDES += -Iinclude/ \ |
| 26 | -I../../include/shared/ |
Caesar Wang | b400374 | 2016-10-12 08:10:12 +0800 | [diff] [blame] | 27 | |
| 28 | # NOTE: Add C source files here |
| 29 | C_SOURCES := src/startup.c \ |
Xing Zheng | 93280b7 | 2016-10-26 21:25:26 +0800 | [diff] [blame] | 30 | src/main.c \ |
| 31 | src/suspend.c \ |
Lin Huang | e7c2422 | 2016-11-30 16:57:08 +0800 | [diff] [blame] | 32 | src/dram.c \ |
| 33 | src/stopwatch.c |
Caesar Wang | b400374 | 2016-10-12 08:10:12 +0800 | [diff] [blame] | 34 | |
| 35 | # Flags definition |
Julius Werner | f585246 | 2017-01-30 16:13:21 -0800 | [diff] [blame] | 36 | COMMON_FLAGS := -g -mcpu=$(ARCH) -mthumb -Wall -O3 -nostdlib -mfloat-abi=soft |
| 37 | CFLAGS := -ffunction-sections -fdata-sections -fomit-frame-pointer -fno-common |
| 38 | ASFLAGS := -Wa,--gdwarf-2 |
| 39 | LDFLAGS := -Wl,--gc-sections -Wl,--build-id=none |
Caesar Wang | b400374 | 2016-10-12 08:10:12 +0800 | [diff] [blame] | 40 | |
| 41 | # Cross tool |
| 42 | CC := ${M0_CROSS_COMPILE}gcc |
| 43 | CPP := ${M0_CROSS_COMPILE}cpp |
Caesar Wang | b400374 | 2016-10-12 08:10:12 +0800 | [diff] [blame] | 44 | AR := ${M0_CROSS_COMPILE}ar |
Caesar Wang | b400374 | 2016-10-12 08:10:12 +0800 | [diff] [blame] | 45 | OC := ${M0_CROSS_COMPILE}objcopy |
| 46 | OD := ${M0_CROSS_COMPILE}objdump |
| 47 | NM := ${M0_CROSS_COMPILE}nm |
Caesar Wang | b400374 | 2016-10-12 08:10:12 +0800 | [diff] [blame] | 48 | |
| 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. |
| 52 | define SOURCES_TO_OBJS |
| 53 | $(notdir $(patsubst %.c,%.o,$(filter %.c,$(1)))) \ |
| 54 | $(notdir $(patsubst %.S,%.o,$(filter %.S,$(1)))) |
| 55 | endef |
| 56 | |
Caesar Wang | b400374 | 2016-10-12 08:10:12 +0800 | [diff] [blame] | 57 | SOURCES := $(C_SOURCES) |
Julius Werner | 705aeeb | 2016-10-31 19:18:47 -0700 | [diff] [blame] | 58 | OBJS := $(addprefix $(BUILD)/,$(call SOURCES_TO_OBJS,$(SOURCES))) |
Xing Zheng | 93280b7 | 2016-10-26 21:25:26 +0800 | [diff] [blame] | 59 | LINKERFILE := $(BUILD)/$(PLAT_M0).ld |
Julius Werner | 705aeeb | 2016-10-31 19:18:47 -0700 | [diff] [blame] | 60 | MAPFILE := $(BUILD)/$(PLAT_M0).map |
| 61 | ELF := $(BUILD)/$(PLAT_M0).elf |
| 62 | BIN := $(BUILD)/$(PLAT_M0).bin |
Xing Zheng | 93280b7 | 2016-10-26 21:25:26 +0800 | [diff] [blame] | 63 | LINKERFILE_SRC := src/$(PLAT_M0).ld.S |
Caesar Wang | b400374 | 2016-10-12 08:10:12 +0800 | [diff] [blame] | 64 | |
| 65 | # Function definition related compilation |
| 66 | define MAKE_C |
| 67 | $(eval OBJ := $(1)/$(patsubst %.c,%.o,$(notdir $(2)))) |
Julius Werner | 844782d | 2016-11-03 12:25:48 -0700 | [diff] [blame] | 68 | -include $(patsubst %.o,%.d,$(OBJ)) |
Caesar Wang | b400374 | 2016-10-12 08:10:12 +0800 | [diff] [blame] | 69 | |
| 70 | $(OBJ) : $(2) |
| 71 | @echo " CC $$<" |
Julius Werner | f585246 | 2017-01-30 16:13:21 -0800 | [diff] [blame] | 72 | $$(Q)$$(CC) $$(COMMON_FLAGS) $$(CFLAGS) $$(INCLUDES) -MMD -MT $$@ -c $$< -o $$@ |
Caesar Wang | b400374 | 2016-10-12 08:10:12 +0800 | [diff] [blame] | 73 | endef |
| 74 | |
| 75 | define MAKE_S |
| 76 | $(eval OBJ := $(1)/$(patsubst %.S,%.o,$(notdir $(2)))) |
| 77 | |
| 78 | $(OBJ) : $(2) |
| 79 | @echo " AS $$<" |
Julius Werner | f585246 | 2017-01-30 16:13:21 -0800 | [diff] [blame] | 80 | $$(Q)$$(CC) -x assembler-with-cpp $$(COMMON_FLAGS) $$(ASFLAGS) -c $$< -o $$@ |
Caesar Wang | b400374 | 2016-10-12 08:10:12 +0800 | [diff] [blame] | 81 | endef |
| 82 | |
| 83 | define 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))) |
| 93 | endef |
| 94 | |
Xing Zheng | 93280b7 | 2016-10-26 21:25:26 +0800 | [diff] [blame] | 95 | .DEFAULT_GOAL := $(BIN) |
| 96 | |
| 97 | $(LINKERFILE): $(LINKERFILE_SRC) |
Julius Werner | f585246 | 2017-01-30 16:13:21 -0800 | [diff] [blame] | 98 | $(CC) $(COMMON_FLAGS) $(INCLUDES) -P -E -D__LINKER__ -MMD -MF $@.d -MT $@ -o $@ $< |
Xing Zheng | 93280b7 | 2016-10-26 21:25:26 +0800 | [diff] [blame] | 99 | -include $(LINKERFILE).d |
Caesar Wang | b400374 | 2016-10-12 08:10:12 +0800 | [diff] [blame] | 100 | |
| 101 | $(ELF) : $(OBJS) $(LINKERFILE) |
| 102 | @echo " LD $@" |
Julius Werner | f585246 | 2017-01-30 16:13:21 -0800 | [diff] [blame] | 103 | $(Q)$(CC) -o $@ $(COMMON_FLAGS) $(LDFLAGS) -Wl,-Map=$(MAPFILE) -Wl,-T$(LINKERFILE) $(OBJS) |
Caesar Wang | b400374 | 2016-10-12 08:10:12 +0800 | [diff] [blame] | 104 | |
| 105 | $(BIN) : $(ELF) |
| 106 | @echo " BIN $@" |
| 107 | $(Q)$(OC) -O binary $< $@ |
| 108 | |
Julius Werner | 705aeeb | 2016-10-31 19:18:47 -0700 | [diff] [blame] | 109 | $(eval $(call MAKE_OBJS,$(BUILD),$(SOURCES),$(1))) |