-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
163 lines (128 loc) · 6.52 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# The Basics
# -------------------------------------------------------------------------------------------------------------
BOARD := adafruit_feather_m0
DEVICE := ttyACM0
TARGET := clearair
PACKAGE_DIR := $(HOME)/.arduino15/packages/adafruit
SAMD_DIR := $(PACKAGE_DIR)/hardware/samd/1.0.9
BAUD := 9600
SOURCES := $(wildcard $(addprefix src/, *.c *.cpp))
HEADERS := $(wildcard $(addprefix src/, *.h))
OUTPUT := bin/deploy
# Tools
# -------------------------------------------------------------------------------------------------------------
TOOLS_DIR := $(PACKAGE_DIR)/tools/arm-none-eabi-gcc/4.8.3-2014q1/bin
CC := $(TOOLS_DIR)/arm-none-eabi-gcc
CPPC := $(TOOLS_DIR)/arm-none-eabi-g++
AR := $(TOOLS_DIR)/arm-none-eabi-ar
OBJCOPY := $(TOOLS_DIR)/arm-none-eabi-objcopy
UPLOADER := $(PACKAGE_DIR)/tools/bossac/1.6.1-arduino/bossac
# Board info
# -------------------------------------------------------------------------------------------------------------
GET_BOARDS_PARAM = $(shell sed -ne "s/$(BOARD).$(1)=\(.*\)/\1/p" $(SAMD_DIR)/boards.txt)
BOARD_BOARD := $(call GET_BOARDS_PARAM,build.board)
BOARD_BOOTLOADER_FILE := $(call GET_BOARDS_PARAM,bootloader.file)
BOARD_BUILD_FCPU := $(call GET_BOARDS_PARAM,build.f_cpu)
BOARD_BUILD_MCU := $(call GET_BOARDS_PARAM,build.mcu)
BOARD_BUILD_VARIANT := $(call GET_BOARDS_PARAM,build.variant)
BOARD_LD_SCRIPT := $(call GET_BOARDS_PARAM,build.ldscript)
BOARD_USB_MANUFACTURER := $(call GET_BOARDS_PARAM,build.usb_manufacturer)
BOARD_USB_PID := $(call GET_BOARDS_PARAM,build.pid)
BOARD_USB_PRODUCT := $(call GET_BOARDS_PARAM,build.usb_product)
BOARD_USB_VID := $(call GET_BOARDS_PARAM,build.vid)
# The variant file for the board contains essential information specific to the device we're compiling for
SOURCES += $(SAMD_DIR)/variants/$(BOARD_BUILD_VARIANT)/variant.cpp
OBJECTS := $(addsuffix .o, $(basename $(SOURCES)))
COMPILED_OBJECTS := $(addprefix $(OUTPUT)/, $(addsuffix .o, $(basename $(SOURCES))))
# Flags
# -------------------------------------------------------------------------------------------------------------
CFLAGS := -mcpu=$(BOARD_BUILD_MCU) -mthumb -c -g -Os -w -std=gnu11 -ffunction-sections -fdata-sections \
-nostdlib --param max-inline-insns-single=500 -MMD
CPPFLAGS := -mcpu=$(BOARD_BUILD_MCU) -mthumb -c -g -Os -w -std=gnu++11 -ffunction-sections -fdata-sections \
-fno-threadsafe-statics -fno-rtti -fno-exceptions -nostdlib --param max-inline-insns-single=500 -MMD
ASMFLAGS := -c -g -x assembler-with-cpp
ELFFLAGS := -Os -Wl,--gc-sections -save-temps --specs=nano.specs --specs=nosys.specs -mcpu=$(BOARD_BUILD_MCU) \
-mthumb -Wl,--cref -Wl,--check-sections -Wl,--gc-sections -Wl,--unresolved-symbols=report-all \
-Wl,--warn-common -Wl,--warn-section-align
# Defines
# -------------------------------------------------------------------------------------------------------------
DEFINES := -DF_CPU=$(BOARD_BUILD_FCPU) -DARDUINO=10605 -DARDUINO_$(BOARD_BOARD) -DARDUINO_ARCH_SAMD \
-D__SAMD21G18A__ -DUSB_VID=$(BOARD_USB_VID) -DUSB_PID=$(BOARD_USB_PID) -DUSBCON \
-DUSB_MANUFACTURER=\"$(BOARD_USB_MANUFACTURER)\" -DUSB_PRODUCT=\"$(BOARD_USB_PRODUCT)\"
# Includes
# -------------------------------------------------------------------------------------------------------------
INCLUDE_DIRS := lib/ \
$(wildcard lib/*) \
$(PACKAGE_DIR)/tools/CMSIS/4.0.0-atmel/CMSIS/Include/ \
$(PACKAGE_DIR)/tools/CMSIS/4.0.0-atmel/Device/ATMEL/ \
$(SAMD_DIR)/cores/arduino \
$(SAMD_DIR)/variants/$(BOARD_BUILD_VARIANT) \
$(SAMD_DIR)/libraries/SPI \
$(SAMD_DIR)/libraries/Wire
# Add the include flag before each include
INCLUDES := $(foreach dir, $(INCLUDE_DIRS), \
$(addprefix -I, $(dir)))
# Project Libraries
# -------------------------------------------------------------------------------------------------------------
LIBRARY_SEARCH_PATHS ?= lib $(SAMD_DIR)/libraries
# Search the headers for what libraries are included
FOUND_LIBRARIES := $(filter $(notdir $(wildcard $(addsuffix /*, $(LIBRARY_SEARCH_PATHS)))), \
$(shell sed -ne "s/^ *\# *include *[<\"]\(.*\)\.h[>\"]/\1/p" $(HEADERS)))
# Add the path to the found libraries
LIBRARY_DIRS := $(foreach lib, $(FOUND_LIBRARIES), \
$(firstword $(wildcard $(addsuffix /$(lib), $(LIBRARY_SEARCH_PATHS)))))
# Explicitly add everything in lib/
EXPLICIT_LIBRARIES = $(notdir $(wildcard lib/*))
LIBRARY_DIRS += $(foreach lib, $(EXPLICIT_LIBRARIES), \
$(shell find lib/$(lib) -type d))
# Platform Libraries
# -------------------------------------------------------------------------------------------------------------
ARDUINOCOREDIR := $(SAMD_DIR)/cores/arduino/ \
$(SAMD_DIR)/cores/arduino/USB/ \
$(SAMD_DIR)/cores/arduino/avr/
# Link all the platform libraries into a single archive
CORE_LIB := $(OUTPUT)/core.a
CORE_LIB_OBJECTS := $(foreach dir, $(ARDUINOCOREDIR) $(LIBRARY_DIRS), \
$(patsubst %, $(OUTPUT)/%.o, $(wildcard $(addprefix $(dir)/, *.c *.cpp *.S))))
# Recipes
# -------------------------------------------------------------------------------------------------------------
.PHONY: all target upload size console clean test
all: target
target: $(TARGET).bin
upload:
$(UPLOADER) -i -d --port=$(DEVICE) -U true -i -e -w -v $(OUTPUT)/$(TARGET).bin -R
size: $(TARGET).elf
echo && avr-size $(OUTPUT)/$(TARGET).elf
console:
screen /dev/$(DEVICE) $(BAUD)
$(TARGET).bin: $(TARGET).elf
$(OBJCOPY) -O binary $(OUTPUT)/$< $(OUTPUT)/$@
$(TARGET).elf: $(CORE_LIB) $(OBJECTS)
$(CC) -L$(OUTPUT) -T$(SAMD_DIR)/variants/$(BOARD_BUILD_VARIANT)/$(BOARD_LD_SCRIPT) \
-Wl,-Map,$(OUTPUT)/$(TARGET).map $(ELFFLAGS) -o $(OUTPUT)/$@ $(COMPILED_OBJECTS) \
-Wl,--start-group -lm $(CORE_LIB) -Wl,--end-group
# Build the source files
# -------------------------------------------------------------------------------------------------------------
%.o: %.c
mkdir -p $(OUTPUT)$(dir $<)
$(CC) $(CFLAGS) $(PROJECT) $(DEFINES) $(INCLUDES) -o $(OUTPUT)/$@ $<
%.o: %.cpp
mkdir -p $(OUTPUT)/$(dir $<)
$(CPPC) $(CPPFLAGS) $(PROJECT) $(DEFINES) $(INCLUDES) -o $(OUTPUT)/$@ $<
# Build the core library files
# -------------------------------------------------------------------------------------------------------------
$(CORE_LIB): $(CORE_LIB_OBJECTS)
$(AR) rcs $@ $?
$(OUTPUT)/%.c.o: %.c
mkdir -p $(dir $@)
$(CC) $(CFLAGS) $(PROJECT) $(DEFINES) $(INCLUDES) -o $@ $<
$(OUTPUT)/%.cpp.o: %.cpp
mkdir -p $(dir $@)
$(CPPC) $(CPPFLAGS) $(PROJECT) $(DEFINES) $(INCLUDES) -o $@ $<
$(OUTPUT)/%.S.o: %.S
mkdir -p $(dir $@)
$(CC) $(CPPFLAGS) $(PROJECT) $(DEFINES) $(INCLUDES) -o $@ $<
clean:
rm -f $(COMPILED_OBJECTS)
rm -f $(OUTPUT)/$(TARGET).elf $(OUTPUT)/$(TARGET).bin $(CORE_LIB) $(OUTPUT)/$(TARGET).map
rm -rf $(OUTPUT)/src $(OUTPUT)/lib