B:=src
SRCS:=$(sort $(wildcard src/*/*.c))
OBJS:=$(SRCS:src/%.c=$(B)/%.o)
DIRS:=$(patsubst src/%/,%,$(sort $(dir $(SRCS))))
BDIRS:=$(DIRS:%=$(B)/%)
NAMES:=$(SRCS:src/%.c=%)
CFLAGS:=-Isrc/common
LDLIBS:=$(B)/common/libtest.a
AR = $(CROSS_COMPILE)ar
RANLIB = $(CROSS_COMPILE)ranlib
all:
%.mk:
config.mak:
cp config.mak.def $@
-include config.mak
define default_template
$(1).BINS_TEMPL:=bin bin-static
$(1).NAMES:=$$(filter $(1)/%,$$(NAMES))
$(1).OBJS:=$$($(1).NAMES:%=$(B)/%.o)
endef
$(foreach d,$(DIRS),$(eval $(call default_template,$(d))))
common.BINS_TEMPL:=
api.BINS_TEMPL:=
math.BINS_TEMPL:=bin
define template
D:=$$(patsubst %/,%,$$(dir $(1)))
N:=$(1)
$(1).BINS := $$($$(D).BINS_TEMPL:bin%=$(B)/$(1)%)
-include src/$(1).mk
#$$(warning D $$(D) N $$(N) B $$($(1).BINS))
$(B)/$(1) $(B)/$(1)-static: $$($(1).OBJS)
$(B)/$(1).so: $$($(1).LOBJS)
# make sure dynamic and static binaries are not run parallel (matters for some tests eg ipc)
$(B)/$(1)-static.err: $(B)/$(1).err
endef
$(foreach n,$(NAMES),$(eval $(call template,$(n))))
BINS:=$(foreach n,$(NAMES),$($(n).BINS)) $(B)/api/main
LIBS:=$(foreach n,$(NAMES),$($(n).LIBS)) $(B)/common/runtest
ERRS:=$(BINS:%=%.err)
debug:
@echo NAMES $(NAMES)
@echo BINS $(BINS)
@echo LIBS $(LIBS)
@echo ERRS $(ERRS)
@echo DIRS $(DIRS)
define target_template
$(1).ERRS:=$$(filter $(B)/$(1)/%,$$(ERRS))
$(B)/$(1)/all: $(B)/$(1)/REPORT
$(B)/$(1)/run: $(B)/$(1)/cleanerr $(B)/$(1)/REPORT
$(B)/$(1)/cleanerr:
rm -f $$(filter-out $(B)/$(1)/%-static.err,$$($(1).ERRS))
$(B)/$(1)/clean:
rm -f $$(filter $(B)/$(1)/%,$$(OBJS) $$(BINS) $$(LIBS)) $(B)/$(1)/*.err
$(B)/$(1)/REPORT: $$($(1).ERRS)
cat $(B)/$(1)/*.err >$$@
run: $(B)/$(1)/run
$(B)/REPORT: $(B)/$(1)/REPORT
.PHONY: $(B)/$(1)/all $(B)/$(1)/clean
endef
$(foreach d,$(DIRS),$(eval $(call target_template,$(d))))
$(B)/common/libtest.a: $(common.OBJS)
rm -f $@
$(AR) rc $@ $^
$(RANLIB) $@
$(ERRS): $(B)/common/runtest | $(BDIRS)
$(BINS) $(LIBS): $(B)/common/libtest.a
$(OBJS): src/common/test.h | $(BDIRS)
$(BDIRS):
mkdir -p $@
$(B)/common/mtest.o: src/common/mtest.h
$(math.OBJS): src/common/mtest.h
$(B)/api/main: $(api.OBJS)
api/main.OBJS:=$(api.OBJS)
$(api.OBJS):CFLAGS+=-pedantic-errors -Werror -Wno-unused -D_XOPEN_SOURCE=700
$(api.OBJS):CFLAGS+=-DX_PS -DX_TPS -DX_SS
all:$(B)/REPORT
run:$(B)/REPORT
clean:
rm -f $(OBJS) $(BINS) $(LIBS) $(B)/common/libtest.a $(B)/common/runtest $(B)/*/*.err
cleanall: clean
rm -f $(B)/REPORT $(B)/*/REPORT
$(B)/REPORT:
cat $^ |tee $@
$(B)/%.o: src/%.c
$(CC) $(CFLAGS) $($*.CFLAGS) -c -o $@ $< 2>$@.err || echo BUILDERROR $@
$(B)/%.s: src/%.c
$(CC) $(CFLAGS) $($*.CFLAGS) -S -o $@ $< || echo BUILDERROR $@
$(B)/%.lo: src/%.c
$(CC) $(CFLAGS) $($*.CFLAGS) -fPIC -DSHARED -c -o $@ $< 2>$@.err || echo BUILDERROR $@
$(B)/%.so: $(B)/%.lo
$(CC) -shared $(LDFLAGS) $($*.so.LDFLAGS) -o $@ $< $($*.so.LOBJS) $(LDLIBS) $($*.so.LDLIBS) 2>$@.err || echo BUILDERROR $@
$(B)/%-static: $(B)/%.o
$(CC) -static $(LDFLAGS) $($*-static.LDFLAGS) -o $@ $< $($*-static.OBJS) $(LDLIBS) $($*-static.LDLIBS) 2>$@.ld.err || echo BUILDERROR $@
$(B)/%: $(B)/%.o
$(CC) $(LDFLAGS) $($*.LDFLAGS) -o $@ $< $($*.OBJS) $(LDLIBS) $($*.LDLIBS) 2>$@.ld.err || echo BUILDERROR $@
%.o.err: %.o
touch $@
%.lo.err: %.lo
touch $@
%.so.err: %.so
touch $@
%.ld.err: %
touch $@
%.err: %
$(B)/common/runtest ./$< >$@ || true
.PHONY: all run clean cleanall