| # Standard variables |
| CC = gcc |
| CXX = g++ |
| |
| # Directories: SRC_DIR - root of all sources, BUILD_DIR - temporary build files, OUTPUT_DIR - final binaries dir |
| SRC_DIR = ../.. |
| BUILD_DIR = build |
| OUTPUT_DIR = dist |
| # Output file |
| EXECUTABLE = $(OUTPUT_DIR)/unit_tests |
| |
| # Includes |
| INCLUDE_DIRS = -I $(SRC_DIR)/src -I$(SRC_DIR)/ext/cvshared/cpp/include -I$(SRC_DIR)/ext/boost |
| |
| # Additional library search paths |
| LIB_DIRS = |
| |
| # Additional libraries |
| LDLIBS = -lcurl -lcrypto -lpthread |
| |
| # C and C++ flags |
| CXXFLAGS = -g -MMD -MP $(INCLUDE_DIRS) |
| CFLAGS = $(CXXFLAGS) |
| |
| # Linker flags |
| LDFLAGS = $(LIB_DIRS) |
| |
| # Utility functions, used later in build |
| # rfind |
| define rfind |
| $(shell find $(1) -name '$(2)') |
| endef |
| # add_src_dir |
| define add_src_dir |
| $(sort $(call rfind,$(SRC_DIR)/$(strip $(1)),*.c) $(call rfind,$(SRC_DIR)/$(strip $(1)),*.cpp)) |
| endef |
| # filter_src |
| define filter_src |
| $(call $(1),$(3),$(call add_src_dir,$(2))) |
| endef |
| # add_src_dir_excluding |
| define add_src_dir_excluding |
| $(call filter_src,filter-out,$(1),$(2)) |
| endef |
| # add_src_dir_including |
| define add_src_dir_including |
| $(call filter_src,filter,$(1),$(2)) |
| endef |
| # c_to_obj |
| define c_to_obj |
| $(patsubst $(SRC_DIR)/%.c, $(BUILD_DIR)/%.o, $(1)) |
| endef |
| # cpp_to_obj |
| define cpp_to_obj |
| $(patsubst $(SRC_DIR)/%.cpp, $(BUILD_DIR)/%.o, $(1)) |
| endef |
| # generate_c_rule |
| define generate_c_rule |
| $(2): $(1) |
| $$(shell mkdir -p $(dir $(2))) |
| $$(CC) $$(CPPFLAGS) $$(CFLAGS) -c $$< -o $$@ |
| endef |
| # generate_cpp_rule |
| define generate_cpp_rule |
| $(2): $(1) |
| $$(shell mkdir -p $(dir $(2))) |
| $$(CXX) $$(CPPFLAGS) $$(CXXFLAGS) -c $$< -o $$@ |
| endef |
| |
| # Source files - modify this part if you want to add new source files (*.c *.cpp). |
| # To recursively add all files in a directory, use: |
| # $(call add_src_dir, <a directory>) |
| # To recursively add all files in a directory, except some files, use: |
| # $(call add_src_dir_excluding, <a directory>, <exclude pattern>) |
| # To recursively add only some files in a directory, use: |
| # $(call add_src_dir_including, <a directory>, <include pattern>) |
| # All directories are specified relatively to $(SRC_DIR). |
| # The patterns must contain the % character to match a portion of the full file pathname. |
| SRC = $(call add_src_dir, src) |
| SRC += $(call add_src_dir_including, ext/cvshared/cpp, \ |
| %linux/CvHttpRequest.cpp %linux/CvThread.cpp %linux/CvLogger.cpp %linux/CvMutex.cpp %CvString.cpp %CvTime.cpp %CvXcode.cpp) |
| SRC += $(call add_src_dir_including, tests, \ |
| %auto_context.cpp %access_number_thread.cpp %memory_storage.cpp %http_request.cpp %unit_tests.cpp) |
| |
| # Generate a list of object files |
| OBJ = $(call cpp_to_obj, $(call c_to_obj, $(SRC))) |
| |
| # Separate .c and .cpp files |
| C_SRC = $(filter %.c, $(SRC)) |
| CPP_SRC = $(filter %.cpp, $(SRC)) |
| |
| # The default target |
| all: $(EXECUTABLE) |
| |
| # Rule for building the executable - depends on all object files |
| $(EXECUTABLE): $(OBJ) |
| $(shell mkdir -p $(dir $(EXECUTABLE))) |
| $(CXX) -o $@ $(OBJ) $(LDFLAGS) $(LDLIBS) |
| |
| # Generate rules for each object file that depends on the corresponding .c file |
| $(foreach cfile, $(C_SRC), $(eval $(call generate_c_rule, $(cfile), $(call c_to_obj, $(cfile))))) |
| |
| # Generate rules for each object file that depends on the corresponding .cpp file |
| $(foreach cppfile, $(CPP_SRC), $(eval $(call generate_cpp_rule, $(cppfile), $(call cpp_to_obj, $(cppfile))))) |
| |
| # Clean target |
| clean: |
| rm -f -R build/** $(EXECUTABLE) |
| |
| # Include all the .d files (generated by the -MMD -MP option) corresponding to each of the object files |
| # This adds to each object target a dependency on all the header files, included in the corresponding c/cpp file |
| -include $(OBJ:%.o=%.d) |