-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
127 lines (92 loc) · 3.85 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
# to modify
# installation locations
ARDUINO_DIR := $(HOME)/bin/arduino-1.8.10
TURTLE_DIR := $(HOME)/bin/turtle_1.3.1
# self-compiled arduino library
ARDUINO_LIB := $(HOME)/lib/arduino/coreObjectFiles/libcore.a
# arduino specifications
F_CPU := 16000000
MCU := atmega328p
# arduino programs
AVR_CC := $(ARDUINO_DIR)/hardware/tools/avr/bin/avr-gcc
AVR_CXX := $(ARDUINO_DIR)/hardware/tools/avr/bin/avr-g++
AVR_AR := $(ARDUINO_DIR)/hardware/tools/avr/bin/avr-ar
AVR_OBJCOPY := $(ARDUINO_DIR)/hardware/tools/avr/bin/avr-objcopy
AVR_DUDE := $(ARDUINO_DIR)/hardware/tools/avr/bin/avrdude
# project-directories
PROJECT_DIR := .
SRC_DIR := src
TEST_DIR := test
BUILD_DIR := build
BIN_DIR := bin
# compiler flags
GENERAL_FLAGS := -std=gnu++14 -Wall
AVRCOMPILE_FLAGS = -x c++ -ffunction-sections -fdata-sections -mmcu=$(MCU) -DF_CPU=$(F_CPU)L -Os -include Arduino.h -MT $@ -MMD -MP -MF $(@:%.avr_o=%.d) $(GENERAL_FLAGS)
AVRLINK_FLAGS := -mmcu=$(MCU) -Wl,--gc-sections -Os
AVR_INC := -I $(ARDUINO_DIR)/hardware/arduino/avr/cores/arduino/ -I $(ARDUINO_DIR)/hardware/arduino/avr/variants/standard
AVRDUDE_CONF := $(ARDUINO_DIR)/hardware/tools/avr/etc/avrdude.conf
CXX := g++
CXX_FLAGS = -Wall -Wno-deprecated-declarations -MT $@ -MMD -MP -MF $(@:%.o=%.d) $(GENERAL_FLAGS)
LDFLAGS := -lboost_unit_test_framework -lm
CXX_INC := -I $(TURTLE_DIR)/include
# files
SRC_EXT := cpp
TARGET_NAME := $(shell basename $(PWD))
SRC_MAIN := $(TARGET_NAME).ino
TARGET := $(BIN_DIR)/$(TARGET_NAME).hex
TARGET_ELF := $(TARGET:.hex=.elf)
SRC := $(shell find $(SRC_DIR) -type f -name *.$(SRC_EXT))
AVR_OBJECTS := $(patsubst $(SRC_DIR)/%,$(BUILD_DIR)/%,$(SRC:.$(SRC_EXT)=.avr_o)) $(BUILD_DIR)/$(TARGET_NAME).avr_o
OBJECTS := $(patsubst $(SRC_DIR)/%,$(BUILD_DIR)/%,$(SRC:.$(SRC_EXT)=.o))
TESTS := $(shell find $(TEST_DIR) -type f -name *_test.$(SRC_EXT))
TEST_TARGETS := $(patsubst $(TEST_DIR)/%,$(BIN_DIR)/%,$(TESTS:_test.$(SRC_EXT)=.test))
TEST_OBJECTS := $(patsubst $(TEST_DIR)/%,$(BUILD_DIR)/%,$(TESTS:.$(SRC_EXT)=.o))
# fill depfiles if build_dir exists
ifeq ($(wildcard $(BUILD_DIR)/),)
DEPFILES :=
else
DEPFILES := $(shell find $(BUILD_DIR) -type f -name *.d)
endif
# use secondexpansion (needed to create directories)
.SECONDEXPANSION:
# target
$(TARGET) :
# upload to arduino
upload : $(TARGET)
$(AVR_DUDE) -C$(AVRDUDE_CONF) -v -p$(MCU) -carduino -P/dev/ttyACM0 -b115200 -D -Uflash:w:$<:i
# arduino compilation
$(BIN_DIR)/%.hex : $(BIN_DIR)/%.elf |$$(@D)/.f
$(AVR_OBJCOPY) -O ihex -R .eeprom $< $@
$(BIN_DIR)/$(TARGET_NAME).elf : $(AVR_OBJECTS) |$$(@D)/.f
$(AVR_CC) $(AVRLINK_FLAGS) -o $@ $^ $(ARDUINO_LIB) -lc -lm
$(BUILD_DIR)/$(TARGET_NAME).avr_o : $(PROJECT_DIR)/$(SRC_MAIN) |$$(@D)/.f
$(AVR_CXX) $(AVR_INC) $(AVRCOMPILE_FLAGS) $< -c -o $@
$(BUILD_DIR)/%.avr_o : $(SRC_DIR)/%.cpp |$$(@D)/.f
$(AVR_CXX) $(AVR_INC) $(AVRCOMPILE_FLAGS) $< -c -o $@
# test compilation
tests : $(TEST_TARGETS)
$(BIN_DIR)/%.test : $(BUILD_DIR)/%_test.o $(BUILD_DIR)/%.o |$$(@D)/.f
$(CXX) $(CXX_FLAGS) $^ -o $@ $(LDFLAGS)
@echo Running $@
@./$@ #--log_level=test_suite
@echo
$(BUILD_DIR)/%_test.o : $(TEST_DIR)/%_test.cpp |$$(@D)/.f
$(CXX) $(CXX_FLAGS) $< -I $(PROJECT_DIR) $(CXX_INC) -c -o $@
$(BUILD_DIR)/%.o: $(SRC_DIR)/%.cpp |$$(@D)/.f
$(CXX) $(CXX_FLAGS) $< -I $(PROJECT_DIR) -c -o $@
# Makefile stuff
# use .d (dependency) files for prerequisite generation
$(DEPFILES):
include $(wildcard $(DEPFILES))
# create directory if not present (if dir/.f does not exist)
%/.f:
mkdir -p $(dir $@)
touch $@
clean :
rm -rf $(BUILD_DIR)/* $(TARGET) $(TARGET_ELF) $(TEST_TARGETS)
# targets that are not files
.PHONY : upload clean tests
# targets that are not explicitly named in Makefile should not automatically be deleted
.SECONDARY : $(TARGET_ELF) $(TARGET) $(AVR_OBJECTS) $(OBJECTS) $(TEST_OBJECTS)
# similar to .secondary, but works with patterns and targets are additionally not deleted when make is killed or interrupted
.PRECIOUS: %/.f