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	sigtimedwait \
45	unlink \
46
47all: check
48
49%_test: %.c test_skel.h $(top_srcdir)/linux_syscall_support.h
50	$(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) -o $@ $<
51
52%_test: %.cc test_skel.h $(top_srcdir)/linux_syscall_support.h
53	$(CXX) $(CXXFLAGS) $(CPPFLAGS) $(LDFLAGS) -o $@ $<
54
55%_run: %_test
56	@t=$(@:_run=_test); \
57	echo "./$$t"; \
58	if ! env -i ./$$t; then \
59		env -i strace -f -v ./$$t; \
60		echo "TRY: gdb -q -ex r -ex bt ./$$t"; \
61		exit 1; \
62	fi
63
64ALL_TEST_TARGETS = $(TESTS:=_test)
65compile_tests: $(ALL_TEST_TARGETS)
66
67ALL_RUN_TARGETS = $(TESTS:=_run)
68check: $(ALL_RUN_TARGETS)
69
70# The "tempfile" targets are the names we use with temp files.
71# Clean them out in case some tests crashed in the middle.
72clean:
73	rm -f *~ *.o tempfile.* a.out core $(ALL_TEST_TARGETS)
74
75.SUFFIXES:
76.PHONY: all check clean compile_tests
77.SECONDARY: $(ALL_TEST_TARGETS)
78
79# Try to cross-compile the tests for all our supported arches.  We test with
80# both gcc and clang.  We don't support execution (yet?), but just compiling
81# & linking helps catch common bugs.
82.PHONY: cross compile_cross
83cross_compile:
84	@echo "Running: $(MAKE) $@ CC='$(CC)' CXX='$(CXX)'"; \
85	if (echo '#include <stdio.h>' | $(CC) -x c -c -o /dev/null -) 2>/dev/null; then \
86		$(MAKE) -s clean; \
87		$(MAKE) -k --no-print-directory compile_tests; \
88	else \
89		echo "Skipping $(CC) test: not installed"; \
90	fi; \
91	echo
92
93# The names here are a best effort.  Not easy to probe for.
94cross:
95	@for cc in \
96		"x86_64-pc-linux-gnu-gcc" \
97		"i686-pc-linux-gnu-gcc" \
98		"x86_64-pc-linux-gnu-gcc -mx32" \
99		"armv7a-unknown-linux-gnueabi-gcc -marm -mhard-float" \
100		"armv7a-unknown-linux-gnueabi-gcc -mthumb -mhard-float" \
101		"powerpc-unknown-linux-gnu-gcc" \
102		"aarch64-unknown-linux-gnu-gcc" \
103		"mips64-unknown-linux-gnu-gcc -mabi=64" \
104		"mips64-unknown-linux-gnu-gcc -mabi=32" \
105		"mips64-unknown-linux-gnu-gcc -mabi=n32" \
106		"s390-ibm-linux-gnu-gcc" \
107		"s390x-ibm-linux-gnu-gcc" \
108	; do \
109		cxx=`echo "$$cc" | sed 's:-gcc:-g++:'`; \
110		$(MAKE) --no-print-directory CC="$$cc" CXX="$$cxx" cross_compile; \
111		\
112		sysroot=`$$cc --print-sysroot 2>/dev/null`; \
113		gccdir=`$$cc -print-file-name=libgcc.a 2>/dev/null`; \
114		gccdir=`dirname "$$gccdir"`; \
115		: Skip building for clang for mips/o32 and s390/31-bit until it works.; \
116		case $$cc in \
117			mips64*-mabi=32) continue;; \
118			s390-*) continue;; \
119		esac; \
120		set -- $$cc; \
121		tuple=$${1%-gcc}; \
122		shift; \
123		cc="clang -target $$tuple $$*"; \
124		: Assume the build system is x86_64 based, so ignore the sysroot.; \
125		case $$tuple in \
126			x86_64*) ;; \
127			*) cc="$$cc --sysroot $$sysroot -B$$gccdir -L$$gccdir";; \
128		esac; \
129		cxx=`echo "$$cc" | sed 's:^clang:clang++:'`; \
130		$(MAKE) --no-print-directory CC="$$cc" CXX="$$cxx" cross_compile; \
131	done
132