69,369
社区成员
发帖
与我相关
我的任务
分享
ifdef ComSpec
EMPTY :=
RM := del
DELIMITER := \$(EMPTY)
TARGET := libzooclient.dll
else
RM := rm
DELIMITER := /
TARGET := libzooclient.so
endif
all: $(TARGET)
src_fmt := *.c *.C *.cc *.CC *.cpp *.cxx
src_dir := src
inc_dir := include
obj_dir := obj
CFLAGS := -fPIC -Wall -g -DTHREADED -I "/usr/local/include" \
$(foreach d,$(inc_dir), -I "$d")
CPPFLAGS := -fPIC -Wall -g -DTHREADED -I "/usr/local/include" \
$(foreach d,$(inc_dir), -I "$d")
LDFLAGS := -L "/usr/local/lib"
LOADLIBES := --shared -lzookeeper_mt -lm -lpthread
# List all sources now.
src :=
$(foreach d,$(src_dir),\
$(foreach f,$(src_fmt),\
$(if $(wildcard $d/$f),$(eval src+=$(wildcard $d/$f)))))
# Check whether sources contains cxx files.
cplusplus := $(filter-out %.c,$(src))
# Check whether builds archive.
archive := $(filter %.a,$(TARGET))
include make.rules
ifneq ($(MAKECMDGOALS),"clean")
ifneq ($(MAKECMDGOALS),"distclean")
# Include dependent files if exist.
-include $(addprefix $(obj_dir)/,$(addsuffix .d, $(notdir $(src))))
# Generate compile rules from source to object file.
# To "C" files, use gcc, otherwise picking g++.
$(foreach f,$(src),\
$(if $(filter %.c,$f),\
$(eval $(call rule-generator-c2o,$(obj_dir),$f)),\
$(eval $(call rule-generator-cxx2o,$(obj_dir),$f))))
# Generate compile rules from object files to executable.
#
# if source-file(s)-exists
# if target-is-xxx.a
# ar rv $@ $^
# else if use-c++
# g++ $^ -o $@
# else
# gcc $^ -o $@
# endif
# endif
$(if $(src),\
$(if $(archive),$(eval $(call archive-target,$(TARGET))),\
$(if $(cplusplus),\
$(eval $(call link-target-cxx,$(TARGET))),\
$(eval $(call link-target-c,$(TARGET))))))
endif
endif
prompt_clean := @echo "Nothing to clean."
prompt_dclean:= @echo "Nothing to distclean."
objects :=
$(foreach d,$(obj_dir),\
$(if $(wildcard $d/*.o), $(eval objects+=$(wildcard $d/*.o))))
depends :=
$(foreach d,$(obj_dir),\
$(if $(wildcard $d/*.d), $(eval depends+=$(wildcard $d/*.d))))
clean_cmd := $(if $(objects)$(depends),\
$(RM) $(objects) $(depends),\
$(prompt_clean))
target :=
$(if $(wildcard $(TARGET)), $(eval target=$(TARGET)))
distclean_cmd := $(if $(objects)$(depends)$(target),\
$(RM) $(objects) $(depends) $(target),\
$(prompt_dclean))
clean:
$(clean_cmd)
distclean:
$(distclean_cmd)
.PHONY: all clean distclean
<make.rules>
# Compiler specific variables.
CC := gcc
CXX := g++
COMPILE.c = $(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c
COMPILE.cxx = $(CXX) $(CXXFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c
LINK.c.o = $(CC) $(LDFLAGS) $(TARGET_ARCH)
LINK.cxx.o = $(CXX) $(LDFLAGS) $(TARGET_ARCH)
AR := ar
ARFLAGS = rv
# Store all object files while generating rules.
allobjects :=
# ******************************** #
# ############### C ###############
# $(call depend-generator-c,<target:$1>,<dependfilepath:$2>,<sourcefilepath:$3>
# Example: $(call depend-generator,obj/main.c.o,obj/main.c.d,src/main.c)
define depend-generator-c
$(CC) $(CFLAGS) -MM -MT $1 -MF $2 -MP $3
endef
# Automatic rules from *.c to *.o
# $(call rule-generator-c2o,<objdir:$1>,<sourcefilepath:$2>)
define rule-generator-c2o
allobjects += $1/$(notdir $2).o
$1/$(notdir $2).o: $2
$$(call depend-generator-c,$1/$(notdir $2).o,$1/$(notdir $2).d,$2)
$(COMPILE.c) $$< -o $$@
endef
# Automatic rules from *.o to executables
# $(call link-target-c,<target:$1>)
define link-target-c
$1: $(allobjects)
$(LINK.c.o) $$^ $(LDLIBS) $(LOADLIBES) -o $$@
endef
# ******************************** #
# ############## C++ ##############
# $(call depend-generator-cxx,<target:$1>,<dependfilename:$2>,<sourcefilename:$3>
# Example: $(call depend-generator,obj/main.cxx.o,obj/main.cxx.d,src/main.cxx)
define depend-generator-cxx
$(CXX) $(CPPFLAGS) -MM -MT $1 -MF $2 -MP $3
endef
# Automatic rules from *.cxx to *.o
# $(call rule-generator-cxx2o,<objdir:$1>,<sourcefile:$2>)
define rule-generator-cxx2o
allobjects += $1/$(notdir $2).o
$1/$(notdir $2).o: $2
$$(call depend-generator-cxx,$1/$(notdir $2).o,$1/$(notdir $2).d,$2)
$(COMPILE.cxx) $$< -o $$@
endef
# Automatic rules from *.o to executables
# $(call link-target-cxx,<target:$1>)
define link-target-cxx
$1: $(allobjects)
$(LINK.cxx.o) $$^ $(LDLIBS) $(LOADLIBES) -o $$@
endef
# *********************************** #
# ############## Archive ##############
# Automatic rule from *.o to archive
define archive-target
$1: $(allobjects)
$(AR) $(ARFLAGS) $$@ $$^
endef
obj = main.c my/my.c include/my.h
main: $(obj)
gcc -o main $(obj)