# # Top-level makefile for txcache # CC = gcc LD = gcc EXPAND = lib/tmpl/expand CFLAGS := -g -Wall -O2 override CFLAGS += -std=gnu99 LDFLAGS := # Debian package: check CHECK_CFLAGS := $(shell pkg-config --cflags check) CHECK_LDFLAGS := $(shell pkg-config --libs check) # Debian package: libpq-dev PG_CFLAGS := -I$(shell pg_config --includedir) PG_LDFLAGS := -L$(shell pg_config --libdir) -lpq # Debian package: php5-dev PHP_CFLAGS := $(shell php-config --includes) PHP_LDFLAGS := -shared # Additional flags PARANOID = 1 ifneq ($(PARANOID),0) override CFLAGS += -DPARANOID=1 $(info WARNING: Paranoid mode enabled) endif PERFTOOLS = 0 ifneq ($(PERFTOOLS),0) override CFLAGS += -DPPROF=1 override LDFLAGS += -lprofiler endif # Make sure all is the default .DEFAULT_GOAL := all # Eliminate default suffix rules .SUFFIXES: # Delete target files if there is an error (or make is interrupted) .DELETE_ON_ERROR: # make it so that no intermediate .o files are ever deleted .PRECIOUS: %.o ################################################################## # Tracing # ifeq ($(V),1) trace = $(3) Q = else trace = @printf "+ %-6s " $(1) ; echo $(2) ; $(3) Q = @ endif ################################################################## # Sub-directories # # The directory of the current make fragment. Each file should # redefine this at the very top with # d := $(dir $(lastword $(MAKEFILE_LIST))) d := # The object directory corresponding to the $(d) o = .obj/$(d) # SRCS is the list of all non-test-related source files. SRCS := # TEST_SRCS is just like SRCS, but these source files will be compiled # with testing related flags. TEST_SRCS := # BINS is a list of target names for non-test binaries. These targets # should depend on the appropriate object files, but should not # contain any commands. BINS := # TEST_BINS is like BINS, but for test binaries. They will be linked # using the appropriate flags. This is also used as the list of tests # to run for the `test' target. TEST_BINS := # add-CFLAGS is a utility macro that takes a space-separated list of # sources and a set of CFLAGS. It sets the CFLAGS for each provided # source. This should be used like # # $(call add-CFLAGS,$(d)a.c $(d)b.c,$(PG_CFLAGS)) define add-CFLAGS $(foreach src,$(1),$(eval CFLAGS-$(src) += $(2))) endef # Like add-CFLAGS, but for LDFLAGS. This should be given a list of # binaries. define add-LDFLAGS $(foreach bin,$(1),$(eval LDFLAGS-$(bin) += $(2))) endef include lib/Rules.mk include server/Rules.mk include pincushion/Rules.mk include client/Rules.mk include phpclient/Rules.mk include support/Rules.mk include invald/Rules.mk ################################################################## # General rules # # # Compilation # # -MD Enable dependency generation and compilation and output to the # .obj directory. -MP Add phony targets so make doesn't complain if # a header file is removed. -MT Explicitly set the target in the # generated rule to the object file we're generating. DEPFLAGS = -Wp,-MD,${@:.o=.d},-MP,-MT,$@ $(call add-CFLAGS,$(TEST_SRCS),$(CHECK_CFLAGS)) OBJS := $(SRCS:%.c=.obj/%.o) $(TEST_SRCS:%.c=.obj/%.o) define compile @mkdir -p $(dir $@) $(call trace,$(1),$<,\ $(CC) -iquote. $(CFLAGS) $(CFLAGS-$<) $(2) $(DEPFLAGS) -E -o .obj/$*.t $<) $(Q)$(EXPAND) $(EXPANDARGS) -o .obj/$*.i .obj/$*.t $(Q)$(CC) $(CFLAGS) $(CFLAGS-$<) $(2) -c -o $@ .obj/$*.i endef # All object files come in two flavors: regular and # position-independent. PIC objects end in -pic.o instead of just .o. # Link targets that build shared objects must depend on the -pic.o # versions. $(OBJS): .obj/%.o: %.c $(call compile,CC,) $(OBJS:%.o=%-pic.o): .obj/%-pic.o: %.c $(call compile,CCPIC,-fPIC) # # Linking # $(call add-LDFLAGS,$(TEST_BINS),$(CHECK_LDFLAGS)) $(BINS) $(TEST_BINS): %: $(call trace,LD,$@,$(LD) -o $@ $^ $(LDFLAGS) $(LDFLAGS-$@)) # # Automatic dependencies # DEPS := $(OBJS:.o=.d) $(OBJS:.o=-pic.d) -include $(DEPS) # # Cleaning # .PHONY: clean clean: $(call trace,RM,binaries,rm -f $(BINS) $(TEST_BINS)) $(call trace,RM,objects,rm -rf .obj) ################################################################## # Targets # .PHONY: all all: $(BINS) $(TEST_BINS:%=run-%): run-%: % $(call trace,RUN,$<,$<) $(TEST_BINS:%=gdb-%): gdb-%: % $(call trace,GDB,$<,CK_FORK=no gdb $<) .PHONY: test test: $(TEST_BINS:%=run-%) .PHONY: check check: test .PHONY: TAGS TAGS: $(Q)rm -f $@ $(call trace,ETAGS,sources,\ etags $(SRCS) $(TEST_SRCS)) $(call trace,ETAGS,headers,\ etags -a $(foreach dir,$(sort $(dir $(SRCS) $(TEST_SRCS))),\ $(wildcard $(dir)*.h)))