-
Notifications
You must be signed in to change notification settings - Fork 14
/
makeNativeArduino.mk
101 lines (76 loc) · 2.91 KB
/
makeNativeArduino.mk
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
penultimateword = $(wordlist $(words $1),$(words $1), x $1)
SKETCH_ROOT := $(shell dirname $(abspath $(call penultimateword, $(MAKEFILE_LIST))))
NATIVE_ROOT := $(shell dirname $(abspath $(lastword $(MAKEFILE_LIST))))
Q ?= @
BUILD_ROOT ?= build
SKETCH ?= $(shell find $(SKETCH_ROOT) -maxdepth 1 -name "*.ino")
TARGET := $(shell basename -s .ino $(SKETCH))
$(shell mkdir -p $(BUILD_ROOT))
CFLAGS += -Wall -Wextra -Wno-unused-parameter
CFLAGS += -DARDUINO=101 -DSKETCH_FILE=\"$(SKETCH)\"
CFLAGS += -std=gnu11
CFLAGS += -lm
CFLAGS += -DARDUINOONPC
#CFLAGS += -DFASTLED_SDL $(shell sdl2-config --cflags)
CXXFLAGS += -Wall -Wextra -Wno-unused-parameter
CXXFLAGS += -DARDUINO=101 -DSKETCH_FILE=\"$(SKETCH)\"
CXXFLAGS += -Wno-class-memaccess # FastLED does some naughty things
CXXFLAGS += -std=gnu++11
CXXFLAGS += -DARDUINOONPC
#CXXFLAGS += -DFASTLED_SDL $(shell sdl2-config --cflags)
LDFLAGS += -Wl,--gc-sections
LDFLAGS += -L/usr/X11R6/lib -lX11 # include X11 library
LDFLAGS += -pthread # include linux thread library
#LDFLAGS += $(shell sdl2-config --libs)
DEPDIR := $(BUILD_ROOT)
DEPFLAGS = -MT $@ -MMD -MP -MF $(DEPDIR)/$*.Td
POSTCOMPILE = @mv -f $(DEPDIR)/$*.Td $(DEPDIR)/$*.d && touch $@
define add_lib
SRC_C += $(shell find $1 -name '*.c')
SRC_CXX += $(shell find $1 -name '*.cpp')
SRC_USER_C = $(shell find $(SKETCH_ROOT) -name "*.c")
SRC_USER_CXX = $(shell find $(SKETCH_ROOT) -name "*.cpp")
INCLUDES += -I$1
endef
INCLUDES += -I$(SKETCH_ROOT)
INCLUDES += -I$(NATIVE_ROOT)/src/cores/arduino -I$(NATIVE_ROOT)/src/system
$(eval $(call add_lib,$(NATIVE_ROOT)/src))
$(foreach lib, $(ARDUINO_LIBS), $(eval $(call add_lib,$(NATIVE_ROOT)/libraries/$(lib))))
USER_FOLDERS = $(shell find $(SKETCH_ROOT) -path $(SKETCH_ROOT)/build -prune -o -type d -print)
INC_USER_FOLDERS = $(foreach folder, $(USER_FOLDERS), -I$(folder))
OBJECTS += $(SRC_C:%.c=$(BUILD_ROOT)/%.o)
OBJECTS += $(SRC_CXX:%.cpp=$(BUILD_ROOT)/%.o)
OBJECTS += $(SRC_USER_C:%.c=$(BUILD_ROOT)/%.o)
OBJECTS += $(SRC_USER_CXX:%.cpp=$(BUILD_ROOT)/%.o)
SRCS += $(SRC_C)
SRCS += $(SRC_CXX)
SRCS += $(SRC_USER_C)
SRCS += $(SRC_USER_CXX)
$(TARGET): $(OBJECTS)
$(Q)$(CXX) $(INC_USER_FOLDERS) $(INCLUDES) $(OBJECTS) $(LDFLAGS) -o $@
@size $@
clean:
rm -r $(BUILD_ROOT)
rm $(TARGET)
print:
@echo "BUILD_ROOT:\t $(BUILD_ROOT)"
@echo "INCLUDES:\t $(INCLUDES)"
@echo "OBJECTS:\t $(OBJECTS)"
@echo "SRCS:\t $(SRCS)"
@echo "SKETCH:\t $(SKETCH)"
@echo "ROOT:\t $(NATIVE_ROOT)"
@echo "SRC_C :\t $(SRC_C)"
@echo "SRC_CXX :\t $(SRC_CXX)"
@echo "SRC_USER:\t $(SRC_USER)"
@echo ":\t $(INC_USER_FOLDERS)"
$(BUILD_ROOT)/%.o : %.c $(DEPDIR)/%.d
@mkdir -p `dirname $@`
$(Q)$(CC) $(DEPFLAGS) $(CFLAGS) $(INCLUDES) -c $< -o $@
$(POSTCOMPILE)
$(BUILD_ROOT)/%.o : %.cpp $(DEPDIR)/%.d
@mkdir -p `dirname $@`
$(Q)$(CXX) $(DEPFLAGS) $(CXXFLAGS) $(INCLUDES) -c $< -o $@
$(POSTCOMPILE)
$(DEPDIR)/%.d: ;
.PRECIOUS: $(DEPDIR)/%.d
include $(wildcard $(patsubst %,$(DEPDIR)/%.d,$(basename $(SRCS))))