-
Notifications
You must be signed in to change notification settings - Fork 0
/
makefile
105 lines (81 loc) · 2.61 KB
/
makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# Compiler
CC := arm-none-eabi-gcc
AS := arm-none-eabi-as
# Targets
TARGET_NAME := gps_time
BUILDPREFIX := build/
SOURCEPREFIX := sources/
SOURCES += $(SOURCEPREFIX)main.c \
$(SOURCEPREFIX)start.c
ASM_SOURCES += $(SOURCEPREFIX)gcc_startup.s
# Primary output target is elf
TARGETS := $(BUILDPREFIX)$(TARGET_NAME).elf
TARGETS += $(BUILDPREFIX)$(TARGET_NAME).hex
# Linker
LDSCRIPT := gcc_nrf51822_gpstime.ld
LDFLAGS := -Wl,--gc-sections
# Initialize compiler flags to empty
CFLAGS :=
# Include paths
CMSIS_PATH := $(SOURCEPREFIX)cmsis/
# Compiler paths
CFLAGS += -Wall -std=gnu99 -mthumb -ggdb -Os -ffunction-sections -fdata-sections
CFLAGS += -Werror -lnosys -lgcc --specs=nano.specs -nostartfiles
CFLAGS += -mcpu=cortex-m0 -DARM_MATH_CM0
ASMFLAGS := $(CFLAGS) -x assembler-with-cpp
# Include all subdir makefiles
include $(SOURCEPREFIX)makefile
CFLAGS += -I$(CMSIS_PATH)
# must escape quotes for echo to work
ESCAPED_FLAGS := $(subst ",\",$(CFLAGS))
# Generate object file names and add source and build path prefixes.
OBJECTS := $(patsubst $(SOURCEPREFIX)%, \
$(BUILDPREFIX)%, \
$(patsubst *.S,*.o,$(ASM_SOURCES:.s=.o)) \
$(SOURCES:.c=.o) $(SYSLIBSOURCES:.c=.o))
define BUILD_HEX_FILE
echo "Converting $(2) -> $(1)"
arm-none-eabi-objcopy $(2) -O ihex $(1)
endef
define LINK
echo " Linking $(1)"
$(CC) $(CFLAGS) $(LDFLAGS) -o $(1) $(2) \
-Wl,-Map,$(patsubst %.elf,%.map,$(1)) \
-Wl,-T,$(LDSCRIPT)
endef
define COMPILE
echo " CC $(2) $(ESCAPED_FLAGS)"
mkdir -p $(dir $(1))
$(CC) $(CFLAGS) -c -o $(1) $(2) -MMD -MF $(1:.o=.d)
endef
define COMPILE_ASM
echo " AS $(2) $(ESCAPED_FLAGS)"
mkdir -p $(dir $(1))
$(CC) $(ASMFLAGS) -c -o $(1) $(2) -MMD -MF $(1:.o=.d)
endef
define CLEAN
$(RM) $(TARGETS) $(OBJECTS) $(AUTODEPS) \
$(patsubst %.elf,%.map,$(filter %.elf,$(TARGETS)))
endef
# Use dependency files automatically generated by GCC.
# First collect all C source files
AUTODEPS := $(patsubst $(SOURCEPREFIX)%.c, $(BUILDPREFIX)%.d, $(SOURCES))
# And then add the asm source files
AUTODEPS := $(patsubst $(SOURCEPREFIX)%.s, $(BUILDPREFIX)%.d, \
$(patsubst $(SOURCEPREFIX)%.S, $(BUILDPREFIX)%.d, \
$(AUTODEPS) $(ASM_SOURCES)))
.SILENT:
.PHONY: all
all: $(TARGETS)
.PHONY: clean
clean:
$(call CLEAN)
$(BUILDPREFIX)$(TARGET_NAME).elf: $(OBJECTS)
$(call LINK,$@,$^)
%.hex: %.elf
$(call BUILD_HEX_FILE,$@,$<)
$(BUILDPREFIX)%.o: $(SOURCEPREFIX)%.c
$(call COMPILE,$@,$<)
$(BUILDPREFIX)%.o: $(SOURCEPREFIX)%.s
$(call COMPILE_ASM,$@,$<)
-include $(AUTODEPS)