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