1# Copyright 2018, Google Inc.  All rights reserved.
2#
3# Redistribution and use in source and binary forms, with or without
4# modification, are permitted provided that the following conditions are
5# met:
6#
7#     * Redistributions of source code must retain the above copyright
8# notice, this list of conditions and the following disclaimer.
9#     * Redistributions in binary form must reproduce the above
10# copyright notice, this list of conditions and the following disclaimer
11# in the documentation and/or other materials provided with the
12# distribution.
13#     * Neither the name of Google Inc. nor the names of its
14# contributors may be used to endorse or promote products derived from
15# this software without specific prior written permission.
16#
17# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29top_srcdir ?= ..
30
31DEF_FLAGS = -g -pipe
32DEF_WFLAGS = -Wall
33CFLAGS ?= $(DEF_FLAGS)
34CXXFLAGS ?= $(DEF_FLAGS)
35CFLAGS += $(DEF_WFLAGS) -Wstrict-prototypes
36CXXFLAGS += $(DEF_WFLAGS)
37CPPFLAGS += -I$(top_srcdir)
38# We use static linking here so that if people run through qemu/etc... by hand,
39# it's a lot easier to run/debug.  Same for strace output.
40LDFLAGS += -static
41
42TESTS = \
43	fallocate \
44	sigaction \
45	getrandom \
46	sigtimedwait \
47	unlink \
48
49all: check
50
51%_test: %.c test_skel.h $(top_srcdir)/linux_syscall_support.h
52	$(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) -o $@ $<
53
54%_test: %.cc test_skel.h $(top_srcdir)/linux_syscall_support.h
55	$(CXX) $(CXXFLAGS) $(CPPFLAGS) $(LDFLAGS) -o $@ $<
56
57%_run: %_test
58	@t=$(@:_run=_test); \
59	echo "./$$t"; \
60	if ! env -i ./$$t; then \
61		env -i strace -f -v ./$$t; \
62		echo "TRY: gdb -q -ex r -ex bt ./$$t"; \
63		exit 1; \
64	fi
65
66ALL_TEST_TARGETS = $(TESTS:=_test)
67compile_tests: $(ALL_TEST_TARGETS)
68
69ALL_RUN_TARGETS = $(TESTS:=_run)
70check: $(ALL_RUN_TARGETS)
71
72# The "tempfile" targets are the names we use with temp files.
73# Clean them out in case some tests crashed in the middle.
74clean:
75	rm -f *~ *.o tempfile.* a.out core $(ALL_TEST_TARGETS)
76
77.SUFFIXES:
78.PHONY: all check clean compile_tests
79.SECONDARY: $(ALL_TEST_TARGETS)
80
81# Try to cross-compile the tests for all our supported arches.  We test with
82# both gcc and clang.  We don't support execution (yet?), but just compiling
83# & linking helps catch common bugs.
84.PHONY: cross compile_cross
85cross_compile:
86	@echo "Running: $(MAKE) $@ CC='$(CC)' CXX='$(CXX)'"; \
87	if (echo '#include <stdio.h>' | $(CC) -x c -c -o /dev/null -) 2>/dev/null; then \
88		$(MAKE) -s clean; \
89		$(MAKE) -k --no-print-directory compile_tests; \
90	else \
91		echo "Skipping $(CC) test: not installed"; \
92	fi; \
93	echo
94
95# The names here are a best effort.  Not easy to probe for.
96cross:
97	@for cc in \
98		"x86_64-pc-linux-gnu-gcc" \
99		"i686-pc-linux-gnu-gcc" \
100		"x86_64-pc-linux-gnu-gcc -mx32" \
101		"armv7a-unknown-linux-gnueabi-gcc -marm -mhard-float" \
102		"armv7a-unknown-linux-gnueabi-gcc -mthumb -mhard-float" \
103		"powerpc-unknown-linux-gnu-gcc" \
104		"aarch64-unknown-linux-gnu-gcc" \
105		"mips64-unknown-linux-gnu-gcc -mabi=64" \
106		"mips64-unknown-linux-gnu-gcc -mabi=32" \
107		"mips64-unknown-linux-gnu-gcc -mabi=n32" \
108		"s390-ibm-linux-gnu-gcc" \
109		"s390x-ibm-linux-gnu-gcc" \
110	; do \
111		cxx=`echo "$$cc" | sed 's:-gcc:-g++:'`; \
112		$(MAKE) --no-print-directory CC="$$cc" CXX="$$cxx" cross_compile; \
113		\
114		sysroot=`$$cc --print-sysroot 2>/dev/null`; \
115		gccdir=`$$cc -print-file-name=libgcc.a 2>/dev/null`; \
116		gccdir=`dirname "$$gccdir"`; \
117		: Skip building for clang for mips/o32 and s390/31-bit until it works.; \
118		case $$cc in \
119			mips64*-mabi=32) continue;; \
120			s390-*) continue;; \
121		esac; \
122		set -- $$cc; \
123		tuple=$${1%-gcc}; \
124		shift; \
125		cc="clang -target $$tuple $$*"; \
126		: Assume the build system is x86_64 based, so ignore the sysroot.; \
127		case $$tuple in \
128			x86_64*) ;; \
129			*) cc="$$cc --sysroot $$sysroot -B$$gccdir -L$$gccdir";; \
130		esac; \
131		cxx=`echo "$$cc" | sed 's:^clang:clang++:'`; \
132		$(MAKE) --no-print-directory CC="$$cc" CXX="$$cxx" cross_compile; \
133	done
134