• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1B:=src
2SRCS:=$(sort $(wildcard src/*/*.c))
3OBJS:=$(SRCS:src/%.c=$(B)/%.o)
4LOBJS:=$(SRCS:src/%.c=$(B)/%.lo)
5DIRS:=$(patsubst src/%/,%,$(sort $(dir $(SRCS))))
6BDIRS:=$(DIRS:%=$(B)/%)
7NAMES:=$(SRCS:src/%.c=%)
8CFLAGS:=-I$(B)/common -Isrc/common #YSROOT_PATH)
9LDLIBS:=$(B)/common/libtest.a
10AR = $(CROSS_COMPILE)ar
11RANLIB = $(CROSS_COMPILE)ranlib
12RUN_TEST = $(RUN_WRAP) $(B)/common/runtest.exe -w '$(RUN_WRAP)'
13
14all:
15%.mk:
16# turn off evil implicit rules
17.SUFFIXES:
18%: %.o
19%: %.c
20%: %.cc
21%: %.C
22%: %.cpp
23%: %.p
24%: %.f
25%: %.F
26%: %.r
27%: %.s
28%: %.S
29%: %.mod
30%: %.sh
31%: %,v
32%: RCS/%,v
33%: RCS/%
34%: s.%
35%: SCCS/s.%
36
37config.mak:
38	cp config.mak.def $@
39-include config.mak
40
41define default_template
42$(1).BINS_TEMPL:=bin.exe bin-static.exe
43$(1).NAMES:=$$(filter $(1)/%,$$(NAMES))
44$(1).OBJS:=$$($(1).NAMES:%=$(B)/%.o)
45endef
46$(foreach d,$(DIRS),$(eval $(call default_template,$(d))))
47common.BINS_TEMPL:=
48api.BINS_TEMPL:=
49math.BINS_TEMPL:=bin.exe
50
51define template
52D:=$$(patsubst %/,%,$$(dir $(1)))
53N:=$(1)
54$(1).BINS := $$($$(D).BINS_TEMPL:bin%=$(B)/$(1)%)
55-include src/$(1).mk
56#$$(warning D $$(D) N $$(N) B $$($(1).BINS))
57$(B)/$(1).exe $(B)/$(1)-static.exe: $$($(1).OBJS)
58$(B)/$(1).so: $$($(1).LOBJS)
59# make sure dynamic and static binaries are not run parallel (matters for some tests eg ipc)
60$(B)/$(1)-static.err: $(B)/$(1).err
61endef
62$(foreach n,$(NAMES),$(eval $(call template,$(n))))
63
64BINS:=$(foreach n,$(NAMES),$($(n).BINS)) $(B)/api/main.exe
65LIBS:=$(foreach n,$(NAMES),$($(n).LIBS)) $(B)/common/runtest.exe
66ERRS:=$(BINS:%.exe=%.err)
67
68debug:
69	@echo NAMES $(NAMES)
70	@echo BINS $(BINS)
71	@echo LIBS $(LIBS)
72	@echo ERRS $(ERRS)
73	@echo DIRS $(DIRS)
74
75define target_template
76$(1).ERRS:=$$(filter $(B)/$(1)/%,$$(ERRS))
77$(B)/$(1)/all: $(B)/$(1)/REPORT
78$(B)/$(1)/run: $(B)/$(1)/cleanerr $(B)/$(1)/REPORT
79$(B)/$(1)/cleanerr:
80	rm -f $$(filter-out $(B)/$(1)/%-static.err,$$($(1).ERRS))
81$(B)/$(1)/clean:
82	rm -f $$(filter $(B)/$(1)/%,$$(OBJS) $$(LOBJS) $$(BINS) $$(LIBS)) $(B)/$(1)/*.err
83$(B)/$(1)/REPORT: $$($(1).ERRS)
84	cat $(B)/$(1)/*.err >$$@
85run: $(B)/$(1)/run
86$(B)/REPORT: $(B)/$(1)/REPORT
87.PHONY: $(B)/$(1)/all $(B)/$(1)/clean
88endef
89$(foreach d,$(DIRS),$(eval $(call target_template,$(d))))
90
91$(B)/common/libtest.a: $(common.OBJS)
92	rm -f $@
93	$(AR) rc $@ $^
94	$(RANLIB) $@
95
96$(B)/common/all: $(B)/common/runtest.exe
97
98$(ERRS): $(B)/common/runtest.exe | $(BDIRS)
99$(BINS) $(LIBS): $(B)/common/libtest.a
100$(OBJS): src/common/test.h | $(BDIRS)
101$(BDIRS):
102	mkdir -p $@
103
104$(B)/common/options.h: src/common/options.h.in
105	$(CC) -E - <$< | awk ' \
106		/optiongroups_unistd_end/ {s=1; next} \
107		!s || !NF || /^#/ {next} \
108		!a {a=$$1; if(NF==1)next} \
109		{print "#define "a" "$$NF; a=""}' >$@.tmp
110	mv $@.tmp $@
111
112$(B)/common/mtest.o: src/common/mtest.h
113$(math.OBJS): src/common/mtest.h
114
115$(B)/api/main.exe: $(api.OBJS)
116api/main.OBJS:=$(api.OBJS)
117$(api.OBJS):$(B)/common/options.h
118$(api.OBJS):CFLAGS+=-pedantic-errors -Werror -Wno-unused -D_XOPEN_SOURCE=700
119
120all run: $(B)/REPORT
121	grep FAIL $< || echo PASS
122clean:
123	rm -f $(OBJS) $(BINS) $(LIBS) $(B)/common/libtest.a $(B)/common/runtest.exe $(B)/common/options.h $(B)/*/*.err
124cleanall: clean
125	rm -f $(B)/REPORT $(B)/*/REPORT
126$(B)/REPORT:
127	cat $^ >$@
128
129$(B)/%.o:: src/%.c
130	$(CC) $(CFLAGS) $($*.CFLAGS) -c -o $@ $< 2>$@.err || echo BUILDERROR $@; cat $@.err
131$(B)/%.s:: src/%.c
132	$(CC) $(CFLAGS) $($*.CFLAGS) -S -o $@ $< || echo BUILDERROR $@; cat $@.err
133$(B)/%.lo:: src/%.c
134	$(CC) $(CFLAGS) $($*.CFLAGS) -fPIC -DSHARED -c -o $@ $< 2>$@.err || echo BUILDERROR $@; cat $@.err
135$(B)/%.so: $(B)/%.lo
136	$(CC) -shared $(LDFLAGS) $($*.so.LDFLAGS) -o $@ $(sort $< $($*.so.LOBJS)) $(LDLIBS) $($*.so.LDLIBS) 2>$@.err || echo BUILDERROR $@; cat $@.err
137$(B)/%-static.exe: $(B)/%.o
138	$(CC) -static $(LDFLAGS) $($*-static.LDFLAGS) -o $@ $(sort $< $($*-static.OBJS)) $(LDLIBS) $($*-static.LDLIBS) 2>$@.ld.err || echo BUILDERROR $@; cat $@.ld.err
139$(B)/%.exe: $(B)/%.o
140	$(CC) $(LDFLAGS) $($*.LDFLAGS) -o $@ $(sort $< $($*.OBJS)) $(LDLIBS) $($*.LDLIBS) 2>$@.ld.err || echo BUILDERROR $@; cat $@.ld.err
141
142%.o.err: %.o
143	touch $@
144%.lo.err: %.lo
145	touch $@
146%.so.err: %.so
147	touch $@
148%.ld.err: %.exe
149	touch $@
150%.err: %.exe
151	$(RUN_TEST) $< >$@ || true
152
153.PHONY: all run clean cleanall
154
155