Makefile revision 31d7dda72726d2332e0168f554a6852f354f79ae
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 120 seconds
22TIMEOUT_VAL = 120
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  := $(strip $(shell grep -sv '^\#' LDFLAGS))
33CFLAGS   := $(strip $(shell grep -sv '^\#' CFLAGS))
34CPPFLAGS := $(strip $(shell grep -sv '^\#' CPPFLAGS))
35
36RUN_TESTS := $(shell $(top_builddir)/locate-test \
37             --execs $(top_builddir)/$(POSIX_TARGET))
38BUILD_TESTS := $(shell $(top_builddir)/locate-test \
39               --buildable $(top_builddir)/$(POSIX_TARGET))
40FUNCTIONAL_MAKE := $(shell $(top_builddir)/locate-test --fmake)
41FUNCTIONAL_RUN := $(shell $(top_builddir)/locate-test --frun)
42STRESS_MAKE := $(shell $(top_builddir)/locate-test --smake)
43STRESS_RUN := $(shell $(top_builddir)/locate-test --srun)
44PWD := $(shell pwd)
45TIMEOUT = $(top_builddir)/t0 $(TIMEOUT_VAL)
46
47
48all: build-tests run-tests 
49
50build-tests: $(BUILD_TESTS:.c=.test)
51run-tests: $(RUN_TESTS:.test=.run-test)
52
53functional-tests: functional-make functional-run
54stress-tests: stress-make stress-run
55
56tests-pretty:
57	$(MAKE) all | column -t -s:
58
59ifeq ($(CFLAGS),)
60CFLAGS = -g -O2
61endif
62CFLAGS += -Wall -Werror
63ifeq ($(CPPFLAGS),)
64CPPFLAGS = -D_POSIX_C_SOURCE=200112L
65endif
66
67# add -std=c99, -std=gnu99 if compiler supports it (gcc-2.95.3 does not).
68check_gcc = $(shell if $(CC) $(1) -S -o /dev/null -xc /dev/null > /dev/null 2>&1; then echo "$(1)"; else echo "$(2)"; fi)
69CFLAGS += $(call check_gcc,-std=gnu99,$(call check_gcc,-std=c99,))
70
71CPPFLAGS += -Iinclude
72
73# FIXME: exaust cmd line length
74clean:
75	@rm -f $(LOGFILE)*
76# Timeout helper files
77	@rm -f $(top_builddir)/t0{,.val}
78# Built runnable tests
79	@find $(top_builddir) -iname \*.test | xargs -n 40 rm -f {}
80	@find $(top_builddir) -iname \*~ -o -iname \*.o | xargs -n 40 rm -f {}
81	@$(foreach DIR,$(FUNCTIONAL_MAKE),make -C $(DIR) clean >> /dev/null 2>&1;) >> /dev/null 2>&1
82
83# Rule to run a build test
84# If the .o doesn't export main, then we don't need to link
85.PRECIOUS: %.test
86%.test: %.o
87	@COMPLOG=$(LOGFILE).$$$$; \
88	[ -f $< ] || exit 0; \
89	{ nm -g $< | grep -q " T main"; } || \
90	{ echo "$(@:.test=): link: SKIP" | tee -a $(LOGFILE) && exit 0; }; \
91	if $(CC) $(CFLAGS) $< -o $@ $(LDFLAGS) > $$COMPLOG 2>&1; \
92	then \
93		echo "$(@:.test=): link: PASS" | tee -a $(LOGFILE); \
94	else \
95		( \
96			echo "$(@:.test=): link: FAILED. Linker output: "; \
97			cat $$COMPLOG; \
98		) >> $(LOGFILE); \
99		echo "$(@:.test=): link: FAILED "; \
100	fi; \
101	rm -f $$COMPLOG;
102
103# Rule to run an executable test
104# If it is only a build test, then the binary exist, so we don't need to run
105.PHONY: %.run-test
106%.run-test: %.test $(top_builddir)/t0
107	@COMPLOG=$(LOGFILE).$$$$; \
108	[ -f $< ] || exit 0; \
109	$(TIMEOUT) $< > $$COMPLOG 2>&1; \
110	RESULT=$$?; \
111	if [ $$RESULT -eq 1 ]; \
112	then \
113		MSG="FAILED"; \
114	fi; \
115	if [ $$RESULT -eq 2 ]; \
116	then \
117		MSG="UNRESOLVED"; \
118	fi; \
119	if [ $$RESULT -eq 4 ]; \
120	then \
121		MSG="UNSUPPORTED"; \
122	fi; \
123	if [ $$RESULT -eq 5 ]; \
124	then \
125		MSG="UNTESTED"; \
126	fi; \
127	if [ $$RESULT -eq $(TIMEOUT_RET) ]; \
128	then \
129		MSG="HUNG"; \
130	fi; \
131	if [  $$RESULT -gt 5  -a  $$RESULT -ne $(TIMEOUT_RET)  ]; \
132	then \
133		MSG="INTERRUPTED"; \
134	fi; \
135	if [ $$RESULT -eq 0 ]; \
136	then \
137		echo "$(@:.run-test=): execution: PASS" | tee -a $(LOGFILE); \
138	else \
139		( \
140			echo "$(@:.run-test=): execution: $$MSG: Output: "; \
141			cat $$COMPLOG; \
142		) >> $(LOGFILE); \
143		echo "$(@:.run-test=): execution: $$MSG "; \
144	fi; \
145	rm -f $$COMPLOG;
146
147$(top_builddir)/t0: $(top_builddir)/t0.c
148	@echo Building timeout helper files; \
149	$(CC) -O2 -o $@ $< ; \
150	echo `$(top_builddir)/t0 0; echo $$?` > $(top_builddir)/t0.val
151	
152%.run-test: %.sh $(top_builddir)/t0
153	@COMPLOG=$(LOGFILE).$$$$; \
154	chmod +x $<; \
155	$(TIMEOUT) $< > $$COMPLOG 2>&1; \
156	RESULT=$$?; \
157	if [ $$RESULT -eq 0 ]; \
158	then \
159		echo "$(@:.run-test=): execution: PASS" | tee -a $(LOGFILE);\
160	else \
161		( \
162			echo "$(@:.run-test=): execution: FAILED: Output: ";\
163			cat $$COMPLOG; \
164		) >> $(LOGFILE); \
165		echo "$(@:.run-test=): execution: FAILED "; \
166	fi; \
167	rm -f $$COMPLOG;
168
169
170.PRECIOUS: %.o
171%.o: %.c
172	@COMPLOG=$(LOGFILE).$$$$; \
173	if $(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $@ $(LDFLAGS) > $$COMPLOG 2>&1; \
174	then \
175		echo "$(@:.o=): build: PASS" | tee -a $(LOGFILE); \
176	else \
177		( \
178			echo "$(@:.o=): build: FAILED: Compiler output: "; \
179			cat $$COMPLOG; \
180		) >> $(LOGFILE); \
181		echo "$(@:.o=): build: FAILED "; \
182	fi; \
183	rm -f $$COMPLOG;
184
185# Functional/Stress test build and execution
186functional-make:
187	$(foreach DIR,$(FUNCTIONAL_MAKE),make -C $(DIR);)
188
189.PHONY: $(FUNCTIONAL_RUN)
190
191functional-run: $(FUNCTIONAL_RUN)
192
193$(FUNCTIONAL_RUN): 
194	cd $@; ./run.sh
195	cd $(PWD)
196
197stress-make:
198	$(foreach DIR,$(STRESS_MAKE),make -C $(DIR);)
199
200.PHONY: $(STRESS_RUN)
201
202stress-run: $(STRESS_RUN)
203
204$(STRESS_RUN): 
205	cd $@; ./run.sh
206	cd $(PWD)
207