-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
113 lines (81 loc) · 2.15 KB
/
Makefile
File metadata and controls
113 lines (81 loc) · 2.15 KB
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
# directory settings
BUILD_DIR ?= build
BUILD_DIR_TEST ?= build_test
SRC_DIRS ?= src/driver src/ros src/algo
# root files with main() a function
ROOT_CXX ?= main.cc
TEST_CXX ?= main.cc
# target names
TARGET ?= out.a
TEST_TARGET ?= test.a
# list of directories searched for include files
# ex.: '#include "foo.h"' instead of '#include "src/foo.h"'
INC_DIRS ?= $(SRC_DIRS)
# compiler settings
CXX = g++
CXXFLAGS = -g -Wall -std=c++17 -MMD -MP $(INC_FLAGS)
CXXFLAGS_TEST = -g -Wall -std=c++17 -DTESTENV -MMD -MP $(INC_FLAGS)
LDLIBS = -lwiringPi
# --------------
# process vars
INC_FLAGS := $(addprefix -I,$(INC_DIRS))
ROOT_OBJ := $(ROOT_CXX:%=$(BUILD_DIR)/%.o)
TEST_OBJ := $(TEST_CXX:%=$(BUILD_DIR_TEST)/%.o)
TARGET_EXEC = $(BUILD_DIR)/$(TARGET)
TEST_EXEC = $(BUILD_DIR_TEST)/$(TEST_TARGET)
# find all .cc files in src directory
SRCS := $(shell find $(SRC_DIRS) -name "*.cc")
OBJS := $(SRCS:%=$(BUILD_DIR)/%.o)
OBJS_TEST := $(SRCS:%=$(BUILD_DIR_TEST)/%.o)
DEPS := $(OBJS_:.o=.d)
DEPS_TEST := $(OBJS_TEST:.o=.d)
# --------------
# build target
$(TARGET_EXEC): $(ROOT_OBJ) $(OBJS)
$(CXX) $(CXXFLAGS) $^ $(LDLIBS) -o $@
# run target
.PHONY: run
run: $(TARGET_EXEC)
@echo ""
@$<
@echo ""
# --------------
# compile .cc files
$(BUILD_DIR)/%.cc.o: %.cc
@$(MKDIR_P) $(dir $@)
$(CXX) $(CXXFLAGS) -c $< -o $@
$(BUILD_DIR_TEST)/%.cc.o: %.cc
@$(MKDIR_P) $(dir $@)
$(CXX) $(CXXFLAGS_TEST) -c $< -o $@
# --------------
# build test
test: $(TEST_EXEC)
# --------------
# run test
testrun: $(TEST_EXEC)
@echo "Run tests:\n"
@$<
@echo ""
# build test
$(TEST_EXEC): $(TEST_OBJ) $(OBJS_TEST)
$(CXX) $(CXXFLAGS) $^ -o $@
# --------------
# clean (remove build directory)
.PHONY: clean
clean:
@echo "\nClean build directory:"
@$(RM) -r $(BUILD_DIR)
@$(RM) -r $(BUILD_DIR_TEST)
@echo "clean successful\n"
# -------------
# cppcheck
CHECKFLAGS = --std=c++11 --enable=all --cppcheck-build-dir=build-check/ $(INC_FLAGS)
.PHONY: check
check:
cppcheck $(CHECKFLAGS) main.cc src/
# --------------
# load all dependencies (auto generated by compiler)
-include $(DEPS)
-include $(DEPS_TEST)
# define command to create directories (and parents), ex.: /build
MKDIR_P ?= mkdir -p