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
|
.PHONY: all clean all:
DEBUG := y TARGET_PROG := main
DEBUG_DIR := ./Debug RELEASE_DIR := ./Release BIN_DIR := $(if $(DEBUG), $(DEBUG_DIR), $(RELEASE_DIR))
CC := gcc CXX := g++ RM := rm -rf MKDIR := mkdir -p SED := sed MV := mv
sources_all := $(shell find . -name "*.c" -o -name "*.cpp" -o -name "*.h") sources_c := $(filter %.c, $(sources_all)) sources_cpp := $(filter %.cpp, $(sources_all)) sources_h := $(filter %.h, $(sources_all)) objs := $(addprefix $(BIN_DIR)/,$(strip $(sources_cpp:.cpp=.o) $(sources_c:.c=.o))) deps := $(addprefix $(BIN_DIR)/,$(strip $(sources_cpp:.cpp=.d) $(sources_c:.c=.d)))
$(foreach dirname,$(sort $(dir $(sources_c) $(sources_cpp))),\ $(shell $(MKDIR) $(BIN_DIR)/$(dirname)))
CFLAGS := $(if $(DEBUG),-g -O, -O2) CFLAGS += $(addprefix -I ,$(sort $(dir $(sources_h)))) CXXFLAGS = $(CFLAGS) LDFLAGS := LOADLIBES += LDLIBS +=
vpath %.h $(sort $(dir $(sources_h))) vpath %.c $(sort $(dir $(sources_c))) vpath %.cpp $(sort $(dir $(sources_cpp)))
ifneq "$(MAKECMDGOALS)" "clean" sinclude $(deps) endif
define make-depend $(RM) $1; \ $4 $(CFLAGS) -MM $2 | \ $(SED) 's,\($(notdir $3)\): ,$3: ,' > $1.tmp; \ $(SED) -e 's/ -e 's/^[^:]*: *//' \ -e 's/ *\\$$//' \ -e '/^$$/ d' \ -e 's/$$/ :/' < $1.tmp >> $1.tmp; \ $(MV) $1.tmp $1; endef
$(BIN_DIR)/%.o: %.c @$(call make-depend,$(patsubst %.o,%.d,$@),$<,$@,$(CC)) $(CC) $(CFLAGS) -o $@ -c $<
$(BIN_DIR)/%.o: %.cpp @$(call make-depend,$(patsubst %.o,%.d,$@),$<,$@,$(CXX)) $(CXX) $(CXXFLAGS) -o $@ -c $<
define add-target REAL_TARGET += $(BIN_DIR)/$1 $(BIN_DIR)/$1: $2 $3 $(LDFLAGS) $$^ $(LOADLIBES) $(LDLIBS) -o $$@ endef
$(foreach targ,$(TARGET_PROG),$(eval $(call add-target,$(targ),$(objs),$(CXX))))
all: $(REAL_TARGET) $(TARGET_LIBS)
clean: $(RM) $(BIN_DIR)
|