1# Copyright (c) 2002, Intel Corporation. All rights reserved.
2# Created by:  inaky.perez-gonzalez REMOVE-THIS AT intel DOT com
3# This file is licensed under the GPL license.  For the full content
4# of this license, see the COPYING file at the top level of this
5# source tree.
6#
7# Kind of a little bit bastardized automakefile ... This is the
8# temporary glue to hold it all together; once our needs change or we
9# need something more advanced, we'll implement it.
10#
11# So far, I understand Make is not the best language, but I felt lazy
12# today and wanted to use the default rules of automake [did I alredy
13# mentioned I am bastardizing it?].
14#
15# Ok, I don't use Automake any more
16#
17# Added patch from dank REMOVE-THIS AT kegel DOT com
18#
19
20# Added tests timeout from Sebastien Decugis (http://nptl.bullopensource.org)
21# Expiration delay is 240 seconds
22TIMEOUT_VAL = 240
23# The following value is the shell return value of a timedout application.
24# with the bash shell, the ret val of a killed application is 128 + signum
25# and under Linux, SIGALRM=14, so we have (Linux+bash) 142.
26TIMEOUT_RET = $(shell cat $(top_builddir)/t0.val)
27
28top_builddir = .
29
30LOGFILE = $(top_builddir)/logfile
31
32LDFLAGS := $(shell cat LDFLAGS | grep -v \^\#)
33
34RUN_TESTS := $(shell $(top_builddir)/locate-test \
35             --execs $(top_builddir)/$(POSIX_TARGET))
36BUILD_TESTS := $(shell $(top_builddir)/locate-test \
37               --buildable $(top_builddir)/$(POSIX_TARGET))
38FUNCTIONAL_MAKE := $(shell $(top_builddir)/locate-test --fmake)
39FUNCTIONAL_RUN := $(shell $(top_builddir)/locate-test --frun)
40STRESS_MAKE := $(shell $(top_builddir)/locate-test --smake)
41STRESS_RUN := $(shell $(top_builddir)/locate-test --srun)
42PWD := $(shell pwd)
43TIMEOUT = $(top_builddir)/t0 $(TIMEOUT_VAL)
44
45
46all: build-tests run-tests
47
48build-tests: $(BUILD_TESTS:.c=.test)
49run-tests: $(RUN_TESTS:.test=.run-test)
50
51functional-tests: functional-make functional-run
52stress-tests: stress-make stress-run
53
54tests-pretty:
55	$(MAKE) all | column -t -s:
56
57CFLAGS = -g -O2 -Wall -Werror -D_POSIX_C_SOURCE=200112L
58
59# add -std=c99, -std=gnu99 if compiler supports it (gcc-2.95.3 does not).
60check_gcc = $(shell if $(CC) $(1) -S -o /dev/null -xc /dev/null > /dev/null 2>&1; then echo "$(1)"; else echo "$(2)"; fi)
61CFLAGS += $(call check_gcc,-std=c99,)
62CFLAGS += $(call check_gcc,-std=gnu99,)
63
64INCLUDE = -Iinclude
65
66# FIXME: exaust cmd line length
67clean:
68	@rm -f $(LOGFILE)
69# Timeout helper files
70	@rm -f $(top_builddir)/t0{,.val}
71# Built runnable tests
72	@find $(top_builddir) -iname \*.test | xargs -n 40 rm -f {}
73	@find $(top_builddir) -iname \*~ -o -iname \*.o | xargs -n 40 rm -f {}
74	@$(foreach DIR,$(FUNCTIONAL_MAKE),make -C $(DIR) clean >> /dev/null 2>&1;) >> /dev/null 2>&1
75
76# Rule to run a build test
77# If the .o doesn't export main, then we don't need to link
78.PRECIOUS: %.test
79%.test: %.o
80	@COMPLOG=$(LOGFILE).$$$$; \
81	[ -f $< ] || exit 0; \
82	{ nm -g $< | grep -q " T main"; } || \
83	{ echo "$(@:.test=): link: SKIP" | tee -a $(LOGFILE) && exit 0; }; \
84	if $(CC) $(CFLAGS) $< -o $@ $(LDFLAGS) > $$COMPLOG 2>&1; \
85	then \
86		echo "$(@:.test=): link: PASS" | tee -a $(LOGFILE); \
87	else \
88		( \
89			echo "$(@:.test=): link: FAILED. Linker output: "; \
90			cat $$COMPLOG; \
91		) >> $(LOGFILE); \
92		echo "$(@:.test=): link: FAILED "; \
93	fi; \
94	rm -f $$COMPLOG;
95
96# Rule to run an executable test
97# If it is only a build test, then the binary exist, so we don't need to run
98.PHONY: %.run-test
99%.run-test: %.test $(top_builddir)/t0 $(top_builddir)/t0.val
100	@COMPLOG=$(LOGFILE).$$$$; \
101	[ -f $< ] || exit 0; \
102	$(TIMEOUT) $< > $$COMPLOG 2>&1; \
103	RESULT=$$?; \
104	if [ $$RESULT -eq 1 ]; \
105	then \
106		MSG="FAILED"; \
107	fi; \
108	if [ $$RESULT -eq 2 ]; \
109	then \
110		MSG="UNRESOLVED"; \
111	fi; \
112	if [ $$RESULT -eq 4 ]; \
113	then \
114		MSG="UNSUPPORTED"; \
115	fi; \
116	if [ $$RESULT -eq 5 ]; \
117	then \
118		MSG="UNTESTED"; \
119	fi; \
120	if [ $$RESULT -eq $(TIMEOUT_RET) ]; \
121	then \
122		MSG="HUNG"; \
123	fi; \
124	if [  $$RESULT -gt 5  -a  $$RESULT -ne $(TIMEOUT_RET)  ]; \
125	then \
126		MSG="INTERRUPTED"; \
127	fi; \
128	if [ $$RESULT -eq 0 ]; \
129	then \
130		echo "$(@:.run-test=): execution: PASS" | tee -a $(LOGFILE); \
131	else \
132		( \
133			echo "$(@:.run-test=): execution: $$MSG: Output: "; \
134			cat $$COMPLOG; \
135		) >> $(LOGFILE); \
136		echo "$(@:.run-test=): execution: $$MSG "; \
137	fi; \
138	rm -f $$COMPLOG;
139
140$(top_builddir)/t0: $(top_builddir)/t0.c
141	@echo Building timeout helper files; \
142	$(CC) -O2 -o $@ $<
143
144$(top_builddir)/t0.val: $(top_builddir)/t0
145	echo `$(top_builddir)/t0 0; echo $$?` > $(top_builddir)/t0.val
146
147%.run-test: %.sh $(top_builddir)/t0 $(top_builddir)/t0.val
148	@COMPLOG=$(LOGFILE).$$$$; \
149	chmod +x $<; \
150	$(TIMEOUT) $< > $$COMPLOG 2>&1; \
151	RESULT=$$?; \
152	if [ $$RESULT -eq 0 ]; \
153	then \
154		echo "$(@:.run-test=): execution: PASS" | tee -a $(LOGFILE);\
155	else \
156		( \
157			echo "$(@:.run-test=): execution: FAILED: Output: ";\
158			cat $$COMPLOG; \
159		) >> $(LOGFILE); \
160		echo "$(@:.run-test=): execution: FAILED "; \
161	fi; \
162	rm -f $$COMPLOG;
163
164
165.PRECIOUS: %.o
166%.o: %.c
167	@COMPLOG=$(LOGFILE).$$$$; \
168	if $(CC) $(CFLAGS) $(INCLUDE) -c $< -o $@ $(LDFLAGS) > $$COMPLOG 2>&1; \
169	then \
170		echo "$(@:.o=): build: PASS" | tee -a $(LOGFILE); \
171	else \
172		( \
173			echo "$(@:.o=): build: FAILED: Compiler output: "; \
174			cat $$COMPLOG; \
175		) >> $(LOGFILE); \
176		echo "$(@:.o=): build: FAILED "; \
177	fi; \
178	rm -f $$COMPLOG;
179
180# Functional/Stress test build and execution
181functional-make:
182	$(foreach DIR,$(FUNCTIONAL_MAKE),make -C $(DIR);)
183
184.PHONY: $(FUNCTIONAL_RUN)
185
186functional-run: $(FUNCTIONAL_RUN)
187
188$(FUNCTIONAL_RUN):
189	cd $@; ./run.sh
190	cd $(PWD)
191
192stress-make:
193	$(foreach DIR,$(STRESS_MAKE),make -C $(DIR);)
194
195.PHONY: $(STRESS_RUN)
196
197stress-run: $(STRESS_RUN)
198
199$(STRESS_RUN):
200	cd $@; ./run.sh
201	cd $(PWD)
202
203