@@ -68,18 +68,19 @@ EPOXY_DUINO_PARENT_DIR := $(abspath $(EPOXY_DUINO_DIR)/..)
6868# directory.
6969ARDUINO_LIB_DIRS ?=
7070
71- # C macro to select a specific core. Valid options are:
72- # EPOXY_CORE_AVR (default), EPOXY_CORE_ESP8266.
71+ # CPP macro to select a specific core. Valid options are:
72+ # EPOXY_CORE_AVR (default), and EPOXY_CORE_ESP8266.
7373EPOXY_CORE ?= EPOXY_CORE_AVR
7474
7575# Define the directory where the <Arduino.h> and other core API files are
7676# located. The default is $(EPOXY_DUINO_DIR)/cores/epoxy.
7777EPOXY_CORE_PATH ?= $(EPOXY_DUINO_DIR)/cores/epoxy
7878
7979# Find the directory paths of the libraries listed in ARDUINO_LIBS by looking
80- # under directory given by EPOXY_DUINO_LIB_DIR, the directory given by
81- # EPOXY_DUINO_PARENT_DIR to look for siblings, and each directory listed in
82- # ARDUINO_LIB_DIRS.
80+ # at the following:
81+ # 1) under the directory given by EPOXY_DUINO_LIB_DIR,
82+ # 2) the sibling directories under EPOXY_DUINO_PARENT_DIR, and
83+ # 3) each directory listed in ARDUINO_LIB_DIRS.
8384EPOXY_MODULES := $(foreach lib,$(ARDUINO_LIBS),${EPOXY_DUINO_LIB_DIR}/${lib})
8485EPOXY_MODULES += $(foreach lib,$(ARDUINO_LIBS),${EPOXY_DUINO_PARENT_DIR}/${lib})
8586EPOXY_MODULES += \
@@ -118,27 +119,37 @@ EPOXY_MODULES += \
118119# like c++ on FreeBSD is actualy clang++. It's possible that FreeBSD has the
119120# latest GNU version of libstdc++, but I'm not sure.
120121#
121- # I added EXTRA_CXXFLAGS to allow end-user Makefiles to specify additional
122- # CXXFLAGs.
122+ # The CFLAGS assume that any C files are C11 files (not C99) because my
123+ # AceTimeC library requires C11. It may be possible to override that by passing
124+ # the `-std=c99` flag in the EXTRA_CFLAGS variable (which overrides the
125+ # `-std=c11` flag), but this has not been tested.
126+ #
127+ # I added EXTRA_CXXFLAGS and EXTRA_CFLAGS to allow end-user Makefiles to
128+ # specify additional flags to CXXFLAGS and CFLAGS.
123129ifeq ($(UNAME), Linux)
124130CXX ?= g++
125- CXXFLAGS ?= -Wextra -Wall -std=gnu++11 -fno-exceptions -fno-threadsafe-statics -flto
131+ CXXFLAGS ?= -Wextra -Wall -std=gnu++11 -flto \
132+ -fno-exceptions -fno-threadsafe-statics
133+ CFLAGS ?= -Wextra -Wall -std=c11 -flto
126134else ifeq ($(UNAME), Darwin)
127135CXX ?= clang++
128136CXXFLAGS ?= -Wextra -Wall -std=c++11 -stdlib=libc++
137+ CFLAGS ?= -Wextra -Wall -std=c11
129138else ifeq ($(UNAME), FreeBSD)
130139CXX ?= clang++
131140CXXFLAGS ?= -Wextra -Wall -std=c++11 -stdlib=libc++
141+ CFLAGS ?= -Wextra -Wall -std=c11
132142endif
133143CXXFLAGS += $(EXTRA_CXXFLAGS)
144+ CFLAGS += $(EXTRA_CFLAGS)
134145
135146# Pre-processor flags (-I, -D, etc), mostly for header files.
136147CPPFLAGS += $(EXTRA_CPPFLAGS)
137148# Define a macro to indicate that EpoxyDuino is being used. Defined here
138149# instead of Arduino.h so that files like 'compat.h' can determine the
139150# compile-time environment without having to include <Arduino.h>.
140151# Also define UNIX_HOST_DUINO for backwards compatibility.
141- CPPFLAGS += -D UNIX_HOST_DUINO -D EPOXY_DUINO -D $(EPOXY_CORE)
152+ CPPFLAGS += -D ARDUINO=100 -D UNIX_HOST_DUINO -D EPOXY_DUINO -D $(EPOXY_CORE)
142153# Add the header files for the Core files.
143154CPPFLAGS += -I$(EPOXY_CORE_PATH)
144155# Add the header files for libraries. Old Arduino libraries place the header
@@ -150,52 +161,88 @@ CPPFLAGS += $(foreach module,$(EPOXY_MODULES),$(CPPFLAGS_EXPANSION))
150161# Linker settings (e.g. -lm).
151162LDFLAGS ?=
152163
153- # Generate list of C++ srcs to compile.
164+ # Collect list of C and C++ srcs to compile.
154165#
155- # 1) Add the source files in the Core directory. Support subdirectory
166+ # 1) Collect the source files in the Epoxy Core directory. Support subdirectory
156167# expansions up to 3 levels below the given target. (There might be a better
157168# way to do this using GNU Make but I can't find a mechanism that doesn't barf
158169# when the 'src/' directory doesn't exist.)
159170EPOXY_SRCS := $(wildcard $(EPOXY_CORE_PATH)/*.cpp) \
160171 $(wildcard $(EPOXY_CORE_PATH)/*/*.cpp) \
161172 $(wildcard $(EPOXY_CORE_PATH)/*/*/*.cpp) \
162173 $(wildcard $(EPOXY_CORE_PATH)/*/*/*/*.cpp)
163- # 2) Add the source files of the libraries. Old Arduino libraries place the
164- # source files at the top level. Later Arduino libraries put the source files
165- # under the src/ directory. Also support 3 levels of subdirectories.
166- MODULE_EXPANSION = $(wildcard $(module)/*.cpp) \
174+ # 2) Collect the source files of the libraries referenced by the EPOXY_MODULES
175+ # parameter. Old Arduino libraries place the source files at the top level.
176+ # Later Arduino libraries put the source files under the src/ directory. Also
177+ # support 3 levels of subdirectories. Support both C and C++ libraries files.
178+ MODULE_EXPANSION_CPP = $(wildcard $(module)/*.cpp) \
167179 $(wildcard $(module)/src/*.cpp) \
168180 $(wildcard $(module)/src/*/*.cpp) \
169181 $(wildcard $(module)/src/*/*/*.cpp) \
170182 $(wildcard $(module)/src/*/*/*/*.cpp)
171- EPOXY_SRCS += $(foreach module,$(EPOXY_MODULES),$(MODULE_EXPANSION))
172- # 3) Add the source files in the application directory, also 3 levels down.
173- EPOXY_SRCS += $(wildcard *.cpp) $(wildcard */*.cpp) $(wildcard */*/*.cpp) \
183+ MODULE_EXPANSION_C = $(wildcard $(module)/*.c) \
184+ $(wildcard $(module)/src/*.c) \
185+ $(wildcard $(module)/src/*/*.c) \
186+ $(wildcard $(module)/src/*/*/*.c) \
187+ $(wildcard $(module)/src/*/*/*/*.c)
188+ MODULE_SRCS_CPP += $(foreach module,$(EPOXY_MODULES),$(MODULE_EXPANSION_CPP))
189+ MODULE_SRCS_C += $(foreach module,$(EPOXY_MODULES),$(MODULE_EXPANSION_C))
190+ # 3) Collect the source files in the application directory, also 3 levels down.
191+ APP_SRCS_CPP += $(wildcard *.cpp) \
192+ $(wildcard */*.cpp) \
193+ $(wildcard */*/*.cpp) \
174194 $(wildcard */*/*/*.cpp)
175-
176- # Objects including *.o from *.ino
177- OBJS += $(EPOXY_SRCS:%.cpp=%.o) $(APP_NAME).o
178-
179- # Finally, the rule to generate the binary for the application.
195+ APP_SRCS_C += $(wildcard *.c) \
196+ $(wildcard */*.c) \
197+ $(wildcard */*/*.c) \
198+ $(wildcard */*/*/*.c)
199+
200+ # Generate the list of object files from the *.cpp, *.c, and *.ino files.
201+ OBJS += $(EPOXY_SRCS:%.cpp=%.o)
202+ OBJS += $(MODULE_SRCS_CPP:%.cpp=%.o)
203+ OBJS += $(MODULE_SRCS_C:%.c=%.o)
204+ OBJS += $(APP_SRCS_CPP:%.cpp=%.o)
205+ OBJS += $(APP_SRCS_C:%.c=%.o)
206+ OBJS +=$(APP_NAME).o
207+
208+ # Finally the rule to generate the *.out binary file for the application.
180209$(APP_NAME).out: $(OBJS)
181210 $(CXX) $(CPPFLAGS) $(CXXFLAGS) -o $@ $^ $(LDFLAGS)
182211
183212# We need to add a rule to treat .ino file as just a normal .cpp.
184213$(APP_NAME).o: $(APP_NAME).ino $(DEPS)
185214 $(CXX) $(CPPFLAGS) $(CXXFLAGS) -x c++ -c $<
186215
187- %.o: %.cpp
188- $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $< -o $@
189-
190- # This simple rule does not capture all header dependencies of a given cpp
191- # file. Maybe it's better to make each cpp to depend on all headers of a given
192- # module, and force a recompilation of all cpp files. As far as I understand,
193- # this is what the Arduino IDE does upon each compile iteration.
216+ # We don't need this rule because the implicit GNU Make rules for converting
217+ # *.c and *.cpp into *.o files are sufficient.
218+ #
219+ # %.o: %.cpp
220+ # $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $< -o $@
221+
222+ # The following simple rules do not capture all header dependencies of a given
223+ # *.cpp or *.c file. It's probably better to make each *.cpp and *.c to depend
224+ # on all headers of a given module, and force a recompilation of all the files.
225+ # As far as I understand, this is what the Arduino IDE does upon each compile
226+ # iteration. But this is good enough for now, but has the disadvantage that we
227+ # have to run `make clean` more often than necessary to reset all the
228+ # dependencies.
194229%.cpp: %.h
230+ %.c: %.h
195231
196- .PHONY: all clean $(MORE_CLEAN)
232+ .PHONY: all clean run $(MORE_CLEAN)
197233
234+ # Use 'make all' or just 'make' to compile the binary.
198235all: $(APP_NAME).out
199236
237+ # Use 'make run' to run the binary created by 'make all'. This is useful when
238+ # the (APP_NAME).out is an AUnit unit test. You can execute ': make run' inside
239+ # the vim editor. The error message of AUnit (as of v1.7) is compatible with
240+ # the quickfix errorformat of vim, so vim will automatically detect assertion
241+ # errors and jump directly to the line where the assertion failed.
242+ run:
243+ ./$(APP_NAME).out
244+
245+ # Use 'make clean' to remove intermediate '*.o' files, the target '*.out' file,
246+ # and any generated files defined by $(GENERATED).
200247clean: $(MORE_CLEAN)
201248 rm -f $(OBJS) $(APP_NAME).out $(GENERATED)
0 commit comments